Fixed bug#920
[gnupg.git] / jnlib / t-stringhelp.c
blobf6590230edf02c479710580daec83bccc8e8fe41
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>
25 #include "stringhelp.h"
27 #include "t-support.h"
31 static void
32 test_percent_escape (void)
34 char *result;
35 static struct {
36 const char *extra;
37 const char *value;
38 const char *expected;
39 } tests[] =
41 { NULL, "", "" },
42 { NULL, "%", "%25" },
43 { NULL, "%%", "%25%25" },
44 { NULL, " %", " %25" },
45 { NULL, ":", "%3a" },
46 { NULL, " :", " %3a" },
47 { NULL, ": ", "%3a " },
48 { NULL, " : ", " %3a " },
49 { NULL, "::", "%3a%3a" },
50 { NULL, ": :", "%3a %3a" },
51 { NULL, "%:", "%25%3a" },
52 { NULL, ":%", "%3a%25" },
53 { "\\\n:", ":%", "%3a%25" },
54 { "\\\n:", "\\:%", "%5c%3a%25" },
55 { "\\\n:", "\n:%", "%0a%3a%25" },
56 { "\\\n:", "\xff:%", "\xff%3a%25" },
57 { "\\\n:", "\xfe:%", "\xfe%3a%25" },
58 { "\\\n:", "\x01:%", "\x01%3a%25" },
59 { "\x01", "\x01:%", "%01%3a%25" },
60 { "\xfe", "\xfe:%", "%fe%3a%25" },
61 { "\xfe", "\xff:%", "\xff%3a%25" },
63 { NULL, NULL, NULL }
65 int testno;
67 result = percent_escape (NULL, NULL);
68 if (result)
69 fail (0);
70 for (testno=0; tests[testno].value; testno++)
72 result = percent_escape (tests[testno].value, tests[testno].extra);
73 if (!result)
74 fail (testno);
75 if (strcmp (result, tests[testno].expected))
76 fail (testno);
77 xfree (result);
83 static void
84 test_compare_filenames (void)
86 struct {
87 const char *a;
88 const char *b;
89 int result;
90 } tests[] = {
91 { "", "", 0 },
92 { "", "a", -1 },
93 { "a", "", 1 },
94 { "a", "a", 0 },
95 { "a", "aa", -1 },
96 { "aa", "a", 1 },
97 { "a", "b", -1 },
99 #ifdef HAVE_W32_SYSTEM
100 { "a", "A", 0 },
101 { "A", "a", 0 },
102 { "foo/bar", "foo\\bar", 0 },
103 { "foo\\bar", "foo/bar", 0 },
104 { "foo\\", "foo/", 0 },
105 { "foo/", "foo\\", 0 },
106 #endif /*HAVE_W32_SYSTEM*/
107 { NULL, NULL, 0}
109 int testno, result;
111 for (testno=0; tests[testno].a; testno++)
113 result = compare_filenames (tests[testno].a, tests[testno].b);
114 result = result < 0? -1 : result > 0? 1 : 0;
115 if (result != tests[testno].result)
116 fail (testno);
122 main (int argc, char **argv)
124 (void)argc;
125 (void)argv;
127 test_percent_escape ();
128 test_compare_filenames ();
130 return 0;