Add help strings for all commands.
[gnupg.git] / jnlib / t-stringhelp.c
blob02041d35e0c4698bf5fdab42ba43e1ebe5ea213f
1 /* t-stringhelp.c - Regression tests for stringhelp.c
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #ifdef HAVE_PWD_H
26 # include <pwd.h>
27 #endif
28 #include <unistd.h>
29 #include <sys/types.h>
31 #include "stringhelp.h"
33 #include "t-support.h"
36 static char *home_buffer;
39 const char *
40 gethome (void)
42 if (!home_buffer)
44 char *home = getenv("HOME");
46 #if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
47 if(home)
48 home_buffer = xstrdup (home);
49 else
51 struct passwd *pwd;
53 pwd = getpwuid (getuid());
54 if (pwd)
55 home_buffer = xstrdup (pwd->pw_dir);
57 #endif
59 return home_buffer;
63 static void
64 test_percent_escape (void)
66 char *result;
67 static struct {
68 const char *extra;
69 const char *value;
70 const char *expected;
71 } tests[] =
73 { NULL, "", "" },
74 { NULL, "%", "%25" },
75 { NULL, "%%", "%25%25" },
76 { NULL, " %", " %25" },
77 { NULL, ":", "%3a" },
78 { NULL, " :", " %3a" },
79 { NULL, ": ", "%3a " },
80 { NULL, " : ", " %3a " },
81 { NULL, "::", "%3a%3a" },
82 { NULL, ": :", "%3a %3a" },
83 { NULL, "%:", "%25%3a" },
84 { NULL, ":%", "%3a%25" },
85 { "\\\n:", ":%", "%3a%25" },
86 { "\\\n:", "\\:%", "%5c%3a%25" },
87 { "\\\n:", "\n:%", "%0a%3a%25" },
88 { "\\\n:", "\xff:%", "\xff%3a%25" },
89 { "\\\n:", "\xfe:%", "\xfe%3a%25" },
90 { "\\\n:", "\x01:%", "\x01%3a%25" },
91 { "\x01", "\x01:%", "%01%3a%25" },
92 { "\xfe", "\xfe:%", "%fe%3a%25" },
93 { "\xfe", "\xff:%", "\xff%3a%25" },
95 { NULL, NULL, NULL }
97 int testno;
99 result = percent_escape (NULL, NULL);
100 if (result)
101 fail (0);
102 for (testno=0; tests[testno].value; testno++)
104 result = percent_escape (tests[testno].value, tests[testno].extra);
105 if (!result)
106 fail (testno);
107 if (strcmp (result, tests[testno].expected))
108 fail (testno);
109 xfree (result);
115 static void
116 test_compare_filenames (void)
118 struct {
119 const char *a;
120 const char *b;
121 int result;
122 } tests[] = {
123 { "", "", 0 },
124 { "", "a", -1 },
125 { "a", "", 1 },
126 { "a", "a", 0 },
127 { "a", "aa", -1 },
128 { "aa", "a", 1 },
129 { "a", "b", -1 },
131 #ifdef HAVE_W32_SYSTEM
132 { "a", "A", 0 },
133 { "A", "a", 0 },
134 { "foo/bar", "foo\\bar", 0 },
135 { "foo\\bar", "foo/bar", 0 },
136 { "foo\\", "foo/", 0 },
137 { "foo/", "foo\\", 0 },
138 #endif /*HAVE_W32_SYSTEM*/
139 { NULL, NULL, 0}
141 int testno, result;
143 for (testno=0; tests[testno].a; testno++)
145 result = compare_filenames (tests[testno].a, tests[testno].b);
146 result = result < 0? -1 : result > 0? 1 : 0;
147 if (result != tests[testno].result)
148 fail (testno);
153 static void
154 test_strconcat (void)
156 char *out;
158 out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
159 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
160 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
161 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
162 "1", "2", "3", "4", "5", "6", "7", NULL);
163 if (!out)
164 fail (0);
165 else
166 xfree (out);
167 out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
168 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
169 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
170 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
171 "1", "2", "3", "4", "5", "6", "7", "8", NULL);
172 if (out)
173 fail (0);
174 else if (errno != EINVAL)
175 fail (0);
177 out = strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
178 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
179 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
180 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
181 "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL);
182 if (out)
183 fail (0);
184 else if (errno != EINVAL)
185 fail (0);
187 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
188 out = strconcat (NULL);
189 if (!out || *out)
190 fail (1);
191 #endif
192 out = strconcat (NULL, NULL);
193 if (!out || *out)
194 fail (1);
195 out = strconcat ("", NULL);
196 if (!out || *out)
197 fail (1);
198 xfree (out);
200 out = strconcat ("", "", NULL);
201 if (!out || *out)
202 fail (2);
203 xfree (out);
205 out = strconcat ("a", "b", NULL);
206 if (!out || strcmp (out, "ab"))
207 fail (3);
208 xfree (out);
209 out = strconcat ("a", "b", "c", NULL);
210 if (!out || strcmp (out, "abc"))
211 fail (3);
212 xfree (out);
214 out = strconcat ("a", "b", "cc", NULL);
215 if (!out || strcmp (out, "abcc"))
216 fail (4);
217 xfree (out);
218 out = strconcat ("a1", "b1", "c1", NULL);
219 if (!out || strcmp (out, "a1b1c1"))
220 fail (4);
221 xfree (out);
223 out = strconcat ("", " long b ", "", "--even-longer--", NULL);
224 if (!out || strcmp (out, " long b --even-longer--"))
225 fail (5);
226 xfree (out);
228 out = strconcat ("", " long b ", "", "--even-longer--", NULL);
229 if (!out || strcmp (out, " long b --even-longer--"))
230 fail (5);
231 xfree (out);
234 static void
235 test_xstrconcat (void)
237 char *out;
239 out = xstrconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
240 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
241 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
242 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
243 "1", "2", "3", "4", "5", "6", "7", NULL);
244 if (!out)
245 fail (0);
247 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
248 out = xstrconcat (NULL);
249 if (!out)
250 fail (1);
251 #endif
252 out = xstrconcat (NULL, NULL);
253 if (!out)
254 fail (1);
255 out = xstrconcat ("", NULL);
256 if (!out || *out)
257 fail (1);
258 xfree (out);
260 out = xstrconcat ("", "", NULL);
261 if (!out || *out)
262 fail (2);
263 xfree (out);
265 out = xstrconcat ("a", "b", NULL);
266 if (!out || strcmp (out, "ab"))
267 fail (3);
268 xfree (out);
269 out = xstrconcat ("a", "b", "c", NULL);
270 if (!out || strcmp (out, "abc"))
271 fail (3);
272 xfree (out);
274 out = xstrconcat ("a", "b", "cc", NULL);
275 if (!out || strcmp (out, "abcc"))
276 fail (4);
277 xfree (out);
278 out = xstrconcat ("a1", "b1", "c1", NULL);
279 if (!out || strcmp (out, "a1b1c1"))
280 fail (4);
281 xfree (out);
283 out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
284 if (!out || strcmp (out, " long b --even-longer--"))
285 fail (5);
286 xfree (out);
288 out = xstrconcat ("", " long b ", "", "--even-longer--", NULL);
289 if (!out || strcmp (out, " long b --even-longer--"))
290 fail (5);
291 xfree (out);
295 static void
296 test_make_filename_try (void)
298 char *out;
299 const char *home = gethome ();
300 size_t homelen = home? strlen (home):0;
302 out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
303 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
304 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
305 "1", "2", "3", NULL);
306 if (out)
307 fail (0);
308 else if (errno != EINVAL)
309 fail (0);
310 xfree (out);
311 out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
312 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
313 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
314 "1", "2", "3", "4", NULL);
315 if (out)
316 fail (0);
317 else if (errno != EINVAL)
318 fail (0);
319 xfree (out);
321 out = make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
322 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
323 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
324 "1", "2", NULL);
325 if (!out || strcmp (out,
326 "1/2/3/4/5/6/7/8/9/10/"
327 "1/2/3/4/5/6/7/8/9/10/"
328 "1/2/3/4/5/6/7/8/9/10/"
329 "1/2"))
330 fail (0);
331 xfree (out);
333 out = make_filename_try ("foo", "~/bar", "baz/cde", NULL);
334 if (!out || strcmp (out, "foo/~/bar/baz/cde"))
335 fail (1);
336 xfree (out);
338 out = make_filename_try ("foo", "~/bar", "baz/cde/", NULL);
339 if (!out || strcmp (out, "foo/~/bar/baz/cde/"))
340 fail (1);
341 xfree (out);
343 out = make_filename_try ("/foo", "~/bar", "baz/cde/", NULL);
344 if (!out || strcmp (out, "/foo/~/bar/baz/cde/"))
345 fail (1);
346 xfree (out);
348 out = make_filename_try ("//foo", "~/bar", "baz/cde/", NULL);
349 if (!out || strcmp (out, "//foo/~/bar/baz/cde/"))
350 fail (1);
351 xfree (out);
353 out = make_filename_try ("", "~/bar", "baz/cde", NULL);
354 if (!out || strcmp (out, "/~/bar/baz/cde"))
355 fail (1);
356 xfree (out);
359 out = make_filename_try ("~/foo", "bar", NULL);
360 if (!out)
361 fail (2);
362 if (home)
364 if (strlen (out) < homelen + 7)
365 fail (2);
366 if (strncmp (out, home, homelen))
367 fail (2);
368 if (strcmp (out+homelen, "/foo/bar"))
369 fail (2);
371 else
373 if (strcmp (out, "~/foo/bar"))
374 fail (2);
376 xfree (out);
378 out = make_filename_try ("~", "bar", NULL);
379 if (!out)
380 fail (2);
381 if (home)
383 if (strlen (out) < homelen + 3)
384 fail (2);
385 if (strncmp (out, home, homelen))
386 fail (2);
387 if (strcmp (out+homelen, "/bar"))
388 fail (2);
390 else
392 if (strcmp (out, "~/bar"))
393 fail (2);
395 xfree (out);
400 main (int argc, char **argv)
402 (void)argc;
403 (void)argv;
405 test_percent_escape ();
406 test_compare_filenames ();
407 test_strconcat ();
408 test_xstrconcat ();
409 test_make_filename_try ();
411 xfree (home_buffer);
412 return 0;