Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / tests / test_silcstringprep.c
1 /* Stringprep tests */
2
3 #include "silcincludes.h"
4
5 typedef struct {
6   const char *comment;
7   const char *in;
8   const char *out;
9   int ret;
10   int enc;
11 } test_st;
12
13 const test_st tests[] = {
14   {"Prohibited *",
15    "foo*", "", SILC_STRINGPREP_ERR_PROHIBITED},
16   {"Prohibited ?",
17    "?foo", "", SILC_STRINGPREP_ERR_PROHIBITED},
18   {"Prohibited ,",
19    "f,f", "", SILC_STRINGPREP_ERR_PROHIBITED},
20   {"Prohibited !",
21    "!", "", SILC_STRINGPREP_ERR_PROHIBITED},
22   {"Prohibited @",
23    "foo@faa", "", SILC_STRINGPREP_ERR_PROHIBITED},
24   {"Normal casefold",
25    "Foobbeli-BofJFlkJDF", "foobbeli-bofjflkjdf"},
26   {"Nothing",
27    "sauna.silcnet.org", "sauna.silcnet.org"},
28   {"Nothing with #",
29    "#silc", "#silc"},
30   {"Locale test",
31    "Päivää", "päivää", 0, SILC_STRING_LOCALE},
32   {"Locale test2",
33    "#öäöö/&#\\#(&(&#(.äöäÄÖäÄÖÄÖ^'", 
34    "#öäöö/&#\\#(&(&#(.äöääöääöäö^'", 0, SILC_STRING_LOCALE},
35
36   /* Some libidn tests */
37   {"Map to nothing",
38    "foo\xC2\xAD\xCD\x8F\xE1\xA0\x86\xE1\xA0\x8B"
39    "bar" "\xE2\x80\x8B\xE2\x81\xA0" "baz\xEF\xB8\x80\xEF\xB8\x88"
40    "\xEF\xB8\x8F\xEF\xBB\xBF", "foobarbaz"},
41   {"Case folding ASCII U+0043 U+0041 U+0046 U+0045", "CAFE", "cafe"},
42   {"Case folding 8bit U+00DF (german sharp s)", "\xC3\x9F", "ss"},
43   {"Case folding U+0130 (turkish capital I with dot)",
44    "\xC4\xB0", "i\xcc\x87"},
45   {"ASCII space character U+0020", "\x20", "\x20",
46    SILC_STRINGPREP_ERR_PROHIBITED},
47   {"ASCII control characters U+0010 U+007F", "\x10\x7F", "\x10\x7F",
48    SILC_STRINGPREP_ERR_PROHIBITED},
49 };
50
51 const test_st tests_norm[] = {
52   {"Casefold 1",
53    "Pekka Riikonen", "pekka riikonen"},
54   {"Casefold 2",
55    "PEKKA RIIKONEN", "pekka riikonen"},
56   {"Casefold 3",
57    "pekka riikonen", "pekka riikonen"},
58   {"Casefold 4",
59    "#ksPPPAA", "#kspppaa"},
60   {"Normal casefold",
61    "Foobbeli-BofJFlkJDF", "foobbeli-bofjflkjdf"},
62   {"Nothing",
63    "sauna.silcnet.org", "sauna.silcnet.org"},
64   {"Locale test",
65    "Päivää", "päivää", 0, SILC_STRING_LOCALE},
66   {"Locale test2",
67    "#öäöö/&#\\#(&(&#(.äöäÄÖäÄÖÄÖ^'", 
68    "#öäöö/&#\\#(&(&#(.äöääöääöäö^'", 0, SILC_STRING_LOCALE},
69 };
70
71 int main(int argc, char **argv)
72 {
73   bool success = FALSE;
74   int i, enc;
75   unsigned char *out = NULL;
76   SilcUInt32 out_len;
77   SilcStringprepStatus ret;
78
79   if (argc > 1 && !strcmp(argv[1], "-d")) {
80     silc_debug = 1;
81     silc_debug_hexdump = 1;
82     silc_log_set_debug_string("*stringprep*,*utf8*");
83   }
84
85   SILC_LOG_DEBUG(("--- Identifier string tests"));
86
87   for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
88     SILC_LOG_DEBUG(("Test case %d", i));
89     SILC_LOG_DEBUG((" %d: %s", i, tests[i].comment));
90     SILC_LOG_DEBUG((" %d: in: %s", i, tests[i].in));
91     SILC_LOG_DEBUG((" %d: out: %s", i, tests[i].out));
92     SILC_LOG_DEBUG((" %d: ret: %d", i, tests[i].ret));
93
94     if (!tests[i].enc)
95       enc = SILC_STRING_UTF8;
96     else
97       enc = tests[i].enc;
98     ret = silc_stringprep(tests[i].in, strlen(tests[i].in),
99                           enc, SILC_IDENTIFIER_PREP, 0,
100                           &out, &out_len, enc);
101     if (ret != SILC_STRINGPREP_OK) {
102       if (tests[i].ret != SILC_STRINGPREP_OK) {
103         SILC_LOG_DEBUG((" %d: Expected ret %d", i, ret));
104       } else {
105         SILC_LOG_DEBUG(("%d: Error: %d", i, ret));
106         goto err;
107       }
108     } else {
109       SILC_LOG_DEBUG((" %d: prepared out: %s", i, out));
110       SILC_LOG_HEXDUMP((" %d: prepared dump", i), out, out_len);
111       if (memcmp(out, tests[i].out, out_len)) {
112         SILC_LOG_DEBUG((" %d: Output mismatch", i));
113         goto err;
114       }
115     }
116     SILC_LOG_DEBUG((" %d: Output match", i));
117
118     silc_free(out);
119     out = NULL;
120   }
121
122   SILC_LOG_DEBUG(("--- Casefold tests"));
123
124   for (i = 0; i < sizeof(tests_norm) / sizeof(tests_norm[0]); i++) {
125     SILC_LOG_DEBUG(("Test case %d", i));
126     SILC_LOG_DEBUG((" %d: %s", i, tests_norm[i].comment));
127     SILC_LOG_DEBUG((" %d: in: %s", i, tests_norm[i].in));
128     SILC_LOG_DEBUG((" %d: out: %s", i, tests_norm[i].out));
129     SILC_LOG_DEBUG((" %d: ret: %d", i, tests_norm[i].ret));
130
131     if (!tests_norm[i].enc)
132       enc = SILC_STRING_UTF8;
133     else
134       enc = tests_norm[i].enc;
135     ret = silc_stringprep(tests_norm[i].in, strlen(tests_norm[i].in),
136                           enc, SILC_CASEFOLD_PREP, 0,
137                           &out, &out_len, enc);
138     if (ret != SILC_STRINGPREP_OK) {
139       if (tests_norm[i].ret != SILC_STRINGPREP_OK) {
140         SILC_LOG_DEBUG((" %d: Expected ret %d", i, ret));
141       } else {
142         SILC_LOG_DEBUG(("%d: Error: %d", i, ret));
143         goto err;
144       }
145     } else {
146       SILC_LOG_DEBUG((" %d: prepared out: %s", i, out));
147       SILC_LOG_HEXDUMP((" %d: prepared dump", i), out, out_len);
148       if (memcmp(out, tests_norm[i].out, out_len)) {
149         SILC_LOG_DEBUG((" %d: Output mismatch", i));
150         goto err;
151       }
152     }
153     SILC_LOG_DEBUG((" %d: Output match", i));
154
155     silc_free(out);
156     out = NULL;
157   }
158
159   success = TRUE;
160
161  err:
162   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
163   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
164
165   return success;
166 }