unit3200.c 4.6 KB

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