unit3200.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "curl_get_line.h"
  26. #include "memdebug.h"
  27. #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
  28. !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
  29. /* The test XML does not supply a way to write files without newlines
  30. * so we write our own
  31. */
  32. #define C64 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  33. #define C256 C64 C64 C64 C64
  34. #define C1024 C256 C256 C256 C256
  35. #define C4096 C1024 C1024 C1024 C1024
  36. static CURLcode unit_setup(void)
  37. {
  38. return CURLE_OK;
  39. }
  40. static CURLcode unit_stop(void)
  41. {
  42. return CURLE_OK;
  43. }
  44. #ifdef __GNUC__
  45. #pragma GCC diagnostic push
  46. #pragma GCC diagnostic ignored "-Woverlength-strings"
  47. #endif
  48. #define NUMTESTS 6
  49. static const char *filecontents[] = {
  50. /* Both should be read */
  51. "LINE1\n"
  52. "LINE2 NEWLINE\n",
  53. /* Both should be read */
  54. "LINE1\n"
  55. "LINE2 NONEWLINE",
  56. /* Only first should be read */
  57. "LINE1\n"
  58. C4096,
  59. /* First line should be read */
  60. "LINE1\n"
  61. C4096 "SOME EXTRA TEXT",
  62. /* Only first should be read */
  63. "LINE1\n"
  64. C4096 "SOME EXTRA TEXT\n"
  65. "LINE3\n",
  66. "LINE1\x1aTEST"
  67. };
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic warning "-Woverlength-strings"
  70. #endif
  71. UNITTEST_START
  72. size_t i;
  73. int rc = 0;
  74. for(i = 0; i < NUMTESTS; i++) {
  75. FILE *fp;
  76. struct dynbuf buf;
  77. size_t len = 4096;
  78. char *line;
  79. Curl_dyn_init(&buf, len);
  80. fp = fopen(arg, "wb");
  81. abort_unless(fp != NULL, "Cannot open testfile");
  82. fwrite(filecontents[i], 1, strlen(filecontents[i]), fp);
  83. fclose(fp);
  84. fp = fopen(arg, "rb");
  85. abort_unless(fp != NULL, "Cannot open testfile");
  86. fprintf(stderr, "Test %zd...", i);
  87. switch(i) {
  88. case 0:
  89. rc = Curl_get_line(&buf, fp);
  90. line = Curl_dyn_ptr(&buf);
  91. fail_unless(line && !strcmp("LINE1\n", line),
  92. "First line failed (1)");
  93. rc = Curl_get_line(&buf, fp);
  94. line = Curl_dyn_ptr(&buf);
  95. fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
  96. "Second line failed (1)");
  97. rc = Curl_get_line(&buf, fp);
  98. abort_unless(!Curl_dyn_len(&buf), "Missed EOF (1)");
  99. break;
  100. case 1:
  101. rc = Curl_get_line(&buf, fp);
  102. line = Curl_dyn_ptr(&buf);
  103. fail_unless(line && !strcmp("LINE1\n", line),
  104. "First line failed (2)");
  105. rc = Curl_get_line(&buf, fp);
  106. line = Curl_dyn_ptr(&buf);
  107. fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
  108. "Second line failed (2)");
  109. rc = Curl_get_line(&buf, fp);
  110. abort_unless(!Curl_dyn_len(&buf), "Missed EOF (2)");
  111. break;
  112. case 2:
  113. rc = Curl_get_line(&buf, fp);
  114. line = Curl_dyn_ptr(&buf);
  115. fail_unless(line && !strcmp("LINE1\n", line),
  116. "First line failed (3)");
  117. rc = Curl_get_line(&buf, fp);
  118. fail_unless(!Curl_dyn_len(&buf),
  119. "Did not detect max read on EOF (3)");
  120. break;
  121. case 3:
  122. rc = Curl_get_line(&buf, fp);
  123. line = Curl_dyn_ptr(&buf);
  124. fail_unless(line && !strcmp("LINE1\n", line),
  125. "First line failed (4)");
  126. rc = Curl_get_line(&buf, fp);
  127. fail_unless(!Curl_dyn_len(&buf),
  128. "Did not ignore partial on EOF (4)");
  129. break;
  130. case 4:
  131. rc = Curl_get_line(&buf, fp);
  132. line = Curl_dyn_ptr(&buf);
  133. fail_unless(line && !strcmp("LINE1\n", line),
  134. "First line failed (5)");
  135. rc = Curl_get_line(&buf, fp);
  136. fail_unless(!Curl_dyn_len(&buf),
  137. "Did not bail out on too long line");
  138. break;
  139. case 5:
  140. rc = Curl_get_line(&buf, fp);
  141. line = Curl_dyn_ptr(&buf);
  142. fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
  143. "Missed/Misinterpreted ^Z (6)");
  144. rc = Curl_get_line(&buf, fp);
  145. abort_unless(!Curl_dyn_len(&buf), "Missed EOF (6)");
  146. break;
  147. default:
  148. abort_unless(1, "Unknown case");
  149. break;
  150. }
  151. Curl_dyn_free(&buf);
  152. fclose(fp);
  153. fprintf(stderr, "OK\n");
  154. }
  155. return (CURLcode)rc;
  156. UNITTEST_STOP
  157. #ifdef __GNUC__
  158. #pragma GCC diagnostic pop
  159. #endif
  160. #else
  161. static CURLcode unit_setup(void)
  162. {
  163. return CURLE_OK;
  164. }
  165. static void unit_stop(void)
  166. {
  167. }
  168. UNITTEST_START
  169. UNITTEST_STOP
  170. #endif