unit1302.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "urldata.h"
  26. #include "url.h" /* for Curl_safefree */
  27. #include "curl_base64.h"
  28. #include "memdebug.h" /* LAST include file */
  29. static struct Curl_easy *data;
  30. static CURLcode unit_setup(void)
  31. {
  32. CURLcode res = CURLE_OK;
  33. global_init(CURL_GLOBAL_ALL);
  34. data = curl_easy_init();
  35. if(!data) {
  36. curl_global_cleanup();
  37. return CURLE_OUT_OF_MEMORY;
  38. }
  39. return res;
  40. }
  41. static void unit_stop(void)
  42. {
  43. curl_easy_cleanup(data);
  44. curl_global_cleanup();
  45. }
  46. UNITTEST_START
  47. char *output;
  48. unsigned char *decoded;
  49. size_t size = 0;
  50. unsigned char anychar = 'x';
  51. CURLcode rc;
  52. rc = Curl_base64_encode("i", 1, &output, &size);
  53. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  54. fail_unless(size == 4, "size should be 4");
  55. verify_memory(output, "aQ==", 4);
  56. Curl_safefree(output);
  57. rc = Curl_base64_encode("ii", 2, &output, &size);
  58. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  59. fail_unless(size == 4, "size should be 4");
  60. verify_memory(output, "aWk=", 4);
  61. Curl_safefree(output);
  62. rc = Curl_base64_encode("iii", 3, &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, "aWlp", 4);
  66. Curl_safefree(output);
  67. rc = Curl_base64_encode("iiii", 4, &output, &size);
  68. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  69. fail_unless(size == 8, "size should be 8");
  70. verify_memory(output, "aWlpaQ==", 8);
  71. Curl_safefree(output);
  72. rc = Curl_base64_encode("\xff\x01\xfe\x02", 4, &output, &size);
  73. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  74. fail_unless(size == 8, "size should be 8");
  75. verify_memory(output, "/wH+Ag==", 8);
  76. Curl_safefree(output);
  77. rc = Curl_base64url_encode("\xff\x01\xfe\x02", 4, &output, &size);
  78. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  79. fail_unless(size == 6, "size should be 6");
  80. verify_memory(output, "_wH-Ag", 6);
  81. Curl_safefree(output);
  82. rc = Curl_base64url_encode("iiii", 4, &output, &size);
  83. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  84. fail_unless(size == 6, "size should be 6");
  85. verify_memory(output, "aWlpaQ", 6);
  86. Curl_safefree(output);
  87. /* 0 length makes it do strlen() */
  88. rc = Curl_base64_encode("iiii", 0, &output, &size);
  89. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  90. fail_unless(size == 8, "size should be 8");
  91. verify_memory(output, "aWlpaQ==", 8);
  92. Curl_safefree(output);
  93. rc = Curl_base64_encode("", 0, &output, &size);
  94. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  95. fail_unless(size == 0, "size should be 0");
  96. fail_unless(output && !output[0], "output should be a zero-length string");
  97. Curl_safefree(output);
  98. rc = Curl_base64url_encode("", 0, &output, &size);
  99. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  100. fail_unless(size == 0, "size should be 0");
  101. fail_unless(output && !output[0], "output should be a zero-length string");
  102. Curl_safefree(output);
  103. rc = Curl_base64_decode("aWlpaQ==", &decoded, &size);
  104. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  105. fail_unless(size == 4, "size should be 4");
  106. verify_memory(decoded, "iiii", 4);
  107. Curl_safefree(decoded);
  108. rc = Curl_base64_decode("aWlp", &decoded, &size);
  109. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  110. fail_unless(size == 3, "size should be 3");
  111. verify_memory(decoded, "iii", 3);
  112. Curl_safefree(decoded);
  113. rc = Curl_base64_decode("aWk=", &decoded, &size);
  114. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  115. fail_unless(size == 2, "size should be 2");
  116. verify_memory(decoded, "ii", 2);
  117. Curl_safefree(decoded);
  118. rc = Curl_base64_decode("aQ==", &decoded, &size);
  119. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  120. fail_unless(size == 1, "size should be 1");
  121. verify_memory(decoded, "i", 2);
  122. Curl_safefree(decoded);
  123. /* This is illegal input as the data is too short */
  124. size = 1; /* not zero */
  125. decoded = &anychar; /* not NULL */
  126. rc = Curl_base64_decode("aQ", &decoded, &size);
  127. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  128. "return code should be CURLE_BAD_CONTENT_ENCODING");
  129. fail_unless(size == 0, "size should be 0");
  130. fail_if(decoded, "returned pointer should be NULL");
  131. /* This is illegal input as it contains three padding characters */
  132. size = 1; /* not zero */
  133. decoded = &anychar; /* not NULL */
  134. rc = Curl_base64_decode("a===", &decoded, &size);
  135. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  136. "return code should be CURLE_BAD_CONTENT_ENCODING");
  137. fail_unless(size == 0, "size should be 0");
  138. fail_if(decoded, "returned pointer should be NULL");
  139. /* This is illegal input as it contains a padding character mid input */
  140. size = 1; /* not zero */
  141. decoded = &anychar; /* not NULL */
  142. rc = Curl_base64_decode("a=Q=", &decoded, &size);
  143. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  144. "return code should be CURLE_BAD_CONTENT_ENCODING");
  145. fail_unless(size == 0, "size should be 0");
  146. fail_if(decoded, "returned pointer should be NULL");
  147. /* This is garbage input as it contains an illegal base64 character */
  148. size = 1; /* not zero */
  149. decoded = &anychar; /* not NULL */
  150. rc = Curl_base64_decode("a\x1f==", &decoded, &size);
  151. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  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. UNITTEST_STOP