2
0

unit1652.c 4.4 KB

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