File: UTF7EncodingTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (293 lines) | stat: -rw-r--r-- 12,504 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding
//
// Authors
//	Patrick Kalkman  kalkman@cistron.nl
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// (C) 2003 Patrick Kalkman
// Copyright (C) 2004 Novell (http://www.novell.com)
//
 
using NUnit.Framework;
using System;
using System.Text;

using AssertType = NUnit.Framework.Assert;

namespace MonoTests.System.Text
{
        [TestFixture]
        public class UTF7EncodingTest 
        {
		[Test]
		public void IsBrowserDisplay ()
		{
			UTF7Encoding utf7 = new UTF7Encoding ();
			Assert.IsTrue (!utf7.IsBrowserDisplay);
		}

		[Test]
		public void IsBrowserSave ()
		{
			UTF7Encoding utf7 = new UTF7Encoding ();
			Assert.IsTrue (!utf7.IsBrowserSave);
		}

		[Test]
		public void IsMailNewsDisplay ()
		{
			UTF7Encoding utf7 = new UTF7Encoding ();
			Assert.IsTrue (utf7.IsMailNewsDisplay);
		}

		[Test]
		public void IsMailNewsSave ()
		{
			UTF7Encoding utf7 = new UTF7Encoding ();
			Assert.IsTrue (utf7.IsMailNewsSave);
		}

                [Test]
                public void TestDirectlyEncoded1() 
                {
                        // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
                        string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
                        byte[] UTF7Bytes = null;
                        UTF7Encoding UTF7enc = new UTF7Encoding ();
                        
                        UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
                        
                        Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
                        Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
                        Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
                        Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
                        Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
                        Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
                        Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
                        Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
                }
        
                [Test]
                public void TestDirectlyEncoded2()
                {
                        // Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
                        string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
                        byte[] UTF7Bytes = new byte [8];
                        int Length = UniCodeString.Length;
                        UTF7Encoding UTF7enc = new UTF7Encoding ();
                        
                        int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
                
		        Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
                        Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
                        Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
                        Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
                        Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
                        Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
                        Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
                        Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
		}
        
                [Test]
                public void TestEncodeOptionalEncoded()
                {
                        string UniCodeString = "\u0021\u0026\u002A\u003B";
                        byte[] UTF7Bytes = null;
                        
                        //Optional Characters are allowed.	
                        UTF7Encoding UTF7enc = new UTF7Encoding (true); 
                        UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
                        
                        Assert.AreEqual (0x21, UTF7Bytes [0], "UTF7 #1");
                        Assert.AreEqual (0x26, UTF7Bytes [1], "UTF7 #2");
                        Assert.AreEqual (0x2A, UTF7Bytes [2], "UTF7 #3");
                        Assert.AreEqual (0x3B, UTF7Bytes [3], "UTF7 #4");
                        
                        //Optional characters are not allowed.
                        UTF7enc = new UTF7Encoding (false);
                        UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
                        
                        Assert.AreEqual (0x2B, UTF7Bytes [0], "UTF7 #5");
                        Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
                        Assert.AreEqual (0x43, UTF7Bytes [2], "UTF7 #7");
                        Assert.AreEqual (0x45, UTF7Bytes [3], "UTF7 #8");
                        Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
                }
        
                [Test]
                public void TestEncodeUnicodeShifted1()
                {
                        string UniCodeString = "\u0041\u2262\u0391\u002E";
                        byte[] UTF7Bytes = null;
                        
                        UTF7Encoding UTF7enc = new UTF7Encoding();
                        UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
                        
                        //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
                        Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
                        Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
                        Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
                        Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
                        Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
                        Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
                        Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
                        Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
                        Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
                        Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
                }
        
                [Test]
                public void TestEncodeUnicodeShifted2()
                {
                        string UniCodeString = "\u0041\u2262\u0391\u002E";
                        byte[] UTF7Bytes = new byte [10];
                        int Length = UniCodeString.Length;
                        UTF7Encoding UTF7enc = new UTF7Encoding ();
                        
                        int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
                        
                        //"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
                        Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
                        Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
                        Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
                        Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
                        Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
                        Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
                        Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
                        Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
                        Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
                        Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
                }
        
		[Test]
		public void RFC1642_Example1 ()
		{
			string UniCodeString = "\u0041\u2262\u0391\u002E";
			char[] expected = UniCodeString.ToCharArray ();

			byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B,  0x51, 0x2D, 0x2E};
			UTF7Encoding UTF7enc = new UTF7Encoding ();
			char[] actual = UTF7enc.GetChars (UTF7Bytes);

