fa3152b10a6a18ec882b2f3da3d6944df2c01ebd
[mono.git] / mcs / class / corlib / Test / System.Text / UTF8EncodingTest.cs
1 //
2 // UTF8EncodingTest.cs - NUnit Test Cases for System.Text.UTF8Encoding
3 //
4 // Authors:
5 //      Patrick Kalkman  kalkman@cistron.nl
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2003 Patrick Kalkman
9 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14 using System.Text;
15
16 #if NET_2_0
17 using DecoderException = System.Text.DecoderFallbackException;
18 #else
19 using DecoderException = System.ArgumentException;
20 #endif
21
22 namespace MonoTests.System.Text {
23
24         [TestFixture]
25         public class UTF8EncodingTest : Assertion {
26
27                 private UTF8Encoding utf8;
28
29                 [SetUp]
30                 public void Create () 
31                 {
32                         utf8 = new UTF8Encoding (true, true);
33                 }
34
35                 [Test]
36                 public void TestEncodingGetBytes1()
37                 {
38                         UTF8Encoding utf8Enc = new UTF8Encoding ();
39                         string UniCode = "\u0041\u2262\u0391\u002E";
40                         
41                         // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E 
42                         // see (RFC 2044)
43                         byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
44                         
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]);
52                 }
53         
54                 [Test]
55                 public void TestEncodingGetBytes2()
56                 {
57                         UTF8Encoding utf8Enc = new UTF8Encoding ();
58                         string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
59                         
60                         // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 
61                         // see (RFC 2044)
62                         byte[] utf8Bytes = new byte [11];
63                         
64                         int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
65                         
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]);
78                 }
79         
80                 [Test]
81                 public void TestDecodingGetChars1()
82                 {
83                         UTF8Encoding utf8Enc = new UTF8Encoding ();
84                         // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>." 
85                         // see (RFC 2044)
86                         byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
87                         char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
88                              
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]);
93                 }
94                 
95                 [Test]
96                 public void TestMaxCharCount()
97                 {
98                         UTF8Encoding UTF8enc = new UTF8Encoding ();
99                         Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
100                 }
101         
102                 [Test]
103                 public void TestMaxByteCount()
104                 {
105                         UTF8Encoding UTF8enc = new UTF8Encoding ();
106                         Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
107                 }
108
109                 // regression for bug #59648
110                 [Test]
111                 public void TestThrowOnInvalid ()
112                 {
113                         UTF8Encoding u = new UTF8Encoding (true, false);
114
115                         byte[] data = new byte [] { 0xC0, 0xAF };
116                         string s = u.GetString (data);
117                         AssertEquals (0, s.Length);
118
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]);
126                 }
127
128                 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
129
130                 [Test]
131                 public void T1_Correct_GreekWord_kosme () 
132                 {
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)));
138                 }
139
140                 [Test]
141                 public void T2_Boundary_1_FirstPossibleSequence_Pass () 
142                 {
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)));
147
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)));
152
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)));
157
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)));
163                 }
164
165                 [Test]
166                 // Fail on MS Fx 1.1
167                 [ExpectedException (typeof (DecoderException))]
168                 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 () 
169                 {
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)));
174                 }
175
176                 [Test]
177                 // Fail on MS Fx 1.1
178                 [ExpectedException (typeof (DecoderException))]
179                 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 () 
180                 {
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)));
185                 }
186
187                 [Test]
188                 public void T2_Boundary_2_LastPossibleSequence_Pass () 
189                 {
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)));
194
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)));
199
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)));
204
205                 }
206
207                 [Test]
208                 // Fail on MS Fx 1.1
209                 [ExpectedException (typeof (DecoderException))]
210                 public void T2_Boundary_2_LastPossibleSequence_Fail_4 () 
211                 {
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)));
216                 }
217
218                 [Test]
219                 // Fail on MS Fx 1.1
220                 [ExpectedException (typeof (DecoderException))]
221                 public void T2_Boundary_2_LastPossibleSequence_Fail_5 () 
222                 {
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)));
227                 }
228
229                 [Test]
230                 // Fail on MS Fx 1.1
231                 [ExpectedException (typeof (DecoderException))]
232                 public void T2_Boundary_2_LastPossibleSequence_Fail_6 () 
233                 {
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)));
238                 }
239
240                 [Test]
241                 public void T2_Boundary_3_Other_Pass () 
242                 {
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)));
247
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)));
252
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)));
257
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)));
263                 }
264
265                 [Test]
266                 // Fail on MS Fx 1.1
267                 [ExpectedException (typeof (DecoderException))]
268                 public void T2_Boundary_3_Other_Fail_5 () 
269                 {
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)));
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (DecoderException))]
278                 public void T3_Malformed_1_UnexpectedContinuation_311 () 
279                 {
280                         byte[] data = { 0x80 };
281                         string s = utf8.GetString (data);
282                         // exception is "really" expected here
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (DecoderException))]
287                 public void T3_Malformed_1_UnexpectedContinuation_312 () 
288                 {
289                         byte[] data = { 0xBF };
290                         string s = utf8.GetString (data);
291                         // exception is "really" expected here
292                 }
293
294                 [Test]
295                 [ExpectedException (typeof (DecoderException))]
296                 public void T3_Malformed_1_UnexpectedContinuation_313 () 
297                 {
298                         byte[] data = { 0x80, 0xBF };
299                         string s = utf8.GetString (data);
300                         // exception is "really" expected here
301                 }
302
303                 [Test]
304                 [ExpectedException (typeof (DecoderException))]
305                 public void T3_Malformed_1_UnexpectedContinuation_314 () 
306                 {
307                         byte[] data = { 0x80, 0xBF, 0x80 };
308                         string s = utf8.GetString (data);
309                         // exception is "really" expected here
310                 }
311
312                 [Test]
313                 [ExpectedException (typeof (DecoderException))]
314                 public void T3_Malformed_1_UnexpectedContinuation_315 () 
315                 {
316                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
317                         string s = utf8.GetString (data);
318                         // exception is "really" expected here
319                 }
320
321                 [Test]
322                 [ExpectedException (typeof (DecoderException))]
323                 public void T3_Malformed_1_UnexpectedContinuation_316 () 
324                 {
325                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
326                         string s = utf8.GetString (data);
327                         // exception is "really" expected here
328                 }
329
330                 [Test]
331                 [ExpectedException (typeof (DecoderException))]
332                 public void T3_Malformed_1_UnexpectedContinuation_317 () 
333                 {
334                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
335                         string s = utf8.GetString (data);
336                         // exception is "really" expected here
337                 }
338
339                 [Test]
340                 [ExpectedException (typeof (DecoderException))]
341                 public void T3_Malformed_1_UnexpectedContinuation_318 () 
342                 {
343                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
344                         string s = utf8.GetString (data);
345                         // exception is "really" expected here
346                 }
347
348                 [Test]
349                 [ExpectedException (typeof (DecoderException))]
350                 public void T3_Malformed_1_UnexpectedContinuation_319 () 
351                 {
352                         // 64 different continuation characters
353                         byte[] data = {
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
360                 }
361
362                 [Test]
363                 [ExpectedException (typeof (DecoderException))]
364                 public void T3_Malformed_2_LonelyStart_321 ()
365                 {
366                         byte[] data = { 
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
373                 }
374
375                 [Test]
376                 [ExpectedException (typeof (DecoderException))]
377                 public void T3_Malformed_2_LonelyStart_322 () 
378                 {
379                         byte[] data = { 
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
384                 }
385
386                 [Test]
387                 [ExpectedException (typeof (DecoderException))]
388                 public void T3_Malformed_2_LonelyStart_323 () 
389                 {
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
393                 }
394
395                 [Test]
396                 [ExpectedException (typeof (DecoderException))]
397                 public void T3_Malformed_2_LonelyStart_324 () 
398                 {
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
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (DecoderException))]
406                 public void T3_Malformed_2_LonelyStart_325 () 
407                 {
408                         byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
409                         string s = utf8.GetString (data);
410                         // exception is "really" expected here
411                 }
412
413                 [Test]
414                 [ExpectedException (typeof (DecoderException))]
415                 public void T3_Malformed_3_LastContinuationMissing_331 () 
416                 {
417                         byte[] data = { 0xC0 };
418                         string s = utf8.GetString (data);
419                         // exception is "really" expected here
420                 }
421
422                 [Test]
423                 [ExpectedException (typeof (DecoderException))]
424                 public void T3_Malformed_3_LastContinuationMissing_332 () 
425                 {
426                         byte[] data = { 0xE0, 0x80 };
427                         string s = utf8.GetString (data);
428                         // exception is "really" expected here
429                 }
430
431                 [Test]
432                 [ExpectedException (typeof (DecoderException))]
433                 public void T3_Malformed_3_LastContinuationMissing_333 () 
434                 {
435                         byte[] data = { 0xF0, 0x80, 0x80 };
436                         string s = utf8.GetString (data);
437                         // exception is "really" expected here
438                 }
439
440                 [Test]
441                 [ExpectedException (typeof (DecoderException))]
442                 public void T3_Malformed_3_LastContinuationMissing_334 () 
443                 {
444                         byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
445                         string s = utf8.GetString (data);
446                         // exception is "really" expected here
447                 }
448
449                 [Test]
450                 [ExpectedException (typeof (DecoderException))]
451                 public void T3_Malformed_3_LastContinuationMissing_335 () 
452                 {
453                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
454                         string s = utf8.GetString (data);
455                         // exception is "really" expected here
456                 }
457
458                 [Test]
459 // MS Fx 1.1 accept this
460 //              [ExpectedException (typeof (DecoderException))]
461                 public void T3_Malformed_3_LastContinuationMissing_336 () 
462                 {
463                         byte[] data = { 0xDF };
464                         try {
465                                 string s = utf8.GetString (data);
466                                 // exception is "really" expected here
467                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
468                         }
469                         catch (DecoderException) {
470                                 // but Mono doesn't - better stick to the standard
471                         }
472                 }
473
474                 [Test]
475 // MS Fx 1.1 accept this
476 //              [ExpectedException (typeof (DecoderException))]
477                 public void T3_Malformed_3_LastContinuationMissing_337 () 
478                 {
479                         byte[] data = { 0xEF, 0xBF };
480                         try {
481                                 string s = utf8.GetString (data);
482                                 // exception is "really" expected here
483                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
484                         }
485                         catch (DecoderException) {
486                                 // but Mono doesn't - better stick to the standard
487                         }
488                 }
489
490                 [Test]
491                 [ExpectedException (typeof (DecoderException))]
492                 public void T3_Malformed_3_LastContinuationMissing_338 () 
493                 {
494                         byte[] data = { 0xF7, 0xBF, 0xBF };
495                         string s = utf8.GetString (data);
496                         // exception is "really" expected here
497                 }
498
499                 [Test]
500                 [ExpectedException (typeof (DecoderException))]
501                 public void T3_Malformed_3_LastContinuationMissing_339 () 
502                 {
503                         byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
504                         string s = utf8.GetString (data);
505                         // exception is "really" expected here
506                 }
507
508                 [Test]
509                 [ExpectedException (typeof (DecoderException))]
510                 public void T3_Malformed_3_LastContinuationMissing_3310 () 
511                 {
512                         byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
513                         string s = utf8.GetString (data);
514                         // exception is "really" expected here
515                 }
516
517                 [Test]
518                 [ExpectedException (typeof (DecoderException))]
519                 public void T3_Malformed_4_ConcatenationImcomplete () 
520                 {
521                         byte[] data = {
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
526                 }
527
528                 [Test]
529                 [ExpectedException (typeof (DecoderException))]
530                 public void T3_Malformed_5_ImpossibleBytes_351 () 
531                 {
532                         byte[] data = { 0xFE };
533                         string s = utf8.GetString (data);
534                         // exception is "really" expected here
535                 }
536
537                 [Test]
538                 [ExpectedException (typeof (DecoderException))]
539                 public void T3_Malformed_5_ImpossibleBytes_352 () 
540                 {
541                         byte[] data = { 0xFF };
542                         string s = utf8.GetString (data);
543                         // exception is "really" expected here
544                 }
545
546                 [Test]
547                 [ExpectedException (typeof (DecoderException))]
548                 public void T3_Malformed_5_ImpossibleBytes_353 () 
549                 {
550                         byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
551                         string s = utf8.GetString (data);
552                         // exception is "really" expected here
553                 }
554
555                 // Overlong == dangereous -> "safe" decoder should reject them
556
557                 [Test]
558                 [ExpectedException (typeof (DecoderException))]
559                 public void T4_Overlong_1_ASCII_Slash_411 () 
560                 {
561                         byte[] data = { 0xC0, 0xAF };
562                         string s = utf8.GetString (data);
563                         // exception is "really" expected here
564                 }
565
566                 [Test]
567                 [ExpectedException (typeof (DecoderException))]
568                 public void T4_Overlong_1_ASCII_Slash_412 () 
569                 {
570                         byte[] data = { 0xE0, 0x80, 0xAF };
571                         string s = utf8.GetString (data);
572                         // exception is "really" expected here
573                 }
574
575                 [Test]
576                 [ExpectedException (typeof (DecoderException))]
577                 public void T4_Overlong_1_ASCII_Slash_413 () 
578                 {
579                         byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
580                         string s = utf8.GetString (data);
581                         // exception is "really" expected here
582                 }
583
584                 [Test]
585                 [ExpectedException (typeof (DecoderException))]
586                 public void T4_Overlong_1_ASCII_Slash_414 () 
587                 {
588                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
589                         string s = utf8.GetString (data);
590                         // exception is "really" expected here
591                 }
592
593                 [Test]
594                 [ExpectedException (typeof (DecoderException))]
595                 public void T4_Overlong_1_ASCII_Slash_415 () 
596                 {
597                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
598                         string s = utf8.GetString (data);
599                         // exception is "really" expected here
600                 }
601
602                 [Test]
603                 [ExpectedException (typeof (DecoderException))]
604                 public void T4_Overlong_2_MaximumBoundary_421 () 
605                 {
606                         byte[] data = { 0xC1, 0xBF };
607                         string s = utf8.GetString (data);
608                         // exception is "really" expected here
609                 }
610
611                 [Test]
612                 [ExpectedException (typeof (DecoderException))]
613                 public void T4_Overlong_2_MaximumBoundary_422 () 
614                 {
615                         byte[] data = { 0xE0, 0x9F, 0xBF };
616                         string s = utf8.GetString (data);
617                         // exception is "really" expected here
618                 }
619
620                 [Test]
621                 [ExpectedException (typeof (DecoderException))]
622                 public void T4_Overlong_2_MaximumBoundary_423 () 
623                 {
624                         byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
625                         string s = utf8.GetString (data);
626                         // exception is "really" expected here
627                 }
628
629                 [Test]
630                 [ExpectedException (typeof (DecoderException))]
631                 public void T4_Overlong_2_MaximumBoundary_424 () 
632                 {
633                         byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
634                         string s = utf8.GetString (data);
635                         // exception is "really" expected here
636                 }
637
638                 [Test]
639                 [ExpectedException (typeof (DecoderException))]
640                 public void T4_Overlong_2_MaximumBoundary_425 () 
641                 {
642                         byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
643                         string s = utf8.GetString (data);
644                         // exception is "really" expected here
645                 }
646
647                 [Test]
648                 [ExpectedException (typeof (DecoderException))]
649                 public void T4_Overlong_3_NUL_431 () 
650                 {
651                         byte[] data = { 0xC0, 0x80 };
652                         string s = utf8.GetString (data);
653                         // exception is "really" expected here
654                 }
655
656                 [Test]
657                 [ExpectedException (typeof (DecoderException))]
658                 public void T4_Overlong_3_NUL_432 () 
659                 {
660                         byte[] data = { 0xE0, 0x80, 0x80 };
661                         string s = utf8.GetString (data);
662                         // exception is "really" expected here
663                 }
664
665                 [Test]
666                 [ExpectedException (typeof (DecoderException))]
667                 public void T4_Overlong_3_NUL_433 () 
668                 {
669                         byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
670                         string s = utf8.GetString (data);
671                         // exception is "really" expected here
672                 }
673
674                 [Test]
675                 [ExpectedException (typeof (DecoderException))]
676                 public void T4_Overlong_3_NUL_434 () 
677                 {
678                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
679                         string s = utf8.GetString (data);
680                         // exception is "really" expected here
681                 }
682
683                 [Test]
684                 [ExpectedException (typeof (DecoderException))]
685                 public void T4_Overlong_3_NUL_435 () 
686                 {
687                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
688                         string s = utf8.GetString (data);
689                         // exception is "really" expected here
690                 }
691
692                 [Test]
693 // MS Fx 1.1 accept this
694 //              [ExpectedException (typeof (DecoderException))]
695                         public void T5_IllegalCodePosition_1_UTF16Surrogates_511 () 
696                 {
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]);
701                 }
702
703                 [Test]
704 // MS Fx 1.1 accept this
705 //              [ExpectedException (typeof (DecoderException))]
706                 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 () 
707                 {
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]);
712                 }
713
714                 [Test]
715 // MS Fx 1.1 accept this
716 //              [ExpectedException (typeof (DecoderException))]
717                 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
718                 {
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]);
723                 }
724
725                 [Test]
726 // MS Fx 1.1 accept this
727 //              [ExpectedException (typeof (DecoderException))]
728                 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 () 
729                 {
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]);
734                 }
735
736                 [Test]
737 // MS Fx 1.1 accept this
738 //              [ExpectedException (typeof (DecoderException))]
739                 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
740                 {
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]);
745                 }
746
747                 [Test]
748 // MS Fx 1.1 accept this
749 //              [ExpectedException (typeof (DecoderException))]
750                 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 () 
751                 {
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]);
756                 }
757
758                 [Test]
759 // MS Fx 1.1 accept this
760 //              [ExpectedException (typeof (DecoderException))]
761                 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 () 
762                 {
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]);
767                 }
768
769                 [Test]
770 // MS Fx 1.1 accept this
771 //              [ExpectedException (typeof (DecoderException))]
772                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 () 
773                 {
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]);
779                 }
780
781                 [Test]
782 // MS Fx 1.1 accept this
783 //              [ExpectedException (typeof (DecoderException))]
784                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 () 
785                 {
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]);
791                 }
792
793                 [Test]
794 // MS Fx 1.1 accept this
795 //              [ExpectedException (typeof (DecoderException))]
796                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 () 
797                 {
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]);
803                 }
804
805                 [Test]
806 // MS Fx 1.1 accept this
807 //              [ExpectedException (typeof (DecoderException))]
808                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 () 
809                 {
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]);
815                 }
816
817                 [Test]
818 // MS Fx 1.1 accept this
819 //              [ExpectedException (typeof (DecoderException))]
820                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 () 
821                 {
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]);
827                 }
828
829                 [Test]
830 // MS Fx 1.1 accept this
831 //              [ExpectedException (typeof (DecoderException))]
832                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 () 
833                 {
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]);
839                 }
840
841                 [Test]
842 // MS Fx 1.1 accept this
843 //              [ExpectedException (typeof (DecoderException))]
844                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 () 
845                 {
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]);
851                 }
852
853                 [Test]
854 // MS Fx 1.1 accept this
855 //              [ExpectedException (typeof (DecoderException))]
856                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 () 
857                 {
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]);
863                 }
864
865                 [Test]
866 // MS Fx 1.1 accept this
867 //              [ExpectedException (typeof (DecoderException))]
868                 public void T5_IllegalCodePosition_3_Other_531 () 
869                 {
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]);
874                 }
875
876                 [Test]
877 // MS Fx 1.1 accept this
878 //              [ExpectedException (typeof (DecoderException))]
879                 public void T5_IllegalCodePosition_3_Other_532 () 
880                 {
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]);
885                 }
886
887                 [Test]
888                 // bug #75065 and #73086.
889                 public void GetCharsFEFF ()
890                 {
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);
895
896                         Encoding utf = Encoding.UTF8;
897                         char[] testChars = {'\uFEFF','A'};
898
899                         byte[] bytes = utf.GetBytes(testChars);
900                         char[] chars = utf.GetChars(bytes);
901                         AssertEquals ("#1", '\uFEFF', chars [0]);
902                         AssertEquals ("#2", 'A', chars [1]);
903                 }
904         }
905 }