Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcstrutil.c
1 /* UTF-8 decoding tests */
2 /* Other string util tests too */
3
4 #include "silc.h"
5
6 #define utf8fail(n, data, len)                  \
7 const unsigned char u##n[] = (data);            \
8 int u##n##l = len;
9
10 #define utf8failc(n)                                                          \
11 do {                                                                          \
12   if (!silc_utf8_valid(u##n, u##n##l))                                        \
13     SILC_LOG_DEBUG(("%d: not valid UTF-8, correctly detected, no error", n)); \
14   else {                                                                      \
15     SILC_LOG_DEBUG(("%d: parser did not detect malformed UTF-8, error", n));  \
16     goto err;                                                                 \
17   }                                                                           \
18 } while(0)
19
20 /* UTF-8 Test vectors that MUST fail */
21 utf8fail(1, "\x80", 1);
22 utf8fail(2, "\xbf", 1);
23 utf8fail(3, "\xfe", 1);
24 utf8fail(4, "\xff", 1);
25 utf8fail(5, "\xfe\xfe\xff\xff", 4);
26 utf8fail(6, "\xc0\xa0", 2);
27 utf8fail(7, "\xe0\x80\xaf", 3);
28 utf8fail(8, "\xf0\x80\x80\xaf", 4);
29 utf8fail(9, "\xf8\x80\x80\x80\xaf", 5);
30 utf8fail(10, "\xfc\x80\x80\x80\x80\xaf", 6);
31 utf8fail(11, "\xc0\x80", 2);
32 utf8fail(12, "\xe0\x80\x80", 3);
33 utf8fail(13, "\xf0\x80\x80\x80", 4);
34 utf8fail(14, "\xf8\x80\x80\x80\x80", 5);
35 utf8fail(15, "\xfc\x80\x80\x80\x80\x80", 6);
36 utf8fail(16, "\xc1\xbf", 2);
37 utf8fail(17, "\xe0\x9f\xbf", 3);
38 utf8fail(18, "\xf0\x8f\xbf\xbf", 4);
39 utf8fail(19, "\xf8\x87\xbf\xbf\xbf", 5);
40 utf8fail(20, "\xfc\x83\xbf\xbf\xbf\xbf", 6);
41 utf8fail(21, "\xed\xa0\x80", 3);
42 utf8fail(22, "\xed\xad\xbf", 3);
43 utf8fail(23, "\xed\xae\x80", 3);
44 utf8fail(24, "\xed\xaf\xbf", 3);
45 utf8fail(25, "\xed\xb0\x80", 3);
46 utf8fail(26, "\xed\xbe\x80", 3);
47 utf8fail(27, "\xed\xbf\xbf", 3);
48 utf8fail(28, "\xfc\x20\xfd\x20", 4);
49 utf8fail(29, "\xf8\xf9\xfa\xfb", 4);
50 utf8fail(30, "\xf0\x20\xf9\x20\xfa\x20\xfb\x20", 8);
51
52 char *render(void *data)
53 {
54   char *buf = data;
55   return strdup(buf);
56 }
57
58 int main(int argc, char **argv)
59 {
60   SilcBool success = FALSE;
61   unsigned char *s1, *s2, *s3, *s4;
62   unsigned char t[16];
63   char h[32 + 1], str[40];
64   int l, opt, i;
65   SilcUInt32 len;
66
67   while ((opt = getopt(argc, argv, "hVd:")) != EOF) {
68       switch(opt) {
69         case 'h':
70           printf("usage: test_silcstrutil\n");
71           exit(0);
72           break;
73         case 'V':
74           printf("Secure Internet Live Conferencing\n");
75           exit(0);
76           break;
77         case 'd':
78           silc_log_debug(TRUE);
79           silc_log_debug_hexdump(TRUE);
80           silc_log_quick(TRUE);
81           if (optarg)
82             silc_log_set_debug_string(optarg);
83           else
84             silc_log_set_debug_string("*strutil*,*errno*");
85           break;
86         default:
87           exit(1);
88           break;
89       }
90   }
91
92   /* Failure tests */
93   utf8failc(1);  utf8failc(2);
94   utf8failc(3);  utf8failc(4);
95   utf8failc(5);  utf8failc(6);
96   utf8failc(7);  utf8failc(8);
97   utf8failc(9);  utf8failc(10);
98   utf8failc(11);  utf8failc(12);
99   utf8failc(13);  utf8failc(14);
100   utf8failc(15);  utf8failc(16);
101   utf8failc(17);  utf8failc(18);
102   utf8failc(19);  utf8failc(20);
103   utf8failc(21);  utf8failc(22);
104   utf8failc(23);  utf8failc(24);
105   utf8failc(25);  utf8failc(26);
106   utf8failc(27);  utf8failc(28);
107   utf8failc(29);  utf8failc(30);
108
109   /* LDAP DN simple test */
110   s1 = "#&?*Pekka, \\Riikonen, <foobar@foobar.com>\xc4\x8d\\ ";
111   SILC_LOG_DEBUG(("s1 = %s", s1));
112
113   /* To LDAP DN */
114   l = silc_utf8_decoded_len(s1, strlen(s1), SILC_STRING_LDAP_DN);
115   if (!l)
116     goto err;
117   s3 = silc_calloc(l + 1, sizeof(*s3));
118   silc_utf8_decode(s1, strlen(s1), SILC_STRING_LDAP_DN, s3, l);
119   SILC_LOG_DEBUG(("ldapdn = %s", s3));
120
121   /* To UTF-8 */
122   l = silc_utf8_encoded_len(s3, strlen(s3), SILC_STRING_LDAP_DN);
123   if (!l)
124     goto err;  
125   s4 = silc_calloc(l + 1, sizeof(*s4));
126   silc_utf8_encode(s3, strlen(s3), SILC_STRING_LDAP_DN, s4, l);
127   SILC_LOG_DEBUG(("utf8 = %s", s4));
128
129   if (memcmp(s4, s1, strlen(s4))) {
130     SILC_LOG_DEBUG(("UTF-8 mismatch"));
131     goto err;
132   }
133   silc_free(s3);
134   silc_free(s4);
135
136   /* UTF-8 strcasecmp test */
137   SILC_LOG_DEBUG(("silc_utf8_strcasecmp test"));
138   s1 = "Päivää vuan Yrjö";
139   s2 = "PÄIVÄÄ VUAN YRJÖ";
140   l = silc_utf8_encoded_len(s1, strlen(s1), SILC_STRING_LOCALE);
141   if (!l)
142     goto err;  
143   s3 = silc_calloc(l + 1, sizeof(*s3));
144   silc_utf8_encode(s1, strlen(s1), SILC_STRING_LOCALE, s3, l);
145
146   l = silc_utf8_encoded_len(s2, strlen(s2), SILC_STRING_LOCALE);
147   if (!l)
148     goto err;  
149   s4 = silc_calloc(l + 1, sizeof(*s4));
150   silc_utf8_encode(s2, strlen(s2), SILC_STRING_LOCALE, s4, l);
151
152   SILC_LOG_DEBUG(("%s == %s", s3, s4));
153   if (!silc_utf8_strcasecmp(s3, s4)) {
154     SILC_LOG_DEBUG(("mismatch"));
155     goto err;
156   }
157   SILC_LOG_DEBUG(("match"));
158
159   silc_free(s3);
160   silc_free(s4);
161
162   /* Regex test */
163   SILC_LOG_DEBUG(("Simple regex test"));
164   s1 = "foo,bar,silc,com";
165   SILC_LOG_DEBUG(("Find 'silc' from %s", s1));
166   if (!silc_string_match(s1, "silc"))
167     goto err;
168   SILC_LOG_DEBUG(("Regex match"));
169   SILC_LOG_DEBUG(("Find 'foobar' from %s", s1));
170   if (silc_string_match(s1, "foobar"))
171     goto err;
172   SILC_LOG_DEBUG(("Regex not found (Ok)"));
173
174   /* HEX to data, data to HEX tests */
175   for (i = 0; i < sizeof(t); i++)
176     t[i] = i;
177   silc_data2hex(t, sizeof(t), h, sizeof(h));
178   silc_hex2data(h, t, sizeof(t), &len);
179   silc_snprintf(h, sizeof(h), "010203ffabdef9ab");
180   silc_hex2data(h, t, sizeof(t), &len);
181   silc_data2hex(t, sizeof(t), h, sizeof(h));
182
183   /* snprintf test */
184   silc_snprintf(str, sizeof(str), "This is %@ rendered\n",
185                 render, "automatically");
186   SILC_LOG_DEBUG((str));
187   SILC_LOG_DEBUG(("This too %@ rendered", render, "is automatically"));
188
189   success = TRUE;
190
191  err:
192   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
193   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
194
195   return success;
196 }