Debian Codegraph @

tests/t-strlist.c

raw
1 /* t-strlist.c - Check strlist functions
2 * Copyright (C) 2015, 2025 g10 Code GmbH
3 *
4 * This file is part of Libgpg-error.
5 *
6 * Libgpg-error is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgpg-error is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #ifdef HAVE_STAT
31 # include <sys/stat.h>
32 #endif
33 #include <sys/types.h>
34 #ifdef HAVE_PWD_H
35 # include <pwd.h>
36 #endif
37 #include <errno.h>
38 #ifdef HAVE_W32_SYSTEM
39 # include <windows.h>
40 #endif
41
42 #define PGM "t-strutils"
43 #include "t-common.h"
44
45
46 static const char *
47 my_strusage (int level)
48 {
49 const char *p;
50
51 switch (level)
52 {
53 case 9: p = "LGPL-2.1-or-later"; break;
54 case 11: p = PGM; break;
55 default: p = NULL;
56 }
57 return p;
58 }
59
60
61 static void
62 check_strlist_rev (void)
63 {
64 gpgrt_strlist_t s = NULL;
65
66 enter_test_function ();
67
68 /* Reversing an empty list should yield the empty list. */
69 if (! (gpgrt_strlist_rev (&s) == NULL))
70 fail ("reversing an empty list does not yield an empty list");
71
72 gpgrt_strlist_add (&s, "1", 0);
73 gpgrt_strlist_add (&s, "2", 0);
74 gpgrt_strlist_add (&s, "3", 0);
75
76 if (strcmp (s->d, "3") != 0)
77 fail ("failed at %d", __LINE__);
78 if (strcmp (s->next->d, "2") != 0)
79 fail ("failed at %d", __LINE__);
80 if (strcmp (s->next->next->d, "1") != 0)
81 fail ("failed at %d", __LINE__);
82 if (s->next->next->next)
83 fail ("failed at %d", __LINE__);
84
85 gpgrt_strlist_rev (&s);
86
87 if (strcmp (s->d, "1") != 0)
88 fail ("failed at %d", __LINE__);
89 if (strcmp (s->next->d, "2") != 0)
90 fail ("failed at %d", __LINE__);
91 if (strcmp (s->next->next->d, "3") != 0)
92 fail ("failed at %d", __LINE__);
93 if (s->next->next->next)
94 fail ("failed at %d", __LINE__);
95
96 gpgrt_strlist_free (s);
97 leave_test_function ();
98 }
99
100
101 static void
102 check_tokenize_to_strlist (void)
103 {
104 struct {
105 const char *s;
106 const char *delim;
107 int error_expected;
108 const char *items_expected[10];
109 } tv[] = {
110 {
111 "", ":",
112 1, { NULL }
113 },
114 {
115 "a", ":",
116 0, { "a", NULL }
117 },
118 {
119 ":", ":",
120 1, { NULL }
121 },
122 {
123 "::", ":",
124 1, { NULL }
125 },
126 {
127 "a:b:c", ":",
128 0, { "a", "b", "c", NULL }
129 },
130 {
131 "a:b:", ":",
132 0, { "a", "b", NULL }
133 },
134 {
135 "a:b", ":",
136 0, { "a", "b", NULL }
137 },
138 {
139 "aa:b:cd", ":",
140 0, { "aa", "b", "cd", NULL }
141 },
142 {
143 "aa::b:cd", ":",
144 0, { "aa", "b", "cd", NULL }
145 },
146 {
147 "::b:cd", ":",
148 0, { "b", "cd", NULL }
149 },
150 {
151 "aa: : b:cd ", ":",
152 0, { "aa", "b", "cd", NULL }
153 },
154 {
155 " aa: : b: cd ", ":",
156 0, { "aa", "b", "cd", NULL }
157 },
158 {
159 " :", ":",
160 1, { NULL }
161 },
162 {
163 " : ", ":",
164 1, { NULL }
165 },
166 {
167 ": ", ":",
168 1, { NULL }
169 },
170 {
171 ": x ", ":",
172 0, { "x", NULL }
173 },
174 {
175 "a:bc:cde:fghi:jklmn::foo:", ":",
176 0, { "a", "bc", "cde", "fghi", "jklmn", "foo", NULL }
177 },
178 {
179 ",a,bc,,def,", ",",
180 0, { "a", "bc", "def", NULL }
181 },
182 {
183 " a ", " ",
184 0, { "a", NULL }
185 },
186 {
187 " ", " ",
188 1, { NULL }
189 },
190 {
191 "a:bc:c de:fg hi:jklmn::foo :", ":",
192 0, { "a", "bc", "c de", "fg hi", "jklmn", "foo", NULL }
193 },
194 {
195 "", " ",
196 1, { NULL }
197 }
198 };
199 const char *prefixes[3] = { "abc", "bcd", "efg" };
200 int tidx;
201 int nprefixes; /* Number of items in already in the list. */
202 gpgrt_strlist_t list = NULL;
203
204 enter_test_function ();
205
206 for (nprefixes = 0; nprefixes < DIM (prefixes); nprefixes++)
207 for (tidx = 0; tidx < DIM(tv); tidx++)
208 {
209 int item_count_expected;
210 int i;
211 gpgrt_strlist_t sl, newitems;
212
213 for (item_count_expected = 0;
214 tv[tidx].items_expected[item_count_expected];
215 item_count_expected++)
216 ;
217
218 /* printf ("np=%d testing %d \"%s\" delim=\"%s\"\n", */
219 /* nprefixes, tidx, tv[tidx].s, tv[tidx].delim); */
220 for (i=0; i < nprefixes; i++)
221 gpgrt_strlist_add (&list, prefixes[i], GPGRT_STRLIST_APPEND);
222
223 newitems = gpgrt_strlist_tokenize (&list, tv[tidx].s, tv[tidx].delim,
224 0);
225 if (!newitems)
226 {
227 if (gpg_err_code_from_syserror () == GPG_ERR_ENOENT
228 && tv[tidx].error_expected)
229 {
230 /* Good. But need to check the prefixes. */
231 for (sl=list, i=0; i < nprefixes; i++, sl=sl->next)
232 {
233 if (!sl || strcmp (prefixes[i], sl->d))
234 {
235 show ("for item %d prefix item %d, expected '%s'\n",
236 tidx, i, prefixes[i]);
237 fail ("error at index %d, line %d",
238 tidx * 1000 + 40 + i + 1, __LINE__);
239 }
240 }
241 }
242 else
243 fail ("error at index %d, line %d", tidx * 1000, __LINE__);
244 }
245 else if (tv[tidx].error_expected)
246 {
247 show ("got items:");
248 for (sl = list; sl; sl = sl->next)
249 show (" \"%s\"", sl->d);
250 fail ("error at index %d, line %d", tidx * 1000, __LINE__);
251 }
252 else
253 {
254 if (gpgrt_strlist_count (list) != nprefixes + item_count_expected)
255 fail ("error at index %d, line %d", tidx * 1000, __LINE__);
256 else
257 {
258 for (sl=list, i=0; i < nprefixes; i++, sl=sl->next)
259 {
260 if (!sl || strcmp (prefixes[i], sl->d))
261 {
262 show ("for item %d prefix item %d, expected '%s'\n",
263 tidx, i, prefixes[i]);
264 fail ("error at index %d, line %d",
265 tidx * 1000 + 50 + i + 1, __LINE__);
266 }
267 }
268 for (i=0; i < item_count_expected; i++, sl=sl->next)
269 {
270 if (!sl)
271 {
272 show ("no item at item index %d\n", i);
273 fail ("error at index %d, line %d",
274 tidx * 1000 + i + 0, __LINE__);
275 break;
276 }
277 if (strcmp (tv[tidx].items_expected[i], sl->d))
278 {
279 show ("for item %d, expected '%s', but got '%s'\n",
280 i, tv[tidx].items_expected[i], sl->d);
281 fail ("error at index %d, line %d",
282 tidx * 1000 + 10 + i + 1, __LINE__);
283 }
284 }
285 }
286 }
287
288 gpgrt_strlist_free (list);
289 list = NULL;
290 }
291
292 leave_test_function ();
293 }
294
295
296 int
297 main (int argc, char **argv)
298 {
299 gpgrt_opt_t opts[] = {
300 ARGPARSE_x ('v', "verbose", NONE, 0, "Print more diagnostics"),
301 ARGPARSE_s_n('d', "debug", "Flyswatter"),
302 ARGPARSE_end()
303 };
304 gpgrt_argparse_t pargs = { &argc, &argv, 0 };
305
306 gpgrt_set_strusage (my_strusage);
307 gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX);
308
309 while (gpgrt_argparse (NULL, &pargs, opts))
310 {
311 switch (pargs.r_opt)
312 {
313 case 'v': verbose++; break;
314 case 'd': debug++; break;
315 default : pargs.err = ARGPARSE_PRINT_ERROR; break;
316 }
317 }
318 gpgrt_argparse (NULL, &pargs, NULL);
319
320 show ("testing strlist functions\n");
321
322 check_strlist_rev ();
323 check_tokenize_to_strlist ();
324
325 show ("testing strlist functions finished\n");
326 return !!errorcount;
327 }