unit1302.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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.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 
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);
46 }
47 
49 
50 char *output;
51 unsigned char *decoded;
52 size_t size = 0;
53 unsigned char anychar = 'x';
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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 
#define UNITTEST_START
Definition: curlcheck.h:86
CURLcode Curl_base64_decode(const char *src, unsigned char **outptr, size_t *outlen)
Definition: base64.c:100
#define global_init(A)
Definition: test.h:431
fail_unless(rc==CURLE_OK,"return code should be CURLE_OK")
CURLcode Curl_base64_encode(struct Curl_easy *data, const char *inputbuff, size_t insize, char **outptr, size_t *outlen)
Definition: base64.c:291
unsigned char * decoded
Definition: unit1302.c:51
CURLcode
Definition: curl.h:454
static struct Curl_easy * data
Definition: unit1302.c:29
static int res
CURLcode Curl_base64url_encode(struct Curl_easy *data, const char *inputbuff, size_t insize, char **outptr, size_t *outlen)
Definition: base64.c:315
UNITTEST_START char * output
Definition: unit1302.c:50
Curl_safefree(output)
static void unit_stop(void)
Definition: unit1302.c:42
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
Definition: curl.h:455
#define UNITTEST_STOP
Definition: curlcheck.h:95
verify_memory(output,"aQ==", 4)
CURLcode rc
Definition: unit1302.c:54
unsigned char anychar
Definition: unit1302.c:53
static CURLcode unit_setup(void)
Definition: unit1302.c:31
size_t size
Definition: unit1302.c:52
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
fail_if(decoded,"returned pointer should be NULL")


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16