1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk_cunit.h" 37 38 #include "util/base64.c" 39 40 char text_A[] = "FZB3"; 41 uint8_t raw_A[] = {0x15, 0x90, 0x77}; 42 char text_B[] = "AbC/1+c="; 43 char text_urlsafe_B[] = "AbC_1-c="; 44 uint8_t raw_B[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7}; 45 char text_C[] = "AbC/1+cC"; 46 char text_urlsafe_C[] = "AbC_1-cC"; 47 uint8_t raw_C[] = {0x01, 0xB0, 0xBF, 0xD7, 0xE7, 0x02}; 48 char text_D[] = "AbC/1w=="; 49 char text_urlsafe_D[] = "AbC_1w=="; 50 uint8_t raw_D[] = {0x01, 0xB0, 0xBF, 0xD7}; 51 char text_E[] = "AbC12==="; 52 char text_F[] = "AbCd112"; 53 char text_G[] = "AbCd12"; 54 char text_H[] = "AbC12"; 55 char text_I[] = "AQu/1+cCCBUnOBFWv+HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R+7khPS";//64 bytes 56 char text_urlsafe_I[] = 57 "AQu_1-cCCBUnOBFWv-HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R-7khPS";//64 bytes 58 uint8_t raw_I[] = {0x01, 0x0B, 0xBF, 0xD7, 0xE7, 0x02, 0x08, 0x15, 0x27, 0x38, 0x11, 0x56, 0xBF, 0xE1, 0xF3, 0xA0, 59 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x99, 0x10, 0xDD, 0xAA, 0x19, 0xF1, 0x02, 0xD2, 60 0x13, 0xE2, 0x10, 0xF5, 0xC1, 0xB4, 0x37, 0xD1, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0x13, 0xD2 61 }; 62 char text_J[] = 63 "AQu/1+cCCBUnOBFWv+HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R+7khPSvcE5VecCCBUZ8QLiEPVm8b3BOVUBItP2GfEC4hD1ZvE5VQEi0/aJZvEfu5LiEPUTvcE5VQEi0/YZEQ=="; 64 char text_urlsafe_J[] = 65 "AQu_1-cCCBUnOBFWv-HzoL3BOVUBItP2mRDdqhnxAtIT4hD1wbQ30Ylm8R-7khPSvcE5VecCCBUZ8QLiEPVm8b3BOVUBItP2GfEC4hD1ZvE5VQEi0_aJZvEfu5LiEPUTvcE5VQEi0_YZEQ=="; 66 uint8_t raw_J[] = {0x01, 0x0B, 0xBF, 0xD7, 0xE7, 0x02, 0x08, 0x15, 0x27, 0x38, 0x11, 0x56, 0xBF, 0xE1, 0xF3, 0xA0, 67 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x99, 0x10, 0xDD, 0xAA, 0x19, 0xF1, 0x02, 0xD2, 68 0x13, 0xE2, 0x10, 0xF5, 0xC1, 0xB4, 0x37, 0xD1, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0x13, 0xD2, 69 0xBD, 0xC1, 0x39, 0x55, 0xE7, 0x02, 0x08, 0x15, 0x19, 0xF1, 0x02, 0xE2, 0x10, 0xF5, 0x66, 0xF1, 70 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x19, 0xF1, 0x02, 0xE2, 0x10, 0xF5, 0x66, 0xF1, 71 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x89, 0x66, 0xF1, 0x1F, 0xBB, 0x92, 0xE2, 0x10, 0xF5, 0x13, 72 0xBD, 0xC1, 0x39, 0x55, 0x01, 0x22, 0xD3, 0xF6, 0x19, 0x11 73 }; 74 75 static void 76 test_base64_get_encoded_strlen(void) 77 { 78 uint32_t raw_lens[4] = {8, 9, 10, 11}; 79 uint32_t text_strlens[4] = {12, 12, 16, 16}; 80 uint32_t text_strlen; 81 int i; 82 83 for (i = 0; i < 4; i++) { 84 text_strlen = spdk_base64_get_encoded_strlen(raw_lens[i]); 85 CU_ASSERT_EQUAL(text_strlen, text_strlens[i]); 86 } 87 } 88 89 static void 90 test_base64_get_decoded_len(void) 91 { 92 uint32_t text_strlens[4] = {8, 10, 11, 12}; 93 uint32_t raw_lens[4] = {6, 7, 8, 9}; 94 uint32_t bin_len; 95 int i; 96 97 for (i = 0; i < 4; i++) { 98 bin_len = spdk_base64_get_decoded_len(text_strlens[i]); 99 CU_ASSERT_EQUAL(bin_len, raw_lens[i]); 100 } 101 } 102 103 static void 104 test_base64_encode(void) 105 { 106 char text[200]; 107 int ret; 108 109 ret = spdk_base64_encode(text, raw_A, sizeof(raw_A)); 110 CU_ASSERT_EQUAL(ret, 0); 111 CU_ASSERT(strcmp(text, text_A) == 0); 112 CU_ASSERT_EQUAL(strlen(text), strlen(text_A)); 113 114 ret = spdk_base64_encode(text, raw_B, sizeof(raw_B)); 115 CU_ASSERT_EQUAL(ret, 0); 116 CU_ASSERT(strcmp(text, text_B) == 0); 117 CU_ASSERT_EQUAL(strlen(text), strlen(text_B)); 118 119 ret = spdk_base64_encode(text, raw_C, sizeof(raw_C)); 120 CU_ASSERT_EQUAL(ret, 0); 121 CU_ASSERT(strcmp(text, text_C) == 0); 122 123 ret = spdk_base64_encode(text, raw_D, sizeof(raw_D)); 124 CU_ASSERT_EQUAL(ret, 0); 125 CU_ASSERT(strcmp(text, text_D) == 0); 126 127 ret = spdk_base64_encode(text, raw_I, sizeof(raw_I)); 128 CU_ASSERT_EQUAL(ret, 0); 129 CU_ASSERT(strcmp(text, text_I) == 0); 130 131 ret = spdk_base64_encode(text, raw_J, sizeof(raw_J)); 132 CU_ASSERT_EQUAL(ret, 0); 133 CU_ASSERT(strcmp(text, text_J) == 0); 134 135 ret = spdk_base64_encode(NULL, raw_A, sizeof(raw_A)); 136 CU_ASSERT_EQUAL(ret, -EINVAL); 137 ret = spdk_base64_encode(text, NULL, sizeof(raw_A)); 138 CU_ASSERT_EQUAL(ret, -EINVAL); 139 ret = spdk_base64_encode(text, raw_A, 0); 140 CU_ASSERT_EQUAL(ret, -EINVAL); 141 } 142 143 static void 144 test_base64_decode(void) 145 { 146 char raw_buf[200]; 147 void *raw = (void *)raw_buf; 148 size_t raw_len; 149 int ret; 150 151 /* len */ 152 ret = spdk_base64_decode(NULL, &raw_len, text_A); 153 CU_ASSERT_EQUAL(ret, 0); 154 CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); 155 156 /* decode */ 157 ret = spdk_base64_decode(raw, &raw_len, text_A); 158 CU_ASSERT_EQUAL(ret, 0); 159 CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); 160 CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0); 161 162 /* len */ 163 ret = spdk_base64_decode(NULL, &raw_len, text_B); 164 CU_ASSERT_EQUAL(ret, 0); 165 CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); 166 167 /* decode */ 168 ret = spdk_base64_decode(raw, &raw_len, text_B); 169 CU_ASSERT_EQUAL(ret, 0); 170 CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); 171 CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0); 172 173 /* len */ 174 ret = spdk_base64_decode(NULL, &raw_len, text_C); 175 CU_ASSERT_EQUAL(ret, 0); 176 CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); 177 178 /* decode */ 179 ret = spdk_base64_decode(raw, &raw_len, text_C); 180 CU_ASSERT_EQUAL(ret, 0); 181 CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); 182 CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0); 183 184 /* len */ 185 ret = spdk_base64_decode(NULL, &raw_len, text_D); 186 CU_ASSERT_EQUAL(ret, 0); 187 CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); 188 189 /* decode */ 190 ret = spdk_base64_decode(raw, &raw_len, text_D); 191 CU_ASSERT_EQUAL(ret, 0); 192 CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); 193 CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0); 194 195 /* len */ 196 ret = spdk_base64_decode(NULL, &raw_len, text_I); 197 CU_ASSERT_EQUAL(ret, 0); 198 CU_ASSERT_EQUAL(raw_len, sizeof(raw_I)); 199 200 /* decode */ 201 ret = spdk_base64_decode(raw, &raw_len, text_I); 202 CU_ASSERT_EQUAL(ret, 0); 203 CU_ASSERT_EQUAL(raw_len, sizeof(raw_I)); 204 CU_ASSERT(memcmp(raw, raw_I, sizeof(raw_I)) == 0); 205 206 /* len */ 207 ret = spdk_base64_decode(NULL, &raw_len, text_J); 208 CU_ASSERT_EQUAL(ret, 0); 209 CU_ASSERT_EQUAL(raw_len, sizeof(raw_J)); 210 211 /* decode */ 212 ret = spdk_base64_decode(raw, &raw_len, text_J); 213 CU_ASSERT_EQUAL(ret, 0); 214 CU_ASSERT_EQUAL(raw_len, sizeof(raw_J)); 215 CU_ASSERT(memcmp(raw, raw_J, sizeof(raw_J)) == 0); 216 217 ret = spdk_base64_decode(raw, &raw_len, text_E); 218 CU_ASSERT_EQUAL(ret, -EINVAL); 219 ret = spdk_base64_decode(raw, &raw_len, text_F); 220 CU_ASSERT_EQUAL(ret, -EINVAL); 221 ret = spdk_base64_decode(raw, &raw_len, text_G); 222 CU_ASSERT_EQUAL(ret, -EINVAL); 223 ret = spdk_base64_decode(raw, &raw_len, text_H); 224 CU_ASSERT_EQUAL(ret, -EINVAL); 225 ret = spdk_base64_decode(raw, &raw_len, NULL); 226 CU_ASSERT_EQUAL(ret, -EINVAL); 227 } 228 229 static void 230 test_base64_urlsafe_encode(void) 231 { 232 char text[200]; 233 int ret; 234 235 ret = spdk_base64_urlsafe_encode(text, raw_A, sizeof(raw_A)); 236 CU_ASSERT_EQUAL(ret, 0); 237 CU_ASSERT(strcmp(text, text_A) == 0); 238 CU_ASSERT_EQUAL(strlen(text), strlen(text_A)); 239 240 ret = spdk_base64_urlsafe_encode(text, raw_B, sizeof(raw_B)); 241 CU_ASSERT_EQUAL(ret, 0); 242 CU_ASSERT(strcmp(text, text_urlsafe_B) == 0); 243 CU_ASSERT_EQUAL(strlen(text), strlen(text_urlsafe_B)); 244 245 ret = spdk_base64_urlsafe_encode(text, raw_C, sizeof(raw_C)); 246 CU_ASSERT_EQUAL(ret, 0); 247 CU_ASSERT(strcmp(text, text_urlsafe_C) == 0); 248 249 ret = spdk_base64_urlsafe_encode(text, raw_D, sizeof(raw_D)); 250 CU_ASSERT_EQUAL(ret, 0); 251 CU_ASSERT(strcmp(text, text_urlsafe_D) == 0); 252 253 ret = spdk_base64_urlsafe_encode(text, raw_I, sizeof(raw_I)); 254 CU_ASSERT_EQUAL(ret, 0); 255 CU_ASSERT(strcmp(text, text_urlsafe_I) == 0); 256 257 ret = spdk_base64_urlsafe_encode(text, raw_J, sizeof(raw_J)); 258 CU_ASSERT_EQUAL(ret, 0); 259 CU_ASSERT(strcmp(text, text_urlsafe_J) == 0); 260 261 ret = spdk_base64_urlsafe_encode(NULL, raw_A, sizeof(raw_A)); 262 CU_ASSERT_EQUAL(ret, -EINVAL); 263 ret = spdk_base64_urlsafe_encode(text, NULL, sizeof(raw_A)); 264 CU_ASSERT_EQUAL(ret, -EINVAL); 265 ret = spdk_base64_urlsafe_encode(text, raw_A, 0); 266 CU_ASSERT_EQUAL(ret, -EINVAL); 267 } 268 269 static void 270 test_base64_urlsafe_decode(void) 271 { 272 char raw_buf[200]; 273 void *raw = (void *)raw_buf; 274 size_t raw_len = 0; 275 int ret; 276 277 /* len */ 278 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_A); 279 CU_ASSERT_EQUAL(ret, 0); 280 CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); 281 282 /* decode */ 283 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_A); 284 CU_ASSERT_EQUAL(ret, 0); 285 CU_ASSERT_EQUAL(raw_len, sizeof(raw_A)); 286 CU_ASSERT(memcmp(raw, raw_A, sizeof(raw_A)) == 0); 287 288 /* len */ 289 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_B); 290 CU_ASSERT_EQUAL(ret, 0); 291 CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); 292 293 /* decode */ 294 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_B); 295 CU_ASSERT_EQUAL(ret, 0); 296 CU_ASSERT_EQUAL(raw_len, sizeof(raw_B)); 297 CU_ASSERT(memcmp(raw, raw_B, sizeof(raw_B)) == 0); 298 299 /* len */ 300 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_C); 301 CU_ASSERT_EQUAL(ret, 0); 302 CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); 303 304 /* decode */ 305 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_C); 306 CU_ASSERT_EQUAL(ret, 0); 307 CU_ASSERT_EQUAL(raw_len, sizeof(raw_C)); 308 CU_ASSERT(memcmp(raw, raw_C, sizeof(raw_C)) == 0); 309 310 /* len */ 311 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_D); 312 CU_ASSERT_EQUAL(ret, 0); 313 CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); 314 315 /* decode */ 316 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_D); 317 CU_ASSERT_EQUAL(ret, 0); 318 CU_ASSERT_EQUAL(raw_len, sizeof(raw_D)); 319 CU_ASSERT(memcmp(raw, raw_D, sizeof(raw_D)) == 0); 320 321 /* len */ 322 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_I); 323 CU_ASSERT_EQUAL(ret, 0); 324 CU_ASSERT_EQUAL(raw_len, sizeof(raw_I)); 325 326 /* decode */ 327 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_I); 328 CU_ASSERT_EQUAL(ret, 0); 329 CU_ASSERT_EQUAL(raw_len, sizeof(raw_I)); 330 CU_ASSERT(memcmp(raw, raw_I, sizeof(raw_I)) == 0); 331 332 /* len */ 333 ret = spdk_base64_urlsafe_decode(NULL, &raw_len, text_urlsafe_J); 334 CU_ASSERT_EQUAL(ret, 0); 335 CU_ASSERT_EQUAL(raw_len, sizeof(raw_J)); 336 337 /* decode */ 338 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_urlsafe_J); 339 CU_ASSERT_EQUAL(ret, 0); 340 CU_ASSERT_EQUAL(raw_len, sizeof(raw_J)); 341 CU_ASSERT(memcmp(raw, raw_J, sizeof(raw_J)) == 0); 342 343 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_E); 344 CU_ASSERT_EQUAL(ret, -EINVAL); 345 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_F); 346 CU_ASSERT_EQUAL(ret, -EINVAL); 347 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_G); 348 CU_ASSERT_EQUAL(ret, -EINVAL); 349 ret = spdk_base64_urlsafe_decode(raw, &raw_len, text_H); 350 CU_ASSERT_EQUAL(ret, -EINVAL); 351 ret = spdk_base64_urlsafe_decode(raw, &raw_len, NULL); 352 CU_ASSERT_EQUAL(ret, -EINVAL); 353 } 354 355 int 356 main(int argc, char **argv) 357 { 358 CU_pSuite suite = NULL; 359 unsigned int num_failures; 360 361 CU_set_error_action(CUEA_ABORT); 362 CU_initialize_registry(); 363 364 suite = CU_add_suite("base64", NULL, NULL); 365 366 CU_ADD_TEST(suite, test_base64_get_encoded_strlen); 367 CU_ADD_TEST(suite, test_base64_get_decoded_len); 368 CU_ADD_TEST(suite, test_base64_encode); 369 CU_ADD_TEST(suite, test_base64_decode); 370 CU_ADD_TEST(suite, test_base64_urlsafe_encode); 371 CU_ADD_TEST(suite, test_base64_urlsafe_decode); 372 373 CU_basic_set_mode(CU_BRM_VERBOSE); 374 375 CU_basic_run_tests(); 376 377 num_failures = CU_get_number_of_failures(); 378 CU_cleanup_registry(); 379 380 return num_failures; 381 } 382