UTF7EncodingTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding
  3. //
  4. // Authors
  5. // Patrick Kalkman [email protected]
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Patrick Kalkman
  9. // Copyright (C) 2004 Novell (http://www.novell.com)
  10. //
  11. using NUnit.Framework;
  12. using System;
  13. using System.Text;
  14. using AssertType = NUnit.Framework.Assert;
  15. namespace MonoTests.System.Text
  16. {
  17. [TestFixture]
  18. public class UTF7EncodingTest
  19. {
  20. [Test]
  21. public void IsBrowserDisplay ()
  22. {
  23. UTF7Encoding utf7 = new UTF7Encoding ();
  24. Assert.IsTrue (!utf7.IsBrowserDisplay);
  25. }
  26. [Test]
  27. public void IsBrowserSave ()
  28. {
  29. UTF7Encoding utf7 = new UTF7Encoding ();
  30. Assert.IsTrue (!utf7.IsBrowserSave);
  31. }
  32. [Test]
  33. public void IsMailNewsDisplay ()
  34. {
  35. UTF7Encoding utf7 = new UTF7Encoding ();
  36. Assert.IsTrue (utf7.IsMailNewsDisplay);
  37. }
  38. [Test]
  39. public void IsMailNewsSave ()
  40. {
  41. UTF7Encoding utf7 = new UTF7Encoding ();
  42. Assert.IsTrue (utf7.IsMailNewsSave);
  43. }
  44. [Test]
  45. public void TestDirectlyEncoded1()
  46. {
  47. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  48. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  49. byte[] UTF7Bytes = null;
  50. UTF7Encoding UTF7enc = new UTF7Encoding ();
  51. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  52. Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
  53. Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
  54. Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
  55. Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
  56. Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
  57. Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
  58. Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
  59. Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
  60. }
  61. [Test]
  62. public void TestDirectlyEncoded2()
  63. {
  64. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  65. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  66. byte[] UTF7Bytes = new byte [8];
  67. int Length = UniCodeString.Length;
  68. UTF7Encoding UTF7enc = new UTF7Encoding ();
  69. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  70. Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
  71. Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
  72. Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
  73. Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
  74. Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
  75. Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
  76. Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
  77. Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
  78. }
  79. [Test]
  80. public void TestEncodeOptionalEncoded()
  81. {
  82. string UniCodeString = "\u0021\u0026\u002A\u003B";
  83. byte[] UTF7Bytes = null;
  84. //Optional Characters are allowed.
  85. UTF7Encoding UTF7enc = new UTF7Encoding (true);
  86. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  87. Assert.AreEqual (0x21, UTF7Bytes [0], "UTF7 #1");
  88. Assert.AreEqual (0x26, UTF7Bytes [1], "UTF7 #2");
  89. Assert.AreEqual (0x2A, UTF7Bytes [2], "UTF7 #3");
  90. Assert.AreEqual (0x3B, UTF7Bytes [3], "UTF7 #4");
  91. //Optional characters are not allowed.
  92. UTF7enc = new UTF7Encoding (false);
  93. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  94. Assert.AreEqual (0x2B, UTF7Bytes [0], "UTF7 #5");
  95. Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
  96. Assert.AreEqual (0x43, UTF7Bytes [2], "UTF7 #7");
  97. Assert.AreEqual (0x45, UTF7Bytes [3], "UTF7 #8");
  98. Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
  99. }
  100. [Test]
  101. public void TestEncodeUnicodeShifted1()
  102. {
  103. string UniCodeString = "\u0041\u2262\u0391\u002E";
  104. byte[] UTF7Bytes = null;
  105. UTF7Encoding UTF7enc = new UTF7Encoding();
  106. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  107. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  108. Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
  109. Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
  110. Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
  111. Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
  112. Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
  113. Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
  114. Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
  115. Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
  116. Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
  117. Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
  118. }
  119. [Test]
  120. public void TestEncodeUnicodeShifted2()
  121. {
  122. string UniCodeString = "\u0041\u2262\u0391\u002E";
  123. byte[] UTF7Bytes = new byte [10];
  124. int Length = UniCodeString.Length;
  125. UTF7Encoding UTF7enc = new UTF7Encoding ();
  126. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  127. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  128. Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
  129. Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
  130. Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
  131. Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
  132. Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
  133. Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
  134. Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
  135. Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
  136. Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
  137. Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
  138. }
  139. [Test]
  140. public void RFC1642_Example1 ()
  141. {
  142. string UniCodeString = "\u0041\u2262\u0391\u002E";
  143. char[] expected = UniCodeString.ToCharArray ();
  144. byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E};
  145. UTF7Encoding UTF7enc = new UTF7Encoding ();
  146. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  147. // "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
  148. Assert.AreEqual (expected [0], actual [0], "UTF #1");
  149. Assert.AreEqual (expected [1], actual [1], "UTF #2");
  150. Assert.AreEqual (expected [2], actual [2], "UTF #3");
  151. Assert.AreEqual (expected [3], actual [3], "UTF #4");
  152. Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
  153. }
  154. [Test]
  155. public void RFC1642_Example2 ()
  156. {
  157. string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021";
  158. char[] expected = UniCodeString.ToCharArray ();
  159. byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D };
  160. UTF7Encoding UTF7enc = new UTF7Encoding ();
  161. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  162. // "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!"
  163. Assert.AreEqual (expected [0], actual [0], "UTF #1");
  164. Assert.AreEqual (expected [1], actual [1], "UTF #2");
  165. Assert.AreEqual (expected [2], actual [2], "UTF #3");
  166. Assert.AreEqual (expected [3], actual [3], "UTF #4");
  167. Assert.AreEqual (expected [4], actual [4], "UTF #5");
  168. Assert.AreEqual (expected [5], actual [5], "UTF #6");
  169. Assert.AreEqual (expected [6], actual [6], "UTF #7");
  170. Assert.AreEqual (expected [7], actual [7], "UTF #8");
  171. Assert.AreEqual (expected [8], actual [8], "UTF #9");
  172. Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
  173. }
  174. [Test]
  175. public void RFC1642_Example3 ()
  176. {
  177. string UniCodeString = "\u65E5\u672C\u8A9E";
  178. char[] expected = UniCodeString.ToCharArray ();
  179. byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D };
  180. UTF7Encoding UTF7enc = new UTF7Encoding ();
  181. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  182. // "+ZeVnLIqe-" is decoded as Japanese "nihongo"
  183. Assert.AreEqual (expected [0], actual [0], "UTF #1");
  184. Assert.AreEqual (expected [1], actual [1], "UTF #2");
  185. Assert.AreEqual (expected [2], actual [2], "UTF #3");
  186. Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
  187. }
  188. [Test]
  189. public void RFC1642_Example4 ()
  190. {
  191. string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E";
  192. char[] expected = UniCodeString.ToCharArray ();
  193. byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E };
  194. UTF7Encoding UTF7enc = new UTF7Encoding ();
  195. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  196. // "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1."
  197. Assert.AreEqual (expected [0], actual [0], "UTF #1");
  198. Assert.AreEqual (expected [1], actual [1], "UTF #2");
  199. Assert.AreEqual (expected [2], actual [2], "UTF #3");
  200. Assert.AreEqual (expected [3], actual [3], "UTF #4");
  201. Assert.AreEqual (expected [4], actual [4], "UTF #5");
  202. Assert.AreEqual (expected [5], actual [5], "UTF #6");
  203. Assert.AreEqual (expected [6], actual [6], "UTF #7");
  204. Assert.AreEqual (expected [7], actual [7], "UTF #8");
  205. Assert.AreEqual (expected [8], actual [8], "UTF #9");
  206. Assert.AreEqual (expected [9], actual [9], "UTF #10");
  207. Assert.AreEqual (expected [10], actual [10], "UTF #11");
  208. Assert.AreEqual (expected [11], actual [11], "UTF #12");
  209. Assert.AreEqual (expected [12], actual [12], "UTF #13");
  210. Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
  211. }
  212. [Test]
  213. public void TestMaxCharCount()
  214. {
  215. UTF7Encoding UTF7enc = new UTF7Encoding ();
  216. Assert.AreEqual (50, UTF7enc.GetMaxCharCount(50), "UTF #1");
  217. }
  218. [Test]
  219. #if NET_2_0
  220. [Category ("NotWorking")]
  221. #endif
  222. public void TestMaxByteCount()
  223. {
  224. UTF7Encoding UTF7enc = new UTF7Encoding ();
  225. #if NET_2_0
  226. Assert.AreEqual (152, UTF7enc.GetMaxByteCount(50), "UTF #1");
  227. #else
  228. Assert.AreEqual (136, UTF7enc.GetMaxByteCount(50), "UTF #1");
  229. #endif
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentException))]
  233. [Category ("NotDotNet")] // MS bug
  234. public void Bug77315 ()
  235. {
  236. string s = new UTF7Encoding ().GetString (
  237. Encoding.ASCII.GetBytes ("+2AA-"));
  238. }
  239. [Test]
  240. public void GetCharCount ()
  241. {
  242. string original = "*123456789*123456789*123456789*123456789*123456789*123456789*123456789*123456789";
  243. byte [] bytes = Encoding.UTF7.GetBytes (original);
  244. AssertType.AreEqual (112, bytes.Length, "#1");
  245. AssertType.AreEqual (80, Encoding.UTF7.GetCharCount (bytes), "#2");
  246. string decoded = Encoding.UTF7.GetString(Encoding.UTF7.GetBytes(original));
  247. AssertType.AreEqual (original, decoded, "#3");
  248. }
  249. }
  250. }