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