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 using AssertType = NUnit.Framework.Assert;
24 namespace MonoTests.System.Text {
27 public class UTF8EncodingTest : Assertion {
29 private UTF8Encoding utf8;
34 utf8 = new UTF8Encoding (true, true);
38 public void TestEncodingGetBytes1()
40 UTF8Encoding utf8Enc = new UTF8Encoding ();
41 string UniCode = "\u0041\u2262\u0391\u002E";
43 // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E
45 byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
47 Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
48 Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
49 Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
50 Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
51 Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
52 Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
53 Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
57 public void TestEncodingGetBytes2()
59 UTF8Encoding utf8Enc = new UTF8Encoding ();
60 string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
62 // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21
64 byte[] utf8Bytes = new byte [11];
66 int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
68 Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
69 Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
70 Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
71 Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
72 Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
73 Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
74 Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
75 Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
76 Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
77 Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
78 Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
79 Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
83 public void TestDecodingGetChars1()
85 UTF8Encoding utf8Enc = new UTF8Encoding ();
86 // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>."
88 byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
89 char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
91 Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
92 Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
93 Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
94 Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
99 [Category ("NotWorking")]
101 public void TestMaxCharCount()
103 UTF8Encoding UTF8enc = new UTF8Encoding ();
105 // hmm, where is this extra 1 coming from?
106 Assertion.AssertEquals ("UTF #1", 51, UTF8enc.GetMaxCharCount(50));
108 Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
114 [Category ("NotWorking")]
116 public void TestMaxByteCount()
118 UTF8Encoding UTF8enc = new UTF8Encoding ();
120 // maybe under .NET 2.0 insufficient surrogate pair is just not handled, and 3 is Preamble size.
121 Assertion.AssertEquals ("UTF #1", 153, UTF8enc.GetMaxByteCount(50));
123 Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
127 // regression for bug #59648
129 public void TestThrowOnInvalid ()
131 UTF8Encoding u = new UTF8Encoding (true, false);
133 byte[] data = new byte [] { 0xC0, 0xAF };
134 string s = u.GetString (data);
135 AssertEquals (0, s.Length);
137 data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
138 s = u.GetString (data);
139 AssertEquals (4, s.Length);
140 AssertEquals (0x30, (int) s [0]);
141 AssertEquals (0x31, (int) s [1]);
142 AssertEquals (0x30, (int) s [2]);
143 AssertEquals (0x32, (int) s [3]);
146 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
149 public void T1_Correct_GreekWord_kosme ()
151 byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
152 string s = utf8.GetString (data);
153 // cute but saving source code in unicode can be problematic
154 // so we just ensure we can re-encode this
155 AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
159 public void T2_Boundary_1_FirstPossibleSequence_Pass ()
161 byte[] data211 = { 0x00 };
162 string s = utf8.GetString (data211);
163 AssertEquals ("1 byte (U-00000000)", "\0", s);
164 AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
166 byte[] data212 = { 0xC2, 0x80 };
167 s = utf8.GetString (data212);
168 AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
169 AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
171 byte[] data213 = { 0xE0, 0xA0, 0x80 };
172 s = utf8.GetString (data213);
173 AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
174 AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
176 byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
177 s = utf8.GetString (data214);
178 AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
179 AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
180 AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
185 [ExpectedException (typeof (DecoderException))]
186 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 ()
188 byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
189 string s = utf8.GetString (data215);
190 AssertNull ("5 bytes (U-00200000)", s);
191 AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
196 [ExpectedException (typeof (DecoderException))]
197 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 ()
199 byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
200 string s = utf8.GetString (data216);
201 AssertNull ("6 bytes (U-04000000)", s);
202 AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
206 public void T2_Boundary_2_LastPossibleSequence_Pass ()
208 byte[] data221 = { 0x7F };
209 string s = utf8.GetString (data221);
210 AssertEquals ("1 byte (U-0000007F)", 127, s [0]);
211 AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
213 byte[] data222 = { 0xDF, 0xBF };
214 s = utf8.GetString (data222);
215 AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
216 AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
218 byte[] data223 = { 0xEF, 0xBF, 0xBF };
219 s = utf8.GetString (data223);
220 AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
221 AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
227 [ExpectedException (typeof (DecoderException))]
228 public void T2_Boundary_2_LastPossibleSequence_Fail_4 ()
230 byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
231 string s = utf8.GetString (data224);
232 AssertNull ("4 bytes (U-001FFFFF)", s);
233 AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
238 [ExpectedException (typeof (DecoderException))]
239 public void T2_Boundary_2_LastPossibleSequence_Fail_5 ()
241 byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
242 string s = utf8.GetString (data225);
243 AssertNull ("5 bytes (U-03FFFFFF)", s);
244 AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
249 [ExpectedException (typeof (DecoderException))]
250 public void T2_Boundary_2_LastPossibleSequence_Fail_6 ()
252 byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
253 string s = utf8.GetString (data226);
254 AssertNull ("6 bytes (U-7FFFFFFF)", s);
255 AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
259 public void T2_Boundary_3_Other_Pass ()
261 byte[] data231 = { 0xED, 0x9F, 0xBF };
262 string s = utf8.GetString (data231);
263 AssertEquals ("U-0000D7FF", 55295, s [0]);
264 AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
266 byte[] data232 = { 0xEE, 0x80, 0x80 };
267 s = utf8.GetString (data232);
268 AssertEquals ("U-0000E000", 57344, s [0]);
269 AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
271 byte[] data233 = { 0xEF, 0xBF, 0xBD };
272 s = utf8.GetString (data233);
273 AssertEquals ("U-0000FFFD", 65533, s [0]);
274 AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
276 byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
277 s = utf8.GetString (data234);
278 AssertEquals ("U-0010FFFF-0", 56319, s [0]);
279 AssertEquals ("U-0010FFFF-1", 57343, s [1]);
280 AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
285 [ExpectedException (typeof (DecoderException))]
286 public void T2_Boundary_3_Other_Fail_5 ()
288 byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
289 string s = utf8.GetString (data235);
290 AssertNull ("U-00110000", s);
291 AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
295 [ExpectedException (typeof (DecoderException))]
296 public void T3_Malformed_1_UnexpectedContinuation_311 ()
298 byte[] data = { 0x80 };
299 string s = utf8.GetString (data);
300 // exception is "really" expected here
304 [ExpectedException (typeof (DecoderException))]
305 public void T3_Malformed_1_UnexpectedContinuation_312 ()
307 byte[] data = { 0xBF };
308 string s = utf8.GetString (data);
309 // exception is "really" expected here
313 [ExpectedException (typeof (DecoderException))]
314 public void T3_Malformed_1_UnexpectedContinuation_313 ()
316 byte[] data = { 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_314 ()
325 byte[] data = { 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_315 ()
334 byte[] data = { 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_316 ()
343 byte[] data = { 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_317 ()
352 byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
353 string s = utf8.GetString (data);
354 // exception is "really" expected here
358 [ExpectedException (typeof (DecoderException))]
359 public void T3_Malformed_1_UnexpectedContinuation_318 ()
361 byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
362 string s = utf8.GetString (data);
363 // exception is "really" expected here
367 [ExpectedException (typeof (DecoderException))]
368 public void T3_Malformed_1_UnexpectedContinuation_319 ()
370 // 64 different continuation characters
372 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
373 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
374 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
375 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
376 string s = utf8.GetString (data);
377 // exception is "really" expected here
381 [ExpectedException (typeof (DecoderException))]
382 public void T3_Malformed_2_LonelyStart_321 ()
385 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20,
386 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20,
387 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20,
388 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
389 string s = utf8.GetString (data);
390 // exception is "really" expected here
394 [ExpectedException (typeof (DecoderException))]
395 public void T3_Malformed_2_LonelyStart_322 ()
398 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20,
399 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
400 string s = utf8.GetString (data);
401 // exception is "really" expected here
405 [ExpectedException (typeof (DecoderException))]
406 public void T3_Malformed_2_LonelyStart_323 ()
408 byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
409 string s = utf8.GetString (data);
410 // exception is "really" expected here
414 [ExpectedException (typeof (DecoderException))]
415 public void T3_Malformed_2_LonelyStart_324 ()
417 byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
418 string s = utf8.GetString (data);
419 // exception is "really" expected here
423 [ExpectedException (typeof (DecoderException))]
424 public void T3_Malformed_2_LonelyStart_325 ()
426 byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
427 string s = utf8.GetString (data);
428 // exception is "really" expected here
432 [ExpectedException (typeof (DecoderException))]
433 public void T3_Malformed_3_LastContinuationMissing_331 ()
435 byte[] data = { 0xC0 };
436 string s = utf8.GetString (data);
437 // exception is "really" expected here
441 [ExpectedException (typeof (DecoderException))]
442 public void T3_Malformed_3_LastContinuationMissing_332 ()
444 byte[] data = { 0xE0, 0x80 };
445 string s = utf8.GetString (data);
446 // exception is "really" expected here
450 [ExpectedException (typeof (DecoderException))]
451 public void T3_Malformed_3_LastContinuationMissing_333 ()
453 byte[] data = { 0xF0, 0x80, 0x80 };
454 string s = utf8.GetString (data);
455 // exception is "really" expected here
459 [ExpectedException (typeof (DecoderException))]
460 public void T3_Malformed_3_LastContinuationMissing_334 ()
462 byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
463 string s = utf8.GetString (data);
464 // exception is "really" expected here
468 [ExpectedException (typeof (DecoderException))]
469 public void T3_Malformed_3_LastContinuationMissing_335 ()
471 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
472 string s = utf8.GetString (data);
473 // exception is "really" expected here
477 // MS Fx 1.1 accept this
478 // [ExpectedException (typeof (DecoderException))]
479 public void T3_Malformed_3_LastContinuationMissing_336 ()
481 byte[] data = { 0xDF };
483 string s = utf8.GetString (data);
484 // exception is "really" expected here
485 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
487 catch (DecoderException) {
488 // but Mono doesn't - better stick to the standard
493 // MS Fx 1.1 accept this
494 // [ExpectedException (typeof (DecoderException))]
495 public void T3_Malformed_3_LastContinuationMissing_337 ()
497 byte[] data = { 0xEF, 0xBF };
499 string s = utf8.GetString (data);
500 // exception is "really" expected here
501 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
503 catch (DecoderException) {
504 // but Mono doesn't - better stick to the standard
509 [ExpectedException (typeof (DecoderException))]
510 public void T3_Malformed_3_LastContinuationMissing_338 ()
512 byte[] data = { 0xF7, 0xBF, 0xBF };
513 string s = utf8.GetString (data);
514 // exception is "really" expected here
518 [ExpectedException (typeof (DecoderException))]
519 public void T3_Malformed_3_LastContinuationMissing_339 ()
521 byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
522 string s = utf8.GetString (data);
523 // exception is "really" expected here
527 [ExpectedException (typeof (DecoderException))]
528 public void T3_Malformed_3_LastContinuationMissing_3310 ()
530 byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
531 string s = utf8.GetString (data);
532 // exception is "really" expected here
536 [ExpectedException (typeof (DecoderException))]
537 public void T3_Malformed_4_ConcatenationImcomplete ()
540 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF,
541 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
542 string s = utf8.GetString (data);
543 // exception is "really" expected here
547 [ExpectedException (typeof (DecoderException))]
548 public void T3_Malformed_5_ImpossibleBytes_351 ()
550 byte[] data = { 0xFE };
551 string s = utf8.GetString (data);
552 // exception is "really" expected here
556 [ExpectedException (typeof (DecoderException))]
557 public void T3_Malformed_5_ImpossibleBytes_352 ()
559 byte[] data = { 0xFF };
560 string s = utf8.GetString (data);
561 // exception is "really" expected here
565 [ExpectedException (typeof (DecoderException))]
566 public void T3_Malformed_5_ImpossibleBytes_353 ()
568 byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
569 string s = utf8.GetString (data);
570 // exception is "really" expected here
573 // Overlong == dangereous -> "safe" decoder should reject them
576 [ExpectedException (typeof (DecoderException))]
577 public void T4_Overlong_1_ASCII_Slash_411 ()
579 byte[] data = { 0xC0, 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_412 ()
588 byte[] data = { 0xE0, 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_413 ()
597 byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
598 string s = utf8.GetString (data);
599 // exception is "really" expected here
603 [ExpectedException (typeof (DecoderException))]
604 public void T4_Overlong_1_ASCII_Slash_414 ()
606 byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
607 string s = utf8.GetString (data);
608 // exception is "really" expected here
612 [ExpectedException (typeof (DecoderException))]
613 public void T4_Overlong_1_ASCII_Slash_415 ()
615 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
616 string s = utf8.GetString (data);
617 // exception is "really" expected here
621 [ExpectedException (typeof (DecoderException))]
622 public void T4_Overlong_2_MaximumBoundary_421 ()
624 byte[] data = { 0xC1, 0xBF };
625 string s = utf8.GetString (data);
626 // exception is "really" expected here
630 [ExpectedException (typeof (DecoderException))]
631 public void T4_Overlong_2_MaximumBoundary_422 ()
633 byte[] data = { 0xE0, 0x9F, 0xBF };
634 string s = utf8.GetString (data);
635 // exception is "really" expected here
639 [ExpectedException (typeof (DecoderException))]
640 public void T4_Overlong_2_MaximumBoundary_423 ()
642 byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
643 string s = utf8.GetString (data);
644 // exception is "really" expected here
648 [ExpectedException (typeof (DecoderException))]
649 public void T4_Overlong_2_MaximumBoundary_424 ()
651 byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
652 string s = utf8.GetString (data);
653 // exception is "really" expected here
657 [ExpectedException (typeof (DecoderException))]
658 public void T4_Overlong_2_MaximumBoundary_425 ()
660 byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
661 string s = utf8.GetString (data);
662 // exception is "really" expected here
666 [ExpectedException (typeof (DecoderException))]
667 public void T4_Overlong_3_NUL_431 ()
669 byte[] data = { 0xC0, 0x80 };
670 string s = utf8.GetString (data);
671 // exception is "really" expected here
675 [ExpectedException (typeof (DecoderException))]
676 public void T4_Overlong_3_NUL_432 ()
678 byte[] data = { 0xE0, 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_433 ()
687 byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
688 string s = utf8.GetString (data);
689 // exception is "really" expected here
693 [ExpectedException (typeof (DecoderException))]
694 public void T4_Overlong_3_NUL_434 ()
696 byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
697 string s = utf8.GetString (data);
698 // exception is "really" expected here
702 [ExpectedException (typeof (DecoderException))]
703 public void T4_Overlong_3_NUL_435 ()
705 byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
706 string s = utf8.GetString (data);
707 // exception is "really" expected here
712 [ExpectedException (typeof (DecoderFallbackException))]
714 // MS Fx 1.1 accept this
715 [Category ("NotDotNet")]
716 [ExpectedException (typeof (DecoderException))]
718 public void T5_IllegalCodePosition_1_UTF16Surrogates_511 ()
720 byte[] data = { 0xED, 0xA0, 0x80 };
721 string s = utf8.GetString (data);
722 // exception is "really" expected here
723 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
728 [ExpectedException (typeof (DecoderFallbackException))]
730 // MS Fx 1.1 accept this
731 [Category ("NotDotNet")]
732 [ExpectedException (typeof (DecoderException))]
734 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 ()
736 byte[] data = { 0xED, 0xAD, 0xBF };
737 string s = utf8.GetString (data);
738 // exception is "really" expected here
739 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
744 [ExpectedException (typeof (DecoderFallbackException))]
746 // MS Fx 1.1 accept this
747 [Category ("NotDotNet")]
748 [ExpectedException (typeof (DecoderException))]
750 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
752 byte[] data = { 0xED, 0xAE, 0x80 };
753 string s = utf8.GetString (data);
754 // exception is "really" expected here
755 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
760 [ExpectedException (typeof (DecoderFallbackException))]
762 // MS Fx 1.1 accept this
763 [Category ("NotDotNet")]
764 [ExpectedException (typeof (DecoderException))]
766 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 ()
768 byte[] data = { 0xED, 0xAF, 0xBF };
769 string s = utf8.GetString (data);
770 // exception is "really" expected here
771 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
776 [ExpectedException (typeof (DecoderFallbackException))]
778 // MS Fx 1.1 accept this
779 [Category ("NotDotNet")]
780 [ExpectedException (typeof (DecoderException))]
782 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
784 byte[] data = { 0xED, 0xB0, 0x80 };
785 string s = utf8.GetString (data);
786 // exception is "really" expected here
787 AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
792 [ExpectedException (typeof (DecoderFallbackException))]
794 // MS Fx 1.1 accept this
795 [Category ("NotDotNet")]
796 [ExpectedException (typeof (DecoderException))]
798 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 ()
800 byte[] data = { 0xED, 0xBE, 0x80 };
801 string s = utf8.GetString (data);
802 // exception is "really" expected here
803 AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
808 [ExpectedException (typeof (DecoderFallbackException))]
810 // MS Fx 1.1 accept this
811 [Category ("NotDotNet")]
812 [ExpectedException (typeof (DecoderException))]
814 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 ()
816 byte[] data = { 0xED, 0xBF, 0xBF };
817 string s = utf8.GetString (data);
818 // exception is "really" expected here
819 AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
824 [ExpectedException (typeof (DecoderFallbackException))]
826 // MS Fx 1.1 accept this
827 [Category ("NotDotNet")]
828 [ExpectedException (typeof (DecoderException))]
830 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 ()
832 byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
833 string s = utf8.GetString (data);
834 // exception is "really" expected here
835 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
836 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
841 [ExpectedException (typeof (DecoderFallbackException))]
843 // MS Fx 1.1 accept this
844 [Category ("NotDotNet")]
845 [ExpectedException (typeof (DecoderException))]
847 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 ()
849 byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
850 string s = utf8.GetString (data);
851 // exception is "really" expected here
852 AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
853 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
858 [ExpectedException (typeof (DecoderFallbackException))]
860 // MS Fx 1.1 accept this
861 [Category ("NotDotNet")]
862 [ExpectedException (typeof (DecoderException))]
864 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 ()
866 byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
867 string s = utf8.GetString (data);
868 // exception is "really" expected here
869 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
870 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
875 [ExpectedException (typeof (DecoderFallbackException))]
877 // MS Fx 1.1 accept this
878 [Category ("NotDotNet")]
879 [ExpectedException (typeof (DecoderException))]
881 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 ()
883 byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
884 string s = utf8.GetString (data);
885 // exception is "really" expected here
886 AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
887 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
892 [ExpectedException (typeof (DecoderFallbackException))]
894 // MS Fx 1.1 accept this
895 [Category ("NotDotNet")]
896 [ExpectedException (typeof (DecoderException))]
898 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 ()
900 byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
901 string s = utf8.GetString (data);
902 // exception is "really" expected here
903 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
904 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
909 [ExpectedException (typeof (DecoderFallbackException))]
911 // MS Fx 1.1 accept this
912 [Category ("NotDotNet")]
913 [ExpectedException (typeof (DecoderException))]
915 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 ()
917 byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
918 string s = utf8.GetString (data);
919 // exception is "really" expected here
920 AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
921 AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
926 [ExpectedException (typeof (DecoderFallbackException))]
928 // MS Fx 1.1 accept this
929 [Category ("NotDotNet")]
930 [ExpectedException (typeof (DecoderException))]
932 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 ()
934 byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
935 string s = utf8.GetString (data);
936 // exception is "really" expected here
937 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
938 AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
943 [ExpectedException (typeof (DecoderFallbackException))]
945 // MS Fx 1.1 accept this
946 [Category ("NotDotNet")]
947 [ExpectedException (typeof (DecoderException))]
949 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 ()
951 byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
952 string s = utf8.GetString (data);
953 // exception is "really" expected here
954 AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
955 AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
959 // MS Fx 1.1 accept this
960 // [ExpectedException (typeof (DecoderException))]
961 public void T5_IllegalCodePosition_3_Other_531 ()
963 byte[] data = { 0xEF, 0xBF, 0xBE };
964 string s = utf8.GetString (data);
965 // exception is "really" expected here
966 AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
970 // MS Fx 1.1 accept this
971 // [ExpectedException (typeof (DecoderException))]
972 public void T5_IllegalCodePosition_3_Other_532 ()
974 byte[] data = { 0xEF, 0xBF, 0xBF };
975 string s = utf8.GetString (data);
976 // exception is "really" expected here
977 AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
981 // bug #75065 and #73086.
982 public void GetCharsFEFF ()
984 byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
985 Encoding enc = new UTF8Encoding (false, true);
986 string s = enc.GetString (data);
987 AssertEquals ("\uFEFF", s);
989 Encoding utf = Encoding.UTF8;
990 char[] testChars = {'\uFEFF','A'};
992 byte[] bytes = utf.GetBytes(testChars);
993 char[] chars = utf.GetChars(bytes);
994 AssertEquals ("#1", '\uFEFF', chars [0]);
995 AssertEquals ("#2", 'A', chars [1]);
1000 public void CloneNotReadOnly ()
1002 Encoding e = Encoding.GetEncoding (65001).Clone ()
1004 AssertEquals (false, e.IsReadOnly);
1005 e.EncoderFallback = new EncoderExceptionFallback ();
1011 [ExpectedException (typeof (DecoderFallbackException))]
1013 [ExpectedException (typeof (ArgumentException))]
1014 [Category ("NotDotNet")] // MS Bug
1016 public void Bug77315 ()
1018 new UTF8Encoding (false, true).GetString (
1019 new byte [] {0xED, 0xA2, 0x8C});
1023 public void SufficientByteArray ()
1025 Encoder e = Encoding.UTF8.GetEncoder ();
1026 byte [] bytes = new byte [0];
1028 char [] chars = new char [] {'\uD800'};
1029 e.GetBytes (chars, 0, 1, bytes, 0, false);
1031 int ret = e.GetBytes (chars, 1, 0, bytes, 0, true);
1033 AssertEquals ("drop insufficient char in 2.0: char[]", 0, ret);
1035 Fail ("ArgumentException is expected: char[]");
1037 } catch (ArgumentException) {
1040 string s = "\uD800";
1042 int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0);
1044 AssertEquals ("drop insufficient char in 2.0: string", 0, ret);
1046 Fail ("ArgumentException is expected: string");
1048 } catch (ArgumentException) {
1053 [Test] // bug #77550
1054 public void DecoderFallbackSimple ()
1056 UTF8Encoding e = new UTF8Encoding (false, false);
1057 AssertType.AreEqual (0, e.GetDecoder ().GetCharCount (
1058 new byte [] {(byte) 183}, 0, 1),
1060 AssertType.AreEqual (0, e.GetDecoder().GetChars (
1061 new byte [] {(byte) 183}, 0, 1,
1064 AssertType.AreEqual (0, e.GetString (new byte [] {(byte) 183}).Length,