2 // UTF8EncodingTest.cs - NUnit Test Cases for System.Text.UTF8Encoding
5 // Patrick Kalkman kalkman@cistron.nl
6 // Sebastien Pouliot (spouliot@motus.com)
8 // (C) 2003 Patrick Kalkman
9 // (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
17 using DecoderException = System.Text.DecoderFallbackException;
19 using DecoderException = System.ArgumentException;
22 namespace MonoTests.System.Text {
25 public class UTF8EncodingTest : Assertion {
27 private UTF8Encoding utf8;
32 utf8 = new UTF8Encoding (true, true);
36 public void TestEncodingGetBytes1()
38 UTF8Encoding utf8Enc = new UTF8Encoding ();
39 string UniCode = "\u0041\u2262\u0391\u002E";
41 // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E
43 byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
45 Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
46 Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
47 Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
48 Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
49 Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
50 Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
51 Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
55 public void TestEncodingGetBytes2()
57 UTF8Encoding utf8Enc = new UTF8Encoding ();
58 string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
60 // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21
62 byte[] utf8Bytes = new byte [11];
64 int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
66 Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
67 Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
68 Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
69 Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
70 Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
71 Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
72 Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
73 Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
74 Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
75 Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
76 Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
77 Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
81 public void TestDecodingGetChars1()
83 UTF8Encoding utf8Enc = new UTF8Encoding ();
84 // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>."
86 byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
87 char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
89 Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
90 Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
91 Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
92 Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
96 public void TestMaxCharCount()
98 UTF8Encoding UTF8enc = new UTF8Encoding ();
99 Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
103 public void TestMaxByteCount()
105 UTF8Encoding UTF8enc = new UTF8Encoding ();
106 Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
109 // regression for bug #59648
111 public void TestThrowOnInvalid ()
113 UTF8Encoding u = new UTF8Encoding (true, false);
115 byte[] data = new byte [] { 0xC0, 0xAF };
116 string s = u.GetString (data);
117 AssertEquals (0, s.Length);
119 data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
120 s = u.GetString (data);
121 AssertEquals (4, s.Length);
122 AssertEquals (0x30, (int) s [0]);
123 AssertEquals (0x31, (int) s [1]);
124 AssertEquals (0x30, (int) s [2]);
125 AssertEquals (0x32, (int) s [3]);
128 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
131 public void T1_Correct_GreekWord_kosme ()
133 byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
134 string s = utf8.GetString (data);
135 // cute but saving source code in unicode can be problematic
136 // so we just ensure we can re-encode this
137 AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
141 public void T2_Boundary_1_FirstPossibleSequence_Pass ()
143 byte[] data211 = { 0x00 };
144 string s = utf8.GetString (data211);
145 AssertEquals ("1 byte (U-00000000)", "\0", s);
146 AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
148 byte[] data212 = { 0xC2, 0x80 };
149 s = utf8.GetString (data212);
150 AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
151 AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
153 byte[] data213 = { 0xE0, 0xA0, 0x80 };
154 s = utf8.GetString (data213);
155 AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
156 AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
158 byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
159 s = utf8.GetString (data214);
160 AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
161 AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
162 AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
167 [ExpectedException (typeof (DecoderException))]
168 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 ()
170 byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
171 string s = utf8.GetString (data215);
172 AssertNull ("5 bytes (U-00200000)", s);
173 AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
178 [ExpectedException (typeof (DecoderException))]
179 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 ()
181 byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
182 string s = utf8.GetString (data216);
183 AssertNull ("6 bytes (U-04000000)", s);
184 AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
188 public void T2_Boundary_2_LastPossibleSequence_Pass ()
190 byte[] data221 = { 0x7F };
191 string s = utf8.GetString (data221);
192 AssertEquals ("1 byte (U-0000007F)", 127, s [0]);
193 AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
195 byte[] data222 = { 0xDF, 0xBF };
196 s = utf8.GetString (data222);
197 AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
198 AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
200 byte[] data223 = { 0xEF, 0xBF, 0xBF };
201 s = utf8.GetString (data223);
202 AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
203 AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
209 [ExpectedException (typeof (DecoderException))]
210 public void T2_Boundary_2_LastPossibleSequence_Fail_4 ()
212 byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
213 string s = utf8.GetString (data224);
214 AssertNull ("4 bytes (U-001FFFFF)", s);
215 AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
220 [ExpectedException (typeof (DecoderException))]
221 public void T2_Boundary_2_LastPossibleSequence_Fail_5 ()
223 byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
224 string s = utf8.GetString (data225);
225 AssertNull ("5 bytes (U-03FFFFFF)", s);
226 AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
231 [ExpectedException (typeof (DecoderException))]
232 public void T2_Boundary_2_LastPossibleSequence_Fail_6 ()
234 byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
235 string s = utf8.GetString (data226);
236 AssertNull ("6 bytes (U-7FFFFFFF)", s);
237 AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
241 public void T2_Boundary_3_Other_Pass ()
243 byte[] data231 = { 0xED, 0x9F, 0xBF };
244 string s = utf8.GetString (data231);
245 AssertEquals ("U-0000D7FF", 55295, s [0]);
246 AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
248 byte[] data232 = { 0xEE, 0x80, 0x80 };
249 s = utf8.GetString (data232);
250 AssertEquals ("U-0000E000", 57344, s [0]);
251 AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
253 byte[] data233 = { 0xEF, 0xBF, 0xBD };
254 s = utf8.GetString (data233);
255 AssertEquals ("U-0000FFFD", 65533, s [0]);
256 AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
258 byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
259 s = utf8.GetString (data234);
260 AssertEquals ("U-0010FFFF-0", 56319, s [0]);
261 AssertEquals ("U-0010FFFF-1", 57343, s [1]);
262 AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
267 [ExpectedException (typeof (DecoderException))]
268 public void T2_Boundary_3_Other_Fail_5 ()
270 byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
271 string s = utf8.GetString (data235);
272 AssertNull ("U-00110000", s);
273 AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
277 [ExpectedException (typeof (DecoderException))]
278 public void T3_Malformed_1_UnexpectedContinuation_311 ()
280 byte[] data = { 0x80 };
281 string s = utf8.GetString (data);
282 // exception is "really" expected here
286 [ExpectedException (typeof (DecoderException))]
287 public void T3_Malformed_1_UnexpectedContinuation_312 ()
289 byte[] data = { 0xBF };
290 string s = utf8.GetString (data);
291 // exception is "really" expected here
295 [ExpectedException (typeof (DecoderException))]
296 public void T3_Malformed_1_UnexpectedContinuation_313 ()
298 byte[] data = { 0x80, 0xBF };
299 string s = utf8.GetString (data);
300 // exception is "really" expected here
304 [ExpectedException (typeof (DecoderException))]
305 public void T3_Malformed_1_UnexpectedContinuation_314 ()
307 byte[] data = { 0x80, 0xBF, 0x80 };
308 string s = utf8.GetString (data);
309 // exception is "really" expected here
313 [ExpectedException (typeof (DecoderException))]
314 public void T3_Malformed_1_UnexpectedContinuation_315 ()
316 byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
317 string s = utf8.GetString (data);
318 // exception is "really" expected here
322 [ExpectedException (typeof (DecoderException))]
323 public void T3_Malformed_1_UnexpectedContinuation_316 ()
325 byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
326 string s = utf8.GetString (data);
327 // exception is "really" expected here
331 [ExpectedException (typeof (DecoderException))]
332 public void T3_Malformed_1_UnexpectedContinuation_317 ()
334 byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
335 string s = utf8.GetString (data);
336 // exception is "really" expected here
340 [ExpectedException (typeof (DecoderException))]
341 public void T3_Malformed_1_UnexpectedContinuation_318 ()
343 byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
344 string s = utf8.GetString (data);
345 // exception is "really" expected here
349 [ExpectedException (typeof (DecoderException))]
350 public void T3_Malformed_1_UnexpectedContinuation_319 ()
352 // 64 different continuation characters
354 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
355 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
356 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
357 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
358 string s = utf8.GetString (data);
359 // exception is "really" expected here
363 [ExpectedException (typeof (DecoderException))]
364 public void T3_Malformed_2_LonelyStart_321 ()
367 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20,
368 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20,
369 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20,
370 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
371 string s = utf8.GetString (data);
372 // exception is "really" expected here
376 [ExpectedException (typeof (DecoderException))]
377 public void T3_Malformed_2_LonelyStart_322 ()
380 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20,
381 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
382 string s = utf8.GetString (data);
383 // exception is "really" expected here
387 [ExpectedException (typeof (DecoderException))]
388 public void T3_Malformed_2_LonelyStart_323 ()
390 byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
391 string s = utf8.GetString (data);
392 // exception is "really" expected here
396 [ExpectedException (typeof (DecoderException))]
397 public void T3_Malformed_2_LonelyStart_324 ()
399 byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
400 string s = utf8.GetString (data);
401 // exception is "really" expected here
405 [ExpectedException (typeof (DecoderException))]
406 public void T3_Malformed_2_LonelyStart_325 ()
408 byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
409 string s = utf8.GetString (data);
410 // exception is "really" expected here
414 [ExpectedException (typeof (DecoderException))]
415 public void T3_Malformed_3_LastContinuationMissing_331 ()
417 byte[] data = { 0xC0 };
418 string s = utf8.GetString (data);
419 // exception is "really" expected here
423 [ExpectedException (typeof (DecoderException))]
424 public void T3_Malformed_3_LastContinuationMissing_332 ()
426 byte[] data = { 0xE0, 0x80 };
427 string s = utf8.GetString (data);
428 // exception is "really" expected here
432 [ExpectedException (typeof (DecoderException))]
433 public void T3_Malformed_3_LastContinuationMissing_333 ()
435 byte[] data = { 0xF0, 0x80, 0x80 };
436 string s = utf8.GetString (data);
437 // exception is "really" expected here
441 [ExpectedException (typeof (DecoderException))]
442 public void T3_Malformed_3_LastContinuationMissing_334 ()
444 byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
445 string s = utf8.GetString (data);
446 // exception is "really" expected here
450 [ExpectedException (typeof (DecoderException))]
451 public void T3_Malformed_3_LastContinuationMissing_335 ()
453 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
454 string s = utf8.GetString (data);
455 // exception is "really" expected here
459 // MS Fx 1.1 accept this
460 // [ExpectedException (typeof (DecoderException))]
461 public void T3_Malformed_3_LastContinuationMissing_336 ()
463 byte[] data = { 0xDF };
465 string s = utf8.GetString (data);
466 // exception is "really" expected here
467 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
469 catch (DecoderException) {
470 // but Mono doesn't - better stick to the standard
475 // MS Fx 1.1 accept this
476 // [ExpectedException (typeof (DecoderException))]
477 public void T3_Malformed_3_LastContinuationMissing_337 ()
479 byte[] data = { 0xEF, 0xBF };
481 string s = utf8.GetString (data);
482 // exception is "really" expected here
483 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
485 catch (DecoderException) {
486 // but Mono doesn't - better stick to the standard
491 [ExpectedException (typeof (DecoderException))]
492 public void T3_Malformed_3_LastContinuationMissing_338 ()
494 byte[] data = { 0xF7, 0xBF, 0xBF };
495 string s = utf8.GetString (data);
496 // exception is "really" expected here
500 [ExpectedException (typeof (DecoderException))]
501 public void T3_Malformed_3_LastContinuationMissing_339 ()
503 byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
504 string s = utf8.GetString (data);
505 // exception is "really" expected here
509 [ExpectedException (typeof (DecoderException))]
510 public void T3_Malformed_3_LastContinuationMissing_3310 ()
512 byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
513 string s = utf8.GetString (data);
514 // exception is "really" expected here
518 [ExpectedException (typeof (DecoderException))]
519 public void T3_Malformed_4_ConcatenationImcomplete ()
522 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF,
523 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
524 string s = utf8.GetString (data);
525 // exception is "really" expected here
529 [ExpectedException (typeof (DecoderException))]
530 public void T3_Malformed_5_ImpossibleBytes_351 ()
532 byte[] data = { 0xFE };
533 string s = utf8.GetString (data);
534 // exception is "really" expected here
538 [ExpectedException (typeof (DecoderException))]
539 public void T3_Malformed_5_ImpossibleBytes_352 ()
541 byte[] data = { 0xFF };
542 string s = utf8.GetString (data);
543 // exception is "really" expected here
547 [ExpectedException (typeof (DecoderException))]
548 public void T3_Malformed_5_ImpossibleBytes_353 ()
550 byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
551 string s = utf8.GetString (data);
552 // exception is "really" expected here
555 // Overlong == dangereous -> "safe" decoder should reject them
558 [ExpectedException (typeof (DecoderException))]
559 public void T4_Overlong_1_ASCII_Slash_411 ()
561 byte[] data = { 0xC0, 0xAF };
562 string s = utf8.GetString (data);
563 // exception is "really" expected here
567 [ExpectedException (typeof (DecoderException))]
568 public void T4_Overlong_1_ASCII_Slash_412 ()
570 byte[] data = { 0xE0, 0x80, 0xAF };
571 string s = utf8.GetString (data);
572 // exception is "really" expected here
576 [ExpectedException (typeof (DecoderException))]
577 public void T4_Overlong_1_ASCII_Slash_413 ()
579 byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
580 string s = utf8.GetString (data);
581 // exception is "really" expected here
585 [ExpectedException (typeof (DecoderException))]
586 public void T4_Overlong_1_ASCII_Slash_414 ()
588 byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
589 string s = utf8.GetString (data);
590 // exception is "really" expected here
594 [ExpectedException (typeof (DecoderException))]
595 public void T4_Overlong_1_ASCII_Slash_415 ()
597 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
598 string s = utf8.GetString (data);
599 // exception is "really" expected here
603 [ExpectedException (typeof (DecoderException))]
604 public void T4_Overlong_2_MaximumBoundary_421 ()
606 byte[] data = { 0xC1, 0xBF };
607 string s = utf8.GetString (data);
608 // exception is "really" expected here
612 [ExpectedException (typeof (DecoderException))]
613 public void T4_Overlong_2_MaximumBoundary_422 ()
615 byte[] data = { 0xE0, 0x9F, 0xBF };
616 string s = utf8.GetString (data);
617 // exception is "really" expected here
621 [ExpectedException (typeof (DecoderException))]
622 public void T4_Overlong_2_MaximumBoundary_423 ()
624 byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
625 string s = utf8.GetString (data);
626 // exception is "really" expected here
630 [ExpectedException (typeof (DecoderException))]
631 public void T4_Overlong_2_MaximumBoundary_424 ()
633 byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
634 string s = utf8.GetString (data);
635 // exception is "really" expected here
639 [ExpectedException (typeof (DecoderException))]
640 public void T4_Overlong_2_MaximumBoundary_425 ()
642 byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
643 string s = utf8.GetString (data);
644 // exception is "really" expected here
648 [ExpectedException (typeof (DecoderException))]
649 public void T4_Overlong_3_NUL_431 ()
651 byte[] data = { 0xC0, 0x80 };
652 string s = utf8.GetString (data);
653 // exception is "really" expected here
657 [ExpectedException (typeof (DecoderException))]
658 public void T4_Overlong_3_NUL_432 ()
660 byte[] data = { 0xE0, 0x80, 0x80 };
661 string s = utf8.GetString (data);
662 // exception is "really" expected here
666 [ExpectedException (typeof (DecoderException))]
667 public void T4_Overlong_3_NUL_433 ()
669 byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
670 string s = utf8.GetString (data);
671 // exception is "really" expected here
675 [ExpectedException (typeof (DecoderException))]
676 public void T4_Overlong_3_NUL_434 ()
678 byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
679 string s = utf8.GetString (data);
680 // exception is "really" expected here
684 [ExpectedException (typeof (DecoderException))]
685 public void T4_Overlong_3_NUL_435 ()
687 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
688 string s = utf8.GetString (data);
689 // exception is "really" expected here
693 // MS Fx 1.1 accept this
694 // [ExpectedException (typeof (DecoderException))]
695 public void T5_IllegalCodePosition_1_UTF16Surrogates_511 ()
697 byte[] data = { 0xED, 0xA0, 0x80 };
698 string s = utf8.GetString (data);
699 // exception is "really" expected here
700 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
704 // MS Fx 1.1 accept this
705 // [ExpectedException (typeof (DecoderException))]
706 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 ()
708 byte[] data = { 0xED, 0xAD, 0xBF };
709 string s = utf8.GetString (data);
710 // exception is "really" expected here
711 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
715 // MS Fx 1.1 accept this
716 // [ExpectedException (typeof (DecoderException))]
717 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
719 byte[] data = { 0xED, 0xAE, 0x80 };
720 string s = utf8.GetString (data);
721 // exception is "really" expected here
722 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
726 // MS Fx 1.1 accept this
727 // [ExpectedException (typeof (DecoderException))]
728 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 ()
730 byte[] data = { 0xED, 0xAF, 0xBF };
731 string s = utf8.GetString (data);
732 // exception is "really" expected here
733 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
737 // MS Fx 1.1 accept this
738 // [ExpectedException (typeof (DecoderException))]
739 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
741 byte[] data = { 0xED, 0xB0, 0x80 };
742 string s = utf8.GetString (data);
743 // exception is "really" expected here
744 AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
748 // MS Fx 1.1 accept this
749 // [ExpectedException (typeof (DecoderException))]
750 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 ()
752 byte[] data = { 0xED, 0xBE, 0x80 };
753 string s = utf8.GetString (data);
754 // exception is "really" expected here
755 AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
759 // MS Fx 1.1 accept this
760 // [ExpectedException (typeof (DecoderException))]
761 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 ()
763 byte[] data = { 0xED, 0xBF, 0xBF };
764 string s = utf8.GetString (data);
765 // exception is "really" expected here
766 AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
770 // MS Fx 1.1 accept this
771 // [ExpectedException (typeof (DecoderException))]
772 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 ()
774 byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
775 string s = utf8.GetString (data);
776 // exception is "really" expected here
777 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
778 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
782 // MS Fx 1.1 accept this
783 // [ExpectedException (typeof (DecoderException))]
784 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 ()
786 byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
787 string s = utf8.GetString (data);
788 // exception is "really" expected here
789 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
790 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
794 // MS Fx 1.1 accept this
795 // [ExpectedException (typeof (DecoderException))]
796 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 ()
798 byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
799 string s = utf8.GetString (data);
800 // exception is "really" expected here
801 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
802 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
806 // MS Fx 1.1 accept this
807 // [ExpectedException (typeof (DecoderException))]
808 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 ()
810 byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
811 string s = utf8.GetString (data);
812 // exception is "really" expected here
813 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
814 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
818 // MS Fx 1.1 accept this
819 // [ExpectedException (typeof (DecoderException))]
820 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 ()
822 byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
823 string s = utf8.GetString (data);
824 // exception is "really" expected here
825 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
826 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
830 // MS Fx 1.1 accept this
831 // [ExpectedException (typeof (DecoderException))]
832 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 ()
834 byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
835 string s = utf8.GetString (data);
836 // exception is "really" expected here
837 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
838 AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
842 // MS Fx 1.1 accept this
843 // [ExpectedException (typeof (DecoderException))]
844 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 ()
846 byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
847 string s = utf8.GetString (data);
848 // exception is "really" expected here
849 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
850 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
854 // MS Fx 1.1 accept this
855 // [ExpectedException (typeof (DecoderException))]
856 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 ()
858 byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
859 string s = utf8.GetString (data);
860 // exception is "really" expected here
861 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
862 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
866 // MS Fx 1.1 accept this
867 // [ExpectedException (typeof (DecoderException))]
868 public void T5_IllegalCodePosition_3_Other_531 ()
870 byte[] data = { 0xEF, 0xBF, 0xBE };
871 string s = utf8.GetString (data);
872 // exception is "really" expected here
873 AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
877 // MS Fx 1.1 accept this
878 // [ExpectedException (typeof (DecoderException))]
879 public void T5_IllegalCodePosition_3_Other_532 ()
881 byte[] data = { 0xEF, 0xBF, 0xBF };
882 string s = utf8.GetString (data);
883 // exception is "really" expected here
884 AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
888 // bug #75065 and #73086.
889 public void GetCharsFEFF ()
891 byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
892 Encoding enc = new UTF8Encoding (false, true);
893 string s = enc.GetString (data);
894 AssertEquals ("\uFEFF", s);
896 Encoding utf = Encoding.UTF8;
897 char[] testChars = {'\uFEFF','A'};
899 byte[] bytes = utf.GetBytes(testChars);
900 char[] chars = utf.GetChars(bytes);
901 AssertEquals ("#1", '\uFEFF', chars [0]);
902 AssertEquals ("#2", 'A', chars [1]);