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