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