[core] http_response_send_file() shared code (#2017)
[lighttpd.git] / src / test_base64.c
blobd3ac57c0b8e36b76a52865eaabc7881b672f3c2c
1 #include "first.h"
3 #include "base64.h"
5 static void check_all_len_0() {
6 buffer *check = buffer_init();
7 const char empty[] = "";
10 unsigned char* check_res;
12 force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_STANDARD));
14 buffer_reset(check);
15 check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_STANDARD);
16 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
17 force_assert(buffer_is_equal_string(check, empty, 0));
21 unsigned char* check_res;
23 force_assert(0 == li_to_base64_no_padding(NULL, 0, NULL, 0, BASE64_URL));
25 buffer_reset(check);
26 check_res = buffer_append_base64_decode(check, NULL, 0, BASE64_URL);
27 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
28 force_assert(buffer_is_equal_string(check, empty, 0));
31 buffer_free(check);
34 static void check_all_len_1() {
35 unsigned int c1;
36 buffer *check = buffer_init();
38 for (c1 = 0; c1 < 256; ++c1) {
40 unsigned char in[] = { c1 };
41 char out[2] = { 0, 0 };
42 unsigned char* check_res;
44 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
46 buffer_reset(check);
47 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
48 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
49 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
53 unsigned char in[] = { c1 };
54 char out[2] = { 0, 0 };
55 unsigned char* check_res;
57 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
59 buffer_reset(check);
60 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
61 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
62 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
66 buffer_free(check);
69 static void check_all_len_2() {
70 unsigned int c1, c2;
71 buffer *check = buffer_init();
73 for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) {
75 unsigned char in[] = { c1, c2 };
76 char out[3] = { 0, 0, 0 };
77 unsigned char* check_res;
79 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
81 buffer_reset(check);
82 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
83 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
84 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
88 unsigned char in[] = { c1, c2 };
89 char out[3] = { 0, 0, 0 };
90 unsigned char* check_res;
92 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
94 buffer_reset(check);
95 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
96 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
97 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
101 buffer_free(check);
104 static void check_all_len_3() {
105 unsigned int c1, c2, c3;
106 buffer *check = buffer_init();
108 for (c1 = 0; c1 < 256; ++c1) for (c2 = 0; c2 < 256; ++c2) for (c3 = 0; c3 < 256; ++c3) {
110 unsigned char in[] = { c1, c2, c3 };
111 char out[4] = { 0, 0, 0, 0 };
112 unsigned char* check_res;
114 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_STANDARD));
116 buffer_reset(check);
117 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_STANDARD);
118 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
119 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
123 unsigned char in[] = { c1, c2, c3 };
124 char out[4] = { 0, 0, 0, 0 };
125 unsigned char* check_res;
127 force_assert(sizeof(out) == li_to_base64_no_padding(out, sizeof(out), in, sizeof(in), BASE64_URL));
129 buffer_reset(check);
130 check_res = buffer_append_base64_decode(check, out, sizeof(out), BASE64_URL);
131 force_assert((check_res != NULL) && (check_res == (unsigned char*) check->ptr));
132 force_assert(buffer_is_equal_string(check, (const char*) in, sizeof(in)));
136 buffer_free(check);
139 int main() {
140 check_all_len_0();
141 check_all_len_1();
142 check_all_len_2();
143 check_all_len_3();
145 return 0;