2 // ApplicationDirectoryMembershipConditionTest.cs -
3 // NUnit Test Cases for ApplicationDirectoryMembershipCondition
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using NUnit.Framework;
32 using System.Reflection;
33 using System.Security;
34 using System.Security.Policy;
36 namespace MonoTests.System.Security.Policy {
39 public class ApplicationDirectoryMembershipConditionTest {
42 public void Constructor ()
44 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
45 Assert.IsNotNull (ad);
51 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
53 Assert.IsFalse (ad.Check (e), "Check (null)");
55 Assert.IsFalse (ad.Check (e), "Check (empty)");
56 e.AddHost (new Zone (SecurityZone.MyComputer));
57 Assert.IsFalse (ad.Check (e), "Check (zone)");
59 string codebase = Assembly.GetExecutingAssembly ().CodeBase;
60 Url u = new Url (codebase);
61 ApplicationDirectory adir = new ApplicationDirectory (codebase);
64 Assert.IsFalse (ad.Check (e), "Check (url-host)"); // not enough
66 Assert.IsFalse (ad.Check (e), "Check (url-host+adir-assembly)");
70 Assert.IsFalse (ad.Check (e), "Check (adir-host)"); // not enough
72 Assert.IsFalse (ad.Check (e), "Check (url-assembly+adir-host)");
77 Assert.IsTrue (ad.Check (e), "Check (url+adir host)"); // both!!
83 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
84 ApplicationDirectoryMembershipCondition copy = (ApplicationDirectoryMembershipCondition)ad.Copy ();
85 Assert.AreEqual (ad, copy, "Equals");
86 Assert.IsFalse (Object.ReferenceEquals (ad, copy), "ReferenceEquals");
92 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
93 Assert.IsFalse (ad.Equals (null), "Equals(null)");
94 ApplicationDirectoryMembershipCondition g2 = new ApplicationDirectoryMembershipCondition ();
95 Assert.IsTrue (ad.Equals (g2), "Equals(g2)");
96 Assert.IsTrue (g2.Equals (ad), "Equals(ad)");
97 Assert.IsFalse (ad.Equals (new object ()), "Equals (object)");
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void FromXml_Null ()
104 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
109 public void FromXml ()
111 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
112 SecurityElement se = ad.ToXml ();
117 [ExpectedException (typeof (ArgumentException))]
118 public void FromXml_InvalidTag ()
120 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
121 SecurityElement se = ad.ToXml ();
122 se.Tag = "IMonoship";
127 [ExpectedException (typeof (ArgumentException))]
128 public void FromXml_WrongTagCase ()
130 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
131 SecurityElement se = ad.ToXml ();
132 se.Tag = "IMEMBERSHIPCONDITION"; // instead of IMembershipCondition
137 public void FromXml_InvalidClass ()
139 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
140 SecurityElement se = ad.ToXml ();
141 se.Attributes ["class"] = "Hello world";
146 public void FromXml_NoClass ()
148 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
149 SecurityElement se = ad.ToXml ();
151 SecurityElement w = new SecurityElement (se.Tag);
152 w.AddAttribute ("version", se.Attribute ("version"));
154 // doesn't even care of the class attribute presence
158 public void FromXml_InvalidVersion ()
160 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
161 SecurityElement se = ad.ToXml ();
163 SecurityElement w = new SecurityElement (se.Tag);
164 w.AddAttribute ("class", se.Attribute ("class"));
165 w.AddAttribute ("version", "2");
167 // doesn't seems to care about the version number!
171 public void FromXml_NoVersion ()
173 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
174 SecurityElement se = ad.ToXml ();
176 SecurityElement w = new SecurityElement (se.Tag);
177 w.AddAttribute ("class", se.Attribute ("class"));
182 [ExpectedException (typeof (ArgumentNullException))]
183 public void FromXml_SecurityElementNull ()
185 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
186 ad.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
190 public void FromXml_PolicyLevelNull ()
192 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
193 SecurityElement se = ad.ToXml ();
194 ad.FromXml (se, null);
198 public void GetHashCode_ ()
200 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
201 ApplicationDirectoryMembershipCondition copy = (ApplicationDirectoryMembershipCondition)ad.Copy ();
202 Assert.AreEqual (ad.GetHashCode (), copy.GetHashCode ());
206 public void ToString_ ()
208 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
209 Assert.AreEqual ("ApplicationDirectory", ad.ToString ());
215 ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
216 SecurityElement se = ad.ToXml ();
217 Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
218 Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.ApplicationDirectoryMembershipCondition"), "class");
219 Assert.AreEqual ("1", se.Attribute ("version"), "version");
220 Assert.AreEqual (se.ToString (), ad.ToXml (null).ToString (), "ToXml(null)");
221 Assert.AreEqual (se.ToString (), ad.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");