1 // 2 // UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding 3 // 4 // Authors 5 // Patrick Kalkman kalkman@cistron.nl 6 // Sebastien Pouliot <sebastien@ximian.com> 7 // 8 // (C) 2003 Patrick Kalkman 9 // Copyright (C) 2004 Novell (http://www.novell.com) 10 // 11 12 using NUnit.Framework; 13 using System; 14 using System.Text; 15 16 using AssertType = NUnit.Framework.Assert; 17 18 namespace MonoTests.System.Text 19 { 20 [TestFixture] 21 public class UTF7EncodingTest 22 { 23 [Test] IsBrowserDisplay()24 public void IsBrowserDisplay () 25 { 26 UTF7Encoding utf7 = new UTF7Encoding (); 27 Assert.IsTrue (!utf7.IsBrowserDisplay); 28 } 29 30 [Test] IsBrowserSave()31 public void IsBrowserSave () 32 { 33 UTF7Encoding utf7 = new UTF7Encoding (); 34 Assert.IsTrue (!utf7.IsBrowserSave); 35 } 36 37 [Test] IsMailNewsDisplay()38 public void IsMailNewsDisplay () 39 { 40 UTF7Encoding utf7 = new UTF7Encoding (); 41 Assert.IsTrue (utf7.IsMailNewsDisplay); 42 } 43 44 [Test] IsMailNewsSave()45 public void IsMailNewsSave () 46 { 47 UTF7Encoding utf7 = new UTF7Encoding (); 48 Assert.IsTrue (utf7.IsMailNewsSave); 49 } 50 51 [Test] TestDirectlyEncoded1()52 public void TestDirectlyEncoded1() 53 { 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 (); 58 59 UTF7Bytes = UTF7enc.GetBytes (UniCodeString); 60 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"); 69 } 70 71 [Test] TestDirectlyEncoded2()72 public void TestDirectlyEncoded2() 73 { 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 (); 79 80 int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0); 81 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"); 90 } 91 92 [Test] TestEncodeOptionalEncoded()93 public void TestEncodeOptionalEncoded() 94 { 95 string UniCodeString = "\u0021\u0026\u002A\u003B"; 96 byte[] UTF7Bytes = null; 97 98 //Optional Characters are allowed. 99 UTF7Encoding UTF7enc = new UTF7Encoding (true); 100 UTF7Bytes = UTF7enc.GetBytes (UniCodeString); 101 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"); 106 107 //Optional characters are not allowed. 108 UTF7enc = new UTF7Encoding (false); 109 UTF7Bytes = UTF7enc.GetBytes (UniCodeString); 110 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"); 116 } 117 118 [Test] TestEncodeUnicodeShifted1()119 public void TestEncodeUnicodeShifted1() 120 { 121 string UniCodeString = "\u0041\u2262\u0391\u002E"; 122 byte[] UTF7Bytes = null; 123 124 UTF7Encoding UTF7enc = new UTF7Encoding(); 125 UTF7Bytes = UTF7enc.GetBytes (UniCodeString); 126 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 140 [Test] TestEncodeUnicodeShifted2()141 public void TestEncodeUnicodeShifted2() 142 { 143 string UniCodeString = "\u0041\u2262\u0391\u002E"; 144 byte[] UTF7Bytes = new byte [10]; 145 int Length = UniCodeString.Length; 146 UTF7Encoding UTF7enc = new UTF7Encoding (); 147 148 int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0); 149 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"); 161 } 162 163 [Test] RFC1642_Example1()164 public void RFC1642_Example1 () 165 { 166 string UniCodeString = "\u0041\u2262\u0391\u002E"; 167 char[] expected = UniCodeString.ToCharArray (); 168 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); 172 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"); 178 179 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString"); 180 } 181 182 [Test] RFC1642_Example2()183 public void RFC1642_Example2 () 184 { 185 string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021"; 186 char[] expected = UniCodeString.ToCharArray (); 187 188 byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D }; 189 190 UTF7Encoding UTF7enc = new UTF7Encoding (); 191 char[] actual = UTF7enc.GetChars (UTF7Bytes); 192 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"); 203 204 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString"); 205 } 206 207 [Test] RFC1642_Example3()208 public void RFC1642_Example3 () 209 { 210 string UniCodeString = "\u65E5\u672C\u8A9E"; 211 char[] expected = UniCodeString.ToCharArray (); 212 213 byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D }; 214 215 UTF7Encoding UTF7enc = new UTF7Encoding (); 216 char[] actual = UTF7enc.GetChars (UTF7Bytes); 217 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"); 222 223 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString"); 224 } 225 226 [Test] RFC1642_Example4()227 public void RFC1642_Example4 () 228 { 229 string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E"; 230 char[] expected = UniCodeString.ToCharArray (); 231 232 byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E }; 233 234 UTF7Encoding UTF7enc = new UTF7Encoding (); 235 char[] actual = UTF7enc.GetChars (UTF7Bytes); 236 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"); 251 252 Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString"); 253 } 254 255 [Test] TestMaxCharCount()256 public void TestMaxCharCount() 257 { 258 UTF7Encoding UTF7enc = new UTF7Encoding (); 259 Assert.AreEqual (50, UTF7enc.GetMaxCharCount(50), "UTF #1"); 260 } 261 262 [Test] 263 [Category ("NotWorking")] TestMaxByteCount()264 public void TestMaxByteCount() 265 { 266 UTF7Encoding UTF7enc = new UTF7Encoding (); 267 Assert.AreEqual (152, UTF7enc.GetMaxByteCount(50), "UTF #1"); 268 } 269 270 [Test] 271 [ExpectedException (typeof (ArgumentException))] 272 [Ignore ("referencesource bug")] Bug77315()273 public void Bug77315 () 274 { 275 string s = new UTF7Encoding ().GetString ( 276 Encoding.ASCII.GetBytes ("+2AA-")); 277 } 278 279 [Test] GetCharCount()280 public void GetCharCount () 281 { 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"); 288 } 289 } 290 } 291 292 293 294