1 /*	$NetBSD: test-utf8.c,v 1.1.1.1 2011/04/13 18:16:00 elric Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <err.h>
42 #include "windlocl.h"
43 
44 static const char *failing_testcases[] = {
45     "\x80",
46     "\xFF",
47     "\xC0",
48     "\xDF",
49     "\xE0",
50     "\xEF",
51     "\xF0",
52     "\xF7",
53     "\xC0\x01",
54     "\xC0\x7F",
55     "\xC0\xFF",
56     "\xC0\x80\x80",
57     "\xE0\x01",
58     "\xE0\x7F",
59     "\xE0\x80",
60     "\xE0\xFF",
61     "\xE0\x80\x20",
62     "\xE0\x80\xFF",
63     "\xE0\x80\x80\x80",
64     "\xF0\x01",
65     "\xF0\x80",
66     "\xF0\x80\x01",
67     "\xF0\x80\x80",
68     "\xF0\x80\x80\x01",
69     "\xF0\x80\x80\xFF",
70     NULL
71 };
72 
73 #define MAX_LENGTH 10
74 
75 struct testcase {
76     const char *utf8_str;
77     size_t len;
78     uint32_t u[MAX_LENGTH];
79     int invalid_ucs2;
80 };
81 
82 static const struct testcase testcases[] = {
83     {"", 0, {0}},
84     {"\x01", 1, {1}},
85     {"\x7F", 1, {0x7F}},
86     {"\x01\x7F", 2, {0x01, 0x7F}},
87     {"\xC0\x80", 1, {0}},
88     {"\xC0\x81", 1, {1}},
89     {"\xC1\x80", 1, {0x40}},
90     {"\xDF\xBF", 1, {0x7FF}},
91     {"\xE0\x80\x80", 1, {0}},
92     {"\xE0\x80\x81", 1, {1}},
93     {"\xE0\x81\x80", 1, {0x40}},
94     {"\xE1\x80\x80", 1, {0x1000}},
95     {"\xEF\xBF\xBF", 1, {0xFFFF}},
96     {"\xF0\x80\x80\x80", 1, {0}},
97     {"\xF0\x80\x80\x81", 1, {1}},
98     {"\xF0\x80\x81\x80", 1, {0x40}},
99     {"\xF0\x81\x80\x80", 1, {0x1000}},
100     {"\xF1\x80\x80\x80", 1, {0x40000}},
101     {"\xF7\xBF\xBF\xBF", 1, {0X1FFFFF}, 1},
102 };
103 
104 int
105 main(void)
106 {
107     unsigned failures = 0;
108     unsigned i;
109     const char **s;
110     int ret;
111     size_t len, len2;
112     uint32_t u[MAX_LENGTH];
113     char str[MAX_LENGTH * 4];
114 
115     for (s = failing_testcases; *s != NULL; ++s) {
116 	len = MAX_LENGTH;
117 	ret = wind_utf8ucs4(*s, u, &len);
118 	if (ret == 0) {
119 	    printf("utf8 decode of \"%s\" should have failed\n", *s);
120 	    ++failures;
121 	}
122     }
123 
124     for (i = 0; i < sizeof(testcases)/sizeof(testcases[0]); ++i) {
125 	const struct testcase *t = &testcases[i];
126 
127 	ret = wind_utf8ucs4_length(t->utf8_str, &len);
128 	if (ret) {
129 	    printf("utf8ucs4 length of \"%s\" should have succeeded\n",
130 		   t->utf8_str);
131 	    ++failures;
132 	    continue;
133 	}
134 	if (len != t->len) {
135 	    printf("utf8ucs4_length of \"%s\" has wrong length: "
136 		   "expected: %u, actual: %u\n",
137 		   t->utf8_str, (unsigned int)t->len, (unsigned int)len);
138 	    ++failures;
139 	    continue;
140 	}
141 
142 	len = MAX_LENGTH;
143 	ret = wind_utf8ucs4(t->utf8_str, u, &len);
144 	if (ret) {
145 	    printf("utf8 decode of \"%s\" should have succeeded\n",
146 		   t->utf8_str);
147 	    ++failures;
148 	    continue;
149 	}
150 	if (len != t->len) {
151 	    printf("utf8 decode of \"%s\" has wrong length: "
152 		   "expected: %u, actual: %u\n",
153 		   t->utf8_str, (unsigned int)t->len, (unsigned int)len);
154 	    ++failures;
155 	    continue;
156 	}
157 	if (memcmp(t->u, u, len * sizeof(uint32_t)) != 0) {
158 	    printf("utf8 decode of \"%s\" has wrong data\n",
159 		   t->utf8_str);
160 	    ++failures;
161 	    continue;
162 	}
163 	if (t->invalid_ucs2 == 0) {
164 	    len2 = sizeof(str);
165 	    ret = wind_ucs4utf8(u, len, str, &len2);
166 	    if (ret) {
167 		printf("ucs4 decode of \"%s\" should have succeeded\n",
168 		       t->utf8_str);
169 		++failures;
170 		continue;
171 	    }
172 	}
173     }
174 
175     return failures != 0;
176 }
177