2006-01-19 Atsushi Enomoto <atsushi@ximian.com>
[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 #if NET_2_0
97                 [Category ("NotWorking")]
98 #endif
99                 public void TestMaxCharCount()
100                 {
101                         UTF8Encoding UTF8enc = new UTF8Encoding ();
102 #if NET_2_0
103                         // hmm, where is this extra 1 coming from?
104                         Assertion.AssertEquals ("UTF #1", 51, UTF8enc.GetMaxCharCount(50));
105 #else
106                         Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
107 #endif
108                 }
109         
110                 [Test]
111 #if NET_2_0
112                 [Category ("NotWorking")]
113 #endif
114                 public void TestMaxByteCount()
115                 {
116                         UTF8Encoding UTF8enc = new UTF8Encoding ();
117 #if NET_2_0
118                         // maybe under .NET 2.0 insufficient surrogate pair is just not handled, and 3 is Preamble size.
119                         Assertion.AssertEquals ("UTF #1", 153, UTF8enc.GetMaxByteCount(50));
120 #else
121                         Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
122 #endif
123                 }
124
125                 // regression for bug #59648
126                 [Test]
127                 public void TestThrowOnInvalid ()
128                 {
129                         UTF8Encoding u = new UTF8Encoding (true, false);
130
131                         byte[] data = new byte [] { 0xC0, 0xAF };
132                         string s = u.GetString (data);
133                         AssertEquals (0, s.Length);
134
135                         data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
136                         s = u.GetString (data);
137                         AssertEquals (4, s.Length);
138                         AssertEquals (0x30, (int) s [0]);
139                         AssertEquals (0x31, (int) s [1]);
140                         AssertEquals (0x30, (int) s [2]);
141                         AssertEquals (0x32, (int) s [3]);
142                 }
143
144                 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
145
146                 [Test]
147                 public void T1_Correct_GreekWord_kosme () 
148                 {
149                         byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
150                         string s = utf8.GetString (data);
151                         // cute but saving source code in unicode can be problematic
152                         // so we just ensure we can re-encode this
153                         AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
154                 }
155
156                 [Test]
157                 public void T2_Boundary_1_FirstPossibleSequence_Pass () 
158                 {
159                         byte[] data211 = { 0x00 };
160                         string s = utf8.GetString (data211);
161                         AssertEquals ("1 byte  (U-00000000)", "\0", s);
162                         AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
163
164                         byte[] data212 = { 0xC2, 0x80 };
165                         s = utf8.GetString (data212);
166                         AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
167                         AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
168
169                         byte[] data213 = { 0xE0, 0xA0, 0x80 };
170                         s = utf8.GetString (data213);
171                         AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
172                         AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
173
174                         byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
175                         s = utf8.GetString (data214);
176                         AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
177                         AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
178                         AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
179                 }
180
181                 [Test]
182                 // Fail on MS Fx 1.1
183                 [ExpectedException (typeof (DecoderException))]
184                 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 () 
185                 {
186                         byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
187                         string s = utf8.GetString (data215);
188                         AssertNull ("5 bytes (U-00200000)", s);
189                         AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
190                 }
191
192                 [Test]
193                 // Fail on MS Fx 1.1
194                 [ExpectedException (typeof (DecoderException))]
195                 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 () 
196                 {
197                         byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
198                         string s = utf8.GetString (data216);
199                         AssertNull ("6 bytes (U-04000000)", s);
200                         AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
201                 }
202
203                 [Test]
204                 public void T2_Boundary_2_LastPossibleSequence_Pass () 
205                 {
206                         byte[] data221 = { 0x7F };
207                         string s = utf8.GetString (data221);
208                         AssertEquals ("1 byte  (U-0000007F)", 127, s [0]);
209                         AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
210
211                         byte[] data222 = { 0xDF, 0xBF };
212                         s = utf8.GetString (data222);
213                         AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
214                         AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
215
216                         byte[] data223 = { 0xEF, 0xBF, 0xBF };
217                         s = utf8.GetString (data223);
218                         AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
219                         AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
220
221                 }
222
223                 [Test]
224                 // Fail on MS Fx 1.1
225                 [ExpectedException (typeof (DecoderException))]
226                 public void T2_Boundary_2_LastPossibleSequence_Fail_4 () 
227                 {
228                         byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
229                         string s = utf8.GetString (data224);
230                         AssertNull ("4 bytes (U-001FFFFF)", s);
231                         AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
232                 }
233
234                 [Test]
235                 // Fail on MS Fx 1.1
236                 [ExpectedException (typeof (DecoderException))]
237                 public void T2_Boundary_2_LastPossibleSequence_Fail_5 () 
238                 {
239                         byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
240                         string s = utf8.GetString (data225);
241                         AssertNull ("5 bytes (U-03FFFFFF)", s);
242                         AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
243                 }
244
245                 [Test]
246                 // Fail on MS Fx 1.1
247                 [ExpectedException (typeof (DecoderException))]
248                 public void T2_Boundary_2_LastPossibleSequence_Fail_6 () 
249                 {
250                         byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
251                         string s = utf8.GetString (data226);
252                         AssertNull ("6 bytes (U-7FFFFFFF)", s);
253                         AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
254                 }
255
256                 [Test]
257                 public void T2_Boundary_3_Other_Pass () 
258                 {
259                         byte[] data231 = { 0xED, 0x9F, 0xBF };
260                         string s = utf8.GetString (data231);
261                         AssertEquals ("U-0000D7FF", 55295, s [0]);
262                         AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
263
264                         byte[] data232 = { 0xEE, 0x80, 0x80 };
265                         s = utf8.GetString (data232);
266                         AssertEquals ("U-0000E000", 57344, s [0]);
267                         AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
268
269                         byte[] data233 = { 0xEF, 0xBF, 0xBD };
270                         s = utf8.GetString (data233);
271                         AssertEquals ("U-0000FFFD", 65533, s [0]);
272                         AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
273
274                         byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
275                         s = utf8.GetString (data234);
276                         AssertEquals ("U-0010FFFF-0", 56319, s [0]);
277                         AssertEquals ("U-0010FFFF-1", 57343, s [1]);
278                         AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
279                 }
280
281                 [Test]
282                 // Fail on MS Fx 1.1
283                 [ExpectedException (typeof (DecoderException))]
284                 public void T2_Boundary_3_Other_Fail_5 () 
285                 {
286                         byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
287                         string s = utf8.GetString (data235);
288                         AssertNull ("U-00110000", s);
289                         AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (DecoderException))]
294                 public void T3_Malformed_1_UnexpectedContinuation_311 () 
295                 {
296                         byte[] data = { 0x80 };
297                         string s = utf8.GetString (data);
298                         // exception is "really" expected here
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (DecoderException))]
303                 public void T3_Malformed_1_UnexpectedContinuation_312 () 
304                 {
305                         byte[] data = { 0xBF };
306                         string s = utf8.GetString (data);
307                         // exception is "really" expected here
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (DecoderException))]
312                 public void T3_Malformed_1_UnexpectedContinuation_313 () 
313                 {
314                         byte[] data = { 0x80, 0xBF };
315                         string s = utf8.GetString (data);
316                         // exception is "really" expected here
317                 }
318
319                 [Test]
320                 [ExpectedException (typeof (DecoderException))]
321                 public void T3_Malformed_1_UnexpectedContinuation_314 () 
322                 {
323                         byte[] data = { 0x80, 0xBF, 0x80 };
324                         string s = utf8.GetString (data);
325                         // exception is "really" expected here
326                 }
327
328                 [Test]
329                 [ExpectedException (typeof (DecoderException))]
330                 public void T3_Malformed_1_UnexpectedContinuation_315 () 
331                 {
332                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
333                         string s = utf8.GetString (data);
334                         // exception is "really" expected here
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (DecoderException))]
339                 public void T3_Malformed_1_UnexpectedContinuation_316 () 
340                 {
341                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
342                         string s = utf8.GetString (data);
343                         // exception is "really" expected here
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (DecoderException))]
348                 public void T3_Malformed_1_UnexpectedContinuation_317 () 
349                 {
350                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
351                         string s = utf8.GetString (data);
352                         // exception is "really" expected here
353                 }
354
355                 [Test]
356                 [ExpectedException (typeof (DecoderException))]
357                 public void T3_Malformed_1_UnexpectedContinuation_318 () 
358                 {
359                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
360                         string s = utf8.GetString (data);
361                         // exception is "really" expected here
362                 }
363
364                 [Test]
365                 [ExpectedException (typeof (DecoderException))]
366                 public void T3_Malformed_1_UnexpectedContinuation_319 () 
367                 {
368                         // 64 different continuation characters
369                         byte[] data = {
370                                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 
371                                 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 
372                                 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 
373                                 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
374                         string s = utf8.GetString (data);
375                         // exception is "really" expected here
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (DecoderException))]
380                 public void T3_Malformed_2_LonelyStart_321 ()
381                 {
382                         byte[] data = { 
383                                 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20, 
384                                 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20, 
385                                 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20, 
386                                 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
387                         string s = utf8.GetString (data);
388                         // exception is "really" expected here
389                 }
390
391                 [Test]
392                 [ExpectedException (typeof (DecoderException))]
393                 public void T3_Malformed_2_LonelyStart_322 () 
394                 {
395                         byte[] data = { 
396                                 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20, 
397                                 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
398                         string s = utf8.GetString (data);
399                         // exception is "really" expected here
400                 }
401
402                 [Test]
403                 [ExpectedException (typeof (DecoderException))]
404                 public void T3_Malformed_2_LonelyStart_323 () 
405                 {
406                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
407                         string s = utf8.GetString (data);
408                         // exception is "really" expected here
409                 }
410
411                 [Test]
412                 [ExpectedException (typeof (DecoderException))]
413                 public void T3_Malformed_2_LonelyStart_324 () 
414                 {
415                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
416                         string s = utf8.GetString (data);
417                         // exception is "really" expected here
418                 }
419
420                 [Test]
421                 [ExpectedException (typeof (DecoderException))]
422                 public void T3_Malformed_2_LonelyStart_325 () 
423                 {
424                         byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
425                         string s = utf8.GetString (data);
426                         // exception is "really" expected here
427                 }
428
429                 [Test]
430                 [ExpectedException (typeof (DecoderException))]
431                 public void T3_Malformed_3_LastContinuationMissing_331 () 
432                 {
433                         byte[] data = { 0xC0 };
434                         string s = utf8.GetString (data);
435                         // exception is "really" expected here
436                 }
437
438                 [Test]
439                 [ExpectedException (typeof (DecoderException))]
440                 public void T3_Malformed_3_LastContinuationMissing_332 () 
441                 {
442                         byte[] data = { 0xE0, 0x80 };
443                         string s = utf8.GetString (data);
444                         // exception is "really" expected here
445                 }
446
447                 [Test]
448                 [ExpectedException (typeof (DecoderException))]
449                 public void T3_Malformed_3_LastContinuationMissing_333 () 
450                 {
451                         byte[] data = { 0xF0, 0x80, 0x80 };
452                         string s = utf8.GetString (data);
453                         // exception is "really" expected here
454                 }
455
456                 [Test]
457                 [ExpectedException (typeof (DecoderException))]
458                 public void T3_Malformed_3_LastContinuationMissing_334 () 
459                 {
460                         byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
461                         string s = utf8.GetString (data);
462                         // exception is "really" expected here
463                 }
464
465                 [Test]
466                 [ExpectedException (typeof (DecoderException))]
467                 public void T3_Malformed_3_LastContinuationMissing_335 () 
468                 {
469                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
470                         string s = utf8.GetString (data);
471                         // exception is "really" expected here
472                 }
473
474                 [Test]
475 // MS Fx 1.1 accept this
476 //              [ExpectedException (typeof (DecoderException))]
477                 public void T3_Malformed_3_LastContinuationMissing_336 () 
478                 {
479                         byte[] data = { 0xDF };
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 // MS Fx 1.1 accept this
492 //              [ExpectedException (typeof (DecoderException))]
493                 public void T3_Malformed_3_LastContinuationMissing_337 () 
494                 {
495                         byte[] data = { 0xEF, 0xBF };
496                         try {
497                                 string s = utf8.GetString (data);
498                                 // exception is "really" expected here
499                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
500                         }
501                         catch (DecoderException) {
502                                 // but Mono doesn't - better stick to the standard
503                         }
504                 }
505
506                 [Test]
507                 [ExpectedException (typeof (DecoderException))]
508                 public void T3_Malformed_3_LastContinuationMissing_338 () 
509                 {
510                         byte[] data = { 0xF7, 0xBF, 0xBF };
511                         string s = utf8.GetString (data);
512                         // exception is "really" expected here
513                 }
514
515                 [Test]
516                 [ExpectedException (typeof (DecoderException))]
517                 public void T3_Malformed_3_LastContinuationMissing_339 () 
518                 {
519                         byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
520                         string s = utf8.GetString (data);
521                         // exception is "really" expected here
522                 }
523
524                 [Test]
525                 [ExpectedException (typeof (DecoderException))]
526                 public void T3_Malformed_3_LastContinuationMissing_3310 () 
527                 {
528                         byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
529                         string s = utf8.GetString (data);
530                         // exception is "really" expected here
531                 }
532
533                 [Test]
534                 [ExpectedException (typeof (DecoderException))]
535                 public void T3_Malformed_4_ConcatenationImcomplete () 
536                 {
537                         byte[] data = {
538                                 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF, 
539                                 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
540                         string s = utf8.GetString (data);
541                         // exception is "really" expected here
542                 }
543
544                 [Test]
545                 [ExpectedException (typeof (DecoderException))]
546                 public void T3_Malformed_5_ImpossibleBytes_351 () 
547                 {
548                         byte[] data = { 0xFE };
549                         string s = utf8.GetString (data);
550                         // exception is "really" expected here
551                 }
552
553                 [Test]
554                 [ExpectedException (typeof (DecoderException))]
555                 public void T3_Malformed_5_ImpossibleBytes_352 () 
556                 {
557                         byte[] data = { 0xFF };
558                         string s = utf8.GetString (data);
559                         // exception is "really" expected here
560                 }
561
562                 [Test]
563                 [ExpectedException (typeof (DecoderException))]
564                 public void T3_Malformed_5_ImpossibleBytes_353 () 
565                 {
566                         byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
567                         string s = utf8.GetString (data);
568                         // exception is "really" expected here
569                 }
570
571                 // Overlong == dangereous -> "safe" decoder should reject them
572
573                 [Test]
574                 [ExpectedException (typeof (DecoderException))]
575                 public void T4_Overlong_1_ASCII_Slash_411 () 
576                 {
577                         byte[] data = { 0xC0, 0xAF };
578                         string s = utf8.GetString (data);
579                         // exception is "really" expected here
580                 }
581
582                 [Test]
583                 [ExpectedException (typeof (DecoderException))]
584                 public void T4_Overlong_1_ASCII_Slash_412 () 
585                 {
586                         byte[] data = { 0xE0, 0x80, 0xAF };
587                         string s = utf8.GetString (data);
588                         // exception is "really" expected here
589                 }
590
591                 [Test]
592                 [ExpectedException (typeof (DecoderException))]
593                 public void T4_Overlong_1_ASCII_Slash_413 () 
594                 {
595                         byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
596                         string s = utf8.GetString (data);
597                         // exception is "really" expected here
598                 }
599
600                 [Test]
601                 [ExpectedException (typeof (DecoderException))]
602                 public void T4_Overlong_1_ASCII_Slash_414 () 
603                 {
604                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
605                         string s = utf8.GetString (data);
606                         // exception is "really" expected here
607                 }
608
609                 [Test]
610                 [ExpectedException (typeof (DecoderException))]
611                 public void T4_Overlong_1_ASCII_Slash_415 () 
612                 {
613                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
614                         string s = utf8.GetString (data);
615                         // exception is "really" expected here
616                 }
617
618                 [Test]
619                 [ExpectedException (typeof (DecoderException))]
620                 public void T4_Overlong_2_MaximumBoundary_421 () 
621                 {
622                         byte[] data = { 0xC1, 0xBF };
623                         string s = utf8.GetString (data);
624                         // exception is "really" expected here
625                 }
626
627                 [Test]
628                 [ExpectedException (typeof (DecoderException))]
629                 public void T4_Overlong_2_MaximumBoundary_422 () 
630                 {
631                         byte[] data = { 0xE0, 0x9F, 0xBF };
632                         string s = utf8.GetString (data);
633                         // exception is "really" expected here
634                 }
635
636                 [Test]
637                 [ExpectedException (typeof (DecoderException))]
638                 public void T4_Overlong_2_MaximumBoundary_423 () 
639                 {
640                         byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
641                         string s = utf8.GetString (data);
642                         // exception is "really" expected here
643                 }
644
645                 [Test]
646                 [ExpectedException (typeof (DecoderException))]
647                 public void T4_Overlong_2_MaximumBoundary_424 () 
648                 {
649                         byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
650                         string s = utf8.GetString (data);
651                         // exception is "really" expected here
652                 }
653
654                 [Test]
655                 [ExpectedException (typeof (DecoderException))]
656                 public void T4_Overlong_2_MaximumBoundary_425 () 
657                 {
658                         byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
659                         string s = utf8.GetString (data);
660                         // exception is "really" expected here
661                 }
662
663                 [Test]
664                 [ExpectedException (typeof (DecoderException))]
665                 public void T4_Overlong_3_NUL_431 () 
666                 {
667                         byte[] data = { 0xC0, 0x80 };
668                         string s = utf8.GetString (data);
669                         // exception is "really" expected here
670                 }
671
672                 [Test]
673                 [ExpectedException (typeof (DecoderException))]
674                 public void T4_Overlong_3_NUL_432 () 
675                 {
676                         byte[] data = { 0xE0, 0x80, 0x80 };
677                         string s = utf8.GetString (data);
678                         // exception is "really" expected here
679                 }
680
681                 [Test]
682                 [ExpectedException (typeof (DecoderException))]
683                 public void T4_Overlong_3_NUL_433 () 
684                 {
685                         byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
686                         string s = utf8.GetString (data);
687                         // exception is "really" expected here
688                 }
689
690                 [Test]
691                 [ExpectedException (typeof (DecoderException))]
692                 public void T4_Overlong_3_NUL_434 () 
693                 {
694                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
695                         string s = utf8.GetString (data);
696                         // exception is "really" expected here
697                 }
698
699                 [Test]
700                 [ExpectedException (typeof (DecoderException))]
701                 public void T4_Overlong_3_NUL_435 () 
702                 {
703                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
704                         string s = utf8.GetString (data);
705                         // exception is "really" expected here
706                 }
707
708                 [Test]
709 #if NET_2_0
710                 [ExpectedException (typeof (DecoderFallbackException))]
711                 [Category ("NotWorking")]
712 #endif
713 // MS Fx 1.1 accept this
714 //              [ExpectedException (typeof (DecoderException))]
715                         public void T5_IllegalCodePosition_1_UTF16Surrogates_511 () 
716                 {
717                         byte[] data = { 0xED, 0xA0, 0x80 };
718                         string s = utf8.GetString (data);
719                         // exception is "really" expected here
720                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
721                 }
722
723                 [Test]
724 #if NET_2_0
725                 [ExpectedException (typeof (DecoderFallbackException))]
726                 [Category ("NotWorking")]
727 #endif
728 // MS Fx 1.1 accept this
729 //              [ExpectedException (typeof (DecoderException))]
730                 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 () 
731                 {
732                         byte[] data = { 0xED, 0xAD, 0xBF };
733                         string s = utf8.GetString (data);
734                         // exception is "really" expected here
735                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
736                 }
737
738                 [Test]
739 #if NET_2_0
740                 [ExpectedException (typeof (DecoderFallbackException))]
741                 [Category ("NotWorking")]
742 #endif
743 // MS Fx 1.1 accept this
744 //              [ExpectedException (typeof (DecoderException))]
745                 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
746                 {
747                         byte[] data = { 0xED, 0xAE, 0x80 };
748                         string s = utf8.GetString (data);
749                         // exception is "really" expected here
750                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
751                 }
752
753                 [Test]
754 #if NET_2_0
755                 [ExpectedException (typeof (DecoderFallbackException))]
756                 [Category ("NotWorking")]
757 #endif
758 // MS Fx 1.1 accept this
759 //              [ExpectedException (typeof (DecoderException))]
760                 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 () 
761                 {
762                         byte[] data = { 0xED, 0xAF, 0xBF };
763                         string s = utf8.GetString (data);
764                         // exception is "really" expected here
765                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
766                 }
767
768                 [Test]
769 #if NET_2_0
770                 [ExpectedException (typeof (DecoderFallbackException))]
771                 [Category ("NotWorking")]
772 #endif
773 // MS Fx 1.1 accept this
774 //              [ExpectedException (typeof (DecoderException))]
775                 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
776                 {
777                         byte[] data = { 0xED, 0xB0, 0x80 };
778                         string s = utf8.GetString (data);
779                         // exception is "really" expected here
780                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
781                 }
782
783                 [Test]
784 #if NET_2_0
785                 [ExpectedException (typeof (DecoderFallbackException))]
786                 [Category ("NotWorking")]
787 #endif
788 // MS Fx 1.1 accept this
789 //              [ExpectedException (typeof (DecoderException))]
790                 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 () 
791                 {
792                         byte[] data = { 0xED, 0xBE, 0x80 };
793                         string s = utf8.GetString (data);
794                         // exception is "really" expected here
795                         AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
796                 }
797
798                 [Test]
799 #if NET_2_0
800                 [ExpectedException (typeof (DecoderFallbackException))]
801                 [Category ("NotWorking")]
802 #endif
803 // MS Fx 1.1 accept this
804 //              [ExpectedException (typeof (DecoderException))]
805                 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 () 
806                 {
807                         byte[] data = { 0xED, 0xBF, 0xBF };
808                         string s = utf8.GetString (data);
809                         // exception is "really" expected here
810                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
811                 }
812
813                 [Test]
814 #if NET_2_0
815                 [ExpectedException (typeof (DecoderFallbackException))]
816                 [Category ("NotWorking")]
817 #endif
818 // MS Fx 1.1 accept this
819 //              [ExpectedException (typeof (DecoderException))]
820                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 () 
821                 {
822                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
823                         string s = utf8.GetString (data);
824                         // exception is "really" expected here
825                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
826                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
827                 }
828
829                 [Test]
830 #if NET_2_0
831                 [ExpectedException (typeof (DecoderFallbackException))]
832                 [Category ("NotWorking")]
833 #endif
834 // MS Fx 1.1 accept this
835 //              [ExpectedException (typeof (DecoderException))]
836                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 () 
837                 {
838                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
839                         string s = utf8.GetString (data);
840                         // exception is "really" expected here
841                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
842                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
843                 }
844
845                 [Test]
846 #if NET_2_0
847                 [ExpectedException (typeof (DecoderFallbackException))]
848                 [Category ("NotWorking")]
849 #endif
850 // MS Fx 1.1 accept this
851 //              [ExpectedException (typeof (DecoderException))]
852                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 () 
853                 {
854                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
855                         string s = utf8.GetString (data);
856                         // exception is "really" expected here
857                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
858                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
859                 }
860
861                 [Test]
862 #if NET_2_0
863                 [ExpectedException (typeof (DecoderFallbackException))]
864                 [Category ("NotWorking")]
865 #endif
866 // MS Fx 1.1 accept this
867 //              [ExpectedException (typeof (DecoderException))]
868                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 () 
869                 {
870                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
871                         string s = utf8.GetString (data);
872                         // exception is "really" expected here
873                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
874                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
875                 }
876
877                 [Test]
878 #if NET_2_0
879                 [ExpectedException (typeof (DecoderFallbackException))]
880                 [Category ("NotWorking")]
881 #endif
882 // MS Fx 1.1 accept this
883 //              [ExpectedException (typeof (DecoderException))]
884                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 () 
885                 {
886                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
887                         string s = utf8.GetString (data);
888                         // exception is "really" expected here
889                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
890                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
891                 }
892
893                 [Test]
894 #if NET_2_0
895                 [ExpectedException (typeof (DecoderFallbackException))]
896                 [Category ("NotWorking")]
897 #endif
898 // MS Fx 1.1 accept this
899 //              [ExpectedException (typeof (DecoderException))]
900                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 () 
901                 {
902                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
903                         string s = utf8.GetString (data);
904                         // exception is "really" expected here
905                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
906                         AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
907                 }
908
909                 [Test]
910 #if NET_2_0
911                 [ExpectedException (typeof (DecoderFallbackException))]
912                 [Category ("NotWorking")]
913 #endif
914 // MS Fx 1.1 accept this
915 //              [ExpectedException (typeof (DecoderException))]
916                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 () 
917                 {
918                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
919                         string s = utf8.GetString (data);
920                         // exception is "really" expected here
921                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
922                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
923                 }
924
925                 [Test]
926 #if NET_2_0
927                 [ExpectedException (typeof (DecoderFallbackException))]
928                 [Category ("NotWorking")]
929 #endif
930 // MS Fx 1.1 accept this
931 //              [ExpectedException (typeof (DecoderException))]
932                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 () 
933                 {
934                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
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", 57343, s [1]);
939                 }
940
941                 [Test]
942 // MS Fx 1.1 accept this
943 //              [ExpectedException (typeof (DecoderException))]
944                 public void T5_IllegalCodePosition_3_Other_531 () 
945                 {
946                         byte[] data = { 0xEF, 0xBF, 0xBE };
947                         string s = utf8.GetString (data);
948                         // exception is "really" expected here
949                         AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
950                 }
951
952                 [Test]
953 // MS Fx 1.1 accept this
954 //              [ExpectedException (typeof (DecoderException))]
955                 public void T5_IllegalCodePosition_3_Other_532 () 
956                 {
957                         byte[] data = { 0xEF, 0xBF, 0xBF };
958                         string s = utf8.GetString (data);
959                         // exception is "really" expected here
960                         AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
961                 }
962
963                 [Test]
964                 // bug #75065 and #73086.
965                 public void GetCharsFEFF ()
966                 {
967                         byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
968                         Encoding enc = new UTF8Encoding (false, true);
969                         string s = enc.GetString (data);
970                         AssertEquals ("\uFEFF", s);
971
972                         Encoding utf = Encoding.UTF8;
973                         char[] testChars = {'\uFEFF','A'};
974
975                         byte[] bytes = utf.GetBytes(testChars);
976                         char[] chars = utf.GetChars(bytes);
977                         AssertEquals ("#1", '\uFEFF', chars [0]);
978                         AssertEquals ("#2", 'A', chars [1]);
979                 }
980         }
981 }