xref: /curl/tests/unit/unit1664.c (revision d5c738c6)
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 
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
28 #endif
29 #ifdef HAVE_NETINET_IN6_H
30 #include <netinet/in6.h>
31 #endif
32 
33 #include <curl/curl.h>
34 
35 #include "strparse.h"
36 
37 #include "memdebug.h" /* LAST include file */
38 
unit_setup(void)39 static CURLcode unit_setup(void)
40 {
41   CURLcode res = CURLE_OK;
42   global_init(CURL_GLOBAL_ALL);
43   return res;
44 }
45 
unit_stop(void)46 static void unit_stop(void)
47 {
48   curl_global_cleanup();
49 }
50 
51 UNITTEST_START
52 {
53   static const char *wordparse[] = {
54     "word",
55     "word ",
56     " word ",
57     "wo rd",
58     "word(",
59     "wor(d",
60     "perfect",
61     "",
62     "longerth",
63     NULL
64   };
65 
66   int i;
67   printf("Curl_str_word\n");
68   for(i = 0; wordparse[i]; i++) {
69     struct Curl_str out;
70     char *line = (char *)wordparse[i];
71     char *orgline = line;
72     int rc = Curl_str_word(&line, &out, 7);
73     printf("%u: (\"%s\") %d, \"%.*s\" [%d], line %d\n",
74            i, orgline, rc, (int)out.len, out.str, (int)out.len,
75            (int)(line - orgline));
76   }
77 
78   printf("Curl_str_until\n");
79   for(i = 0; wordparse[i]; i++) {
80     struct Curl_str out;
81     char *line = (char *)wordparse[i];
82     char *orgline = line;
83     int rc = Curl_str_until(&line, &out, 7, 'd');
84     printf("%u: (\"%s\") %d, \"%.*s\" [%d], line %d\n",
85            i, orgline, rc, (int)out.len, out.str, (int)out.len,
86            (int)(line - orgline));
87   }
88 
89   {
90     static const char *qwords[] = {
91       "\"word\"",
92       "\"word",
93       "word\"",
94       "\"word\"\"",
95       "\"word\" ",
96       " \"word\"",
97       "\"perfect\"",
98       "\"p r e t\"",
99       "\"perfec\\\"",
100       "\"\"",
101       "",
102       "\"longerth\"",
103       NULL
104     };
105 
106     printf("Curl_str_quotedword\n");
107     for(i = 0; qwords[i]; i++) {
108       struct Curl_str out;
109       char *line = (char *)qwords[i];
110       char *orgline = line;
111       int rc = Curl_str_quotedword(&line, &out, 7);
112       printf("%u: (\"%s\") %d, \"%.*s\" [%d], line %d\n",
113              i, orgline, rc, (int)out.len, out.str, (int)out.len,
114              (int)(line - orgline));
115     }
116   }
117 
118   {
119     static const char *single[] = {
120       "a",
121       "aa",
122       "A",
123       "b",
124       "\\",
125       " ",
126       "",
127       NULL
128     };
129     printf("Curl_str_single\n");
130     for(i = 0; single[i]; i++) {
131       char *line = (char *)single[i];
132       char *orgline = line;
133       int rc = Curl_str_single(&line, 'a');
134       printf("%u: (\"%s\") %d, line %d\n",
135              i, orgline, rc, (int)(line - orgline));
136     }
137   }
138   {
139     static const char *single[] = {
140       "a",
141       "aa",
142       "A",
143       "b",
144       "\\",
145       " ",
146       "\t",
147       "\n",
148       "",
149       NULL
150     };
151     printf("Curl_str_singlespace\n");
152     for(i = 0; single[i]; i++) {
153       char *line = (char *)single[i];
154       char *orgline = line;
155       int rc = Curl_str_singlespace(&line);
156       printf("%u: (\"%s\") %d, line %d\n",
157              i, orgline, rc, (int)(line - orgline));
158     }
159   }
160 
161   {
162     static const char *single[] = {
163       "a",
164       "aa",
165       "A",
166       "b",
167       "\\",
168       " ",
169       "",
170       NULL
171     };
172     printf("Curl_str_single\n");
173     for(i = 0; single[i]; i++) {
174       char *line = (char *)single[i];
175       char *orgline = line;
176       int rc = Curl_str_single(&line, 'a');
177       printf("%u: (\"%s\") %d, line %d\n",
178              i, orgline, rc, (int)(line - orgline));
179     }
180   }
181   {
182     static const char *nums[] = {
183       "1",
184       "10000",
185       "1234",
186       "1235",
187       "1236",
188       "01234",
189       "00000000000000000000000000001234",
190       "0123 345",
191       "0123O345",
192       "-12",
193       " 123",
194       "",
195       NULL
196     };
197     printf("Curl_str_number\n");
198     for(i = 0; nums[i]; i++) {
199       size_t num;
200       char *line = (char *)nums[i];
201       char *orgline = line;
202       int rc = Curl_str_number(&line, &num, 1235);
203       printf("%u: (\"%s\") %d, [%u] line %d\n",
204              i, orgline, rc, (int)num, (int)(line - orgline));
205     }
206   }
207 
208   {
209     /* SIZE_T_MAX is typically 18446744073709551615 */
210     static const char *nums[] = {
211       "9223372036854775808", /* 2^63 */
212       "9223372036854775809", /* 2^63 + 1 */
213       "18446744073709551615", /* 2^64 - 1 */
214       "18446744073709551616", /* 2^64 */
215       "18446744073709551617", /* 2^64 + 1 */
216       NULL
217     };
218     printf("Curl_str_number / max\n");
219     for(i = 0; nums[i]; i++) {
220       size_t num;
221       char *line = (char *)nums[i];
222       char *orgline = line;
223       int rc = Curl_str_number(&line, &num, SIZE_T_MAX);
224       printf("%u: (\"%s\") %d, [%zu] line %d\n",
225              i, orgline, rc, num, (int)(line - orgline));
226     }
227   }
228 
229   {
230     static const char *newl[] = {
231       "a",
232       "aa",
233       "A",
234       "b",
235       "\\",
236       " ",
237       "\n",
238       "\r",
239       "\r\n",
240       "",
241       NULL
242     };
243     printf("Curl_str_newline\n");
244     for(i = 0; newl[i]; i++) {
245       char *line = (char *)newl[i];
246       char *orgline = line;
247       int rc = Curl_str_newline(&line);
248       printf("%u: (\"%s\") %d, line %d\n",
249              i, orgline, rc, (int)(line - orgline));
250     }
251   }
252 
253 }
254 UNITTEST_STOP
255