			// "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
			Assert.AreEqual (expected [0], actual [0], "UTF #1");
			Assert.AreEqual (expected [1], actual [1], "UTF #2");
			Assert.AreEqual (expected [2], actual [2], "UTF #3");
			Assert.AreEqual (expected [3], actual [3], "UTF #4");

			Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
                }

		[Test]
		public void RFC1642_Example2 ()
		{
			string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021";
			char[] expected = UniCodeString.ToCharArray ();

			byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D };

			UTF7Encoding UTF7enc = new UTF7Encoding ();
			char[] actual = UTF7enc.GetChars (UTF7Bytes);

			// "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!"
			Assert.AreEqual (expected [0], actual [0], "UTF #1");
			Assert.AreEqual (expected [1], actual [1], "UTF #2");
			Assert.AreEqual (expected [2], actual [2], "UTF #3");
			Assert.AreEqual (expected [3], actual [3], "UTF #4");
			Assert.AreEqual (expected [4], actual [4], "UTF #5");
			Assert.AreEqual (expected [5], actual [5], "UTF #6");
			Assert.AreEqual (expected [6], actual [6], "UTF #7");
			Assert.AreEqual (expected [7], actual [7], "UTF #8");
			Assert.AreEqual (expected [8], actual [8], "UTF #9");

			Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
		}

		[Test]
		public void RFC1642_Example3 ()
		{
			string UniCodeString = "\u65E5\u672C\u8A9E";
			char[] expected = UniCodeString.ToCharArray ();

			byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D };

			UTF7Encoding UTF7enc = new UTF7Encoding ();
			char[] actual = UTF7enc.GetChars (UTF7Bytes);

			// "+ZeVnLIqe-" is decoded as Japanese "nihongo"
			Assert.AreEqual (expected [0], actual [0], "UTF #1");
			Assert.AreEqual (expected [1], actual [1], "UTF #2");
			Assert.AreEqual (expected [2], actual [2], "UTF #3");

			Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
		}

		[Test]
		public void RFC1642_Example4 ()
		{
			string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E";
			char[] expected = UniCodeString.ToCharArray ();

			byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E };

			UTF7Encoding UTF7enc = new UTF7Encoding ();
			char[] actual = UTF7enc.GetChars (UTF7Bytes);

			// "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1."
			Assert.AreEqual (expected [0], actual [0], "UTF #1");
			Assert.AreEqual (expected [1], actual [1], "UTF #2");
			Assert.AreEqual (expected [2], actual [2], "UTF #3");
			Assert.AreEqual (expected [3], actual [3], "UTF #4");
			Assert.AreEqual (expected [4], actual [4], "UTF #5");
			Assert.AreEqual (expected [5], actual [5], "UTF #6");
			Assert.AreEqual (expected [6], actual [6], "UTF #7");
			Assert.AreEqual (expected [7], actual [7], "UTF #8");
			Assert.AreEqual (expected [8], actual [8], "UTF #9");
			Assert.AreEqual (expected [9], actual [9], "UTF #10");
			Assert.AreEqual (expected [10], actual [10], "UTF #11");
			Assert.AreEqual (expected [11], actual [11], "UTF #12");
			Assert.AreEqual (expected [12], actual [12], "UTF #13");

			Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
		}

                [Test]
                public void TestMaxCharCount()
                {
                        UTF7Encoding UTF7enc = new UTF7Encoding ();
                        Assert.AreEqual (50, UTF7enc.GetMaxCharCount(50), "UTF #1");
                }
        
                [Test]
                [Category ("NotWorking")]
                public void TestMaxByteCount()
                {
                        UTF7Encoding UTF7enc = new UTF7Encoding ();
                        Assert.AreEqual (152, UTF7enc.GetMaxByteCount(50), "UTF #1");
                }

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		[Ignore ("referencesource bug")]
		public void Bug77315 ()
		{
			string s = new UTF7Encoding ().GetString (
				Encoding.ASCII.GetBytes ("+2AA-"));
		}

		[Test]
		public void GetCharCount ()
		{
			string original = "*123456789*123456789*123456789*123456789*123456789*123456789*123456789*123456789";
			byte [] bytes = Encoding.UTF7.GetBytes (original);
			AssertType.AreEqual (112, bytes.Length, "#1");
			AssertType.AreEqual (80, Encoding.UTF7.GetCharCount (bytes), "#2");
			string decoded = Encoding.UTF7.GetString(Encoding.UTF7.GetBytes(original));
			AssertType.AreEqual (original, decoded, "#3");
		}
        }
}