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