xref: /curl/tests/unit/unit3200.c (revision 25cbc2f7)
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 
unit_setup(void)39 static CURLcode unit_setup(void)
40 {
41   return CURLE_OK;
42 }
43 
unit_stop(void)44 static 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
55 static 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   /* Only first 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 
85 UNITTEST_START
86   size_t i;
87   int rc = 0;
88   for(i = 0; i < NUMTESTS; i++) {
89     FILE *fp;
90     struct dynbuf buf;
91     size_t len = 4096;
92     char *line;
93     Curl_dyn_init(&buf, len);
94 
95     fp = fopen(arg, "wb");
96     abort_unless(fp != NULL, "Cannot open testfile");
97     fwrite(filecontents[i], 1, strlen(filecontents[i]), fp);
98     fclose(fp);
99 
100     fp = fopen(arg, "rb");
101     abort_unless(fp != NULL, "Cannot open testfile");
102 
103     fprintf(stderr, "Test %zd...", i);
104     switch(i) {
105       case 0:
106         rc = Curl_get_line(&buf, fp);
107         line = Curl_dyn_ptr(&buf);
108         fail_unless(line && !strcmp("LINE1\n", line),
109                     "First line failed (1)");
110         rc = Curl_get_line(&buf, fp);
111         line = Curl_dyn_ptr(&buf);
112         fail_unless(line && !strcmp("LINE2 NEWLINE\n", line),
113                     "Second line failed (1)");
114         rc = Curl_get_line(&buf, fp);
115         abort_unless(!Curl_dyn_len(&buf), "Missed EOF (1)");
116         break;
117       case 1:
118         rc = Curl_get_line(&buf, fp);
119         line = Curl_dyn_ptr(&buf);
120         fail_unless(line && !strcmp("LINE1\n", line),
121                     "First line failed (2)");
122         rc = Curl_get_line(&buf, fp);
123         line = Curl_dyn_ptr(&buf);
124         fail_unless(line && !strcmp("LINE2 NONEWLINE\n", line),
125                     "Second line failed (2)");
126         rc = Curl_get_line(&buf, fp);
127         abort_unless(!Curl_dyn_len(&buf), "Missed EOF (2)");
128         break;
129       case 2:
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 (3)");
134         rc = Curl_get_line(&buf, fp);
135         fail_unless(!Curl_dyn_len(&buf),
136                     "Did not detect max read on EOF (3)");
137         break;
138       case 3:
139         rc = Curl_get_line(&buf, fp);
140         line = Curl_dyn_ptr(&buf);
141         fail_unless(line && !strcmp("LINE1\n", line),
142                     "First line failed (4)");
143         rc = Curl_get_line(&buf, fp);
144         fail_unless(!Curl_dyn_len(&buf),
145                     "Did not ignore partial on EOF (4)");
146         break;
147       case 4:
148         rc = Curl_get_line(&buf, fp);
149         line = Curl_dyn_ptr(&buf);
150         fail_unless(line && !strcmp("LINE1\n", line),
151                     "First line failed (5)");
152         rc = Curl_get_line(&buf, fp);
153         fail_unless(!Curl_dyn_len(&buf),
154                     "Did not bail out on too long line");
155         break;
156       case 5:
157         rc = Curl_get_line(&buf, fp);
158         line = Curl_dyn_ptr(&buf);
159         fail_unless(line && !strcmp("LINE1\x1aTEST\n", line),
160                     "Missed/Misinterpreted ^Z (6)");
161         rc = Curl_get_line(&buf, fp);
162         abort_unless(!Curl_dyn_len(&buf), "Missed EOF (6)");
163         break;
164       default:
165         abort_unless(1, "Unknown case");
166         break;
167     }
168     Curl_dyn_free(&buf);
169     fclose(fp);
170     fprintf(stderr, "OK\n");
171   }
172   return (CURLcode)rc;
173 UNITTEST_STOP
174 
175 #ifdef __GNUC__
176 #pragma GCC diagnostic pop
177 #endif
178 
179 #else
180 static CURLcode unit_setup(void)
181 {
182   return CURLE_OK;
183 }
184 static void unit_stop(void)
185 {
186 }
187 UNITTEST_START
188 UNITTEST_STOP
189 
190 #endif
191