1 /* Copyright The libuv project and contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "task.h"
23 #include "../src/idna.h"
24 #include <string.h>
25
TEST_IMPL(utf8_decode1)26 TEST_IMPL(utf8_decode1) {
27 const char* p;
28 char b[32];
29 int i;
30
31 /* ASCII. */
32 p = b;
33 snprintf(b, sizeof(b), "%c\x7F", 0x00);
34 ASSERT(0 == uv__utf8_decode1(&p, b + sizeof(b)));
35 ASSERT(p == b + 1);
36 ASSERT(127 == uv__utf8_decode1(&p, b + sizeof(b)));
37 ASSERT(p == b + 2);
38
39 /* Two-byte sequences. */
40 p = b;
41 snprintf(b, sizeof(b), "\xC2\x80\xDF\xBF");
42 ASSERT(128 == uv__utf8_decode1(&p, b + sizeof(b)));
43 ASSERT(p == b + 2);
44 ASSERT(0x7FF == uv__utf8_decode1(&p, b + sizeof(b)));
45 ASSERT(p == b + 4);
46
47 /* Three-byte sequences. */
48 p = b;
49 snprintf(b, sizeof(b), "\xE0\xA0\x80\xEF\xBF\xBF");
50 ASSERT(0x800 == uv__utf8_decode1(&p, b + sizeof(b)));
51 ASSERT(p == b + 3);
52 ASSERT(0xFFFF == uv__utf8_decode1(&p, b + sizeof(b)));
53 ASSERT(p == b + 6);
54
55 /* Four-byte sequences. */
56 p = b;
57 snprintf(b, sizeof(b), "\xF0\x90\x80\x80\xF4\x8F\xBF\xBF");
58 ASSERT(0x10000 == uv__utf8_decode1(&p, b + sizeof(b)));
59 ASSERT(p == b + 4);
60 ASSERT(0x10FFFF == uv__utf8_decode1(&p, b + sizeof(b)));
61 ASSERT(p == b + 8);
62
63 /* Four-byte sequences > U+10FFFF; disallowed. */
64 p = b;
65 snprintf(b, sizeof(b), "\xF4\x90\xC0\xC0\xF7\xBF\xBF\xBF");
66 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
67 ASSERT(p == b + 4);
68 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
69 ASSERT(p == b + 8);
70
71 /* Overlong; disallowed. */
72 p = b;
73 snprintf(b, sizeof(b), "\xC0\x80\xC1\x80");
74 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
75 ASSERT(p == b + 2);
76 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
77 ASSERT(p == b + 4);
78
79 /* Surrogate pairs; disallowed. */
80 p = b;
81 snprintf(b, sizeof(b), "\xED\xA0\x80\xED\xA3\xBF");
82 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
83 ASSERT(p == b + 3);
84 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
85 ASSERT(p == b + 6);
86
87 /* Simply illegal. */
88 p = b;
89 snprintf(b, sizeof(b), "\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF");
90
91 for (i = 1; i <= 8; i++) {
92 ASSERT((unsigned) -1 == uv__utf8_decode1(&p, b + sizeof(b)));
93 ASSERT(p == b + i);
94 }
95
96 return 0;
97 }
98
TEST_IMPL(utf8_decode1_overrun)99 TEST_IMPL(utf8_decode1_overrun) {
100 const char* p;
101 char b[1];
102 char c[1];
103 /* Single byte. */
104 p = b;
105 b[0] = 0x7F;
106 ASSERT((unsigned int)0x7F == uv__utf8_decode1(&p, b + 1));
107 ASSERT_EQ(p, b + 1);
108
109 /* Multi-byte. */
110 p = b;
111 b[0] = 0xC0;
112 ASSERT_EQ((unsigned int) -1, uv__utf8_decode1(&p, b + 1));
113 ASSERT_PTR_EQ(p, b + 1);
114
115 b[0] = 0x7F;
116 ASSERT_EQ(UV_EINVAL, uv__idna_toascii(b, b + 0, c, c + 1));
117 ASSERT_EQ(UV_EINVAL, uv__idna_toascii(b, b + 1, c, c + 1));
118
119 return 0;
120 }
121
122 /* Doesn't work on z/OS because that platform uses EBCDIC, not ASCII. */
123 #ifndef __MVS__
124
125 #define F(input, err) \
126 do { \
127 char d[256] = {0}; \
128 static const char s[] = "" input ""; \
129 ASSERT(err == uv__idna_toascii(s, s + sizeof(s) - 1, d, d + sizeof(d))); \
130 } while (0)
131
132 #define T(input, expected) \
133 do { \
134 long n; \
135 char d1[256] = {0}; \
136 char d2[256] = {0}; \
137 static const char s[] = "" input ""; \
138 n = uv__idna_toascii(s, s + sizeof(s) - 1, d1, d1 + sizeof(d1)); \
139 ASSERT(n == sizeof(expected)); \
140 ASSERT(0 == memcmp(d1, expected, n)); \
141 /* Sanity check: encoding twice should not change the output. */ \
142 n = uv__idna_toascii(d1, d1 + strlen(d1), d2, d2 + sizeof(d2)); \
143 ASSERT(n == sizeof(expected)); \
144 ASSERT(0 == memcmp(d2, expected, n)); \
145 ASSERT(0 == memcmp(d1, d2, sizeof(d2))); \
146 } while (0)
147
TEST_IMPL(idna_toascii)148 TEST_IMPL(idna_toascii) {
149 /* Illegal inputs. */
150 F("\xC0\x80\xC1\x80", UV_EINVAL); /* Overlong UTF-8 sequence. */
151 F("\xC0\x80\xC1\x80.com", UV_EINVAL); /* Overlong UTF-8 sequence. */
152 F("", UV_EINVAL);
153 /* No conversion. */
154 T(".", ".");
155 T(".com", ".com");
156 T("example", "example");
157 T("example-", "example-");
158 T("straße.de", "xn--strae-oqa.de");
159 /* Test cases adapted from punycode.js. Most are from RFC 3492. */
160 T("foo.bar", "foo.bar");
161 T("mañana.com", "xn--maana-pta.com");
162 T("example.com.", "example.com.");
163 T("bücher.com", "xn--bcher-kva.com");
164 T("café.com", "xn--caf-dma.com");
165 T("café.café.com", "xn--caf-dma.xn--caf-dma.com");
166 T("☃-⌘.com", "xn----dqo34k.com");
167 T("퐀☃-⌘.com", "xn----dqo34kn65z.com");
168 T(".la", "xn--ls8h.la");
169 T("mañana.com", "xn--maana-pta.com");
170 T("mañana。com", "xn--maana-pta.com");
171 T("mañana.com", "xn--maana-pta.com");
172 T("mañana。com", "xn--maana-pta.com");
173 T("ü", "xn--tda");
174 T(".ü", ".xn--tda");
175 T("ü.ü", "xn--tda.xn--tda");
176 T("ü.ü.", "xn--tda.xn--tda.");
177 T("üëäö♥", "xn--4can8av2009b");
178 T("Willst du die Blüthe des frühen, die Früchte des späteren Jahres",
179 "xn--Willst du die Blthe des frhen, "
180 "die Frchte des spteren Jahres-x9e96lkal");
181 T("ليهمابتكلموشعربي؟", "xn--egbpdaj6bu4bxfgehfvwxn");
182 T("他们为什么不说中文", "xn--ihqwcrb4cv8a8dqg056pqjye");
183 T("他們爲什麽不說中文", "xn--ihqwctvzc91f659drss3x8bo0yb");
184 T("Pročprostěnemluvíčesky", "xn--Proprostnemluvesky-uyb24dma41a");
185 T("למההםפשוטלאמדבריםעברית", "xn--4dbcagdahymbxekheh6e0a7fei0b");
186 T("यहलोगहिन्दीक्योंनहींबोलसकतेहैं",
187 "xn--i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd");
188 T("なぜみんな日本語を話してくれないのか",
189 "xn--n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa");
190 T("세계의모든사람들이한국어를이해한다면얼마나좋을까",
191 "xn--989aomsvi5e83db1d2a355cv1e0vak1d"
192 "wrv93d5xbh15a0dt30a5jpsd879ccm6fea98c");
193 T("почемужеонинеговорятпорусски", "xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
194 T("PorquénopuedensimplementehablarenEspañol",
195 "xn--PorqunopuedensimplementehablarenEspaol-fmd56a");
196 T("TạisaohọkhôngthểchỉnóitiếngViệt",
197 "xn--TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g");
198 T("3年B組金八先生", "xn--3B-ww4c5e180e575a65lsy2b");
199 T("安室奈美恵-with-SUPER-MONKEYS",
200 "xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n");
201 T("Hello-Another-Way-それぞれの場所",
202 "xn--Hello-Another-Way--fc4qua05auwb3674vfr0b");
203 T("ひとつ屋根の下2", "xn--2-u9tlzr9756bt3uc0v");
204 T("MajiでKoiする5秒前", "xn--MajiKoi5-783gue6qz075azm5e");
205 T("パフィーdeルンバ", "xn--de-jg4avhby1noc0d");
206 T("そのスピードで", "xn--d9juau41awczczp");
207 T("-> $1.00 <-", "-> $1.00 <-");
208 /* Test cases from https://unicode.org/reports/tr46/ */
209 T("faß.de", "xn--fa-hia.de");
210 T("βόλος.com", "xn--nxasmm1c.com");
211 T("ශ්රී.com", "xn--10cl1a0b660p.com");
212 T("نامهای.com", "xn--mgba3gch31f060k.com");
213 return 0;
214 }
215
216 #undef T
217
218 #endif /* __MVS__ */
219