113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci#include "curlcheck.h"
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci#include "urldata.h"
2713498266Sopenharmony_ci#include "url.h" /* for Curl_safefree */
2813498266Sopenharmony_ci#include "curl_base64.h"
2913498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */
3013498266Sopenharmony_ci
3113498266Sopenharmony_cistatic struct Curl_easy *data;
3213498266Sopenharmony_ci
3313498266Sopenharmony_cistatic CURLcode unit_setup(void)
3413498266Sopenharmony_ci{
3513498266Sopenharmony_ci  CURLcode res = CURLE_OK;
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci  global_init(CURL_GLOBAL_ALL);
3813498266Sopenharmony_ci  data = curl_easy_init();
3913498266Sopenharmony_ci  if(!data) {
4013498266Sopenharmony_ci    curl_global_cleanup();
4113498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
4213498266Sopenharmony_ci  }
4313498266Sopenharmony_ci  return res;
4413498266Sopenharmony_ci}
4513498266Sopenharmony_ci
4613498266Sopenharmony_cistatic void unit_stop(void)
4713498266Sopenharmony_ci{
4813498266Sopenharmony_ci  curl_easy_cleanup(data);
4913498266Sopenharmony_ci  curl_global_cleanup();
5013498266Sopenharmony_ci}
5113498266Sopenharmony_ci
5213498266Sopenharmony_ciUNITTEST_START
5313498266Sopenharmony_ci
5413498266Sopenharmony_cichar *output;
5513498266Sopenharmony_ciunsigned char *decoded;
5613498266Sopenharmony_cisize_t size = 0;
5713498266Sopenharmony_ciunsigned char anychar = 'x';
5813498266Sopenharmony_ciCURLcode rc;
5913498266Sopenharmony_ci
6013498266Sopenharmony_circ = Curl_base64_encode("i", 1, &output, &size);
6113498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
6213498266Sopenharmony_cifail_unless(size == 4, "size should be 4");
6313498266Sopenharmony_civerify_memory(output, "aQ==", 4);
6413498266Sopenharmony_ciCurl_safefree(output);
6513498266Sopenharmony_ci
6613498266Sopenharmony_circ = Curl_base64_encode("ii", 2, &output, &size);
6713498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
6813498266Sopenharmony_cifail_unless(size == 4, "size should be 4");
6913498266Sopenharmony_civerify_memory(output, "aWk=", 4);
7013498266Sopenharmony_ciCurl_safefree(output);
7113498266Sopenharmony_ci
7213498266Sopenharmony_circ = Curl_base64_encode("iii", 3, &output, &size);
7313498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
7413498266Sopenharmony_cifail_unless(size == 4, "size should be 4");
7513498266Sopenharmony_civerify_memory(output, "aWlp", 4);
7613498266Sopenharmony_ciCurl_safefree(output);
7713498266Sopenharmony_ci
7813498266Sopenharmony_circ = Curl_base64_encode("iiii", 4, &output, &size);
7913498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
8013498266Sopenharmony_cifail_unless(size == 8, "size should be 8");
8113498266Sopenharmony_civerify_memory(output, "aWlpaQ==", 8);
8213498266Sopenharmony_ciCurl_safefree(output);
8313498266Sopenharmony_ci
8413498266Sopenharmony_circ = Curl_base64_encode("\xff\x01\xfe\x02", 4, &output, &size);
8513498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
8613498266Sopenharmony_cifail_unless(size == 8, "size should be 8");
8713498266Sopenharmony_civerify_memory(output, "/wH+Ag==", 8);
8813498266Sopenharmony_ciCurl_safefree(output);
8913498266Sopenharmony_ci
9013498266Sopenharmony_circ = Curl_base64url_encode("\xff\x01\xfe\x02", 4, &output, &size);
9113498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
9213498266Sopenharmony_cifail_unless(size == 6, "size should be 6");
9313498266Sopenharmony_civerify_memory(output, "_wH-Ag", 6);
9413498266Sopenharmony_ciCurl_safefree(output);
9513498266Sopenharmony_ci
9613498266Sopenharmony_circ = Curl_base64url_encode("iiii", 4, &output, &size);
9713498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
9813498266Sopenharmony_cifail_unless(size == 6, "size should be 6");
9913498266Sopenharmony_civerify_memory(output, "aWlpaQ", 6);
10013498266Sopenharmony_ciCurl_safefree(output);
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci/* 0 length makes it do strlen() */
10313498266Sopenharmony_circ = Curl_base64_encode("iiii", 0, &output, &size);
10413498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
10513498266Sopenharmony_cifail_unless(size == 8, "size should be 8");
10613498266Sopenharmony_civerify_memory(output, "aWlpaQ==", 8);
10713498266Sopenharmony_ciCurl_safefree(output);
10813498266Sopenharmony_ci
10913498266Sopenharmony_circ = Curl_base64_encode("", 0, &output, &size);
11013498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
11113498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
11213498266Sopenharmony_cifail_unless(output && !output[0], "output should be a zero-length string");
11313498266Sopenharmony_ciCurl_safefree(output);
11413498266Sopenharmony_ci
11513498266Sopenharmony_circ = Curl_base64url_encode("", 0, &output, &size);
11613498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
11713498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
11813498266Sopenharmony_cifail_unless(output && !output[0], "output should be a zero-length string");
11913498266Sopenharmony_ciCurl_safefree(output);
12013498266Sopenharmony_ci
12113498266Sopenharmony_circ = Curl_base64_decode("aWlpaQ==", &decoded, &size);
12213498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
12313498266Sopenharmony_cifail_unless(size == 4, "size should be 4");
12413498266Sopenharmony_civerify_memory(decoded, "iiii", 4);
12513498266Sopenharmony_ciCurl_safefree(decoded);
12613498266Sopenharmony_ci
12713498266Sopenharmony_circ = Curl_base64_decode("aWlp", &decoded, &size);
12813498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
12913498266Sopenharmony_cifail_unless(size == 3, "size should be 3");
13013498266Sopenharmony_civerify_memory(decoded, "iii", 3);
13113498266Sopenharmony_ciCurl_safefree(decoded);
13213498266Sopenharmony_ci
13313498266Sopenharmony_circ = Curl_base64_decode("aWk=", &decoded, &size);
13413498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
13513498266Sopenharmony_cifail_unless(size == 2, "size should be 2");
13613498266Sopenharmony_civerify_memory(decoded, "ii", 2);
13713498266Sopenharmony_ciCurl_safefree(decoded);
13813498266Sopenharmony_ci
13913498266Sopenharmony_circ = Curl_base64_decode("aQ==", &decoded, &size);
14013498266Sopenharmony_cifail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
14113498266Sopenharmony_cifail_unless(size == 1, "size should be 1");
14213498266Sopenharmony_civerify_memory(decoded, "i", 2);
14313498266Sopenharmony_ciCurl_safefree(decoded);
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci/* This is illegal input as the data is too short */
14613498266Sopenharmony_cisize = 1; /* not zero */
14713498266Sopenharmony_cidecoded = &anychar; /* not NULL */
14813498266Sopenharmony_circ = Curl_base64_decode("aQ", &decoded, &size);
14913498266Sopenharmony_cifail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
15013498266Sopenharmony_ci            "return code should be CURLE_BAD_CONTENT_ENCODING");
15113498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
15213498266Sopenharmony_cifail_if(decoded, "returned pointer should be NULL");
15313498266Sopenharmony_ci
15413498266Sopenharmony_ci/* This is illegal input as it contains three padding characters */
15513498266Sopenharmony_cisize = 1; /* not zero */
15613498266Sopenharmony_cidecoded = &anychar; /* not NULL */
15713498266Sopenharmony_circ = Curl_base64_decode("a===", &decoded, &size);
15813498266Sopenharmony_cifail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
15913498266Sopenharmony_ci            "return code should be CURLE_BAD_CONTENT_ENCODING");
16013498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
16113498266Sopenharmony_cifail_if(decoded, "returned pointer should be NULL");
16213498266Sopenharmony_ci
16313498266Sopenharmony_ci/* This is illegal input as it contains a padding character mid input */
16413498266Sopenharmony_cisize = 1; /* not zero */
16513498266Sopenharmony_cidecoded = &anychar; /* not NULL */
16613498266Sopenharmony_circ = Curl_base64_decode("a=Q=", &decoded, &size);
16713498266Sopenharmony_cifail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
16813498266Sopenharmony_ci            "return code should be CURLE_BAD_CONTENT_ENCODING");
16913498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
17013498266Sopenharmony_cifail_if(decoded, "returned pointer should be NULL");
17113498266Sopenharmony_ci
17213498266Sopenharmony_ci/* This is also illegal input as it contains a padding character mid input */
17313498266Sopenharmony_cisize = 1; /* not zero */
17413498266Sopenharmony_cidecoded = &anychar; /* not NULL */
17513498266Sopenharmony_circ = Curl_base64_decode("aWlpa=Q=", &decoded, &size);
17613498266Sopenharmony_cifail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
17713498266Sopenharmony_ci            "return code should be CURLE_BAD_CONTENT_ENCODING");
17813498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
17913498266Sopenharmony_cifail_if(decoded, "returned pointer should be NULL");
18013498266Sopenharmony_ci
18113498266Sopenharmony_ci/* This is garbage input as it contains an illegal base64 character */
18213498266Sopenharmony_cisize = 1; /* not zero */
18313498266Sopenharmony_cidecoded = &anychar; /* not NULL */
18413498266Sopenharmony_circ = Curl_base64_decode("a\x1f==", &decoded, &size);
18513498266Sopenharmony_cifail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
18613498266Sopenharmony_ci            "return code should be CURLE_BAD_CONTENT_ENCODING");
18713498266Sopenharmony_cifail_unless(size == 0, "size should be 0");
18813498266Sopenharmony_cifail_if(decoded, "returned pointer should be NULL");
18913498266Sopenharmony_ci
19013498266Sopenharmony_ci
19113498266Sopenharmony_ciUNITTEST_STOP
192