New test.
[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 using AssertType = NUnit.Framework.Assert;
23
24 namespace MonoTests.System.Text {
25
26         [TestFixture]
27         public class UTF8EncodingTest : Assertion {
28
29                 private UTF8Encoding utf8;
30
31                 [SetUp]
32                 public void Create () 
33                 {
34                         utf8 = new UTF8Encoding (true, true);
35                 }
36
37                 [Test]
38                 public void TestEncodingGetBytes1()
39                 {
40                         UTF8Encoding utf8Enc = new UTF8Encoding ();
41                         string UniCode = "\u0041\u2262\u0391\u002E";
42                         
43                         // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E 
44                         // see (RFC 2044)
45                         byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
46                         
47                         Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
48                         Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
49                         Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
50                         Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
51                         Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
52                         Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
53                         Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
54                 }
55         
56                 [Test]
57                 public void TestEncodingGetBytes2()
58                 {
59                         UTF8Encoding utf8Enc = new UTF8Encoding ();
60                         string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
61                         
62                         // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 
63                         // see (RFC 2044)
64                         byte[] utf8Bytes = new byte [11];
65                         
66                         int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
67                         
68                         Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
69                         Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
70                         Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
71                         Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
72                         Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
73                         Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
74                         Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
75                         Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
76                         Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
77                         Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
78                         Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
79                         Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
80                 }
81         
82                 [Test]
83                 public void TestDecodingGetChars1()
84                 {
85                         UTF8Encoding utf8Enc = new UTF8Encoding ();
86                         // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>." 
87                         // see (RFC 2044)
88                         byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
89                         char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
90                              
91                         Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
92                         Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
93                         Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
94                         Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
95                 }
96                 
97                 [Test]
98 #if NET_2_0
99                 [Category ("NotWorking")]
100 #endif
101                 public void TestMaxCharCount()
102                 {
103                         UTF8Encoding UTF8enc = new UTF8Encoding ();
104 #if NET_2_0
105                         // hmm, where is this extra 1 coming from?
106                         Assertion.AssertEquals ("UTF #1", 51, UTF8enc.GetMaxCharCount(50));
107 #else
108                         Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
109 #endif
110                 }
111         
112                 [Test]
113 #if NET_2_0
114                 [Category ("NotWorking")]
115 #endif
116                 public void TestMaxByteCount()
117                 {
118                         UTF8Encoding UTF8enc = new UTF8Encoding ();
119 #if NET_2_0
120                         // maybe under .NET 2.0 insufficient surrogate pair is just not handled, and 3 is Preamble size.
121                         Assertion.AssertEquals ("UTF #1", 153, UTF8enc.GetMaxByteCount(50));
122 #else
123                         Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
124 #endif
125                 }
126
127                 // regression for bug #59648
128                 [Test]
129                 public void TestThrowOnInvalid ()
130                 {
131                         UTF8Encoding u = new UTF8Encoding (true, false);
132
133                         byte[] data = new byte [] { 0xC0, 0xAF };
134                         string s = u.GetString (data);
135                         AssertEquals (0, s.Length);
136
137                         data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
138                         s = u.GetString (data);
139                         AssertEquals (4, s.Length);
140                         AssertEquals (0x30, (int) s [0]);
141                         AssertEquals (0x31, (int) s [1]);
142                         AssertEquals (0x30, (int) s [2]);
143                         AssertEquals (0x32, (int) s [3]);
144                 }
145
146                 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
147
148                 [Test]
149                 public void T1_Correct_GreekWord_kosme () 
150                 {
151                         byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
152                         string s = utf8.GetString (data);
153                         // cute but saving source code in unicode can be problematic
154                         // so we just ensure we can re-encode this
155                         AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
156                 }
157
158                 [Test]
159                 public void T2_Boundary_1_FirstPossibleSequence_Pass () 
160                 {
161                         byte[] data211 = { 0x00 };
162                         string s = utf8.GetString (data211);
163                         AssertEquals ("1 byte  (U-00000000)", "\0", s);
164                         AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
165
166                         byte[] data212 = { 0xC2, 0x80 };
167                         s = utf8.GetString (data212);
168                         AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
169                         AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
170
171                         byte[] data213 = { 0xE0, 0xA0, 0x80 };
172                         s = utf8.GetString (data213);
173                         AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
174                         AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
175
176                         byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
177                         s = utf8.GetString (data214);
178                         AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
179                         AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
180                         AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
181                 }
182
183                 [Test]
184                 // Fail on MS Fx 1.1
185                 [ExpectedException (typeof (DecoderException))]
186                 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 () 
187                 {
188                         byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
189                         string s = utf8.GetString (data215);
190                         AssertNull ("5 bytes (U-00200000)", s);
191                         AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
192                 }
193
194                 [Test]
195                 // Fail on MS Fx 1.1
196                 [ExpectedException (typeof (DecoderException))]
197                 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 () 
198                 {
199                         byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
200                         string s = utf8.GetString (data216);
201                         AssertNull ("6 bytes (U-04000000)", s);
202                         AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
203                 }
204
205                 [Test]
206                 public void T2_Boundary_2_LastPossibleSequence_Pass () 
207                 {
208                         byte[] data221 = { 0x7F };
209                         string s = utf8.GetString (data221);
210                         AssertEquals ("1 byte  (U-0000007F)", 127, s [0]);
211                         AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
212
213                         byte[] data222 = { 0xDF, 0xBF };
214                         s = utf8.GetString (data222);
215                         AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
216                         AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
217
218                         byte[] data223 = { 0xEF, 0xBF, 0xBF };
219                         s = utf8.GetString (data223);
220                         AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
221                         AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
222
223                 }
224
225                 [Test]
226                 // Fail on MS Fx 1.1
227                 [ExpectedException (typeof (DecoderException))]
228                 public void T2_Boundary_2_LastPossibleSequence_Fail_4 () 
229                 {
230                         byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
231                         string s = utf8.GetString (data224);
232                         AssertNull ("4 bytes (U-001FFFFF)", s);
233                         AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
234                 }
235
236                 [Test]
237                 // Fail on MS Fx 1.1
238                 [ExpectedException (typeof (DecoderException))]
239                 public void T2_Boundary_2_LastPossibleSequence_Fail_5 () 
240                 {
241                         byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
242                         string s = utf8.GetString (data225);
243                         AssertNull ("5 bytes (U-03FFFFFF)", s);
244                         AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
245                 }
246
247                 [Test]
248                 // Fail on MS Fx 1.1
249                 [ExpectedException (typeof (DecoderException))]
250                 public void T2_Boundary_2_LastPossibleSequence_Fail_6 () 
251                 {
252                         byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
253                         string s = utf8.GetString (data226);
254                         AssertNull ("6 bytes (U-7FFFFFFF)", s);
255                         AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
256                 }
257
258                 [Test]
259                 public void T2_Boundary_3_Other_Pass () 
260                 {
261                         byte[] data231 = { 0xED, 0x9F, 0xBF };
262                         string s = utf8.GetString (data231);
263                         AssertEquals ("U-0000D7FF", 55295, s [0]);
264                         AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
265
266                         byte[] data232 = { 0xEE, 0x80, 0x80 };
267                         s = utf8.GetString (data232);
268                         AssertEquals ("U-0000E000", 57344, s [0]);
269                         AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
270
271                         byte[] data233 = { 0xEF, 0xBF, 0xBD };
272                         s = utf8.GetString (data233);
273                         AssertEquals ("U-0000FFFD", 65533, s [0]);
274                         AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
275
276                         byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
277                         s = utf8.GetString (data234);
278                         AssertEquals ("U-0010FFFF-0", 56319, s [0]);
279                         AssertEquals ("U-0010FFFF-1", 57343, s [1]);
280                         AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
281                 }
282
283                 [Test]
284                 // Fail on MS Fx 1.1
285                 [ExpectedException (typeof (DecoderException))]
286                 public void T2_Boundary_3_Other_Fail_5 () 
287                 {
288                         byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
289                         string s = utf8.GetString (data235);
290                         AssertNull ("U-00110000", s);
291                         AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
292                 }
293
294                 [Test]
295                 [ExpectedException (typeof (DecoderException))]
296                 public void T3_Malformed_1_UnexpectedContinuation_311 () 
297                 {
298                         byte[] data = { 0x80 };
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_312 () 
306                 {
307                         byte[] data = { 0xBF };
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_313 () 
315                 {
316                         byte[] data = { 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_314 () 
324                 {
325                         byte[] data = { 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_315 () 
333                 {
334                         byte[] data = { 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_316 () 
342                 {
343                         byte[] data = { 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_317 () 
351                 {
352                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
353                         string s = utf8.GetString (data);
354                         // exception is "really" expected here
355                 }
356
357                 [Test]
358                 [ExpectedException (typeof (DecoderException))]
359                 public void T3_Malformed_1_UnexpectedContinuation_318 () 
360                 {
361                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
362                         string s = utf8.GetString (data);
363                         // exception is "really" expected here
364                 }
365
366                 [Test]
367                 [ExpectedException (typeof (DecoderException))]
368                 public void T3_Malformed_1_UnexpectedContinuation_319 () 
369                 {
370                         // 64 different continuation characters
371                         byte[] data = {
372                                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 
373                                 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 
374                                 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 
375                                 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
376                         string s = utf8.GetString (data);
377                         // exception is "really" expected here
378                 }
379
380                 [Test]
381                 [ExpectedException (typeof (DecoderException))]
382                 public void T3_Malformed_2_LonelyStart_321 ()
383                 {
384                         byte[] data = { 
385                                 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20, 
386                                 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20, 
387                                 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20, 
388                                 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
389                         string s = utf8.GetString (data);
390                         // exception is "really" expected here
391                 }
392
393                 [Test]
394                 [ExpectedException (typeof (DecoderException))]
395                 public void T3_Malformed_2_LonelyStart_322 () 
396                 {
397                         byte[] data = { 
398                                 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20, 
399                                 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
400                         string s = utf8.GetString (data);
401                         // exception is "really" expected here
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (DecoderException))]
406                 public void T3_Malformed_2_LonelyStart_323 () 
407                 {
408                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
409                         string s = utf8.GetString (data);
410                         // exception is "really" expected here
411                 }
412
413                 [Test]
414                 [ExpectedException (typeof (DecoderException))]
415                 public void T3_Malformed_2_LonelyStart_324 () 
416                 {
417                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
418                         string s = utf8.GetString (data);
419                         // exception is "really" expected here
420                 }
421
422                 [Test]
423                 [ExpectedException (typeof (DecoderException))]
424                 public void T3_Malformed_2_LonelyStart_325 () 
425                 {
426                         byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
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_331 () 
434                 {
435                         byte[] data = { 0xC0 };
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_332 () 
443                 {
444                         byte[] data = { 0xE0, 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_333 () 
452                 {
453                         byte[] data = { 0xF0, 0x80, 0x80 };
454                         string s = utf8.GetString (data);
455                         // exception is "really" expected here
456                 }
457
458                 [Test]
459                 [ExpectedException (typeof (DecoderException))]
460                 public void T3_Malformed_3_LastContinuationMissing_334 () 
461                 {
462                         byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
463                         string s = utf8.GetString (data);
464                         // exception is "really" expected here
465                 }
466
467                 [Test]
468                 [ExpectedException (typeof (DecoderException))]
469                 public void T3_Malformed_3_LastContinuationMissing_335 () 
470                 {
471                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
472                         string s = utf8.GetString (data);
473                         // exception is "really" expected here
474                 }
475
476                 [Test]
477 // MS Fx 1.1 accept this
478 //              [ExpectedException (typeof (DecoderException))]
479                 public void T3_Malformed_3_LastContinuationMissing_336 () 
480                 {
481                         byte[] data = { 0xDF };
482                         try {
483                                 string s = utf8.GetString (data);
484                                 // exception is "really" expected here
485                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
486                         }
487                         catch (DecoderException) {
488                                 // but Mono doesn't - better stick to the standard
489                         }
490                 }
491
492                 [Test]
493 // MS Fx 1.1 accept this
494 //              [ExpectedException (typeof (DecoderException))]
495                 public void T3_Malformed_3_LastContinuationMissing_337 () 
496                 {
497                         byte[] data = { 0xEF, 0xBF };
498                         try {
499                                 string s = utf8.GetString (data);
500                                 // exception is "really" expected here
501                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
502                         }
503                         catch (DecoderException) {
504                                 // but Mono doesn't - better stick to the standard
505                         }
506                 }
507
508                 [Test]
509                 [ExpectedException (typeof (DecoderException))]
510                 public void T3_Malformed_3_LastContinuationMissing_338 () 
511                 {
512                         byte[] data = { 0xF7, 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_3_LastContinuationMissing_339 () 
520                 {
521                         byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
522                         string s = utf8.GetString (data);
523                         // exception is "really" expected here
524                 }
525
526                 [Test]
527                 [ExpectedException (typeof (DecoderException))]
528                 public void T3_Malformed_3_LastContinuationMissing_3310 () 
529                 {
530                         byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
531                         string s = utf8.GetString (data);
532                         // exception is "really" expected here
533                 }
534
535                 [Test]
536                 [ExpectedException (typeof (DecoderException))]
537                 public void T3_Malformed_4_ConcatenationImcomplete () 
538                 {
539                         byte[] data = {
540                                 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF, 
541                                 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
542                         string s = utf8.GetString (data);
543                         // exception is "really" expected here
544                 }
545
546                 [Test]
547                 [ExpectedException (typeof (DecoderException))]
548                 public void T3_Malformed_5_ImpossibleBytes_351 () 
549                 {
550                         byte[] data = { 0xFE };
551                         string s = utf8.GetString (data);
552                         // exception is "really" expected here
553                 }
554
555                 [Test]
556                 [ExpectedException (typeof (DecoderException))]
557                 public void T3_Malformed_5_ImpossibleBytes_352 () 
558                 {
559                         byte[] data = { 0xFF };
560                         string s = utf8.GetString (data);
561                         // exception is "really" expected here
562                 }
563
564                 [Test]
565                 [ExpectedException (typeof (DecoderException))]
566                 public void T3_Malformed_5_ImpossibleBytes_353 () 
567                 {
568                         byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
569                         string s = utf8.GetString (data);
570                         // exception is "really" expected here
571                 }
572
573                 // Overlong == dangereous -> "safe" decoder should reject them
574
575                 [Test]
576                 [ExpectedException (typeof (DecoderException))]
577                 public void T4_Overlong_1_ASCII_Slash_411 () 
578                 {
579                         byte[] data = { 0xC0, 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_412 () 
587                 {
588                         byte[] data = { 0xE0, 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_413 () 
596                 {
597                         byte[] data = { 0xF0, 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_1_ASCII_Slash_414 () 
605                 {
606                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
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_1_ASCII_Slash_415 () 
614                 {
615                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
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_421 () 
623                 {
624                         byte[] data = { 0xC1, 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_422 () 
632                 {
633                         byte[] data = { 0xE0, 0x9F, 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_423 () 
641                 {
642                         byte[] data = { 0xF0, 0x8F, 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_2_MaximumBoundary_424 () 
650                 {
651                         byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
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_2_MaximumBoundary_425 () 
659                 {
660                         byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
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_431 () 
668                 {
669                         byte[] data = { 0xC0, 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_432 () 
677                 {
678                         byte[] data = { 0xE0, 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_433 () 
686                 {
687                         byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
688                         string s = utf8.GetString (data);
689                         // exception is "really" expected here
690                 }
691
692                 [Test]
693                 [ExpectedException (typeof (DecoderException))]
694                 public void T4_Overlong_3_NUL_434 () 
695                 {
696                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
697                         string s = utf8.GetString (data);
698                         // exception is "really" expected here
699                 }
700
701                 [Test]
702                 [ExpectedException (typeof (DecoderException))]
703                 public void T4_Overlong_3_NUL_435 () 
704                 {
705                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
706                         string s = utf8.GetString (data);
707                         // exception is "really" expected here
708                 }
709
710                 [Test]
711 #if NET_2_0
712                 [ExpectedException (typeof (DecoderFallbackException))]
713 #else
714 // MS Fx 1.1 accept this
715                 [Category ("NotDotNet")]
716                 [ExpectedException (typeof (DecoderException))]
717 #endif
718                         public void T5_IllegalCodePosition_1_UTF16Surrogates_511 () 
719                 {
720                         byte[] data = { 0xED, 0xA0, 0x80 };
721                         string s = utf8.GetString (data);
722                         // exception is "really" expected here
723                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
724                 }
725
726                 [Test]
727 #if NET_2_0
728                 [ExpectedException (typeof (DecoderFallbackException))]
729 #else
730 // MS Fx 1.1 accept this
731                 [Category ("NotDotNet")]
732                 [ExpectedException (typeof (DecoderException))]
733 #endif
734                 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 () 
735                 {
736                         byte[] data = { 0xED, 0xAD, 0xBF };
737                         string s = utf8.GetString (data);
738                         // exception is "really" expected here
739                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
740                 }
741
742                 [Test]
743 #if NET_2_0
744                 [ExpectedException (typeof (DecoderFallbackException))]
745 #else
746 // MS Fx 1.1 accept this
747                 [Category ("NotDotNet")]
748                 [ExpectedException (typeof (DecoderException))]
749 #endif
750                 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
751                 {
752                         byte[] data = { 0xED, 0xAE, 0x80 };
753                         string s = utf8.GetString (data);
754                         // exception is "really" expected here
755                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
756                 }
757
758                 [Test]
759 #if NET_2_0
760                 [ExpectedException (typeof (DecoderFallbackException))]
761 #else
762 // MS Fx 1.1 accept this
763                 [Category ("NotDotNet")]
764                 [ExpectedException (typeof (DecoderException))]
765 #endif
766                 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 () 
767                 {
768                         byte[] data = { 0xED, 0xAF, 0xBF };
769                         string s = utf8.GetString (data);
770                         // exception is "really" expected here
771                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
772                 }
773
774                 [Test]
775 #if NET_2_0
776                 [ExpectedException (typeof (DecoderFallbackException))]
777 #else
778 // MS Fx 1.1 accept this
779                 [Category ("NotDotNet")]
780                 [ExpectedException (typeof (DecoderException))]
781 #endif
782                 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
783                 {
784                         byte[] data = { 0xED, 0xB0, 0x80 };
785                         string s = utf8.GetString (data);
786                         // exception is "really" expected here
787                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
788                 }
789
790                 [Test]
791 #if NET_2_0
792                 [ExpectedException (typeof (DecoderFallbackException))]
793 #else
794 // MS Fx 1.1 accept this
795                 [Category ("NotDotNet")]
796                 [ExpectedException (typeof (DecoderException))]
797 #endif
798                 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 () 
799                 {
800                         byte[] data = { 0xED, 0xBE, 0x80 };
801                         string s = utf8.GetString (data);
802                         // exception is "really" expected here
803                         AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
804                 }
805
806                 [Test]
807 #if NET_2_0
808                 [ExpectedException (typeof (DecoderFallbackException))]
809 #else
810 // MS Fx 1.1 accept this
811                 [Category ("NotDotNet")]
812                 [ExpectedException (typeof (DecoderException))]
813 #endif
814                 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 () 
815                 {
816                         byte[] data = { 0xED, 0xBF, 0xBF };
817                         string s = utf8.GetString (data);
818                         // exception is "really" expected here
819                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
820                 }
821
822                 [Test]
823 #if NET_2_0
824                 [ExpectedException (typeof (DecoderFallbackException))]
825 #else
826 // MS Fx 1.1 accept this
827                 [Category ("NotDotNet")]
828                 [ExpectedException (typeof (DecoderException))]
829 #endif
830                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 () 
831                 {
832                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
833                         string s = utf8.GetString (data);
834                         // exception is "really" expected here
835                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
836                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
837                 }
838
839                 [Test]
840 #if NET_2_0
841                 [ExpectedException (typeof (DecoderFallbackException))]
842 #else
843 // MS Fx 1.1 accept this
844                 [Category ("NotDotNet")]
845                 [ExpectedException (typeof (DecoderException))]
846 #endif
847                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 () 
848                 {
849                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
850                         string s = utf8.GetString (data);
851                         // exception is "really" expected here
852                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
853                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
854                 }
855
856                 [Test]
857 #if NET_2_0
858                 [ExpectedException (typeof (DecoderFallbackException))]
859 #else
860 // MS Fx 1.1 accept this
861                 [Category ("NotDotNet")]
862                 [ExpectedException (typeof (DecoderException))]
863 #endif
864                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 () 
865                 {
866                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
867                         string s = utf8.GetString (data);
868                         // exception is "really" expected here
869                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
870                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
871                 }
872
873                 [Test]
874 #if NET_2_0
875                 [ExpectedException (typeof (DecoderFallbackException))]
876 #else
877 // MS Fx 1.1 accept this
878                 [Category ("NotDotNet")]
879                 [ExpectedException (typeof (DecoderException))]
880 #endif
881                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 () 
882                 {
883                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
884                         string s = utf8.GetString (data);
885                         // exception is "really" expected here
886                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
887                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
888                 }
889
890                 [Test]
891 #if NET_2_0
892                 [ExpectedException (typeof (DecoderFallbackException))]
893 #else
894 // MS Fx 1.1 accept this
895                 [Category ("NotDotNet")]
896                 [ExpectedException (typeof (DecoderException))]
897 #endif
898                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 () 
899                 {
900                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
901                         string s = utf8.GetString (data);
902                         // exception is "really" expected here
903                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
904                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
905                 }
906
907                 [Test]
908 #if NET_2_0
909                 [ExpectedException (typeof (DecoderFallbackException))]
910 #else
911 // MS Fx 1.1 accept this
912                 [Category ("NotDotNet")]
913                 [ExpectedException (typeof (DecoderException))]
914 #endif
915                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 () 
916                 {
917                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
918                         string s = utf8.GetString (data);
919                         // exception is "really" expected here
920                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
921                         AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
922                 }
923
924                 [Test]
925 #if NET_2_0
926                 [ExpectedException (typeof (DecoderFallbackException))]
927 #else
928 // MS Fx 1.1 accept this
929                 [Category ("NotDotNet")]
930                 [ExpectedException (typeof (DecoderException))]
931 #endif
932                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 () 
933                 {
934                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
935                         string s = utf8.GetString (data);
936                         // exception is "really" expected here
937                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
938                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
939                 }
940
941                 [Test]
942 #if NET_2_0
943                 [ExpectedException (typeof (DecoderFallbackException))]
944 #else
945 // MS Fx 1.1 accept this
946                 [Category ("NotDotNet")]
947                 [ExpectedException (typeof (DecoderException))]
948 #endif
949                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 () 
950                 {
951                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
952                         string s = utf8.GetString (data);
953                         // exception is "really" expected here
954                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
955                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
956                 }
957
958                 [Test]
959 // MS Fx 1.1 accept this
960 //              [ExpectedException (typeof (DecoderException))]
961                 public void T5_IllegalCodePosition_3_Other_531 () 
962                 {
963                         byte[] data = { 0xEF, 0xBF, 0xBE };
964                         string s = utf8.GetString (data);
965                         // exception is "really" expected here
966                         AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
967                 }
968
969                 [Test]
970 // MS Fx 1.1 accept this
971 //              [ExpectedException (typeof (DecoderException))]
972                 public void T5_IllegalCodePosition_3_Other_532 () 
973                 {
974                         byte[] data = { 0xEF, 0xBF, 0xBF };
975                         string s = utf8.GetString (data);
976                         // exception is "really" expected here
977                         AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
978                 }
979
980                 [Test]
981                 // bug #75065 and #73086.
982                 public void GetCharsFEFF ()
983                 {
984                         byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
985                         Encoding enc = new UTF8Encoding (false, true);
986                         string s = enc.GetString (data);
987                         AssertEquals ("\uFEFF", s);
988
989                         Encoding utf = Encoding.UTF8;
990                         char[] testChars = {'\uFEFF','A'};
991
992                         byte[] bytes = utf.GetBytes(testChars);
993                         char[] chars = utf.GetChars(bytes);
994                         AssertEquals ("#1", '\uFEFF', chars [0]);
995                         AssertEquals ("#2", 'A', chars [1]);
996                 }
997
998 #if NET_2_0
999                 [Test]
1000                 public void CloneNotReadOnly ()
1001                 {
1002                         Encoding e = Encoding.GetEncoding (65001).Clone ()
1003                                 as Encoding;
1004                         AssertEquals (false, e.IsReadOnly);
1005                         e.EncoderFallback = new EncoderExceptionFallback ();
1006                 }
1007 #endif
1008
1009                 [Test]
1010 #if NET_2_0
1011                 [ExpectedException (typeof (DecoderFallbackException))]
1012 #else
1013                 [ExpectedException (typeof (ArgumentException))]
1014                 [Category ("NotDotNet")] // MS Bug
1015 #endif
1016                 public void Bug77315 ()
1017                 {
1018                         new UTF8Encoding (false, true).GetString (
1019                                 new byte [] {0xED, 0xA2, 0x8C});
1020                 }
1021
1022                 [Test]
1023                 public void SufficientByteArray ()
1024                 {
1025                         Encoder e = Encoding.UTF8.GetEncoder ();
1026                         byte [] bytes = new byte [0];
1027
1028                         char [] chars = new char [] {'\uD800'};
1029                         e.GetBytes (chars, 0, 1, bytes, 0, false);
1030                         try {
1031                                 int ret = e.GetBytes (chars, 1, 0, bytes, 0, true);
1032 #if NET_2_0
1033                                 AssertEquals ("drop insufficient char in 2.0: char[]", 0, ret);
1034 #else
1035                                 Fail ("ArgumentException is expected: char[]");
1036 #endif
1037                         } catch (ArgumentException) {
1038                         }
1039
1040                         string s = "\uD800";
1041                         try {
1042                                 int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0);
1043 #if NET_2_0
1044                                 AssertEquals ("drop insufficient char in 2.0: string", 0, ret);
1045 #else
1046                                 Fail ("ArgumentException is expected: string");
1047 #endif
1048                         } catch (ArgumentException) {
1049                         }
1050                 }
1051
1052 #if NET_2_0
1053                 [Test] // bug #77550
1054                 public void DecoderFallbackSimple ()
1055                 {
1056                         UTF8Encoding e = new UTF8Encoding (false, false);
1057                         AssertType.AreEqual (0, e.GetDecoder ().GetCharCount (
1058                                         new byte [] {(byte) 183}, 0, 1),
1059                                         "#1");
1060                         AssertType.AreEqual (0, e.GetDecoder().GetChars (
1061                                         new byte [] {(byte) 183}, 0, 1,
1062                                         new char [100], 0),
1063                                         "#2");
1064                         AssertType.AreEqual (0, e.GetString (new byte [] {(byte) 183}).Length,
1065                                         "#3");
1066                 }
1067 #endif
1068         }
1069 }