xref: /curl/tests/unit/unit1396.c (revision 2bc1d775)
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 "curlcheck.h"
25 
26 static CURL *hnd;
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   CURLcode res = CURLE_OK;
31 
32   global_init(CURL_GLOBAL_ALL);
33   return res;
34 }
35 
unit_stop(void)36 static void unit_stop(void)
37 {
38   if(hnd)
39     curl_easy_cleanup(hnd);
40   curl_global_cleanup();
41 }
42 
43 struct test {
44   const char *in;
45   int inlen;
46   const char *out;
47   int outlen;
48 };
49 
50 UNITTEST_START
51 {
52   /* unescape, this => that */
53   const struct test list1[]={
54     {"%61", 3, "a", 1},
55     {"%61a", 4, "aa", 2},
56     {"%61b", 4, "ab", 2},
57     {"%6 1", 4, "%6 1", 4},
58     {"%61", 1, "%", 1},
59     {"%61", 2, "%6", 2},
60     {"%6%a", 4, "%6%a", 4},
61     {"%6a", 0, "j", 1},
62     {"%FF", 0, "\xff", 1},
63     {"%FF%00%ff", 9, "\xff\x00\xff", 3},
64     {"%-2", 0, "%-2", 3},
65     {"%FG", 0, "%FG", 3},
66     {NULL, 0, NULL, 0} /* end of list marker */
67   };
68   /* escape, this => that */
69   const struct test list2[]={
70     {"a", 1, "a", 1},
71     {"/", 1, "%2F", 3},
72     {"a=b", 3, "a%3Db", 5},
73     {"a=b", 0, "a%3Db", 5},
74     {"a=b", 1, "a", 1},
75     {"a=b", 2, "a%3D", 4},
76     {"1/./0", 5, "1%2F.%2F0", 9},
77     {"-._~!#%&", 0, "-._~%21%23%25%26", 16},
78     {"a", 2, "a%00", 4},
79     {"a\xff\x01g", 4, "a%FF%01g", 8},
80     {NULL, 0, NULL, 0} /* end of list marker */
81   };
82   int i;
83 
84   hnd = curl_easy_init();
85   abort_unless(hnd != NULL, "returned NULL!");
86   for(i = 0; list1[i].in; i++) {
87     int outlen;
88     char *out = curl_easy_unescape(hnd,
89                                    list1[i].in, list1[i].inlen,
90                                    &outlen);
91 
92     abort_unless(out != NULL, "returned NULL!");
93     fail_unless(outlen == list1[i].outlen, "wrong output length returned");
94     fail_unless(!memcmp(out, list1[i].out, list1[i].outlen),
95                 "bad output data returned");
96 
97     printf("curl_easy_unescape test %d DONE\n", i);
98 
99     curl_free(out);
100   }
101 
102   for(i = 0; list2[i].in; i++) {
103     int outlen;
104     char *out = curl_easy_escape(hnd, list2[i].in, list2[i].inlen);
105     abort_unless(out != NULL, "returned NULL!");
106 
107     outlen = (int)strlen(out);
108     fail_unless(outlen == list2[i].outlen, "wrong output length returned");
109     fail_unless(!memcmp(out, list2[i].out, list2[i].outlen),
110                 "bad output data returned");
111 
112     printf("curl_easy_escape test %d DONE (%s)\n", i, out);
113 
114     curl_free(out);
115   }
116 }
117 UNITTEST_STOP
118