unit1652.c 4.3 KB

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