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