UTF7EncodingTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. namespace MonoTests.System.Text
  15. {
  16. [TestFixture]
  17. public class UTF7EncodingTest : Assertion
  18. {
  19. [Test]
  20. public void TestDirectlyEncoded1()
  21. {
  22. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  23. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  24. byte[] UTF7Bytes = null;
  25. UTF7Encoding UTF7enc = new UTF7Encoding ();
  26. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  27. Assertion.AssertEquals ("UTF7 #1", 0x61, UTF7Bytes [0]);
  28. Assertion.AssertEquals ("UTF7 #2", 0x7A, UTF7Bytes [1]);
  29. Assertion.AssertEquals ("UTF7 #3", 0x41, UTF7Bytes [2]);
  30. Assertion.AssertEquals ("UTF7 #4", 0x5A, UTF7Bytes [3]);
  31. Assertion.AssertEquals ("UTF7 #5", 0x30, UTF7Bytes [4]);
  32. Assertion.AssertEquals ("UTF7 #6", 0x39, UTF7Bytes [5]);
  33. Assertion.AssertEquals ("UTF7 #7", 0x27, UTF7Bytes [6]);
  34. Assertion.AssertEquals ("UTF7 #8", 0x3F, UTF7Bytes [7]);
  35. }
  36. [Test]
  37. public void TestDirectlyEncoded2()
  38. {
  39. // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
  40. string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
  41. byte[] UTF7Bytes = new byte [8];
  42. int Length = UniCodeString.Length;
  43. UTF7Encoding UTF7enc = new UTF7Encoding ();
  44. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  45. Assertion.AssertEquals ("UTF7 #1", 0x61, UTF7Bytes [0]);
  46. Assertion.AssertEquals ("UTF7 #2", 0x7A, UTF7Bytes [1]);
  47. Assertion.AssertEquals ("UTF7 #3", 0x41, UTF7Bytes [2]);
  48. Assertion.AssertEquals ("UTF7 #4", 0x5A, UTF7Bytes [3]);
  49. Assertion.AssertEquals ("UTF7 #5", 0x30, UTF7Bytes [4]);
  50. Assertion.AssertEquals ("UTF7 #6", 0x39, UTF7Bytes [5]);
  51. Assertion.AssertEquals ("UTF7 #7", 0x27, UTF7Bytes [6]);
  52. Assertion.AssertEquals ("UTF7 #8", 0x3F, UTF7Bytes [7]);
  53. }
  54. [Test]
  55. public void TestEncodeOptionalEncoded()
  56. {
  57. string UniCodeString = "\u0021\u0026\u002A\u003B";
  58. byte[] UTF7Bytes = null;
  59. //Optional Characters are allowed.
  60. UTF7Encoding UTF7enc = new UTF7Encoding (true);
  61. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  62. Assertion.AssertEquals ("UTF7 #1", 0x21, UTF7Bytes [0]);
  63. Assertion.AssertEquals ("UTF7 #2", 0x26, UTF7Bytes [1]);
  64. Assertion.AssertEquals ("UTF7 #3", 0x2A, UTF7Bytes [2]);
  65. Assertion.AssertEquals ("UTF7 #4", 0x3B, UTF7Bytes [3]);
  66. //Optional characters are not allowed.
  67. UTF7enc = new UTF7Encoding (false);
  68. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  69. Assertion.AssertEquals ("UTF7 #5", 0x2B, UTF7Bytes [0]);
  70. Assertion.AssertEquals ("UTF7 #6", 0x41, UTF7Bytes [1]);
  71. Assertion.AssertEquals ("UTF7 #7", 0x43, UTF7Bytes [2]);
  72. Assertion.AssertEquals ("UTF7 #8", 0x45, UTF7Bytes [3]);
  73. Assertion.AssertEquals ("UTF7 #6", 0x41, UTF7Bytes [1]);
  74. }
  75. [Test]
  76. public void TestEncodeUnicodeShifted1()
  77. {
  78. string UniCodeString = "\u0041\u2262\u0391\u002E";
  79. byte[] UTF7Bytes = null;
  80. UTF7Encoding UTF7enc = new UTF7Encoding();
  81. UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
  82. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  83. Assertion.AssertEquals ("UTF7 #1", 0x41, UTF7Bytes [0]);
  84. Assertion.AssertEquals ("UTF7 #2", 0x2B, UTF7Bytes [1]);
  85. Assertion.AssertEquals ("UTF7 #3", 0x49, UTF7Bytes [2]);
  86. Assertion.AssertEquals ("UTF7 #4", 0x6D, UTF7Bytes [3]);
  87. Assertion.AssertEquals ("UTF7 #5", 0x49, UTF7Bytes [4]);
  88. Assertion.AssertEquals ("UTF7 #6", 0x44, UTF7Bytes [5]);
  89. Assertion.AssertEquals ("UTF7 #7", 0x6B, UTF7Bytes [6]);
  90. Assertion.AssertEquals ("UTF7 #8", 0x51, UTF7Bytes [7]);
  91. Assertion.AssertEquals ("UTF7 #9", 0x2D, UTF7Bytes [8]);
  92. Assertion.AssertEquals ("UTF7 #10", 0x2E, UTF7Bytes [9]);
  93. }
  94. [Test]
  95. public void TestEncodeUnicodeShifted2()
  96. {
  97. string UniCodeString = "\u0041\u2262\u0391\u002E";
  98. byte[] UTF7Bytes = new byte [10];
  99. int Length = UniCodeString.Length;
  100. UTF7Encoding UTF7enc = new UTF7Encoding ();
  101. int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
  102. //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
  103. Assertion.AssertEquals ("UTF7 #1", 0x41, UTF7Bytes [0]);
  104. Assertion.AssertEquals ("UTF7 #2", 0x2B, UTF7Bytes [1]);
  105. Assertion.AssertEquals ("UTF7 #3", 0x49, UTF7Bytes [2]);
  106. Assertion.AssertEquals ("UTF7 #4", 0x6D, UTF7Bytes [3]);
  107. Assertion.AssertEquals ("UTF7 #5", 0x49, UTF7Bytes [4]);
  108. Assertion.AssertEquals ("UTF7 #6", 0x44, UTF7Bytes [5]);
  109. Assertion.AssertEquals ("UTF7 #7", 0x6B, UTF7Bytes [6]);
  110. Assertion.AssertEquals ("UTF7 #8", 0x51, UTF7Bytes [7]);
  111. Assertion.AssertEquals ("UTF7 #9", 0x2D, UTF7Bytes [8]);
  112. Assertion.AssertEquals ("UTF7 #10", 0x2E, UTF7Bytes [9]);
  113. }
  114. [Test]
  115. public void RFC1642_Example1 ()
  116. {
  117. string UniCodeString = "\u0041\u2262\u0391\u002E";
  118. char[] expected = UniCodeString.ToCharArray ();
  119. byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E};
  120. UTF7Encoding UTF7enc = new UTF7Encoding ();
  121. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  122. // "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
  123. AssertEquals ("UTF #1", expected [0], actual [0]);
  124. AssertEquals ("UTF #2", expected [1], actual [1]);
  125. AssertEquals ("UTF #3", expected [2], actual [2]);
  126. AssertEquals ("UTF #4", expected [3], actual [3]);
  127. AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes));
  128. }
  129. [Test]
  130. public void RFC1642_Example2 ()
  131. {
  132. string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021";
  133. char[] expected = UniCodeString.ToCharArray ();
  134. byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D };
  135. UTF7Encoding UTF7enc = new UTF7Encoding ();
  136. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  137. // "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!"
  138. AssertEquals ("UTF #1", expected [0], actual [0]);
  139. AssertEquals ("UTF #2", expected [1], actual [1]);
  140. AssertEquals ("UTF #3", expected [2], actual [2]);
  141. AssertEquals ("UTF #4", expected [3], actual [3]);
  142. AssertEquals ("UTF #5", expected [4], actual [4]);
  143. AssertEquals ("UTF #6", expected [5], actual [5]);
  144. AssertEquals ("UTF #7", expected [6], actual [6]);
  145. AssertEquals ("UTF #8", expected [7], actual [7]);
  146. AssertEquals ("UTF #9", expected [8], actual [8]);
  147. AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes));
  148. }
  149. [Test]
  150. public void RFC1642_Example3 ()
  151. {
  152. string UniCodeString = "\u65E5\u672C\u8A9E";
  153. char[] expected = UniCodeString.ToCharArray ();
  154. byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D };
  155. UTF7Encoding UTF7enc = new UTF7Encoding ();
  156. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  157. // "+ZeVnLIqe-" is decoded as Japanese "nihongo"
  158. AssertEquals ("UTF #1", expected [0], actual [0]);
  159. AssertEquals ("UTF #2", expected [1], actual [1]);
  160. AssertEquals ("UTF #3", expected [2], actual [2]);
  161. AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes));
  162. }
  163. [Test]
  164. public void RFC1642_Example4 ()
  165. {
  166. string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E";
  167. char[] expected = UniCodeString.ToCharArray ();
  168. byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E };
  169. UTF7Encoding UTF7enc = new UTF7Encoding ();
  170. char[] actual = UTF7enc.GetChars (UTF7Bytes);
  171. // "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1."
  172. AssertEquals ("UTF #1", expected [0], actual [0]);
  173. AssertEquals ("UTF #2", expected [1], actual [1]);
  174. AssertEquals ("UTF #3", expected [2], actual [2]);
  175. AssertEquals ("UTF #4", expected [3], actual [3]);
  176. AssertEquals ("UTF #5", expected [4], actual [4]);
  177. AssertEquals ("UTF #6", expected [5], actual [5]);
  178. AssertEquals ("UTF #7", expected [6], actual [6]);
  179. AssertEquals ("UTF #8", expected [7], actual [7]);
  180. AssertEquals ("UTF #9", expected [8], actual [8]);
  181. AssertEquals ("UTF #10", expected [9], actual [9]);
  182. AssertEquals ("UTF #11", expected [10], actual [10]);
  183. AssertEquals ("UTF #12", expected [11], actual [11]);
  184. AssertEquals ("UTF #13", expected [12], actual [12]);
  185. AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes));
  186. }
  187. [Test]
  188. public void TestMaxCharCount()
  189. {
  190. UTF7Encoding UTF7enc = new UTF7Encoding ();
  191. Assertion.AssertEquals ("UTF #1", 50, UTF7enc.GetMaxCharCount(50));
  192. }
  193. [Test]
  194. public void TestMaxByteCount()
  195. {
  196. UTF7Encoding UTF7enc = new UTF7Encoding ();
  197. Assertion.AssertEquals ("UTF #1", 136, UTF7enc.GetMaxByteCount(50));
  198. }
  199. }
  200. }