UTF8EncodingTest.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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.Text;
  14. namespace MonoTests.System.Text {
  15. [TestFixture]
  16. public class UTF8EncodingTest : Assertion {
  17. private UTF8Encoding utf8;
  18. [SetUp]
  19. public void Create ()
  20. {
  21. utf8 = new UTF8Encoding (true, true);
  22. }
  23. [Test]
  24. public void TestEncodingGetBytes1()
  25. {
  26. UTF8Encoding utf8Enc = new UTF8Encoding ();
  27. string UniCode = "\u0041\u2262\u0391\u002E";
  28. // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E
  29. // see (RFC 2044)
  30. byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
  31. Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
  32. Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
  33. Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
  34. Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
  35. Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
  36. Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
  37. Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
  38. }
  39. [Test]
  40. public void TestEncodingGetBytes2()
  41. {
  42. UTF8Encoding utf8Enc = new UTF8Encoding ();
  43. string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
  44. // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21
  45. // see (RFC 2044)
  46. byte[] utf8Bytes = new byte [11];
  47. int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
  48. Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
  49. Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
  50. Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
  51. Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
  52. Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
  53. Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
  54. Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
  55. Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
  56. Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
  57. Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
  58. Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
  59. Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
  60. }
  61. [Test]
  62. public void TestDecodingGetChars1()
  63. {
  64. UTF8Encoding utf8Enc = new UTF8Encoding ();
  65. // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>."
  66. // see (RFC 2044)
  67. byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
  68. char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
  69. Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
  70. Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
  71. Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
  72. Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
  73. }
  74. [Test]
  75. public void TestMaxCharCount()
  76. {
  77. UTF8Encoding UTF8enc = new UTF8Encoding ();
  78. Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
  79. }
  80. [Test]
  81. public void TestMaxByteCount()
  82. {
  83. UTF8Encoding UTF8enc = new UTF8Encoding ();
  84. Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
  85. }
  86. // regression for bug #59648
  87. [Test]
  88. public void TestThrowOnInvalid ()
  89. {
  90. UTF8Encoding u = new UTF8Encoding (true, false);
  91. byte[] data = new byte [] { 0xC0, 0xAF };
  92. string s = u.GetString (data);
  93. AssertEquals (0, s.Length);
  94. data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
  95. s = u.GetString (data);
  96. AssertEquals (4, s.Length);
  97. AssertEquals (0x30, (int) s [0]);
  98. AssertEquals (0x31, (int) s [1]);
  99. AssertEquals (0x30, (int) s [2]);
  100. AssertEquals (0x32, (int) s [3]);
  101. }
  102. // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
  103. [Test]
  104. public void T1_Correct_GreekWord_kosme ()
  105. {
  106. byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
  107. string s = utf8.GetString (data);
  108. // cute but saving source code in unicode can be problematic
  109. // so we just ensure we can re-encode this
  110. AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
  111. }
  112. [Test]
  113. public void T2_Boundary_1_FirstPossibleSequence_Pass ()
  114. {
  115. byte[] data211 = { 0x00 };
  116. string s = utf8.GetString (data211);
  117. AssertEquals ("1 byte (U-00000000)", "\0", s);
  118. AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
  119. byte[] data212 = { 0xC2, 0x80 };
  120. s = utf8.GetString (data212);
  121. AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
  122. AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
  123. byte[] data213 = { 0xE0, 0xA0, 0x80 };
  124. s = utf8.GetString (data213);
  125. AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
  126. AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
  127. byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
  128. s = utf8.GetString (data214);
  129. AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
  130. AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
  131. AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
  132. }
  133. [Test]
  134. // Fail on MS Fx 1.1
  135. [ExpectedException (typeof (ArgumentException))]
  136. public void T2_Boundary_1_FirstPossibleSequence_Fail_5 ()
  137. {
  138. byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
  139. string s = utf8.GetString (data215);
  140. AssertNull ("5 bytes (U-00200000)", s);
  141. AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
  142. }
  143. [Test]
  144. // Fail on MS Fx 1.1
  145. [ExpectedException (typeof (ArgumentException))]
  146. public void T2_Boundary_1_FirstPossibleSequence_Fail_6 ()
  147. {
  148. byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
  149. string s = utf8.GetString (data216);
  150. AssertNull ("6 bytes (U-04000000)", s);
  151. AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
  152. }
  153. [Test]
  154. public void T2_Boundary_2_LastPossibleSequence_Pass ()
  155. {
  156. byte[] data221 = { 0x7F };
  157. string s = utf8.GetString (data221);
  158. AssertEquals ("1 byte (U-0000007F)", 127, s [0]);
  159. AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
  160. byte[] data222 = { 0xDF, 0xBF };
  161. s = utf8.GetString (data222);
  162. AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
  163. AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
  164. byte[] data223 = { 0xEF, 0xBF, 0xBF };
  165. s = utf8.GetString (data223);
  166. AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
  167. AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
  168. }
  169. [Test]
  170. // Fail on MS Fx 1.1
  171. [ExpectedException (typeof (ArgumentException))]
  172. public void T2_Boundary_2_LastPossibleSequence_Fail_4 ()
  173. {
  174. byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
  175. string s = utf8.GetString (data224);
  176. AssertNull ("4 bytes (U-001FFFFF)", s);
  177. AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
  178. }
  179. [Test]
  180. // Fail on MS Fx 1.1
  181. [ExpectedException (typeof (ArgumentException))]
  182. public void T2_Boundary_2_LastPossibleSequence_Fail_5 ()
  183. {
  184. byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
  185. string s = utf8.GetString (data225);
  186. AssertNull ("5 bytes (U-03FFFFFF)", s);
  187. AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
  188. }
  189. [Test]
  190. // Fail on MS Fx 1.1
  191. [ExpectedException (typeof (ArgumentException))]
  192. public void T2_Boundary_2_LastPossibleSequence_Fail_6 ()
  193. {
  194. byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
  195. string s = utf8.GetString (data226);
  196. AssertNull ("6 bytes (U-7FFFFFFF)", s);
  197. AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
  198. }
  199. [Test]
  200. public void T2_Boundary_3_Other_Pass ()
  201. {
  202. byte[] data231 = { 0xED, 0x9F, 0xBF };
  203. string s = utf8.GetString (data231);
  204. AssertEquals ("U-0000D7FF", 55295, s [0]);
  205. AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
  206. byte[] data232 = { 0xEE, 0x80, 0x80 };
  207. s = utf8.GetString (data232);
  208. AssertEquals ("U-0000E000", 57344, s [0]);
  209. AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
  210. byte[] data233 = { 0xEF, 0xBF, 0xBD };
  211. s = utf8.GetString (data233);
  212. AssertEquals ("U-0000FFFD", 65533, s [0]);
  213. AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
  214. byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
  215. s = utf8.GetString (data234);
  216. AssertEquals ("U-0010FFFF-0", 56319, s [0]);
  217. AssertEquals ("U-0010FFFF-1", 57343, s [1]);
  218. AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
  219. }
  220. [Test]
  221. // Fail on MS Fx 1.1
  222. [ExpectedException (typeof (ArgumentException))]
  223. public void T2_Boundary_3_Other_Fail_5 ()
  224. {
  225. byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
  226. string s = utf8.GetString (data235);
  227. AssertNull ("U-00110000", s);
  228. AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
  229. }
  230. [Test]
  231. [ExpectedException (typeof (ArgumentException))]
  232. public void T3_Malformed_1_UnexpectedContinuation_311 ()
  233. {
  234. byte[] data = { 0x80 };
  235. string s = utf8.GetString (data);
  236. // exception is "really" expected here
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ArgumentException))]
  240. public void T3_Malformed_1_UnexpectedContinuation_312 ()
  241. {
  242. byte[] data = { 0xBF };
  243. string s = utf8.GetString (data);
  244. // exception is "really" expected here
  245. }
  246. [Test]
  247. [ExpectedException (typeof (ArgumentException))]
  248. public void T3_Malformed_1_UnexpectedContinuation_313 ()
  249. {
  250. byte[] data = { 0x80, 0xBF };
  251. string s = utf8.GetString (data);
  252. // exception is "really" expected here
  253. }
  254. [Test]
  255. [ExpectedException (typeof (ArgumentException))]
  256. public void T3_Malformed_1_UnexpectedContinuation_314 ()
  257. {
  258. byte[] data = { 0x80, 0xBF, 0x80 };
  259. string s = utf8.GetString (data);
  260. // exception is "really" expected here
  261. }
  262. [Test]
  263. [ExpectedException (typeof (ArgumentException))]
  264. public void T3_Malformed_1_UnexpectedContinuation_315 ()
  265. {
  266. byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
  267. string s = utf8.GetString (data);
  268. // exception is "really" expected here
  269. }
  270. [Test]
  271. [ExpectedException (typeof (ArgumentException))]
  272. public void T3_Malformed_1_UnexpectedContinuation_316 ()
  273. {
  274. byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
  275. string s = utf8.GetString (data);
  276. // exception is "really" expected here
  277. }
  278. [Test]
  279. [ExpectedException (typeof (ArgumentException))]
  280. public void T3_Malformed_1_UnexpectedContinuation_317 ()
  281. {
  282. byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
  283. string s = utf8.GetString (data);
  284. // exception is "really" expected here
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ArgumentException))]
  288. public void T3_Malformed_1_UnexpectedContinuation_318 ()
  289. {
  290. byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
  291. string s = utf8.GetString (data);
  292. // exception is "really" expected here
  293. }
  294. [Test]
  295. [ExpectedException (typeof (ArgumentException))]
  296. public void T3_Malformed_1_UnexpectedContinuation_319 ()
  297. {
  298. // 64 different continuation characters
  299. byte[] data = {
  300. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
  301. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
  302. 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
  303. 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
  304. string s = utf8.GetString (data);
  305. // exception is "really" expected here
  306. }
  307. [Test]
  308. [ExpectedException (typeof (ArgumentException))]
  309. public void T3_Malformed_2_LonelyStart_321 ()
  310. {
  311. byte[] data = {
  312. 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20,
  313. 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20,
  314. 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20,
  315. 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
  316. string s = utf8.GetString (data);
  317. // exception is "really" expected here
  318. }
  319. [Test]
  320. [ExpectedException (typeof (ArgumentException))]
  321. public void T3_Malformed_2_LonelyStart_322 ()
  322. {
  323. byte[] data = {
  324. 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20,
  325. 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
  326. string s = utf8.GetString (data);
  327. // exception is "really" expected here
  328. }
  329. [Test]
  330. [ExpectedException (typeof (ArgumentException))]
  331. public void T3_Malformed_2_LonelyStart_323 ()
  332. {
  333. byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
  334. string s = utf8.GetString (data);
  335. // exception is "really" expected here
  336. }
  337. [Test]
  338. [ExpectedException (typeof (ArgumentException))]
  339. public void T3_Malformed_2_LonelyStart_324 ()
  340. {
  341. byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
  342. string s = utf8.GetString (data);
  343. // exception is "really" expected here
  344. }
  345. [Test]
  346. [ExpectedException (typeof (ArgumentException))]
  347. public void T3_Malformed_2_LonelyStart_325 ()
  348. {
  349. byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
  350. string s = utf8.GetString (data);
  351. // exception is "really" expected here
  352. }
  353. [Test]
  354. [ExpectedException (typeof (ArgumentException))]
  355. public void T3_Malformed_3_LastContinuationMissing_331 ()
  356. {
  357. byte[] data = { 0xC0 };
  358. string s = utf8.GetString (data);
  359. // exception is "really" expected here
  360. }
  361. [Test]
  362. [ExpectedException (typeof (ArgumentException))]
  363. public void T3_Malformed_3_LastContinuationMissing_332 ()
  364. {
  365. byte[] data = { 0xE0, 0x80 };
  366. string s = utf8.GetString (data);
  367. // exception is "really" expected here
  368. }
  369. [Test]
  370. [ExpectedException (typeof (ArgumentException))]
  371. public void T3_Malformed_3_LastContinuationMissing_333 ()
  372. {
  373. byte[] data = { 0xF0, 0x80, 0x80 };
  374. string s = utf8.GetString (data);
  375. // exception is "really" expected here
  376. }
  377. [Test]
  378. [ExpectedException (typeof (ArgumentException))]
  379. public void T3_Malformed_3_LastContinuationMissing_334 ()
  380. {
  381. byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
  382. string s = utf8.GetString (data);
  383. // exception is "really" expected here
  384. }
  385. [Test]
  386. [ExpectedException (typeof (ArgumentException))]
  387. public void T3_Malformed_3_LastContinuationMissing_335 ()
  388. {
  389. byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
  390. string s = utf8.GetString (data);
  391. // exception is "really" expected here
  392. }
  393. [Test]
  394. // MS Fx 1.1 accept this
  395. // [ExpectedException (typeof (ArgumentException))]
  396. public void T3_Malformed_3_LastContinuationMissing_336 ()
  397. {
  398. byte[] data = { 0xDF };
  399. try {
  400. string s = utf8.GetString (data);
  401. // exception is "really" expected here
  402. AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
  403. }
  404. catch (ArgumentException) {
  405. // but Mono doesn't - better stick to the standard
  406. }
  407. }
  408. [Test]
  409. // MS Fx 1.1 accept this
  410. // [ExpectedException (typeof (ArgumentException))]
  411. public void T3_Malformed_3_LastContinuationMissing_337 ()
  412. {
  413. byte[] data = { 0xEF, 0xBF };
  414. try {
  415. string s = utf8.GetString (data);
  416. // exception is "really" expected here
  417. AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
  418. }
  419. catch (ArgumentException) {
  420. // but Mono doesn't - better stick to the standard
  421. }
  422. }
  423. [Test]
  424. [ExpectedException (typeof (ArgumentException))]
  425. public void T3_Malformed_3_LastContinuationMissing_338 ()
  426. {
  427. byte[] data = { 0xF7, 0xBF, 0xBF };
  428. string s = utf8.GetString (data);
  429. // exception is "really" expected here
  430. }
  431. [Test]
  432. [ExpectedException (typeof (ArgumentException))]
  433. public void T3_Malformed_3_LastContinuationMissing_339 ()
  434. {
  435. byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
  436. string s = utf8.GetString (data);
  437. // exception is "really" expected here
  438. }
  439. [Test]
  440. [ExpectedException (typeof (ArgumentException))]
  441. public void T3_Malformed_3_LastContinuationMissing_3310 ()
  442. {
  443. byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
  444. string s = utf8.GetString (data);
  445. // exception is "really" expected here
  446. }
  447. [Test]
  448. [ExpectedException (typeof (ArgumentException))]
  449. public void T3_Malformed_4_ConcatenationImcomplete ()
  450. {
  451. byte[] data = {
  452. 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF,
  453. 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
  454. string s = utf8.GetString (data);
  455. // exception is "really" expected here
  456. }
  457. [Test]
  458. [ExpectedException (typeof (ArgumentException))]
  459. public void T3_Malformed_5_ImpossibleBytes_351 ()
  460. {
  461. byte[] data = { 0xFE };
  462. string s = utf8.GetString (data);
  463. // exception is "really" expected here
  464. }
  465. [Test]
  466. [ExpectedException (typeof (ArgumentException))]
  467. public void T3_Malformed_5_ImpossibleBytes_352 ()
  468. {
  469. byte[] data = { 0xFF };
  470. string s = utf8.GetString (data);
  471. // exception is "really" expected here
  472. }
  473. [Test]
  474. [ExpectedException (typeof (ArgumentException))]
  475. public void T3_Malformed_5_ImpossibleBytes_353 ()
  476. {
  477. byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
  478. string s = utf8.GetString (data);
  479. // exception is "really" expected here
  480. }
  481. // Overlong == dangereous -> "safe" decoder should reject them
  482. [Test]
  483. [ExpectedException (typeof (ArgumentException))]
  484. public void T4_Overlong_1_ASCII_Slash_411 ()
  485. {
  486. byte[] data = { 0xC0, 0xAF };
  487. string s = utf8.GetString (data);
  488. // exception is "really" expected here
  489. }
  490. [Test]
  491. [ExpectedException (typeof (ArgumentException))]
  492. public void T4_Overlong_1_ASCII_Slash_412 ()
  493. {
  494. byte[] data = { 0xE0, 0x80, 0xAF };
  495. string s = utf8.GetString (data);
  496. // exception is "really" expected here
  497. }
  498. [Test]
  499. [ExpectedException (typeof (ArgumentException))]
  500. public void T4_Overlong_1_ASCII_Slash_413 ()
  501. {
  502. byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
  503. string s = utf8.GetString (data);
  504. // exception is "really" expected here
  505. }
  506. [Test]
  507. [ExpectedException (typeof (ArgumentException))]
  508. public void T4_Overlong_1_ASCII_Slash_414 ()
  509. {
  510. byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
  511. string s = utf8.GetString (data);
  512. // exception is "really" expected here
  513. }
  514. [Test]
  515. [ExpectedException (typeof (ArgumentException))]
  516. public void T4_Overlong_1_ASCII_Slash_415 ()
  517. {
  518. byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
  519. string s = utf8.GetString (data);
  520. // exception is "really" expected here
  521. }
  522. [Test]
  523. [ExpectedException (typeof (ArgumentException))]
  524. public void T4_Overlong_2_MaximumBoundary_421 ()
  525. {
  526. byte[] data = { 0xC1, 0xBF };
  527. string s = utf8.GetString (data);
  528. // exception is "really" expected here
  529. }
  530. [Test]
  531. [ExpectedException (typeof (ArgumentException))]
  532. public void T4_Overlong_2_MaximumBoundary_422 ()
  533. {
  534. byte[] data = { 0xE0, 0x9F, 0xBF };
  535. string s = utf8.GetString (data);
  536. // exception is "really" expected here
  537. }
  538. [Test]
  539. [ExpectedException (typeof (ArgumentException))]
  540. public void T4_Overlong_2_MaximumBoundary_423 ()
  541. {
  542. byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
  543. string s = utf8.GetString (data);
  544. // exception is "really" expected here
  545. }
  546. [Test]
  547. [ExpectedException (typeof (ArgumentException))]
  548. public void T4_Overlong_2_MaximumBoundary_424 ()
  549. {
  550. byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
  551. string s = utf8.GetString (data);
  552. // exception is "really" expected here
  553. }
  554. [Test]
  555. [ExpectedException (typeof (ArgumentException))]
  556. public void T4_Overlong_2_MaximumBoundary_425 ()
  557. {
  558. byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
  559. string s = utf8.GetString (data);
  560. // exception is "really" expected here
  561. }
  562. [Test]
  563. [ExpectedException (typeof (ArgumentException))]
  564. public void T4_Overlong_3_NUL_431 ()
  565. {
  566. byte[] data = { 0xC0, 0x80 };
  567. string s = utf8.GetString (data);
  568. // exception is "really" expected here
  569. }
  570. [Test]
  571. [ExpectedException (typeof (ArgumentException))]
  572. public void T4_Overlong_3_NUL_432 ()
  573. {
  574. byte[] data = { 0xE0, 0x80, 0x80 };
  575. string s = utf8.GetString (data);
  576. // exception is "really" expected here
  577. }
  578. [Test]
  579. [ExpectedException (typeof (ArgumentException))]
  580. public void T4_Overlong_3_NUL_433 ()
  581. {
  582. byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
  583. string s = utf8.GetString (data);
  584. // exception is "really" expected here
  585. }
  586. [Test]
  587. [ExpectedException (typeof (ArgumentException))]
  588. public void T4_Overlong_3_NUL_434 ()
  589. {
  590. byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
  591. string s = utf8.GetString (data);
  592. // exception is "really" expected here
  593. }
  594. [Test]
  595. [ExpectedException (typeof (ArgumentException))]
  596. public void T4_Overlong_3_NUL_435 ()
  597. {
  598. byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
  599. string s = utf8.GetString (data);
  600. // exception is "really" expected here
  601. }
  602. [Test]
  603. // MS Fx 1.1 accept this
  604. // [ExpectedException (typeof (ArgumentException))]
  605. public void T5_IllegalCodePosition_1_UTF16Surrogates_511 ()
  606. {
  607. byte[] data = { 0xED, 0xA0, 0x80 };
  608. string s = utf8.GetString (data);
  609. // exception is "really" expected here
  610. AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
  611. }
  612. [Test]
  613. // MS Fx 1.1 accept this
  614. // [ExpectedException (typeof (ArgumentException))]
  615. public void T5_IllegalCodePosition_1_UTF16Surrogates_512 ()
  616. {
  617. byte[] data = { 0xED, 0xAD, 0xBF };
  618. string s = utf8.GetString (data);
  619. // exception is "really" expected here
  620. AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
  621. }
  622. [Test]
  623. // MS Fx 1.1 accept this
  624. // [ExpectedException (typeof (ArgumentException))]
  625. public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
  626. {
  627. byte[] data = { 0xED, 0xAE, 0x80 };
  628. string s = utf8.GetString (data);
  629. // exception is "really" expected here
  630. AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
  631. }
  632. [Test]
  633. // MS Fx 1.1 accept this
  634. // [ExpectedException (typeof (ArgumentException))]
  635. public void T5_IllegalCodePosition_1_UTF16Surrogates_514 ()
  636. {
  637. byte[] data = { 0xED, 0xAF, 0xBF };
  638. string s = utf8.GetString (data);
  639. // exception is "really" expected here
  640. AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
  641. }
  642. [Test]
  643. // MS Fx 1.1 accept this
  644. // [ExpectedException (typeof (ArgumentException))]
  645. public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
  646. {
  647. byte[] data = { 0xED, 0xB0, 0x80 };
  648. string s = utf8.GetString (data);
  649. // exception is "really" expected here
  650. AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
  651. }
  652. [Test]
  653. // MS Fx 1.1 accept this
  654. // [ExpectedException (typeof (ArgumentException))]
  655. public void T5_IllegalCodePosition_1_UTF16Surrogates_516 ()
  656. {
  657. byte[] data = { 0xED, 0xBE, 0x80 };
  658. string s = utf8.GetString (data);
  659. // exception is "really" expected here
  660. AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
  661. }
  662. [Test]
  663. // MS Fx 1.1 accept this
  664. // [ExpectedException (typeof (ArgumentException))]
  665. public void T5_IllegalCodePosition_1_UTF16Surrogates_517 ()
  666. {
  667. byte[] data = { 0xED, 0xBF, 0xBF };
  668. string s = utf8.GetString (data);
  669. // exception is "really" expected here
  670. AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
  671. }
  672. [Test]
  673. // MS Fx 1.1 accept this
  674. // [ExpectedException (typeof (ArgumentException))]
  675. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 ()
  676. {
  677. byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
  678. string s = utf8.GetString (data);
  679. // exception is "really" expected here
  680. AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
  681. AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
  682. }
  683. [Test]
  684. // MS Fx 1.1 accept this
  685. // [ExpectedException (typeof (ArgumentException))]
  686. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 ()
  687. {
  688. byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
  689. string s = utf8.GetString (data);
  690. // exception is "really" expected here
  691. AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
  692. AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
  693. }
  694. [Test]
  695. // MS Fx 1.1 accept this
  696. // [ExpectedException (typeof (ArgumentException))]
  697. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 ()
  698. {
  699. byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
  700. string s = utf8.GetString (data);
  701. // exception is "really" expected here
  702. AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
  703. AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
  704. }
  705. [Test]
  706. // MS Fx 1.1 accept this
  707. // [ExpectedException (typeof (ArgumentException))]
  708. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 ()
  709. {
  710. byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
  711. string s = utf8.GetString (data);
  712. // exception is "really" expected here
  713. AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
  714. AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
  715. }
  716. [Test]
  717. // MS Fx 1.1 accept this
  718. // [ExpectedException (typeof (ArgumentException))]
  719. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 ()
  720. {
  721. byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
  722. string s = utf8.GetString (data);
  723. // exception is "really" expected here
  724. AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
  725. AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
  726. }
  727. [Test]
  728. // MS Fx 1.1 accept this
  729. // [ExpectedException (typeof (ArgumentException))]
  730. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 ()
  731. {
  732. byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
  733. string s = utf8.GetString (data);
  734. // exception is "really" expected here
  735. AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
  736. AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
  737. }
  738. [Test]
  739. // MS Fx 1.1 accept this
  740. // [ExpectedException (typeof (ArgumentException))]
  741. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 ()
  742. {
  743. byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
  744. string s = utf8.GetString (data);
  745. // exception is "really" expected here
  746. AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
  747. AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
  748. }
  749. [Test]
  750. // MS Fx 1.1 accept this
  751. // [ExpectedException (typeof (ArgumentException))]
  752. public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 ()
  753. {
  754. byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
  755. string s = utf8.GetString (data);
  756. // exception is "really" expected here
  757. AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
  758. AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
  759. }
  760. [Test]
  761. // MS Fx 1.1 accept this
  762. // [ExpectedException (typeof (ArgumentException))]
  763. public void T5_IllegalCodePosition_3_Other_531 ()
  764. {
  765. byte[] data = { 0xEF, 0xBF, 0xBE };
  766. string s = utf8.GetString (data);
  767. // exception is "really" expected here
  768. AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
  769. }
  770. [Test]
  771. // MS Fx 1.1 accept this
  772. // [ExpectedException (typeof (ArgumentException))]
  773. public void T5_IllegalCodePosition_3_Other_532 ()
  774. {
  775. byte[] data = { 0xEF, 0xBF, 0xBF };
  776. string s = utf8.GetString (data);
  777. // exception is "really" expected here
  778. AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
  779. }
  780. }
  781. }