2
0

unit1396.c 3.1 KB

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