1 
2 /*
3  * Copyright (C) NGINX, Inc.
4  */
5 
6 #include <nxt_main.h>
7 #include "nxt_tests.h"
8 
9 
10 nxt_int_t
nxt_base64_test(nxt_thread_t * thr)11 nxt_base64_test(nxt_thread_t *thr)
12 {
13     ssize_t     ret;
14     nxt_uint_t  i;
15 
16     static struct {
17         nxt_str_t  enc;
18         nxt_str_t  dec;
19 
20     } tests[] = {
21         { nxt_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22                      "abcdefghijklmnopqrstuvwxyz"
23                      "0123456789+//+9876543210"
24                      "zyxwvutsrqponmlkjihgfedcba"
25                      "ZYXWVUTSRQPONMLKJIHGFEDCBA"),
26           nxt_string("\x00\x10\x83\x10\x51\x87\x20\x92\x8b\x30\xd3\x8f"
27                      "\x41\x14\x93\x51\x55\x97\x61\x96\x9b\x71\xd7\x9f"
28                      "\x82\x18\xa3\x92\x59\xa7\xa2\x9a\xab\xb2\xdb\xaf"
29                      "\xc3\x1c\xb3\xd3\x5d\xb7\xe3\x9e\xbb\xf3\xdf\xbf"
30                      "\xff\xef\x7c\xef\xae\x78\xdf\x6d\x74\xcf\x2c\x70"
31                      "\xbe\xeb\x6c\xae\xaa\x68\x9e\x69\x64\x8e\x28\x60"
32                      "\x7d\xe7\x5c\x6d\xa6\x58\x5d\x65\x54\x4d\x24\x50"
33                      "\x3c\xe3\x4c\x2c\xa2\x48\x1c\x61\x44\x0c\x20\x40") },
34 
35         { nxt_string("Aa=="),
36           nxt_string("\x01") },
37         { nxt_string("0Z"),
38           nxt_string("\xd1") },
39         { nxt_string("0aA="),
40           nxt_string("\xd1\xa0") },
41         { nxt_string("z/+"),
42           nxt_string("\xcf\xff") },
43         { nxt_string("z9+Npe=="),
44           nxt_string("\xcf\xdf\x8d\xa5") },
45         { nxt_string("/+98765"),
46           nxt_string("\xff\xef\x7c\xef\xae") },
47 
48         { nxt_string("aBc_"),
49           nxt_null_string },
50         { nxt_string("5"),
51           nxt_null_string },
52         { nxt_string("M==="),
53           nxt_null_string },
54         { nxt_string("===="),
55           nxt_null_string },
56         { nxt_string("Ab="),
57           nxt_null_string },
58         { nxt_string("00=0"),
59           nxt_null_string },
60         { nxt_string("\0"),
61           nxt_null_string },
62         { nxt_string("\r\naaaa"),
63           nxt_null_string },
64         { nxt_string("=0000"),
65           nxt_null_string },
66     };
67 
68     u_char  buf[96];
69 
70     nxt_thread_time_update(thr);
71 
72     for (i = 0; i < nxt_nitems(tests); i++) {
73         ret = nxt_base64_decode(NULL, tests[i].enc.start, tests[i].enc.length);
74 
75         if (ret == NXT_ERROR && tests[i].dec.start == NULL) {
76             continue;
77         }
78 
79         if ((size_t) ret != tests[i].dec.length) {
80             nxt_log_alert(thr->log,
81                           "nxt_base64_decode() test \"%V\" failed: incorrect "
82                           "length of decoded string %z, expected %uz",
83                           &tests[i].enc, ret, tests[i].dec.length);
84             return NXT_ERROR;
85         }
86 
87         ret = nxt_base64_decode(buf, tests[i].enc.start, tests[i].enc.length);
88 
89         if (!nxt_str_eq(&tests[i].dec, buf, (size_t) ret)) {
90             nxt_log_alert(thr->log, "nxt_base64_decode() test \"%V\" failed");
91             return NXT_ERROR;
92         }
93     }
94 
95     nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_base64_decode() test passed");
96 
97     return NXT_OK;
98 }
99