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