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