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.
31 using NUnit.Framework;
34 using System.Security;
35 using System.Security.Permissions;
37 namespace MonoTests.System.Net {
40 public class DnsPermissionTest {
43 public void PermissionState_None ()
45 PermissionState ps = PermissionState.None;
46 DnsPermission dp = new DnsPermission (ps);
47 Assert.IsFalse (dp.IsUnrestricted (), "IsUnrestricted");
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");
54 DnsPermission copy = (DnsPermission)dp.Copy ();
55 Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
56 Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
60 public void PermissionState_Unrestricted ()
62 PermissionState ps = PermissionState.Unrestricted;
63 DnsPermission dp = new DnsPermission (ps);
64 Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
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");
71 DnsPermission copy = (DnsPermission)dp.Copy ();
72 Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
73 Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
77 public void PermissionState_Bad ()
79 PermissionState ps = (PermissionState)Int32.MinValue;
80 DnsPermission dp = new DnsPermission (ps);
81 // no ArgumentException here
82 Assert.IsFalse (dp.IsUnrestricted ());
86 public void Intersect ()
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");
92 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
93 Assert.IsNull (dpu.Intersect (null), "Unrestricted N null");
95 DnsPermission result = (DnsPermission) dpu.Intersect (dpu);
96 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted N Unrestricted");
98 Assert.IsNull (dpn.Intersect (dpu), "None N Unrestricted");
99 Assert.IsNull (dpu.Intersect (dpn), "Unrestricted N None");
103 [ExpectedException (typeof (ArgumentException))]
104 public void Intersect_BadPermission ()
106 DnsPermission dp = new DnsPermission (PermissionState.None);
107 dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
111 public void IsSubset ()
113 DnsPermission dpn = new DnsPermission (PermissionState.None);
114 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
116 Assert.IsTrue (dpn.IsSubsetOf (null), "None IsSubsetOf null");
117 Assert.IsFalse (dpu.IsSubsetOf (null), "Unrestricted IsSubsetOf null");
119 Assert.IsTrue (dpn.IsSubsetOf (dpn), "None IsSubsetOf None");
120 Assert.IsTrue (dpu.IsSubsetOf (dpu), "Unrestricted IsSubsetOf Unrestricted");
122 Assert.IsTrue (dpn.IsSubsetOf (dpu), "None IsSubsetOf Unrestricted");
123 Assert.IsFalse (dpu.IsSubsetOf (dpn), "Unrestricted IsSubsetOf None");
127 [ExpectedException (typeof (ArgumentException))]
128 public void IsSubset_BadPermission ()
130 DnsPermission dp = new DnsPermission (PermissionState.None);
131 dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
137 DnsPermission dpn = new DnsPermission (PermissionState.None);
138 DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
140 DnsPermission result = (DnsPermission) dpn.Union (null);
141 Assert.IsFalse (result.IsUnrestricted (), "None U null");
143 result = (DnsPermission) dpu.Union (null);
144 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U null");
146 result = (DnsPermission) dpn.Union (dpn);
147 Assert.IsFalse (result.IsUnrestricted (), "None U None");
149 result = (DnsPermission) dpu.Union (dpu);
150 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U Unrestricted");
152 result = (DnsPermission) dpn.Union (dpu);
153 Assert.IsTrue (result.IsUnrestricted (), "None U Unrestricted");
155 result = (DnsPermission) dpu.Union (dpn);
156 Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U None");
160 [ExpectedException (typeof (ArgumentException))]
161 public void Union_BadPermission ()
163 DnsPermission dp = new DnsPermission (PermissionState.None);
164 dp.Union (new SecurityPermission (PermissionState.Unrestricted));
168 [ExpectedException (typeof (ArgumentNullException))]
169 public void FromXml_Null ()
171 DnsPermission dp = new DnsPermission (PermissionState.None);
176 [ExpectedException (typeof (ArgumentException))]
177 public void FromXml_WrongTag ()
179 DnsPermission dp = new DnsPermission (PermissionState.None);
180 SecurityElement se = dp.ToXml ();
183 // note: normally IPermission classes (in corlib) DO care about the
188 [ExpectedException (typeof (ArgumentException))]
189 public void FromXml_WrongTagCase ()
191 DnsPermission dp = new DnsPermission (PermissionState.None);
192 SecurityElement se = dp.ToXml ();
193 se.Tag = "IPERMISSION"; // instead of IPermission
195 // note: normally IPermission classes (in corlib) DO care about the
200 public void FromXml_WrongClass ()
202 DnsPermission dp = new DnsPermission (PermissionState.None);
203 SecurityElement se = dp.ToXml ();
205 SecurityElement w = new SecurityElement (se.Tag);
206 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
207 w.AddAttribute ("version", se.Attribute ("version"));
209 // doesn't care of the class name at that stage
210 // anyway the class has already be created so...
214 [ExpectedException (typeof (ArgumentException))]
215 public void FromXml_NoClass ()
217 DnsPermission dp = new DnsPermission (PermissionState.None);
218 SecurityElement se = dp.ToXml ();
220 SecurityElement w = new SecurityElement (se.Tag);
221 w.AddAttribute ("version", se.Attribute ("version"));
223 // note: normally IPermission classes (in corlib) DO NOT care about
224 // attribute "class" name presence in the XML
228 [ExpectedException (typeof (ArgumentException))]
229 public void FromXml_WrongVersion ()
231 DnsPermission dp = new DnsPermission (PermissionState.None);
232 SecurityElement se = dp.ToXml ();
233 se.Attributes.Remove ("version");
234 se.Attributes.Add ("version", "2");
239 public void FromXml_NoVersion ()
241 DnsPermission dp = new DnsPermission (PermissionState.None);
242 SecurityElement se = dp.ToXml ();
244 SecurityElement w = new SecurityElement (se.Tag);
245 w.AddAttribute ("class", se.Attribute ("class"));