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