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