2 // DnsPermissionTest.cs - NUnit Test Cases for DnsPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Net {
38 public class DnsPermissionTest {
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 ()");
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 ()");
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 ());
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");
101 [ExpectedException (typeof (ArgumentException))]
102 public void Intersect_BadPermission ()
104 DnsPermission dp = new DnsPermission (PermissionState.None);
105 dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
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");
125 [ExpectedException (typeof (ArgumentException))]
126 public void IsSubset_BadPermission ()
128 DnsPermission dp = new DnsPermission (PermissionState.None);
129 dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
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");
158 [ExpectedException (typeof (ArgumentException))]
159 public void Union_BadPermission ()
161 DnsPermission dp = new DnsPermission (PermissionState.None);
162 dp.Union (new SecurityPermission (PermissionState.Unrestricted));
166 [ExpectedException (typeof (ArgumentNullException))]
167 public void FromXml_Null ()
169 DnsPermission dp = new DnsPermission (PermissionState.None);
174 [ExpectedException (typeof (ArgumentException))]
175 public void FromXml_WrongTag ()
177 DnsPermission dp = new DnsPermission (PermissionState.None);
178 SecurityElement se = dp.ToXml ();
181 // note: normally IPermission classes (in corlib) DO care about the
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
193 // note: normally IPermission classes (in corlib) DO care about the
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"));
207 // doesn't care of the class name at that stage
208 // anyway the class has already be created so...
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"));
221 // note: normally IPermission classes (in corlib) DO NOT care about
222 // attribute "class" name presence in the XML
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");
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"));