"Fossies" - the Fresh Open Source Software Archive

Member "dovecot-2.3.8/src/lib/test-istream-base64-encoder.c" (8 Oct 2019, 6802 Bytes) of package /linux/misc/dovecot-2.3.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test-istream-base64-encoder.c": 2.3.7.2_vs_2.3.8.

    1 /* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
    2 
    3 #include "test-lib.h"
    4 #include "str.h"
    5 #include "istream-private.h"
    6 #include "istream-base64.h"
    7 
    8 struct base64_istream_test {
    9     const char *input;
   10     unsigned int chars_per_line;
   11     bool crlf;
   12     const char *output;
   13 };
   14 
   15 static const struct base64_istream_test base64_tests[] = {
   16     { "", 80, FALSE, "" },
   17     { "1", 80, FALSE, "MQ==" },
   18     { "12", 80, FALSE, "MTI=" },
   19     { "123", 80, FALSE, "MTIz" },
   20     { "1234", 80, FALSE, "MTIzNA==" },
   21     { "12345", 80, FALSE, "MTIzNDU=" },
   22     { "hello world", 80, FALSE, "aGVsbG8gd29ybGQ=" },
   23     { "hello world", 4, FALSE, "aGVs\nbG8g\nd29y\nbGQ=" },
   24     { "hello world", 4, TRUE, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=" },
   25     { "hello worlds", 80, FALSE, "aGVsbG8gd29ybGRz" },
   26     { "hello worlds", 4, FALSE, "aGVs\nbG8g\nd29y\nbGRz" },
   27     { "hello worlds", 4, TRUE, "aGVs\r\nbG8g\r\nd29y\r\nbGRz" },
   28     { "hell world", 80, FALSE, "aGVsbCB3b3JsZA==" },
   29     { "hell world", 4, FALSE, "aGVs\nbCB3\nb3Js\nZA==" },
   30     { "hell world", 4, TRUE, "aGVs\r\nbCB3\r\nb3Js\r\nZA==" },
   31     { "hello to the world!!", 80, FALSE,
   32         "aGVsbG8gdG8gdGhlIHdvcmxkISE=" },
   33     { "hello to the world!!", 8, FALSE,
   34         "aGVsbG8g\ndG8gdGhl\nIHdvcmxk\nISE=" },
   35     { "hello to the world!!", 8, TRUE,
   36         "aGVsbG8g\r\ndG8gdGhl\r\nIHdvcmxk\r\nISE=" },
   37     { "\xd0\x93\xd0\xbe\xd0\xb2\xd0\xbe\xd1\x80\xd1\x8f\xcc"
   38       "\x81\xd1\x82\x2c\x20\xd1\x87\xd1\x82\xd0\xbe\x20\xd0"
   39       "\xba\xd1\x83\xd1\x80\x20\xd0\xb4\xd0\xbe\xd1\x8f\xcc"
   40       "\x81\xd1\x82\x2e", 80, FALSE,
   41       "0JPQvtCy0L7RgNGPzIHRgiwg0YfRgtC+INC60YPRgCDQtNC+0Y/MgdGCLg==" },
   42 };
   43 
   44 static const struct base64_istream_test base64url_tests[] = {
   45     { "", 80, FALSE, "" },
   46     { "1", 80, FALSE, "MQ==" },
   47     { "12", 80, FALSE, "MTI=" },
   48     { "123", 80, FALSE, "MTIz" },
   49     { "1234", 80, FALSE, "MTIzNA==" },
   50     { "12345", 80, FALSE, "MTIzNDU=" },
   51     { "hello world", 80, FALSE, "aGVsbG8gd29ybGQ=" },
   52     { "hello world", 4, FALSE, "aGVs\nbG8g\nd29y\nbGQ=" },
   53     { "hello world", 4, TRUE, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=" },
   54     { "hello worlds", 80, FALSE, "aGVsbG8gd29ybGRz" },
   55     { "hello worlds", 4, FALSE, "aGVs\nbG8g\nd29y\nbGRz" },
   56     { "hello worlds", 4, TRUE, "aGVs\r\nbG8g\r\nd29y\r\nbGRz" },
   57     { "hell world", 80, FALSE, "aGVsbCB3b3JsZA==" },
   58     { "hell world", 4, FALSE, "aGVs\nbCB3\nb3Js\nZA==" },
   59     { "hell world", 4, TRUE, "aGVs\r\nbCB3\r\nb3Js\r\nZA==" },
   60     { "hello to the world!!", 80, FALSE,
   61         "aGVsbG8gdG8gdGhlIHdvcmxkISE=" },
   62     { "hello to the world!!", 8, FALSE,
   63         "aGVsbG8g\ndG8gdGhl\nIHdvcmxk\nISE=" },
   64     { "hello to the world!!", 8, TRUE,
   65         "aGVsbG8g\r\ndG8gdGhl\r\nIHdvcmxk\r\nISE=" },
   66     { "\xd0\x93\xd0\xbe\xd0\xb2\xd0\xbe\xd1\x80\xd1\x8f\xcc"
   67       "\x81\xd1\x82\x2c\x20\xd1\x87\xd1\x82\xd0\xbe\x20\xd0"
   68       "\xba\xd1\x83\xd1\x80\x20\xd0\xb4\xd0\xbe\xd1\x8f\xcc"
   69       "\x81\xd1\x82\x2e", 80, FALSE,
   70       "0JPQvtCy0L7RgNGPzIHRgiwg0YfRgtC-INC60YPRgCDQtNC-0Y_MgdGCLg==" },
   71 };
   72 
   73 static const char *hello = "hello world";
   74 
   75 static void encode_test(unsigned int text_len,
   76             struct istream *input, struct istream *input_data,
   77             const char *output)
   78 {
   79     unsigned int i;
   80     const unsigned char *data;
   81     uoff_t stream_size;
   82     size_t size;
   83     ssize_t ret;
   84 
   85     for (i = 1; i <= text_len; i++) {
   86         test_istream_set_size(input_data, i);
   87         while ((ret = i_stream_read(input)) > 0) ;
   88         test_assert(ret == 0);
   89     }
   90     test_istream_set_allow_eof(input_data, TRUE);
   91     while ((ret = i_stream_read(input)) > 0) ;
   92     test_assert(ret == -1);
   93 
   94     data = i_stream_get_data(input, &size);
   95     test_assert(size == strlen(output) && memcmp(data, output, size) == 0);
   96 
   97     ret = i_stream_get_size(input, TRUE, &stream_size);
   98     test_assert(ret > 0);
   99     test_assert(size == stream_size);
  100 }
  101 
  102 static void
  103 encode_base64_test(const char *text, unsigned int chars_per_line,
  104            bool crlf, const char *output)
  105 {
  106     unsigned int text_len = strlen(text);
  107     struct istream *input, *input_data;
  108 
  109     input_data = test_istream_create_data(text, text_len);
  110     test_istream_set_allow_eof(input_data, FALSE);
  111     input = i_stream_create_base64_encoder(input_data, chars_per_line,
  112                            crlf);
  113 
  114     encode_test(text_len, input, input_data, output);
  115 
  116     i_stream_unref(&input);
  117     i_stream_unref(&input_data);
  118 }
  119 
  120 static void
  121 encode_base64url_test(const char *text, unsigned int chars_per_line,
  122               bool crlf, const char *output)
  123 {
  124     unsigned int text_len = strlen(text);
  125     struct istream *input, *input_data;
  126 
  127     input_data = test_istream_create_data(text, text_len);
  128     test_istream_set_allow_eof(input_data, FALSE);
  129     input = i_stream_create_base64url_encoder(input_data, chars_per_line,
  130                           crlf);
  131 
  132     encode_test(text_len, input, input_data, output);
  133 
  134     i_stream_unref(&input);
  135     i_stream_unref(&input_data);
  136 }
  137 
  138 static void
  139 test_encoder_seek(struct istream *input, const char *textout)
  140 {
  141     unsigned int offset, len = strlen(textout);
  142     const unsigned char *data;
  143     size_t size;
  144     ssize_t ret;
  145 
  146     while (i_stream_read(input) > 0) ;
  147     i_stream_skip(input, i_stream_get_data_size(input));
  148 
  149     for (offset = 0; offset < len; offset++) {
  150         i_stream_seek(input, offset);
  151         while ((ret = i_stream_read(input)) > 0) ;
  152         test_assert(ret == -1);
  153 
  154         data = i_stream_get_data(input, &size);
  155         test_assert(size == len-offset);
  156         test_assert(memcmp(data, textout+offset, size) == 0);
  157         i_stream_skip(input, size);
  158     }
  159 }
  160 
  161 static void
  162 test_istream_base64_encoder_seek(const char *textin, const char *textout)
  163 {
  164     struct istream *input, *input_data;
  165 
  166     input_data = i_stream_create_from_data(textin, strlen(textin));
  167     input = i_stream_create_base64_encoder(input_data, 4, TRUE);
  168 
  169     test_encoder_seek(input, textout);
  170 
  171     i_stream_unref(&input);
  172     i_stream_unref(&input_data);
  173 }
  174 
  175 static void
  176 test_istream_base64url_encoder_seek(const char *textin, const char *textout)
  177 {
  178     struct istream *input, *input_data;
  179 
  180     input_data = i_stream_create_from_data(textin, strlen(textin));
  181     input = i_stream_create_base64url_encoder(input_data, 4, TRUE);
  182 
  183     test_encoder_seek(input, textout);
  184 
  185     i_stream_unref(&input);
  186     i_stream_unref(&input_data);
  187 }
  188 
  189 void test_istream_base64_encoder(void)
  190 {
  191     unsigned int i;
  192 
  193     for (i = 0; i < N_ELEMENTS(base64_tests); i++) {
  194         const struct base64_istream_test *test = &base64_tests[i];
  195 
  196         test_begin(t_strdup_printf(
  197             "istream base64 encoder %u", i+1));
  198         encode_base64_test(test->input, test->chars_per_line,
  199                    test->crlf, test->output);
  200         test_end();
  201     }
  202 
  203     for (i = 0; i < N_ELEMENTS(base64url_tests); i++) {
  204         const struct base64_istream_test *test = &base64url_tests[i];
  205 
  206         test_begin(t_strdup_printf(
  207             "istream base64url encoder %u", i+1));
  208         encode_base64url_test(test->input, test->chars_per_line,
  209                       test->crlf, test->output);
  210         test_end();
  211     }
  212 
  213     test_begin("istream base64 encoder seek");
  214     test_istream_base64_encoder_seek(
  215         hello, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=");
  216     test_end();
  217 
  218     test_begin("istream base64url encoder seek");
  219     test_istream_base64url_encoder_seek(
  220         hello, "aGVs\r\nbG8g\r\nd29y\r\nbGQ=");
  221     test_end();
  222 }