unit1302.c 5.6 KB

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