Debian Codegraph @

tests/t-stringutils.c

raw
1 /* t-stringutils.c - Check some string utilities
2 * Copyright (C) 2020 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-stringutils"
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 const char *
62 mygethome (void)
63 {
64 static char *home_buffer;
65
66 if (!home_buffer)
67 #ifdef HAVE_W32_SYSTEM
68 {
69 home_buffer = gpgrt_fnameconcat ("~", NULL);
70 }
71 #else
72 {
73 char *home = getenv("HOME");
74
75 if(home)
76 home_buffer = xstrdup (home);
77 # if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
78 else
79 {
80 struct passwd *pwd;
81
82 pwd = getpwuid (getuid());
83 if (pwd)
84 home_buffer = xstrdup (pwd->pw_dir);
85 }
86 # endif
87 }
88 #endif
89 return home_buffer;
90 }
91
92
93 #ifdef HAVE_W32_SYSTEM
94 static wchar_t *
95 utf8_to_wchar (const char *string)
96 {
97 int n;
98 wchar_t *result;
99 size_t nbytes;
100 int cbmultibyte = -1;
101
102 n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, NULL, 0);
103 if (n < 0 || (n+1) <= 0)
104 die ("utf8_to_wchar failed\n");
105 nbytes = (size_t)(n+1) * sizeof(*result);
106 if (nbytes / sizeof(*result) != (n+1))
107 die ("utf8_to_wchar failed\n");
108 result = xmalloc (nbytes);
109 n = MultiByteToWideChar (CP_UTF8, 0, string, cbmultibyte, result, n);
110 if (n < 0)
111 die ("utf8_to_wchar failed\n");
112 return result;
113
114 }
115
116
117 static char *
118 wchar_to_utf8 (const wchar_t *string, size_t length)
119 {
120 int n;
121 char *result;
122
123 n = WideCharToMultiByte (CP_UTF8, 0, string, length, NULL, 0, NULL, NULL);
124 if (n < 0 || (n+1) <= 0)
125 die ("wchar_to_utf8 failed\n");
126
127 result = xmalloc (n+1);
128 if (!result)
129 die ("wchar_to_utf8 failed\n");
130 n = WideCharToMultiByte (CP_UTF8, 0, string, length, result, n, NULL, NULL);
131 if (n < 0)
132 die ("wchar_to_utf8 failed\n");
133 result[n] = 0;
134 return result;
135 }
136 #endif
137
138 static char *
139 mygetcwd (void)
140 {
141 #ifdef HAVE_W32_SYSTEM
142 wchar_t wbuffer[MAX_PATH + sizeof(wchar_t)];
143 wchar_t *wp;
144 DWORD wlen;
145 char *buf, *p;
146
147 wlen = GetCurrentDirectoryW (MAX_PATH, wbuffer);
148 if (!wlen)
149 die ("GCDW failed - error code: %d\n", (int)GetLastError ());
150 else if (wlen > MAX_PATH)
151 die ("GCDW failed - wlen too large\n");
152
153 buf = wchar_to_utf8 (wbuffer, wlen);
154
155 /* Quick test that the reverse works. */
156 wp = utf8_to_wchar (buf);
157 if (wcscmp (wp, wbuffer))
158 die ("GCDW: reverse converting failed\n");
159 xfree (wp);
160
161 for (p=buf; *p; p++)
162 if (*p == '\\')
163 *p = '/';
164 return buf;
165
166 #else
167 char *buffer;
168 size_t size = 100;
169
170 for (;;)
171 {
172 buffer = xmalloc (size+1);
173 if (getcwd (buffer, size) == buffer)
174 {
175 return buffer;
176 }
177 xfree (buffer);
178 if (errno != ERANGE)
179 die ("error getting current cwd: %s\n", strerror (errno));
180 size *= 2;
181 }
182 #endif
183 }
184
185
186 static void
187 check_fnameconcat (void)
188 {
189 char *out;
190 const char *home = mygethome ();
191 size_t homelen = home? strlen (home):0;
192
193 out = gpgrt_fnameconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
194 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
195 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
196 "1", "2", "3", NULL);
197 if (out)
198 fail ("fnameconcat succeeded but should not at line %d\n", __LINE__);
199 else if (errno != EINVAL)
200 fail ("fnameconcat return wrong error at line %d\n", __LINE__);
201 xfree (out);
202
203 out = gpgrt_fnameconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
204 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
205 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
206 "1", "2", "3", "4", NULL);
207 if (out)
208 fail ("fnameconcat succeeded but should not at line %d\n", __LINE__);
209 else if (errno != EINVAL)
210 fail ("fnameconcat return wrong error at line %d\n", __LINE__);
211 xfree (out);
212
213 out = gpgrt_fnameconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
214 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
215 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
216 "1", "2", NULL);
217 if (!out || strcmp (out,
218 "1/2/3/4/5/6/7/8/9/10/"
219 "1/2/3/4/5/6/7/8/9/10/"
220 "1/2/3/4/5/6/7/8/9/10/"
221 "1/2"))
222 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
223 xfree (out);
224
225 out = gpgrt_fnameconcat ("foo", "~/bar", "baz/cde", NULL);
226 if (!out || strcmp (out, "foo/~/bar/baz/cde"))
227 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
228 xfree (out);
229
230 out = gpgrt_fnameconcat ("foo", "~/bar", "baz/cde/", NULL);
231 if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
232 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
233 xfree (out);
234
235 out = gpgrt_fnameconcat ("/foo", "~/bar", "baz/cde/", NULL);
236 if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
237 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
238 xfree (out);
239
240 out = gpgrt_fnameconcat ("//foo", "~/bar", "baz/cde/", NULL);
241 if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
242 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
243 xfree (out);
244
245 out = gpgrt_fnameconcat ("", "~/bar", "baz/cde", NULL);
246 if (!out || strcmp (out, "/~/bar/baz/cde"))
247 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
248 xfree (out);
249
250 out = gpgrt_fnameconcat ("~/foo", "bar", NULL);
251 if (!out)
252 fail ("fnameconcat failed at line %d\n", __LINE__);
253 else if (home)
254 {
255 if (strlen (out) < homelen + 7)
256 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
257 else if (strncmp (out, home, homelen))
258 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
259 else if (strcmp (out+homelen, "/foo/bar"))
260 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
261 }
262 else
263 {
264 if (strcmp (out, "~/foo/bar"))
265 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
266 }
267 xfree (out);
268
269 out = gpgrt_fnameconcat ("~", "bar", NULL);
270 if (!out)
271 fail ("fnameconcat failed at line %d\n", __LINE__);
272 else if (home)
273 {
274 if (strlen (out) < homelen + 3)
275 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
276 else if (strncmp (out, home, homelen))
277 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
278 else if (strcmp (out+homelen, "/bar"))
279 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
280 }
281 else
282 {
283 if (strcmp (out, "~/bar"))
284 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
285 }
286 xfree (out);
287 }
288
289
290 static void
291 check_absfnameconcat (void)
292 {
293 char *out;
294 char *cwd = mygetcwd ();
295 size_t cwdlen = strlen (cwd);
296
297 out = gpgrt_absfnameconcat ("foo", "bar", NULL);
298 if (!out)
299 fail ("fnameconcat failed at line %d\n", __LINE__);
300 else if (strlen (out) < cwdlen + 7)
301 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
302 else if (strncmp (out, cwd, cwdlen))
303 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
304 else if (strcmp (out+cwdlen, "/foo/bar"))
305 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
306 xfree (out);
307
308 out = gpgrt_absfnameconcat ("./foo", NULL);
309 if (!out)
310 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
311 else if (strlen (out) < cwdlen + 5)
312 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
313 else if (strncmp (out, cwd, cwdlen))
314 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
315 else if (strcmp (out+cwdlen, "/./foo"))
316 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
317 xfree (out);
318
319 out = gpgrt_absfnameconcat (".", NULL);
320 if (!out)
321 fail ("fnameconcat failed at line %d\n", __LINE__);
322 else if (strlen (out) < cwdlen)
323 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
324 else if (strncmp (out, cwd, cwdlen))
325 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
326 else if (strcmp (out+cwdlen, ""))
327 fail ("fnameconcat failed at line %d (out=%s)\n", __LINE__, out);
328 xfree (out);
329
330 xfree (cwd);
331 }
332
333
334 static void
335 check_access (void)
336 {
337 char *cwd = mygetcwd ();
338
339 if (gpgrt_access (cwd, F_OK))
340 fail ("gpgrt_access(%s) failed: %s\n",
341 cwd, gpg_strerror (gpg_error_from_syserror ()));
342 else
343 show ("gpgrt_access(%s) succeeded\n", cwd);
344
345 xfree (cwd);
346 }
347
348
349 int
350 main (int argc, char **argv)
351 {
352 gpgrt_opt_t opts[] = {
353 ARGPARSE_x ('v', "verbose", NONE, 0, "Print more diagnostics"),
354 ARGPARSE_s_n('d', "debug", "Flyswatter"),
355 ARGPARSE_x (501, "pwd", NONE, 0, "Print working directory"),
356 ARGPARSE_end()
357 };
358 gpgrt_argparse_t pargs = { &argc, &argv, 0 };
359 char *cwd;
360 int opt_pwd = 0;
361
362 gpgrt_set_strusage (my_strusage);
363 gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX);
364
365 while (gpgrt_argparse (NULL, &pargs, opts))
366 {
367 switch (pargs.r_opt)
368 {
369 case 'v': verbose++; break;
370 case 'd': debug++; break;
371 case 501: opt_pwd = 1; break;
372 default : pargs.err = ARGPARSE_PRINT_ERROR; break;
373 }
374 }
375 gpgrt_argparse (NULL, &pargs, NULL);
376
377 cwd = gpgrt_getcwd ();
378 if (!cwd)
379 fail ("gpgrt_getcwd returned error: %s\n",
380 gpg_strerror (gpg_error_from_syserror ()));
381 else
382 {
383 if (opt_pwd)
384 {
385 int save_verbose = verbose;
386 verbose = 1;
387 show ("getcwd -> '%s'\n", cwd);
388 verbose = save_verbose;
389 }
390 xfree (cwd);
391 }
392
393 show ("testing string utilities\n");
394
395 check_fnameconcat ();
396 check_absfnameconcat ();
397 check_access ();
398
399 show ("testing string utilities finished\n");
400 return !!errorcount;
401 }