2 // UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding
5 // Patrick Kalkman kalkman@cistron.nl
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Patrick Kalkman
9 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
16 using AssertType = NUnit.Framework.Assert;
18 namespace MonoTests.System.Text
21 public class UTF7EncodingTest
24 public void IsBrowserDisplay ()
26 UTF7Encoding utf7 = new UTF7Encoding ();
27 Assert.IsTrue (!utf7.IsBrowserDisplay);
31 public void IsBrowserSave ()
33 UTF7Encoding utf7 = new UTF7Encoding ();
34 Assert.IsTrue (!utf7.IsBrowserSave);
38 public void IsMailNewsDisplay ()
40 UTF7Encoding utf7 = new UTF7Encoding ();
41 Assert.IsTrue (utf7.IsMailNewsDisplay);
45 public void IsMailNewsSave ()
47 UTF7Encoding utf7 = new UTF7Encoding ();
48 Assert.IsTrue (utf7.IsMailNewsSave);
52 public void TestDirectlyEncoded1()
54 // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
55 string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
56 byte[] UTF7Bytes = null;
57 UTF7Encoding UTF7enc = new UTF7Encoding ();
59 UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
61 Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
62 Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
63 Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
64 Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
65 Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
66 Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
67 Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
68 Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
72 public void TestDirectlyEncoded2()
74 // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
75 string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
76 byte[] UTF7Bytes = new byte [8];
77 int Length = UniCodeString.Length;
78 UTF7Encoding UTF7enc = new UTF7Encoding ();
80 int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
82 Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
83 Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
84 Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
85 Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
86 Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
87 Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
88 Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
89 Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
93 public void TestEncodeOptionalEncoded()
95 string UniCodeString = "\u0021\u0026\u002A\u003B";
96 byte[] UTF7Bytes = null;
98 //Optional Characters are allowed.
99 UTF7Encoding UTF7enc = new UTF7Encoding (true);
100 UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
102 Assert.AreEqual (0x21, UTF7Bytes [0], "UTF7 #1");
103 Assert.AreEqual (0x26, UTF7Bytes [1], "UTF7 #2");
104 Assert.AreEqual (0x2A, UTF7Bytes [2], "UTF7 #3");
105 Assert.AreEqual (0x3B, UTF7Bytes [3], "UTF7 #4");
107 //Optional characters are not allowed.
108 UTF7enc = new UTF7Encoding (false);
109 UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
111 Assert.AreEqual (0x2B, UTF7Bytes [0], "UTF7 #5");
112 Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
113 Assert.AreEqual (0x43, UTF7Bytes [2], "UTF7 #7");
114 Assert.AreEqual (0x45, UTF7Bytes [3], "UTF7 #8");
115 Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
119 public void TestEncodeUnicodeShifted1()
121 string UniCodeString = "\u0041\u2262\u0391\u002E";
122 byte[] UTF7Bytes = null;
124 UTF7Encoding UTF7enc = new UTF7Encoding();
125 UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
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");
141 public void TestEncodeUnicodeShifted2()
143 string UniCodeString = "\u0041\u2262\u0391\u002E";
144 byte[] UTF7Bytes = new byte [10];
145 int Length = UniCodeString.Length;
146 UTF7Encoding UTF7enc = new UTF7Encoding ();
148 int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
150 //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
151 Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
152 Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
153 Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
154 Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
155 Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
156 Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
157 Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
158 Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
159 Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
160 Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
164 public void RFC1642_Example1 ()
166 string UniCodeString = "\u0041\u2262\u0391\u002E";
167 char[] expected = UniCodeString.ToCharArray ();
169 byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E};
170 UTF7Encoding UTF7enc = new UTF7Encoding ();
171 char[] actual = UTF7enc.GetChars (UTF7Bytes);
173 // "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
174 Assert.AreEqual (expected [0], actual [0], "UTF #1");
175 Assert.AreEqual (expected [1], actual [1], "UTF #2");
176 Assert.AreEqual (expected [2], actual [2], "UTF #3");
177 Assert.AreEqual (expected [3], actual [3], "UTF #4");
179 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
183 public void RFC1642_Example2 ()
185 string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021";
186 char[] expected = UniCodeString.ToCharArray ();
188 byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D };
190 UTF7Encoding UTF7enc = new UTF7Encoding ();
191 char[] actual = UTF7enc.GetChars (UTF7Bytes);
193 // "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!"
194 Assert.AreEqual (expected [0], actual [0], "UTF #1");
195 Assert.AreEqual (expected [1], actual [1], "UTF #2");
196 Assert.AreEqual (expected [2], actual [2], "UTF #3");
197 Assert.AreEqual (expected [3], actual [3], "UTF #4");
198 Assert.AreEqual (expected [4], actual [4], "UTF #5");
199 Assert.AreEqual (expected [5], actual [5], "UTF #6");
200 Assert.AreEqual (expected [6], actual [6], "UTF #7");
201 Assert.AreEqual (expected [7], actual [7], "UTF #8");
202 Assert.AreEqual (expected [8], actual [8], "UTF #9");
204 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
208 public void RFC1642_Example3 ()
210 string UniCodeString = "\u65E5\u672C\u8A9E";
211 char[] expected = UniCodeString.ToCharArray ();
213 byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D };
215 UTF7Encoding UTF7enc = new UTF7Encoding ();
216 char[] actual = UTF7enc.GetChars (UTF7Bytes);
218 // "+ZeVnLIqe-" is decoded as Japanese "nihongo"
219 Assert.AreEqual (expected [0], actual [0], "UTF #1");
220 Assert.AreEqual (expected [1], actual [1], "UTF #2");
221 Assert.AreEqual (expected [2], actual [2], "UTF #3");
223 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
227 public void RFC1642_Example4 ()
229 string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E";
230 char[] expected = UniCodeString.ToCharArray ();
232 byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E };
234 UTF7Encoding UTF7enc = new UTF7Encoding ();
235 char[] actual = UTF7enc.GetChars (UTF7Bytes);
237 // "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1."
238 Assert.AreEqual (expected [0], actual [0], "UTF #1");
239 Assert.AreEqual (expected [1], actual [1], "UTF #2");
240 Assert.AreEqual (expected [2], actual [2], "UTF #3");
241 Assert.AreEqual (expected [3], actual [3], "UTF #4");
242 Assert.AreEqual (expected [4], actual [4], "UTF #5");
243 Assert.AreEqual (expected [5], actual [5], "UTF #6");
244 Assert.AreEqual (expected [6], actual [6], "UTF #7");
245 Assert.AreEqual (expected [7], actual [7], "UTF #8");
246 Assert.AreEqual (expected [8], actual [8], "UTF #9");
247 Assert.AreEqual (expected [9], actual [9], "UTF #10");
248 Assert.AreEqual (expected [10], actual [10], "UTF #11");
249 Assert.AreEqual (expected [11], actual [11], "UTF #12");
250 Assert.AreEqual (expected [12], actual [12], "UTF #13");
252 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
256 public void TestMaxCharCount()
258 UTF7Encoding UTF7enc = new UTF7Encoding ();
259 Assert.AreEqual (50, UTF7enc.GetMaxCharCount(50), "UTF #1");
263 [Category ("NotWorking")]
264 public void TestMaxByteCount()
266 UTF7Encoding UTF7enc = new UTF7Encoding ();
267 Assert.AreEqual (152, UTF7enc.GetMaxByteCount(50), "UTF #1");
271 [ExpectedException (typeof (ArgumentException))]
272 [Ignore ("referencesource bug")]
273 public void Bug77315 ()
275 string s = new UTF7Encoding ().GetString (
276 Encoding.ASCII.GetBytes ("+2AA-"));
280 public void GetCharCount ()
282 string original = "*123456789*123456789*123456789*123456789*123456789*123456789*123456789*123456789";
283 byte [] bytes = Encoding.UTF7.GetBytes (original);
284 AssertType.AreEqual (112, bytes.Length, "#1");
285 AssertType.AreEqual (80, Encoding.UTF7.GetCharCount (bytes), "#2");
286 string decoded = Encoding.UTF7.GetString(Encoding.UTF7.GetBytes(original));
287 AssertType.AreEqual (original, decoded, "#3");