unit3200.c 5.3 KB

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