unit1302.c 5.7 KB

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