unit3200.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* First and third line 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. for(i = 0; i < NUMTESTS; i++) {
  73. FILE *fp;
  74. char buf[4096];
  75. int len = 4096;
  76. char *line;
  77. fp = fopen(arg, "wb");
  78. abort_unless(fp != NULL, "Cannot open testfile");
  79. fwrite(filecontents[i], 1, strlen(filecontents[i]), fp);
  80. fclose(fp);
  81. fp = fopen(arg, "rb");
  82. abort_unless(fp != NULL, "Cannot open testfile");
  83. fprintf(stderr, "Test %zd...", i);
  84. switch(i) {
  85. case 0:
  86. line = Curl_get_line(buf, len, fp);
  87. fail_unless(line && !strcmp("LINE1\n", line),
  88. "First line failed (1)");
  89. line = Curl_get_line(buf, len, fp);
  90. fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
  91. "Second line failed (1)");
  92. line = Curl_get_line(buf, len, fp);
  93. abort_unless(line == NULL, "Missed EOF (1)");
  94. break;
  95. case 1:
  96. line = Curl_get_line(buf, len, fp);
  97. fail_unless(line && !strcmp("LINE1\n", line),
  98. "First line failed (2)");
  99. line = Curl_get_line(buf, len, fp);
  100. fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
  101. "Second line failed (2)");
  102. line = Curl_get_line(buf, len, fp);
  103. abort_unless(line == NULL, "Missed EOF (2)");
  104. break;
  105. case 2:
  106. line = Curl_get_line(buf, len, fp);
  107. fail_unless(line && !strcmp("LINE1\n", line),
  108. "First line failed (3)");
  109. line = Curl_get_line(buf, len, fp);
  110. fail_unless(line == NULL,
  111. "Did not detect max read on EOF (3)");
  112. break;
  113. case 3:
  114. line = Curl_get_line(buf, len, fp);
  115. fail_unless(line && !strcmp("LINE1\n", line),
  116. "First line failed (4)");
  117. line = Curl_get_line(buf, len, fp);
  118. fail_unless(line == NULL,
  119. "Did not ignore partial on EOF (4)");
  120. break;
  121. case 4:
  122. line = Curl_get_line(buf, len, fp);
  123. fail_unless(line && !strcmp("LINE1\n", line),
  124. "First line failed (5)");
  125. line = Curl_get_line(buf, len, fp);
  126. fail_unless(line && !strcmp("LINE3\n", line),
  127. "Third line failed (5)");
  128. line = Curl_get_line(buf, len, fp);
  129. abort_unless(line == NULL, "Missed EOF (5)");
  130. break;
  131. case 5:
  132. line = Curl_get_line(buf, len, fp);
  133. fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
  134. "Missed/Misinterpreted ^Z (6)");
  135. line = Curl_get_line(buf, len, fp);
  136. abort_unless(line == NULL, "Missed EOF (6)");
  137. break;
  138. default:
  139. abort_unless(1, "Unknown case");
  140. break;
  141. }
  142. fclose(fp);
  143. fprintf(stderr, "OK\n");
  144. }
  145. UNITTEST_STOP
  146. #ifdef __GNUC__
  147. #pragma GCC diagnostic pop
  148. #endif
  149. #else
  150. static CURLcode unit_setup(void)
  151. {
  152. return CURLE_OK;
  153. }
  154. static void unit_stop(void)
  155. {
  156. }
  157. UNITTEST_START
  158. UNITTEST_STOP
  159. #endif