Merge pull request #2400 from esdrubal/extrahead
[mono.git] / mcs / class / System / Test / System.Net / DnsPermissionTest.cs
1 //
2 // DnsPermissionTest.cs - NUnit Test Cases for DnsPermission
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 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 #if !MOBILE
30
31 using NUnit.Framework;
32 using System;
33 using System.Net;
34 using System.Security;
35 using System.Security.Permissions;
36
37 namespace MonoTests.System.Net {
38
39         [TestFixture]
40         public class DnsPermissionTest {
41
42                 [Test]
43                 public void PermissionState_None ()
44                 {
45                         PermissionState ps = PermissionState.None;
46                         DnsPermission dp = new DnsPermission (ps);
47                         Assert.IsFalse (dp.IsUnrestricted (), "IsUnrestricted");
48
49                         SecurityElement se = dp.ToXml ();
50                         // only class and version are present
51                         Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes#");
52                         Assert.IsNull (se.Children, "Xml-Children");
53
54                         DnsPermission copy = (DnsPermission)dp.Copy ();
55                         Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
56                         Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
57                 }
58
59                 [Test]
60                 public void PermissionState_Unrestricted ()
61                 {
62                         PermissionState ps = PermissionState.Unrestricted;
63                         DnsPermission dp = new DnsPermission (ps);
64                         Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
65
66                         SecurityElement se = dp.ToXml ();
67                         Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
68                         Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes#");
69                         Assert.IsNull (se.Children, "Xml-Children");
70
71                         DnsPermission copy = (DnsPermission)dp.Copy ();
72                         Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
73                         Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
74                 }
75
76                 [Test]
77                 public void PermissionState_Bad ()
78                 {
79                         PermissionState ps = (PermissionState)Int32.MinValue;
80                         DnsPermission dp = new DnsPermission (ps);
81                         // no ArgumentException here
82                         Assert.IsFalse (dp.IsUnrestricted ());
83                 }
84
85                 [Test]
86                 public void Intersect ()
87                 {
88                         DnsPermission dpn = new DnsPermission (PermissionState.None);
89                         Assert.IsNull (dpn.Intersect (null), "None N null");
90                         Assert.IsNull (dpn.Intersect (dpn), "None N None");
91
92                         DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
93                         Assert.IsNull (dpu.Intersect (null), "Unrestricted N null");
94
95                         DnsPermission result = (DnsPermission) dpu.Intersect (dpu);
96                         Assert.IsTrue (result.IsUnrestricted (), "Unrestricted N Unrestricted");
97
98                         Assert.IsNull (dpn.Intersect (dpu), "None N Unrestricted");
99                         Assert.IsNull (dpu.Intersect (dpn), "Unrestricted N None");
100                 }
101
102                 [Test]
103                 [ExpectedException (typeof (ArgumentException))]
104                 public void Intersect_BadPermission ()
105                 {
106                         DnsPermission dp = new DnsPermission (PermissionState.None);
107                         dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
108                 }
109
110                 [Test]
111                 public void IsSubset ()
112                 {
113                         DnsPermission dpn = new DnsPermission (PermissionState.None);
114                         DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
115
116                         Assert.IsTrue (dpn.IsSubsetOf (null), "None IsSubsetOf null");
117                         Assert.IsFalse (dpu.IsSubsetOf (null), "Unrestricted IsSubsetOf null");
118
119                         Assert.IsTrue (dpn.IsSubsetOf (dpn), "None IsSubsetOf None");
120                         Assert.IsTrue (dpu.IsSubsetOf (dpu), "Unrestricted IsSubsetOf Unrestricted");
121
122                         Assert.IsTrue (dpn.IsSubsetOf (dpu), "None IsSubsetOf Unrestricted");
123                         Assert.IsFalse (dpu.IsSubsetOf (dpn), "Unrestricted IsSubsetOf None");
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentException))]
128                 public void IsSubset_BadPermission ()
129                 {
130                         DnsPermission dp = new DnsPermission (PermissionState.None);
131                         dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
132                 }
133
134                 [Test]
135                 public void Union ()
136                 {
137                         DnsPermission dpn = new DnsPermission (PermissionState.None);
138                         DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
139                         
140                         DnsPermission result = (DnsPermission) dpn.Union (null);
141                         Assert.IsFalse (result.IsUnrestricted (), "None U null");
142                         
143                         result = (DnsPermission) dpu.Union (null);
144                         Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U null");
145
146                         result = (DnsPermission) dpn.Union (dpn);
147                         Assert.IsFalse (result.IsUnrestricted (), "None U None");
148
149                         result = (DnsPermission) dpu.Union (dpu);
150                         Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U Unrestricted");
151
152                         result = (DnsPermission) dpn.Union (dpu);
153                         Assert.IsTrue (result.IsUnrestricted (), "None U Unrestricted");
154
155                         result = (DnsPermission) dpu.Union (dpn);
156                         Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U None");
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (ArgumentException))]
161                 public void Union_BadPermission ()
162                 {
163                         DnsPermission dp = new DnsPermission (PermissionState.None);
164                         dp.Union (new SecurityPermission (PermissionState.Unrestricted));
165                 }
166
167                 [Test]
168                 [ExpectedException (typeof (ArgumentNullException))]
169                 public void FromXml_Null ()
170                 {
171                         DnsPermission dp = new DnsPermission (PermissionState.None);
172                         dp.FromXml (null);
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (ArgumentException))]
177                 public void FromXml_WrongTag ()
178                 {
179                         DnsPermission dp = new DnsPermission (PermissionState.None);
180                         SecurityElement se = dp.ToXml ();
181                         se.Tag = "IMono";
182                         dp.FromXml (se);
183                         // note: normally IPermission classes (in corlib) DO care about the
184                         // IPermission tag
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (ArgumentException))]
189                 public void FromXml_WrongTagCase ()
190                 {
191                         DnsPermission dp = new DnsPermission (PermissionState.None);
192                         SecurityElement se = dp.ToXml ();
193                         se.Tag = "IPERMISSION"; // instead of IPermission
194                         dp.FromXml (se);
195                         // note: normally IPermission classes (in corlib) DO care about the
196                         // IPermission tag
197                 }
198
199                 [Test]
200                 public void FromXml_WrongClass ()
201                 {
202                         DnsPermission dp = new DnsPermission (PermissionState.None);
203                         SecurityElement se = dp.ToXml ();
204
205                         SecurityElement w = new SecurityElement (se.Tag);
206                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
207                         w.AddAttribute ("version", se.Attribute ("version"));
208                         dp.FromXml (w);
209                         // doesn't care of the class name at that stage
210                         // anyway the class has already be created so...
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentException))]
215                 public void FromXml_NoClass ()
216                 {
217                         DnsPermission dp = new DnsPermission (PermissionState.None);
218                         SecurityElement se = dp.ToXml ();
219
220                         SecurityElement w = new SecurityElement (se.Tag);
221                         w.AddAttribute ("version", se.Attribute ("version"));
222                         dp.FromXml (w);
223                         // note: normally IPermission classes (in corlib) DO NOT care about
224                         // attribute "class" name presence in the XML
225                 }
226
227                 [Test]
228                 [ExpectedException (typeof (ArgumentException))]
229                 public void FromXml_WrongVersion ()
230                 {
231                         DnsPermission dp = new DnsPermission (PermissionState.None);
232                         SecurityElement se = dp.ToXml ();
233                         se.Attributes.Remove ("version");
234                         se.Attributes.Add ("version", "2");
235                         dp.FromXml (se);
236                 }
237
238                 [Test]
239                 public void FromXml_NoVersion ()
240                 {
241                         DnsPermission dp = new DnsPermission (PermissionState.None);
242                         SecurityElement se = dp.ToXml ();
243
244                         SecurityElement w = new SecurityElement (se.Tag);
245                         w.AddAttribute ("class", se.Attribute ("class"));
246                         dp.FromXml (w);
247                 }
248         }
249 }
250
251 #endif