UTF8EncodingTest.cs 35 KB

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