Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / corlib / Test / System.Globalization / IdnMappingTest.cs
1 //
2 // IdnMappingTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using NUnit.Framework;
31 using System.IO;
32 using System;
33 using System.Globalization;
34 using System.Threading;
35 using System.Text;
36
37 namespace MonoTests.System.Globalization
38 {
39         [TestFixture]
40         public class IdnMappingTest
41         {
42                 void GetAscii (IdnMapping m, string source, string expected, object label)
43                 {
44                         Assert.AreEqual (expected, m.GetAscii (source), label != null ? label.ToString () : expected);
45                 }
46
47                 void GetAsciiInvalid (IdnMapping m, string s, object label)
48                 {
49                         try {
50                                 m.GetAscii (s);
51                                 Assert.Fail (label != null ? label.ToString () + ":" + s : s);
52                         } catch (ArgumentException) {
53                         }
54                 }
55
56                 void GetUnicode (IdnMapping m, string source, string expected, object label)
57                 {
58                         Assert.AreEqual (expected, m.GetUnicode (source), label != null ? label.ToString () : expected);
59                 }
60
61                 void GetUnicodeInvalid (IdnMapping m, string s, object label)
62                 {
63                         try {
64                                 m.GetUnicode (s);
65                                 Assert.Fail (label != null ? label.ToString () + ":" + s : s);
66                         } catch (ArgumentException) {
67                         }
68                 }
69
70                 [Test]
71                 public void GetAsciiInvalid ()
72                 {
73                         // hmm... according to RFC 3490, there are couple of
74                         // invalid characters in RFC 3491 [Nameprop] ...
75                         // comment out them for now.
76
77                         IdnMapping m = new IdnMapping ();
78                         GetAsciiInvalid (m, "\x0", 1);
79                         GetAsciiInvalid (m, "\xD\xA", 2);
80                         GetAsciiInvalid (m, "\x1F", 3);
81                         GetAsciiInvalid (m, "\x7F", 4);
82                         GetAsciiInvalid (m, "\x80", 5);
83                         // GetAsciiInvalid (m, "\xA0", 6);
84                         // GetAsciiInvalid (m, "\u1680", 7);
85                         GetAsciiInvalid (m, "\u200E", 8);
86                         // GetAsciiInvalid (m, "\u0341", 9);
87                         GetAsciiInvalid (m, "\uE000", 10);
88                         GetAsciiInvalid (m, "\uFFFF", 11);
89                         GetAsciiInvalid (m, "\u2028", 12);
90                         GetAsciiInvalid (m, "\uD800", 13);
91                         // GetAsciiInvalid (m, "\u3000", 14);
92                 }
93
94                 [Test]
95                 public void GetAscii ()
96                 {
97                         IdnMapping m = new IdnMapping ();
98                         GetAscii (m, "www.mono-project.com", "www.mono-project.com", 1);
99                         GetAscii (m, "a\uFF61b", "a.b", 2);
100
101                         // umm... does it reject anything larger than U+FFFF ??
102                         // GetAscii (m, "\uD800\uDC00", "", 3);
103
104                         // hmm, according to RFC 3490, ToASCII never changes
105                         // the input whose codepoints are all within ASCII.
106                         GetAscii (m, "A\uFF61B", "a.b", 4);
107                         GetAscii (m, "A.B", "A.B", 5); // no lowercasing
108                 }
109
110                 [Test]
111                 public void Dots ()
112                 {
113                         IdnMapping m = new IdnMapping ();
114                         GetAsciiInvalid (m, ".", 1);
115                         GetAsciiInvalid (m, "\uFF61", 2);
116                         GetAscii (m, "Am running now.", "Am running now.", 3);
117                         GetAsciiInvalid (m, ".bashrc", 4);
118                 }
119
120                 [Test]
121                 public void UnassignedCharacter ()
122                 {
123                         IdnMapping m = new IdnMapping ();
124                         //GetAsciiInvalid (m, "\u18b0.com", 1);
125                         m.AllowUnassigned = true;
126                         GetAscii (m, "\u18b0.com", "xn--6bf.com", 2);
127                 }
128
129                 [Test]
130                 public void UseStd3AsciiRules ()
131                 {
132                         IdnMapping m = new IdnMapping ();
133                         GetAscii (m, "-_-.com", "-_-.com", 1);
134                         m.UseStd3AsciiRules = true;
135                         GetAsciiInvalid (m, "a b.com", 1.5);
136                         GetAsciiInvalid (m, "_.com", 2);
137                         GetAsciiInvalid (m, "-abc.com", 3);
138                         GetAscii (m, "abc-def.com", "abc-def.com", 4); // hyphen in the middle is okay
139                         GetAsciiInvalid (m, "abc-.def.com", 5); // hyphen is *not* in the middle.
140                 }
141
142                 [Test]
143                 public void AcePrefix ()
144                 {
145                         IdnMapping m = new IdnMapping ();
146                         GetAscii (m, "bl--.com", "bl--.com", 1); // only ascii
147                         GetAscii (m, "bl--\xC0.com", "xn--bl---3na.com", 2);
148                         GetAscii (m, "xn--.com", "xn--.com", 3); // only ascii
149                         GetAsciiInvalid (m, "xn--\xC0.com", 4);
150                         GetAsciiInvalid (m, "xN--\xC0.com", 5);
151                         GetAsciiInvalid (m, "Xn--\xC0.com", 6);
152                         GetAsciiInvalid (m, "XN--\xC0.com", 7);
153                         GetAscii (m, "xN\xC0.com", "xn--xn-kia.com", 8);
154                         GetAscii (m, "bl--\xC0.wl--\xC0.com", "xn--bl---3na.xn--wl---3na.com", 9);
155                 }
156
157                 [Test]
158                 public void GetAsciiRFC3492Examples ()
159                 {
160                         IdnMapping m = new IdnMapping ();
161                         // 3<nen>B<gumi><kin><pachi><sen><sei>
162                         GetAscii (m, "3\u5E74B\u7D44\u91D1\u516B\u5148\u751F", "xn--3b-ww4c5e180e575a65lsy2b", "(L)");
163                         // Maji<de>Koi<suru>5<byoumae>
164                         GetAscii (m, "Maji\u3067Koi\u3059\u308B5\u79D2\u524D", "xn--majikoi5-783gue6qz075azm5e", "(P)");
165                 }
166
167                 [Test]
168                 public void GetUnicode ()
169                 {
170                         IdnMapping m = new IdnMapping ();
171                         GetAscii (m, "www.mono-project.com", "www.mono-project.com", 1);
172                         GetAscii (m, "a\uFF61b", "a.b", 2);
173
174                         // umm... does it reject anything larger than U+FFFF ??
175                         // GetAscii (m, "\uD800\uDC00", "", 3);
176
177                         // hmm, according to RFC 3490, ToASCII never changes
178                         // the input whose codepoints are all within ASCII.
179                         GetAscii (m, "A\uFF61B", "a.b", 4);
180                         GetAscii (m, "A.B", "A.B", 5); // no lowercasing
181                 }
182
183                 [Test]
184                 public void GetUnicodeRFC3492Examples ()
185                 {
186                         // uppercases in the ASCII strings are ignored.
187                         IdnMapping m = new IdnMapping ();
188                         // 3<nen>B<gumi><kin><pachi><sen><sei>
189                         GetUnicode (m, "xn--3B-ww4c5e180e575a65lsy2b", "3\u5E74b\u7D44\u91D1\u516B\u5148\u751F", "(L)");
190                         // Maji<de>Koi<suru>5<byoumae>
191                         GetUnicode (m, "xn--MajiKoi5-783gue6qz075azm5e", "maji\u3067koi\u3059\u308B5\u79D2\u524D", "(P)");
192                 }
193         }
194 }
195