unit1652.c 4.8 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. /*
  28. * This test hardcodes the knowledge of the buffer size which is internal to
  29. * Curl_infof(). If that buffer is changed in size, this tests needs to be
  30. * updated to still be valid.
  31. */
  32. static struct Curl_easy *testdata;
  33. static char input[4096];
  34. static char output[4096];
  35. int debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
  36. void *userptr);
  37. /*
  38. * This debugf callback is simply dumping the string into the static buffer
  39. * for the unit test to inspect. Since we know that we're only dealing with
  40. * text we can afford the luxury of skipping the type check here.
  41. */
  42. int
  43. debugf_cb(CURL *handle, curl_infotype type, char *buf, size_t size,
  44. void *userptr)
  45. {
  46. (void)handle;
  47. (void)type;
  48. (void)userptr;
  49. memset(output, '\0', sizeof(output));
  50. memcpy(output, buf, size);
  51. return 0;
  52. }
  53. static CURLcode
  54. unit_setup(void)
  55. {
  56. CURLcode res = CURLE_OK;
  57. global_init(CURL_GLOBAL_ALL);
  58. testdata = curl_easy_init();
  59. if(!testdata) {
  60. curl_global_cleanup();
  61. return CURLE_OUT_OF_MEMORY;
  62. }
  63. curl_easy_setopt(testdata, CURLOPT_DEBUGFUNCTION, debugf_cb);
  64. curl_easy_setopt(testdata, CURLOPT_VERBOSE, 1L);
  65. return res;
  66. }
  67. static void
  68. unit_stop(void)
  69. {
  70. curl_easy_cleanup(testdata);
  71. curl_global_cleanup();
  72. }
  73. static int verify(const char *info, const char *two)
  74. {
  75. /* the 'info' one has a newline appended */
  76. char *nl = strchr(info, '\n');
  77. if(!nl)
  78. return 1; /* nope */
  79. return strncmp(info, two, nl - info);
  80. }
  81. UNITTEST_START
  82. #if defined(__GNUC__) && !defined(__clang__)
  83. #pragma GCC diagnostic push
  84. #pragma GCC diagnostic ignored "-Wformat"
  85. #pragma GCC diagnostic ignored "-Wformat-zero-length"
  86. #if __GNUC__ >= 7
  87. #pragma GCC diagnostic ignored "-Wformat-overflow"
  88. #endif
  89. #endif
  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. #if defined(__GNUC__) && !defined(__clang__)
  129. #pragma GCC diagnostic pop
  130. #endif
  131. UNITTEST_STOP