unit3200.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ignored "-Woverlength-strings"
  45. #endif
  46. #define NUMTESTS 6
  47. static const char *filecontents[] = {
  48. /* Both should be read */
  49. "LINE1\n"
  50. "LINE2 NEWLINE\n",
  51. /* Both should be read */
  52. "LINE1\n"
  53. "LINE2 NONEWLINE",
  54. /* Only first should be read */
  55. "LINE1\n"
  56. C4096,
  57. /* First line should be read */
  58. "LINE1\n"
  59. C4096 "SOME EXTRA TEXT",
  60. /* First and third line should be read */
  61. "LINE1\n"
  62. C4096 "SOME EXTRA TEXT\n"
  63. "LINE3\n",
  64. "LINE1\x1aTEST"
  65. };
  66. #ifdef __GNUC__
  67. #pragma GCC diagnostic warning "-Woverlength-strings"
  68. #endif
  69. UNITTEST_START
  70. size_t i;
  71. for(i = 0; i < NUMTESTS; i++) {
  72. FILE *fp;
  73. char buf[4096];
  74. int len = 4096;
  75. char *line;
  76. fp = fopen(arg, "wb");
  77. abort_unless(fp != NULL, "Cannot open testfile");
  78. fwrite(filecontents[i], 1, strlen(filecontents[i]), fp);
  79. fclose(fp);
  80. fp = fopen(arg, "rb");
  81. abort_unless(fp != NULL, "Cannot open testfile");
  82. fprintf(stderr, "Test %zd...", i);
  83. switch(i) {
  84. case 0:
  85. line = Curl_get_line(buf, len, fp);
  86. fail_unless(line && !strcmp("LINE1\n", line),
  87. "First line failed (1)");
  88. line = Curl_get_line(buf, len, fp);
  89. fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
  90. "Second line failed (1)");
  91. line = Curl_get_line(buf, len, fp);
  92. abort_unless(line == NULL, "Missed EOF (1)");
  93. break;
  94. case 1:
  95. line = Curl_get_line(buf, len, fp);
  96. fail_unless(line && !strcmp("LINE1\n", line),
  97. "First line failed (2)");
  98. line = Curl_get_line(buf, len, fp);
  99. fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
  100. "Second line failed (2)");
  101. line = Curl_get_line(buf, len, fp);
  102. abort_unless(line == NULL, "Missed EOF (2)");
  103. break;
  104. case 2:
  105. line = Curl_get_line(buf, len, fp);
  106. fail_unless(line && !strcmp("LINE1\n", line),
  107. "First line failed (3)");
  108. line = Curl_get_line(buf, len, fp);
  109. fail_unless(line == NULL,
  110. "Did not detect max read on EOF (3)");
  111. break;
  112. case 3:
  113. line = Curl_get_line(buf, len, fp);
  114. fail_unless(line && !strcmp("LINE1\n", line),
  115. "First line failed (4)");
  116. line = Curl_get_line(buf, len, fp);
  117. fail_unless(line == NULL,
  118. "Did not ignore partial on EOF (4)");
  119. break;
  120. case 4:
  121. line = Curl_get_line(buf, len, fp);
  122. fail_unless(line && !strcmp("LINE1\n", line),
  123. "First line failed (5)");
  124. line = Curl_get_line(buf, len, fp);
  125. fail_unless(line && !strcmp("LINE3\n", line),
  126. "Third line failed (5)");
  127. line = Curl_get_line(buf, len, fp);
  128. abort_unless(line == NULL, "Missed EOF (5)");
  129. break;
  130. case 5:
  131. line = Curl_get_line(buf, len, fp);
  132. fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
  133. "Missed/Misinterpreted ^Z (6)");
  134. line = Curl_get_line(buf, len, fp);
  135. abort_unless(line == NULL, "Missed EOF (6)");
  136. break;
  137. default:
  138. abort_unless(1, "Unknown case");
  139. break;
  140. }
  141. fclose(fp);
  142. fprintf(stderr, "OK\n");
  143. }
  144. UNITTEST_STOP
  145. #else
  146. static CURLcode unit_setup(void)
  147. {
  148. return CURLE_OK;
  149. }
  150. static void unit_stop(void)
  151. {
  152. }
  153. UNITTEST_START
  154. UNITTEST_STOP
  155. #endif