1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, 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 http://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 SessionHandle *data; 30 31static CURLcode unit_setup( void ) 32{ 33 data = curl_easy_init(); 34 if (!data) 35 return CURLE_OUT_OF_MEMORY; 36 return CURLE_OK; 37} 38 39static void unit_stop( void ) 40{ 41 curl_easy_cleanup(data); 42} 43 44UNITTEST_START 45 46char *output; 47unsigned char *decoded; 48size_t rc; 49 50rc = Curl_base64_encode(data, "i", 1, &output); 51fail_unless( rc == 4 , "return code should be 4" ); 52verify_memory( output, "aQ==", 4); 53Curl_safefree(output); 54 55rc = Curl_base64_encode(data, "ii", 2, &output); 56fail_unless( rc == 4 , "return code should be 4" ); 57verify_memory( output, "aWk=", 4); 58Curl_safefree(output); 59 60rc = Curl_base64_encode(data, "iii", 3, &output); 61fail_unless( rc == 4 , "return code should be 4" ); 62verify_memory( output, "aWlp", 4); 63Curl_safefree(output); 64 65rc = Curl_base64_encode(data, "iiii", 4, &output); 66fail_unless( rc == 8 , "return code should be 8" ); 67verify_memory( output, "aWlpaQ==", 8); 68Curl_safefree(output); 69 70/* 0 length makes it do strlen() */ 71rc = Curl_base64_encode(data, "iiii", 0, &output); 72fail_unless( rc == 8 , "return code should be 8" ); 73verify_memory( output, "aWlpaQ==", 8); 74Curl_safefree(output); 75 76rc = Curl_base64_decode("aWlpaQ==", &decoded); 77fail_unless(rc == 4, "return code should be 4"); 78verify_memory(decoded, "iiii", 4); 79Curl_safefree(decoded); 80 81rc = Curl_base64_decode("aWlp", &decoded); 82fail_unless(rc == 3, "return code should be 3"); 83verify_memory(decoded, "iii", 3); 84Curl_safefree(decoded); 85 86rc = Curl_base64_decode("aWk=", &decoded); 87fail_unless(rc == 2, "return code should be 2"); 88verify_memory(decoded, "ii", 2); 89Curl_safefree(decoded); 90 91rc = Curl_base64_decode("aQ==", &decoded); 92fail_unless(rc == 1, "return code should be 1"); 93verify_memory(decoded, "i", 2); 94Curl_safefree(decoded); 95 96/* this is an illegal input */ 97decoded = NULL; 98rc = Curl_base64_decode("aQ", &decoded); 99fail_unless(rc == 0, "return code should be 0"); 100fail_if(decoded, "returned pointer should be NULL"); 101 102/* this is garbage input that libcurl decodes as far as possible */ 103rc = Curl_base64_decode("a\x1f==", &decoded); 104fail_unless(rc == 1, "return code should be 1"); 105Curl_safefree(decoded); 106 107UNITTEST_STOP 108