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