2
0

unit1652.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 *data;
  33. static char input[4096];
  34. static char result[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(result, '\0', sizeof(result));
  50. memcpy(result, 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. data = curl_easy_init();
  59. if(!data) {
  60. curl_global_cleanup();
  61. return CURLE_OUT_OF_MEMORY;
  62. }
  63. curl_easy_setopt(data, CURLOPT_DEBUGFUNCTION, debugf_cb);
  64. curl_easy_setopt(data, CURLOPT_VERBOSE, 1L);
  65. return res;
  66. }
  67. static void
  68. unit_stop(void)
  69. {
  70. curl_easy_cleanup(data);
  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. /* Injecting a simple short string via a format */
  83. msnprintf(input, sizeof(input), "Simple Test");
  84. Curl_infof(data, "%s", input);
  85. fail_unless(verify(result, input) == 0, "Simple string test");
  86. /* Injecting a few different variables with a format */
  87. Curl_infof(data, "%s %u testing %lu", input, 42, 43L);
  88. fail_unless(verify(result, "Simple Test 42 testing 43\n") == 0,
  89. "Format string");
  90. /* Variations of empty strings */
  91. Curl_infof(data, "");
  92. fail_unless(strlen(result) == 1, "Empty string");
  93. Curl_infof(data, "%s", NULL);
  94. fail_unless(verify(result, "(nil)") == 0, "Passing NULL as string");
  95. /* A string just long enough to not be truncated */
  96. memset(input, '\0', sizeof(input));
  97. memset(input, 'A', 2047);
  98. Curl_infof(data, "%s", input);
  99. fail_unless(strlen(result) == 2048, "No truncation of infof input");
  100. fail_unless(verify(result, input) == 0, "No truncation of infof input");
  101. fail_unless(result[sizeof(result) - 1] == '\0',
  102. "No truncation of infof input");
  103. /* Just over the limit for truncation without newline */
  104. memset(input + 2047, 'A', 4);
  105. Curl_infof(data, "%s", input);
  106. fail_unless(strlen(result) == 2048, "Truncation of infof input 1");
  107. fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1");
  108. /* Just over the limit for truncation with newline */
  109. memset(input + 2047, 'A', 4);
  110. memset(input + 2047 + 4, '\n', 1);
  111. Curl_infof(data, "%s", input);
  112. fail_unless(strlen(result) == 2048, "Truncation of infof input 2");
  113. fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2");
  114. /* Way over the limit for truncation with newline */
  115. memset(input, '\0', sizeof(input));
  116. memset(input, 'A', sizeof(input) - 1);
  117. Curl_infof(data, "%s", input);
  118. fail_unless(strlen(result) == 2048, "Truncation of infof input 3");
  119. fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3");
  120. UNITTEST_STOP