blob: afc5716626307570d6bebf28a7cc65aae4e76e47 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, 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
31static struct Curl_easy *data;
32
33static CURLcode unit_setup(void)
34{
35 CURLcode res = CURLE_OK;
36
37 global_init(CURL_GLOBAL_ALL);
38 data = curl_easy_init();
39 if(!data) {
40 curl_global_cleanup();
41 return CURLE_OUT_OF_MEMORY;
42 }
43 return res;
44}
45
46static void unit_stop(void)
47{
48 curl_easy_cleanup(data);
49 curl_global_cleanup();
50}
51
52UNITTEST_START
53
54char *output;
55unsigned char *decoded;
56size_t size = 0;
57unsigned char anychar = 'x';
58CURLcode rc;
59
60rc = Curl_base64_encode("i", 1, &output, &size);
61fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
62fail_unless(size == 4, "size should be 4");
63verify_memory(output, "aQ==", 4);
64Curl_safefree(output);
65
66rc = Curl_base64_encode("ii", 2, &output, &size);
67fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
68fail_unless(size == 4, "size should be 4");
69verify_memory(output, "aWk=", 4);
70Curl_safefree(output);
71
72rc = Curl_base64_encode("iii", 3, &output, &size);
73fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
74fail_unless(size == 4, "size should be 4");
75verify_memory(output, "aWlp", 4);
76Curl_safefree(output);
77
78rc = Curl_base64_encode("iiii", 4, &output, &size);
79fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
80fail_unless(size == 8, "size should be 8");
81verify_memory(output, "aWlpaQ==", 8);
82Curl_safefree(output);
83
84rc = Curl_base64_encode("\xff\x01\xfe\x02", 4, &output, &size);
85fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
86fail_unless(size == 8, "size should be 8");
87verify_memory(output, "/wH+Ag==", 8);
88Curl_safefree(output);
89
90rc = Curl_base64url_encode("\xff\x01\xfe\x02", 4, &output, &size);
91fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
92fail_unless(size == 6, "size should be 6");
93verify_memory(output, "_wH-Ag", 6);
94Curl_safefree(output);
95
96rc = Curl_base64url_encode("iiii", 4, &output, &size);
97fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
98fail_unless(size == 6, "size should be 6");
99verify_memory(output, "aWlpaQ", 6);
100Curl_safefree(output);
101
102/* 0 length makes it do strlen() */
103rc = Curl_base64_encode("iiii", 0, &output, &size);
104fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
105fail_unless(size == 8, "size should be 8");
106verify_memory(output, "aWlpaQ==", 8);
107Curl_safefree(output);
108
109rc = Curl_base64_encode("", 0, &output, &size);
110fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
111fail_unless(size == 0, "size should be 0");
112fail_unless(output && !output[0], "output should be a zero-length string");
113Curl_safefree(output);
114
115rc = Curl_base64url_encode("", 0, &output, &size);
116fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
117fail_unless(size == 0, "size should be 0");
118fail_unless(output && !output[0], "output should be a zero-length string");
119Curl_safefree(output);
120
121rc = Curl_base64_decode("aWlpaQ==", &decoded, &size);
122fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
123fail_unless(size == 4, "size should be 4");
124verify_memory(decoded, "iiii", 4);
125Curl_safefree(decoded);
126
127rc = Curl_base64_decode("aWlp", &decoded, &size);
128fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
129fail_unless(size == 3, "size should be 3");
130verify_memory(decoded, "iii", 3);
131Curl_safefree(decoded);
132
133rc = Curl_base64_decode("aWk=", &decoded, &size);
134fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
135fail_unless(size == 2, "size should be 2");
136verify_memory(decoded, "ii", 2);
137Curl_safefree(decoded);
138
139rc = Curl_base64_decode("aQ==", &decoded, &size);
140fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
141fail_unless(size == 1, "size should be 1");
142verify_memory(decoded, "i", 2);
143Curl_safefree(decoded);
144
145/* This is illegal input as the data is too short */
146size = 1; /* not zero */
147decoded = &anychar; /* not NULL */
148rc = Curl_base64_decode("aQ", &decoded, &size);
149fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
150 "return code should be CURLE_BAD_CONTENT_ENCODING");
151fail_unless(size == 0, "size should be 0");
152fail_if(decoded, "returned pointer should be NULL");
153
154/* This is illegal input as it contains three padding characters */
155size = 1; /* not zero */
156decoded = &anychar; /* not NULL */
157rc = Curl_base64_decode("a===", &decoded, &size);
158fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
159 "return code should be CURLE_BAD_CONTENT_ENCODING");
160fail_unless(size == 0, "size should be 0");
161fail_if(decoded, "returned pointer should be NULL");
162
163/* This is illegal input as it contains a padding character mid input */
164size = 1; /* not zero */
165decoded = &anychar; /* not NULL */
166rc = Curl_base64_decode("a=Q=", &decoded, &size);
167fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
168 "return code should be CURLE_BAD_CONTENT_ENCODING");
169fail_unless(size == 0, "size should be 0");
170fail_if(decoded, "returned pointer should be NULL");
171
172/* This is garbage input as it contains an illegal base64 character */
173size = 1; /* not zero */
174decoded = &anychar; /* not NULL */
175rc = Curl_base64_decode("a\x1f==", &decoded, &size);
176fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
177 "return code should be CURLE_BAD_CONTENT_ENCODING");
178fail_unless(size == 0, "size should be 0");
179fail_if(decoded, "returned pointer should be NULL");
180
181UNITTEST_STOP