1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "curlcheck.h"
25
26 #include "urldata.h"
27 #include "url.h" /* for Curl_safefree */
28 #include "curl_base64.h"
29 #include "memdebug.h" /* LAST include file */
30
31 static struct Curl_easy *testdata;
32
unit_setup(void)33 static CURLcode unit_setup(void)
34 {
35 CURLcode res = CURLE_OK;
36
37 global_init(CURL_GLOBAL_ALL);
38 testdata = curl_easy_init();
39 if(!testdata) {
40 curl_global_cleanup();
41 return CURLE_OUT_OF_MEMORY;
42 }
43 return res;
44 }
45
unit_stop(void)46 static void unit_stop(void)
47 {
48 curl_easy_cleanup(testdata);
49 curl_global_cleanup();
50 }
51
52 UNITTEST_START
53
54 char *output;
55 unsigned char *decoded;
56 size_t size = 0;
57 unsigned char anychar = 'x';
58 CURLcode rc;
59
60 rc = Curl_base64_encode("i", 1, &output, &size);
61 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
62 fail_unless(size == 4, "size should be 4");
63 verify_memory(output, "aQ==", 4);
64 Curl_safefree(output);
65
66 rc = Curl_base64_encode("ii", 2, &output, &size);
67 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
68 fail_unless(size == 4, "size should be 4");
69 verify_memory(output, "aWk=", 4);
70 Curl_safefree(output);
71
72 rc = Curl_base64_encode("iii", 3, &output, &size);
73 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
74 fail_unless(size == 4, "size should be 4");
75 verify_memory(output, "aWlp", 4);
76 Curl_safefree(output);
77
78 rc = Curl_base64_encode("iiii", 4, &output, &size);
79 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
80 fail_unless(size == 8, "size should be 8");
81 verify_memory(output, "aWlpaQ==", 8);
82 Curl_safefree(output);
83
84 rc = Curl_base64_encode("\xff\x01\xfe\x02", 4, &output, &size);
85 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
86 fail_unless(size == 8, "size should be 8");
87 verify_memory(output, "/wH+Ag==", 8);
88 Curl_safefree(output);
89
90 rc = Curl_base64url_encode("\xff\x01\xfe\x02", 4, &output, &size);
91 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
92 fail_unless(size == 6, "size should be 6");
93 verify_memory(output, "_wH-Ag", 6);
94 Curl_safefree(output);
95
96 rc = Curl_base64url_encode("iiii", 4, &output, &size);
97 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
98 fail_unless(size == 6, "size should be 6");
99 verify_memory(output, "aWlpaQ", 6);
100 Curl_safefree(output);
101
102 /* 0 length makes it do strlen() */
103 rc = Curl_base64_encode("iiii", 0, &output, &size);
104 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
105 fail_unless(size == 8, "size should be 8");
106 verify_memory(output, "aWlpaQ==", 8);
107 Curl_safefree(output);
108
109 rc = Curl_base64_encode("", 0, &output, &size);
110 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
111 fail_unless(size == 0, "size should be 0");
112 fail_unless(output && !output[0], "output should be a zero-length string");
113 Curl_safefree(output);
114
115 rc = Curl_base64url_encode("", 0, &output, &size);
116 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
117 fail_unless(size == 0, "size should be 0");
118 fail_unless(output && !output[0], "output should be a zero-length string");
119 Curl_safefree(output);
120
121 rc = Curl_base64_decode("aWlpaQ==", &decoded, &size);
122 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
123 fail_unless(size == 4, "size should be 4");
124 verify_memory(decoded, "iiii", 4);
125 Curl_safefree(decoded);
126
127 rc = Curl_base64_decode("aWlp", &decoded, &size);
128 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
129 fail_unless(size == 3, "size should be 3");
130 verify_memory(decoded, "iii", 3);
131 Curl_safefree(decoded);
132
133 rc = Curl_base64_decode("aWk=", &decoded, &size);
134 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
135 fail_unless(size == 2, "size should be 2");
136 verify_memory(decoded, "ii", 2);
137 Curl_safefree(decoded);
138
139 rc = Curl_base64_decode("aQ==", &decoded, &size);
140 fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
141 fail_unless(size == 1, "size should be 1");
142 verify_memory(decoded, "i", 2);
143 Curl_safefree(decoded);
144
145 /* This is illegal input as the data is too short */
146 size = 1; /* not zero */
147 decoded = &anychar; /* not NULL */
148 rc = Curl_base64_decode("aQ", &decoded, &size);
149 fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
150 "return code should be CURLE_BAD_CONTENT_ENCODING");
151 fail_unless(size == 0, "size should be 0");
152 fail_if(decoded, "returned pointer should be NULL");
153
154 /* This is illegal input as it contains three padding characters */
155 size = 1; /* not zero */
156 decoded = &anychar; /* not NULL */
157 rc = Curl_base64_decode("a===", &decoded, &size);
158 fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
159 "return code should be CURLE_BAD_CONTENT_ENCODING");
160 fail_unless(size == 0, "size should be 0");
161 fail_if(decoded, "returned pointer should be NULL");
162
163 /* This is illegal input as it contains a padding character mid input */
164 size = 1; /* not zero */
165 decoded = &anychar; /* not NULL */
166 rc = Curl_base64_decode("a=Q=", &decoded, &size);
167 fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
168 "return code should be CURLE_BAD_CONTENT_ENCODING");
169 fail_unless(size == 0, "size should be 0");
170 fail_if(decoded, "returned pointer should be NULL");
171
172 /* This is also illegal input as it contains a padding character mid input */
173 size = 1; /* not zero */
174 decoded = &anychar; /* not NULL */
175 rc = Curl_base64_decode("aWlpa=Q=", &decoded, &size);
176 fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
177 "return code should be CURLE_BAD_CONTENT_ENCODING");
178 fail_unless(size == 0, "size should be 0");
179 fail_if(decoded, "returned pointer should be NULL");
180
181 /* This is garbage input as it contains an illegal base64 character */
182 size = 1; /* not zero */
183 decoded = &anychar; /* not NULL */
184 rc = Curl_base64_decode("a\x1f==", &decoded, &size);
185 fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
186 "return code should be CURLE_BAD_CONTENT_ENCODING");
187 fail_unless(size == 0, "size should be 0");
188 fail_if(decoded, "returned pointer should be NULL");
189
190
191 UNITTEST_STOP
192