unit1652.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 "sendf.h"
  27. #ifdef __GNUC__
  28. #pragma GCC diagnostic push
  29. #pragma GCC diagnostic ignored "-Wformat"
  30. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  31. #if !defined(__clang__) && __GNUC__ >= 7
  32. #pragma GCC diagnostic ignored "-Wformat-overflow"
  33. #endif
  34. #endif
  35. /*
  36. * This test hardcodes the knowledge of the buffer size which is internal to
  37. * Curl_infof(). If that buffer is changed in size, this tests needs to be
  38. * updated to still be valid.
  39. */
  40. static struct Curl_easy *testdata;
  41. static char input[4096];
  42. static char output[4096];
  43. int debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
  44. void *userptr);
  45. /*
  46. * This debugf callback is simply dumping the string into the static buffer
  47. * for the unit test to inspect. Since we know that we're only dealing with
  48. * text we can afford the luxury of skipping the type check here.
  49. */
  50. int
  51. debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
  52. void *userptr)
  53. {
  54. (void)handle;
  55. (void)type;
  56. (void)userptr;
  57. memset(output, '\0', sizeof(output));
  58. memcpy(output, buf, size);
  59. return 0;
  60. }
  61. static CURLcode
  62. unit_setup(void)
  63. {
  64. CURLcode res = CURLE_OK;
  65. global_init(CURL_GLOBAL_ALL);
  66. testdata = curl_easy_init();
  67. if(!testdata) {
  68. curl_global_cleanup();
  69. return CURLE_OUT_OF_MEMORY;
  70. }
  71. curl_easy_setopt(testdata, CURLOPT_DEBUGFUNCTION, debugf_cb);
  72. curl_easy_setopt(testdata, CURLOPT_VERBOSE, 1L);
  73. return res;
  74. }
  75. static void
  76. unit_stop(void)
  77. {
  78. curl_easy_cleanup(testdata);
  79. curl_global_cleanup();
  80. }
  81. static int verify(const char *info, const char *two)
  82. {
  83. /* the 'info' one has a newline appended */
  84. char *nl = strchr(info, '\n');
  85. if(!nl)
  86. return 1; /* nope */
  87. return strncmp(info, two, nl - info);
  88. }
  89. UNITTEST_START
  90. /* Injecting a simple short string via a format */
  91. msnprintf(input, sizeof(input), "Simple Test");
  92. Curl_infof(testdata, "%s", input);
  93. fail_unless(verify(output, input) == 0, "Simple string test");
  94. /* Injecting a few different variables with a format */
  95. Curl_infof(testdata, "%s %u testing %lu", input, 42, 43L);
  96. fail_unless(verify(output, "Simple Test 42 testing 43\n") == 0,
  97. "Format string");
  98. /* Variations of empty strings */
  99. Curl_infof(testdata, "");
  100. fail_unless(strlen(output) == 1, "Empty string");
  101. Curl_infof(testdata, "%s", (char *)NULL);
  102. fail_unless(verify(output, "(nil)") == 0, "Passing NULL as string");
  103. /* A string just long enough to not be truncated */
  104. memset(input, '\0', sizeof(input));
  105. memset(input, 'A', 2047);
  106. Curl_infof(testdata, "%s", input);
  107. fail_unless(strlen(output) == 2048, "No truncation of infof input");
  108. fail_unless(verify(output, input) == 0, "No truncation of infof input");
  109. fail_unless(output[sizeof(output) - 1] == '\0',
  110. "No truncation of infof input");
  111. /* Just over the limit without newline for truncation via '...' */
  112. memset(input + 2047, 'A', 4);
  113. Curl_infof(testdata, "%s", input);
  114. fail_unless(strlen(output) == 2051, "Truncation of infof input 1");
  115. fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 1");
  116. /* Just over the limit with newline for truncation via '...' */
  117. memset(input + 2047, 'A', 4);
  118. memset(input + 2047 + 4, '\n', 1);
  119. Curl_infof(testdata, "%s", input);
  120. fail_unless(strlen(output) == 2051, "Truncation of infof input 2");
  121. fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 2");
  122. /* Way over the limit for truncation via '...' */
  123. memset(input, '\0', sizeof(input));
  124. memset(input, 'A', sizeof(input) - 1);
  125. Curl_infof(testdata, "%s", input);
  126. fail_unless(strlen(output) == 2051, "Truncation of infof input 3");
  127. fail_unless(output[sizeof(output) - 1] == '\0', "Truncation of infof input 3");
  128. UNITTEST_STOP
  129. #ifdef __GNUC__
  130. #pragma GCC diagnostic pop
  131. #endif