unit1302.c (7000B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 "unitcheck.h" 25 26 #include "urldata.h" 27 #include "url.h" /* for Curl_safefree */ 28 #include "memdebug.h" /* LAST include file */ 29 30 struct etest { 31 const char *input; 32 size_t ilen; 33 const char *output; 34 size_t olen; 35 }; 36 37 static CURLcode test_unit1302(char *arg) 38 { 39 UNITTEST_BEGIN_SIMPLE 40 CURLcode rc; 41 unsigned int i; 42 43 /* common base64 encoding */ 44 struct etest encode[] = { 45 {"iiiiii", 1, "aQ==", 4 }, 46 {"iiiiii", 2, "aWk=", 4 }, 47 {"iiiiii", 3, "aWlp", 4 }, 48 {"iiiiii", 4, "aWlpaQ==", 8 }, 49 {"iiiiii", 5, "aWlpaWk=", 8 }, 50 {"iiiiii", 6, "aWlpaWlp", 8 }, 51 {"iiiiiii", 7, "aWlpaWlpaQ==", 12 }, 52 {"iiiiiiii", 8, "aWlpaWlpaWk=", 12 }, 53 {"iiiiiiiii", 9, "aWlpaWlpaWlp", 12 }, 54 {"iiiiiiiiii", 10, "aWlpaWlpaWlpaQ==", 16 }, 55 {"iiiiiiiiiii", 11, "aWlpaWlpaWlpaWk=", 16 }, 56 {"iiiiiiiiiiii", 12, "aWlpaWlpaWlpaWlp", 16 }, 57 {"\xff\x01\xfe\x02", 4, "/wH+Ag==", 8 }, 58 {"\xff\xff\xff\xff", 4, "/////w==", 8 }, 59 {"\x00\x00\x00\x00", 4, "AAAAAA==", 8 }, 60 {"\x00\x00\x00\x00", 1, "AA==", 4 }, 61 }; 62 63 /* base64 URL encoding */ 64 struct etest url[] = { 65 {"", 0, "", 0 }, 66 {"iiiiiiiiiii", 1, "aQ", 2 }, 67 {"iiiiiiiiiii", 2, "aWk", 3 }, 68 {"iiiiiiiiiii", 3, "aWlp", 4 }, 69 {"iiiiiiiiiii", 4, "aWlpaQ", 6 }, 70 {"iiiiiiiiiii", 5, "aWlpaWk", 7 }, 71 {"iiiiiiiiiii", 6, "aWlpaWlp", 8 }, 72 {"iiiiiiiiiii", 7, "aWlpaWlpaQ", 10 }, 73 {"iiiiiiiiiii", 8, "aWlpaWlpaWk", 11 }, 74 {"iiiiiiiiiii", 9, "aWlpaWlpaWlp", 12 }, 75 {"iiiiiiiiiii", 10, "aWlpaWlpaWlpaQ", 14 }, 76 {"iiiiiiiiiii", 11, "aWlpaWlpaWlpaWk", 15 }, 77 {"iiiiiiiiiiii", 12, "aWlpaWlpaWlpaWlp", 16 }, 78 {"\xff\x01\xfe\x02", 4, "_wH-Ag", 6 }, 79 {"\xff\xff\xff\xff", 4, "_____w", 6 }, 80 {"\xff\x00\xff\x00", 4, "_wD_AA", 6 }, 81 {"\x00\xff\x00\xff", 4, "AP8A_w", 6 }, 82 {"\x00\x00\x00\x00", 4, "AAAAAA", 6 }, 83 {"\x00", 1, "AA", 2 }, 84 {"\x01", 1, "AQ", 2 }, 85 {"\x02", 1, "Ag", 2 }, 86 {"\x03", 1, "Aw", 2 }, 87 {"\x04", 1, "BA", 2 }, 88 {"\x05", 1, "BQ", 2 }, 89 {"\x06", 1, "Bg", 2 }, 90 {"\x07", 1, "Bw", 2 }, 91 {"\x08", 1, "CA", 2 }, 92 {"\x09", 1, "CQ", 2 }, 93 {"\x0a", 1, "Cg", 2 }, 94 {"\x0b", 1, "Cw", 2 }, 95 {"\x0c", 1, "DA", 2 }, 96 {"\x0d", 1, "DQ", 2 }, 97 {"\x0e", 1, "Dg", 2 }, 98 {"\x0f", 1, "Dw", 2 }, 99 {"\x10", 1, "EA", 2 }, 100 }; 101 102 /* bad decode inputs */ 103 struct etest badecode[] = { 104 {"", 0, "", 0 }, /* no dats means error */ 105 {"", 0, "a", 1 }, /* data is too short */ 106 {"", 0, "aQ", 2 }, /* data is too short */ 107 {"", 0, "aQ=", 3 }, /* data is too short */ 108 {"", 0, "====", 1 }, /* data is only padding characters */ 109 {"", 0, "====", 2 }, /* data is only padding characters */ 110 {"", 0, "====", 3 }, /* data is only padding characters */ 111 {"", 0, "====", 4 }, /* data is only padding characters */ 112 {"", 0, "a===", 4 }, /* contains three padding characters */ 113 {"", 0, "a=Q=", 4 }, /* contains a padding character mid input */ 114 {"", 0, "aWlpa=Q=", 8 }, /* contains a padding character mid input */ 115 {"", 0, "a\x1f==", 4 }, /* contains illegal base64 character */ 116 {"", 0, "abcd ", 5 }, /* contains illegal base64 character */ 117 {"", 0, "abcd ", 6 }, /* contains illegal base64 character */ 118 {"", 0, " abcd", 5 }, /* contains illegal base64 character */ 119 {"", 0, "_abcd", 5 }, /* contains illegal base64 character */ 120 {"", 0, "abcd-", 5 }, /* contains illegal base64 character */ 121 {"", 0, "abcd_", 5 }, /* contains illegal base64 character */ 122 {"", 0, "aWlpaWlpaQ==-", 17}, /* bad character after padding */ 123 {"", 0, "aWlpaWlpaQ==_", 17}, /* bad character after padding */ 124 {"", 0, "aWlpaWlpaQ== ", 17}, /* bad character after padding */ 125 {"", 0, "aWlpaWlpaQ=", 15} /* unaligned size, missing a padding char */ 126 }; 127 128 for(i = 0 ; i < CURL_ARRAYSIZE(encode); i++) { 129 struct etest *e = &encode[i]; 130 char *out; 131 unsigned char *decoded; 132 size_t olen; 133 size_t dlen; 134 135 /* first encode */ 136 rc = curlx_base64_encode(e->input, e->ilen, &out, &olen); 137 abort_unless(rc == CURLE_OK, "return code should be CURLE_OK"); 138 abort_unless(olen == e->olen, "wrong output size"); 139 if(memcmp(out, e->output, e->olen)) { 140 fprintf(stderr, "Test %u encoded badly\n", i); 141 unitfail++; 142 } 143 Curl_safefree(out); 144 145 /* then verify decode */ 146 rc = curlx_base64_decode(e->output, &decoded, &dlen); 147 if(rc != CURLE_OK) { 148 fprintf(stderr, "Test %u URL decode returned %d\n", i, (int)rc); 149 unitfail++; 150 } 151 if(dlen != e->ilen) { 152 fprintf(stderr, "Test %u URL decode output length %d instead of %d\n", 153 i, (int)dlen, (int)e->ilen); 154 unitfail++; 155 } 156 if(memcmp(decoded, e->input, dlen)) { 157 fprintf(stderr, "Test %u URL decoded badly. Got '%s', expected '%s'\n", 158 i, decoded, e->input); 159 unitfail++; 160 } 161 162 Curl_safefree(decoded); 163 } 164 165 for(i = 0 ; i < CURL_ARRAYSIZE(url); i++) { 166 struct etest *e = &url[i]; 167 char *out; 168 size_t olen; 169 rc = curlx_base64url_encode(e->input, e->ilen, &out, &olen); 170 abort_unless(rc == CURLE_OK, "return code should be CURLE_OK"); 171 if(olen != e->olen) { 172 fprintf(stderr, "Test %u URL encoded output length %d instead of %d\n", 173 i, (int)olen, (int)e->olen); 174 } 175 if(memcmp(out, e->output, e->olen)) { 176 fprintf(stderr, "Test %u URL encoded badly. Got '%s', expected '%s'\n", 177 i, out, e->output); 178 unitfail++; 179 } 180 Curl_safefree(out); 181 } 182 183 for(i = 0 ; i < CURL_ARRAYSIZE(badecode); i++) { 184 struct etest *e = &badecode[i]; 185 unsigned char *decoded; 186 size_t dlen; 187 188 /* then verify decode with illegal inputs */ 189 rc = curlx_base64_decode(e->output, &decoded, &dlen); 190 if(rc != CURLE_BAD_CONTENT_ENCODING) { 191 fprintf(stderr, "Test %u URL bad decoded badly. " 192 "Returned '%d', expected '%d'\n", 193 i, (int)rc, CURLE_BAD_CONTENT_ENCODING); 194 unitfail++; 195 } 196 } 197 198 UNITTEST_END_SIMPLE 199 }