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