|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "TestHarness.h" |
|
7 |
|
8 nsresult TestGoodSurrogatePair() |
|
9 { |
|
10 // When this string is decoded, the surrogate pair is U+10302 and the rest of |
|
11 // the string is specified by indexes 2 onward. |
|
12 const char16_t goodPairData[] = { 0xD800, 0xDF02, 0x65, 0x78, 0x0 }; |
|
13 nsDependentString goodPair16(goodPairData); |
|
14 |
|
15 uint32_t byteCount = 0; |
|
16 char* goodPair8 = ToNewUTF8String(goodPair16, &byteCount); |
|
17 if (!goodPair8) |
|
18 { |
|
19 fail("out of memory creating goodPair8"); |
|
20 return NS_ERROR_OUT_OF_MEMORY; |
|
21 } |
|
22 |
|
23 if (byteCount != 6) |
|
24 { |
|
25 fail("wrong number of bytes; expected 6, got %lu", byteCount); |
|
26 return NS_ERROR_FAILURE; |
|
27 } |
|
28 |
|
29 const unsigned char expected8[] = |
|
30 { 0xF0, 0x90, 0x8C, 0x82, 0x65, 0x78, 0x0 }; |
|
31 if (0 != memcmp(expected8, goodPair8, sizeof(expected8))) |
|
32 { |
|
33 fail("wrong translation to UTF8"); |
|
34 return NS_ERROR_FAILURE; |
|
35 } |
|
36 |
|
37 // This takes a different code path from the above, so test it to make sure |
|
38 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
|
39 nsDependentCString expected((const char*)expected8); |
|
40 if (0 != CompareUTF8toUTF16(expected, goodPair16)) |
|
41 { |
|
42 fail("bad comparison between UTF-8 and equivalent UTF-16"); |
|
43 return NS_ERROR_FAILURE; |
|
44 } |
|
45 |
|
46 NS_Free(goodPair8); |
|
47 |
|
48 passed("TestGoodSurrogatePair"); |
|
49 return NS_OK; |
|
50 } |
|
51 |
|
52 nsresult TestBackwardsSurrogatePair() |
|
53 { |
|
54 // When this string is decoded, the two surrogates are wrongly ordered and |
|
55 // must each be interpreted as U+FFFD. |
|
56 const char16_t backwardsPairData[] = { 0xDDDD, 0xD863, 0x65, 0x78, 0x0 }; |
|
57 nsDependentString backwardsPair16(backwardsPairData); |
|
58 |
|
59 uint32_t byteCount = 0; |
|
60 char* backwardsPair8 = ToNewUTF8String(backwardsPair16, &byteCount); |
|
61 if (!backwardsPair8) |
|
62 { |
|
63 fail("out of memory creating backwardsPair8"); |
|
64 return NS_ERROR_OUT_OF_MEMORY; |
|
65 } |
|
66 |
|
67 if (byteCount != 8) |
|
68 { |
|
69 fail("wrong number of bytes; expected 8, got %lu", byteCount); |
|
70 return NS_ERROR_FAILURE; |
|
71 } |
|
72 |
|
73 const unsigned char expected8[] = |
|
74 { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x65, 0x78, 0x0 }; |
|
75 if (0 != memcmp(expected8, backwardsPair8, sizeof(expected8))) |
|
76 { |
|
77 fail("wrong translation to UTF8"); |
|
78 return NS_ERROR_FAILURE; |
|
79 } |
|
80 |
|
81 // This takes a different code path from the above, so test it to make sure |
|
82 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
|
83 nsDependentCString expected((const char*)expected8); |
|
84 if (0 != CompareUTF8toUTF16(expected, backwardsPair16)) |
|
85 { |
|
86 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
|
87 return NS_ERROR_FAILURE; |
|
88 } |
|
89 |
|
90 NS_Free(backwardsPair8); |
|
91 |
|
92 passed("TestBackwardsSurrogatePair"); |
|
93 return NS_OK; |
|
94 } |
|
95 |
|
96 nsresult TestMalformedUTF16OrphanHighSurrogate() |
|
97 { |
|
98 // When this string is decoded, the high surrogate should be replaced and the |
|
99 // rest of the string is specified by indexes 1 onward. |
|
100 const char16_t highSurrogateData[] = { 0xD863, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
|
101 nsDependentString highSurrogate16(highSurrogateData); |
|
102 |
|
103 uint32_t byteCount = 0; |
|
104 char* highSurrogate8 = ToNewUTF8String(highSurrogate16, &byteCount); |
|
105 if (!highSurrogate8) |
|
106 { |
|
107 fail("out of memory creating highSurrogate8"); |
|
108 return NS_ERROR_OUT_OF_MEMORY; |
|
109 } |
|
110 |
|
111 if (byteCount != 7) |
|
112 { |
|
113 fail("wrong number of bytes; expected 7, got %lu", byteCount); |
|
114 return NS_ERROR_FAILURE; |
|
115 } |
|
116 |
|
117 const unsigned char expected8[] = |
|
118 { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
|
119 if (0 != memcmp(expected8, highSurrogate8, sizeof(expected8))) |
|
120 { |
|
121 fail("wrong translation to UTF8"); |
|
122 return NS_ERROR_FAILURE; |
|
123 } |
|
124 |
|
125 // This takes a different code path from the above, so test it to make sure |
|
126 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
|
127 nsDependentCString expected((const char*)expected8); |
|
128 if (0 != CompareUTF8toUTF16(expected, highSurrogate16)) |
|
129 { |
|
130 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
|
131 return NS_ERROR_FAILURE; |
|
132 } |
|
133 |
|
134 NS_Free(highSurrogate8); |
|
135 |
|
136 passed("TestMalformedUTF16OrphanHighSurrogate"); |
|
137 return NS_OK; |
|
138 } |
|
139 |
|
140 nsresult TestMalformedUTF16OrphanLowSurrogate() |
|
141 { |
|
142 // When this string is decoded, the low surrogate should be replaced and the |
|
143 // rest of the string is specified by indexes 1 onward. |
|
144 const char16_t lowSurrogateData[] = { 0xDDDD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
|
145 nsDependentString lowSurrogate16(lowSurrogateData); |
|
146 |
|
147 uint32_t byteCount = 0; |
|
148 char* lowSurrogate8 = ToNewUTF8String(lowSurrogate16, &byteCount); |
|
149 if (!lowSurrogate8) |
|
150 { |
|
151 fail("out of memory creating lowSurrogate8"); |
|
152 return NS_ERROR_OUT_OF_MEMORY; |
|
153 } |
|
154 |
|
155 if (byteCount != 7) |
|
156 { |
|
157 fail("wrong number of bytes; expected 7, got %lu", byteCount); |
|
158 return NS_ERROR_FAILURE; |
|
159 } |
|
160 |
|
161 const unsigned char expected8[] = |
|
162 { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
|
163 if (0 != memcmp(expected8, lowSurrogate8, sizeof(expected8))) |
|
164 { |
|
165 fail("wrong translation to UTF8"); |
|
166 return NS_ERROR_FAILURE; |
|
167 } |
|
168 |
|
169 // This takes a different code path from the above, so test it to make sure |
|
170 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
|
171 nsDependentCString expected((const char*)expected8); |
|
172 if (0 != CompareUTF8toUTF16(expected, lowSurrogate16)) |
|
173 { |
|
174 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
|
175 return NS_ERROR_FAILURE; |
|
176 } |
|
177 |
|
178 NS_Free(lowSurrogate8); |
|
179 |
|
180 passed("TestMalformedUTF16OrphanLowSurrogate"); |
|
181 return NS_OK; |
|
182 } |
|
183 |
|
184 |
|
185 int main(int argc, char** argv) |
|
186 { |
|
187 ScopedXPCOM xpcom("TestEncoding"); |
|
188 if (xpcom.failed()) |
|
189 return 1; |
|
190 |
|
191 int rv = 0; |
|
192 |
|
193 if (NS_FAILED(TestGoodSurrogatePair())) |
|
194 rv = 1; |
|
195 if (NS_FAILED(TestBackwardsSurrogatePair())) |
|
196 rv = 1; |
|
197 if (NS_FAILED(TestMalformedUTF16OrphanHighSurrogate())) |
|
198 rv = 1; |
|
199 if (NS_FAILED(TestMalformedUTF16OrphanLowSurrogate())) |
|
200 rv = 1; |
|
201 |
|
202 return rv; |
|
203 } |