2
0

unit1302.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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. /* 0 length makes it do strlen() */
  66. rc = Curl_base64_encode(data, "iiii", 0, &output, &size);
  67. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  68. fail_unless(size == 8, "size should be 8");
  69. verify_memory( output, "aWlpaQ==", 8);
  70. Curl_safefree(output);
  71. rc = Curl_base64_decode("aWlpaQ==", &decoded, &size);
  72. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  73. fail_unless(size == 4, "size should be 4");
  74. verify_memory(decoded, "iiii", 4);
  75. Curl_safefree(decoded);
  76. rc = Curl_base64_decode("aWlp", &decoded, &size);
  77. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  78. fail_unless(size == 3, "size should be 3");
  79. verify_memory(decoded, "iii", 3);
  80. Curl_safefree(decoded);
  81. rc = Curl_base64_decode("aWk=", &decoded, &size);
  82. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  83. fail_unless(size == 2, "size should be 2");
  84. verify_memory(decoded, "ii", 2);
  85. Curl_safefree(decoded);
  86. rc = Curl_base64_decode("aQ==", &decoded, &size);
  87. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  88. fail_unless(size == 1, "size should be 1");
  89. verify_memory(decoded, "i", 2);
  90. Curl_safefree(decoded);
  91. /* this is an illegal input */
  92. size = 1; /* not zero */
  93. decoded = &anychar; /* not NULL */
  94. rc = Curl_base64_decode("aQ", &decoded, &size);
  95. /* return code indiferent, but output shall be as follows */
  96. fail_unless(size == 0, "size should be 0");
  97. fail_if(decoded, "returned pointer should be NULL");
  98. /* this is garbage input that libcurl decodes as far as possible */
  99. size = 0;
  100. decoded = NULL;
  101. rc = Curl_base64_decode("a\x1f==", &decoded, &size);
  102. fail_unless(rc == CURLE_OK, "return code should be CURLE_OK");
  103. fail_unless(size == 1, "size should be 1");
  104. fail_if(!decoded, "returned pointer should not be NULL");
  105. Curl_safefree(decoded);
  106. UNITTEST_STOP