**** Merged from MCS ****
[mono-project.git] / mcs / class / System / Test / System.Net / DnsPermissionTest.cs
blob45f3dc2c7d370a4742ace1134d285d9f4aefa9f5
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.
29 using NUnit.Framework;
30 using System;
31 using System.Net;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Net {
37 [TestFixture]
38 public class DnsPermissionTest {
40 [Test]
41 public void PermissionState_None ()
43 PermissionState ps = PermissionState.None;
44 DnsPermission dp = new DnsPermission (ps);
45 Assert.IsFalse (dp.IsUnrestricted (), "IsUnrestricted");
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");
52 DnsPermission copy = (DnsPermission)dp.Copy ();
53 Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
54 Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
57 [Test]
58 public void PermissionState_Unrestricted ()
60 PermissionState ps = PermissionState.Unrestricted;
61 DnsPermission dp = new DnsPermission (ps);
62 Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
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");
69 DnsPermission copy = (DnsPermission)dp.Copy ();
70 Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
71 Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
74 [Test]
75 public void PermissionState_Bad ()
77 PermissionState ps = (PermissionState)Int32.MinValue;
78 DnsPermission dp = new DnsPermission (ps);
79 // no ArgumentException here
80 Assert.IsFalse (dp.IsUnrestricted ());
83 [Test]
84 public void Intersect ()
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");
90 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
91 Assert.IsNull (dpu.Intersect (null), "Unrestricted N null");
93 DnsPermission result = (DnsPermission) dpu.Intersect (dpu);
94 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted N Unrestricted");
96 Assert.IsNull (dpn.Intersect (dpu), "None N Unrestricted");
97 Assert.IsNull (dpu.Intersect (dpn), "Unrestricted N None");
100 [Test]
101 [ExpectedException (typeof (ArgumentException))]
102 public void Intersect_BadPermission ()
104 DnsPermission dp = new DnsPermission (PermissionState.None);
105 dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
108 [Test]
109 public void IsSubset ()
111 DnsPermission dpn = new DnsPermission (PermissionState.None);
112 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
114 Assert.IsTrue (dpn.IsSubsetOf (null), "None IsSubsetOf null");
115 Assert.IsFalse (dpu.IsSubsetOf (null), "Unrestricted IsSubsetOf null");
117 Assert.IsTrue (dpn.IsSubsetOf (dpn), "None IsSubsetOf None");
118 Assert.IsTrue (dpu.IsSubsetOf (dpu), "Unrestricted IsSubsetOf Unrestricted");
120 Assert.IsTrue (dpn.IsSubsetOf (dpu), "None IsSubsetOf Unrestricted");
121 Assert.IsFalse (dpu.IsSubsetOf (dpn), "Unrestricted IsSubsetOf None");
124 [Test]
125 [ExpectedException (typeof (ArgumentException))]
126 public void IsSubset_BadPermission ()
128 DnsPermission dp = new DnsPermission (PermissionState.None);
129 dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
132 [Test]
133 public void Union ()
135 DnsPermission dpn = new DnsPermission (PermissionState.None);
136 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
138 DnsPermission result = (DnsPermission) dpn.Union (null);
139 Assert.IsFalse (result.IsUnrestricted (), "None U null");
141 result = (DnsPermission) dpu.Union (null);
142 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U null");
144 result = (DnsPermission) dpn.Union (dpn);
145 Assert.IsFalse (result.IsUnrestricted (), "None U None");
147 result = (DnsPermission) dpu.Union (dpu);
148 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U Unrestricted");
150 result = (DnsPermission) dpn.Union (dpu);
151 Assert.IsTrue (result.IsUnrestricted (), "None U Unrestricted");
153 result = (DnsPermission) dpu.Union (dpn);
154 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U None");
157 [Test]
158 [ExpectedException (typeof (ArgumentException))]
159 public void Union_BadPermission ()
161 DnsPermission dp = new DnsPermission (PermissionState.None);
162 dp.Union (new SecurityPermission (PermissionState.Unrestricted));
165 [Test]
166 [ExpectedException (typeof (ArgumentNullException))]
167 public void FromXml_Null ()
169 DnsPermission dp = new DnsPermission (PermissionState.None);
170 dp.FromXml (null);
173 [Test]
174 [ExpectedException (typeof (ArgumentException))]
175 public void FromXml_WrongTag ()
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
185 [Test]
186 [ExpectedException (typeof (ArgumentException))]
187 public void FromXml_WrongTagCase ()
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
197 [Test]
198 public void FromXml_WrongClass ()
200 DnsPermission dp = new DnsPermission (PermissionState.None);
201 SecurityElement se = dp.ToXml ();
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...
211 [Test]
212 [ExpectedException (typeof (ArgumentException))]
213 public void FromXml_NoClass ()
215 DnsPermission dp = new DnsPermission (PermissionState.None);
216 SecurityElement se = dp.ToXml ();
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
225 [Test]
226 [ExpectedException (typeof (ArgumentException))]
227 public void FromXml_WrongVersion ()
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);
236 [Test]
237 public void FromXml_NoVersion ()
239 DnsPermission dp = new DnsPermission (PermissionState.None);
240 SecurityElement se = dp.ToXml ();
242 SecurityElement w = new SecurityElement (se.Tag);
243 w.AddAttribute ("class", se.Attribute ("class"));
244 dp.FromXml (w);