unit1302.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. int 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 == 8, "size should be 8");
  80. verify_memory(output, "_wH-Ag==", 8);
  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 == 8, "size should be 8");
  85. verify_memory(output, "aWlpaQ==", 8);
  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_decode("aWlpaQ==", &decoded, &size);
  94. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  95. fail_unless(size == 4, "size should be 4");
  96. verify_memory(decoded, "iiii", 4);
  97. Curl_safefree(decoded);
  98. rc = Curl_base64_decode("aWlp", &decoded, &size);
  99. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  100. fail_unless(size == 3, "size should be 3");
  101. verify_memory(decoded, "iii", 3);
  102. Curl_safefree(decoded);
  103. rc = Curl_base64_decode("aWk=", &decoded, &size);
  104. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  105. fail_unless(size == 2, "size should be 2");
  106. verify_memory(decoded, "ii", 2);
  107. Curl_safefree(decoded);
  108. rc = Curl_base64_decode("aQ==", &decoded, &size);
  109. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  110. fail_unless(size == 1, "size should be 1");
  111. verify_memory(decoded, "i", 2);
  112. Curl_safefree(decoded);
  113. /* This is illegal input as the data is too short */
  114. size = 1; /* not zero */
  115. decoded = &anychar; /* not NULL */
  116. rc = Curl_base64_decode("aQ", &decoded, &size);
  117. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  118. "return code should be CURLE_BAD_CONTENT_ENCODING");
  119. fail_unless(size == 0, "size should be 0");
  120. fail_if(decoded, "returned pointer should be NULL");
  121. /* This is illegal input as it contains three padding characters */
  122. size = 1; /* not zero */
  123. decoded = &anychar; /* not NULL */
  124. rc = Curl_base64_decode("a===", &decoded, &size);
  125. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  126. "return code should be CURLE_BAD_CONTENT_ENCODING");
  127. fail_unless(size == 0, "size should be 0");
  128. fail_if(decoded, "returned pointer should be NULL");
  129. /* This is illegal input as it contains a padding character mid input */
  130. size = 1; /* not zero */
  131. decoded = &anychar; /* not NULL */
  132. rc = Curl_base64_decode("a=Q=", &decoded, &size);
  133. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  134. "return code should be CURLE_BAD_CONTENT_ENCODING");
  135. fail_unless(size == 0, "size should be 0");
  136. fail_if(decoded, "returned pointer should be NULL");
  137. /* This is garbage input as it contains an illegal base64 character */
  138. size = 1; /* not zero */
  139. decoded = &anychar; /* not NULL */
  140. rc = Curl_base64_decode("a\x1f==", &decoded, &size);
  141. fail_unless(rc == CURLE_BAD_CONTENT_ENCODING,
  142. "return code should be CURLE_BAD_CONTENT_ENCODING");
  143. fail_unless(size == 0, "size should be 0");
  144. fail_if(decoded, "returned pointer should be NULL");
  145. UNITTEST_STOP