unit1652.c 4.4 KB

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