unit1396.c 3.2 KB

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