keysym-utf: reject out-of-range Unicode codepoints in xkb_keysym_to_utf{8,32}
[platform/upstream/libxkbcommon.git] / test / utf8.c
1 /*
2  * Copyright © 2014 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <string.h>
29
30 #include "utf8.h"
31 #include "utils.h"
32
33 #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1))
34 #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1))
35
36 static void
37 test_is_valid_utf8(void)
38 {
39     /*
40      * Mostly taken from:
41      * https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
42      */
43
44     VALID("ascii");
45     VALID("\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5");
46
47     VALID("");
48     VALID("\x00");
49     VALID("\x00\x00");
50
51     VALID("\x50");
52     VALID("\xC2\x80");
53     VALID("\xE0\xA0\x80");
54     VALID("\xF0\x90\x80\x80");
55
56     /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
57     INVALID("\xF8\x88\x80\x80\x80");
58     INVALID("\xFC\x84\x80\x80\x80\x80");
59
60     VALID("\x7F");
61     VALID("\xDF\xBF");
62     VALID("\xEF\xBF\xBF");
63     /* VALID("\xF7\xBF\xBF\xBF"); */
64
65     /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
66     INVALID("\xFB\xBF\xBF\xBF\xBF");
67     INVALID("\xFD\xBFxBF\xBF\xBF");
68
69     VALID("\xED\x9F\xBF");
70     VALID("\xEE\x80\x80");
71     VALID("\xEF\xBF\xBD");
72     VALID("\xF4\x8F\xBF\xBF");
73     /* VALID("\xF4\x90\x80\x80"); */
74
75     INVALID("\x80");
76     INVALID("\xBF");
77     INVALID("\x80\xBF");
78     INVALID("\x80\xBF\x80");
79     INVALID("\x80\xBF\x80\xBF");
80     INVALID("\x80\xBF\x80\xBF\x80");
81     INVALID("\x80\xBF\x80\xBF\x80\xBF");
82     INVALID("\x80\xBF\x80\xBF\x80\xBF\x80");
83     INVALID("\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
84             "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
85             "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
86             "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
87
88     INVALID("\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 \xC8 \xC9 \xCA \xCB \xCC "
89             "\xCD \xCE \xCF "
90             "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 \xD8 \xD9 \xDA \xDB \xDD "
91             "\xDD \xDE \xDF ");
92     INVALID("\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ");
93     INVALID("\xF8 \xF9 \xFA \xFB ");
94     INVALID("\xFC \xFD ");
95
96     INVALID("\xC0");
97     INVALID("\xE0\x80");
98     INVALID("\xF0\x80\x80");
99     INVALID("\xF8\x80\x80\x80");
100     INVALID("\xFC\x80\x80\x80\x80");
101     INVALID("\xDF");
102     INVALID("\xEF\xBF");
103     INVALID("\xF7\xBF\xBF");
104     INVALID("\xFB\xBF\xBF\xBF");
105     INVALID("\xFD\xBF\xBF\xBF\xBF");
106
107     INVALID("\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
108             "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF");
109
110     INVALID("\xFE");
111     INVALID("\xFF");
112     INVALID("\xFE\xFE\xFF\xFF");
113
114     INVALID("\xC0\xAF");
115     INVALID("\xE0\x80\xAF");
116     INVALID("\xF0\x80\x80\xAF");
117     INVALID("\xF8\x80\x80\x80\xAF");
118     INVALID("\xFC\x80\x80\x80\x80\xAF");
119
120     INVALID("\xC1\xBF");
121     INVALID("\xE0\x9F\xBF");
122     INVALID("\xF0\x8F\xBF\xBF");
123     INVALID("\xF8\x87\xBF\xBF\xBF");
124     INVALID("\xFC\x83\xBF\xBF\xBF\xBF");
125
126     INVALID("\xC0\x80");
127     INVALID("\xE0\x80\x80");
128     INVALID("\xF0\x80\x80\x80");
129     INVALID("\xF8\x80\x80\x80\x80");
130     INVALID("\xFC\x80\x80\x80\x80\x80");
131
132     INVALID("\xED\xA0\x80");
133     INVALID("\xED\xAD\xBF");
134     INVALID("\xED\xAE\x80");
135     INVALID("\xED\xAF\xBF");
136     INVALID("\xED\xB0\x80");
137     INVALID("\xED\xBE\x80");
138     INVALID("\xED\xBF\xBF");
139
140     INVALID("\xED\xA0\x80\xED\xB0\x80");
141     INVALID("\xED\xA0\x80\xED\xBF\xBF");
142     INVALID("\xED\xAD\xBF\xED\xB0\x80");
143     INVALID("\xED\xAD\xBF\xED\xBF\xBF");
144     INVALID("\xED\xAE\x80\xED\xB0\x80");
145     INVALID("\xED\xAE\x80\xED\xBF\xBF");
146     INVALID("\xED\xAF\xBF\xED\xB0\x80");
147     INVALID("\xED\xAF\xBF\xED\xBF\xBF");
148
149     /* INVALID("\xEF\xBF\xBE"); */
150     /* INVALID("\xEF\xBF\xBF"); */
151 }
152
153 static void
154 check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) {
155     char buffer[7];
156     int length;
157
158     length = utf32_to_utf8(unichar, buffer);
159
160     assert(length == expected_length);
161     assert(streq(buffer, expected));
162 }
163
164 static void
165 test_utf32_to_utf8(void)
166 {
167     check_utf32_to_utf8(0x0, 2, "");
168     check_utf32_to_utf8(0x40, 2, "\x40");
169     check_utf32_to_utf8(0xA1, 3, "\xc2\xa1");
170     check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81");
171     check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84");
172     check_utf32_to_utf8(0x110000, 0, "");
173     check_utf32_to_utf8(0xffffffff, 0, "");
174 }
175
176 int
177 main(void)
178 {
179     test_is_valid_utf8();
180     test_utf32_to_utf8();
181
182     return 0;
183 }