New test.
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationDirectoryMembershipConditionTest.cs
1 //
2 // ApplicationDirectoryMembershipConditionTest.cs -
3 //      NUnit Test Cases for ApplicationDirectoryMembershipCondition
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Reflection;
33 using System.Security;
34 using System.Security.Policy;
35
36 namespace MonoTests.System.Security.Policy {
37
38         [TestFixture]
39         public class ApplicationDirectoryMembershipConditionTest {
40
41                 [Test]
42                 public void Constructor ()
43                 {
44                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
45                         Assert.IsNotNull (ad);
46                 }
47
48                 [Test]
49                 public void Check ()
50                 {
51                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
52                         Evidence e = null;
53                         Assert.IsFalse (ad.Check (e), "Check (null)");
54                         e = new Evidence ();
55                         Assert.IsFalse (ad.Check (e), "Check (empty)");
56                         e.AddHost (new Zone (SecurityZone.MyComputer));
57                         Assert.IsFalse (ad.Check (e), "Check (zone)");
58
59                         string codebase = Assembly.GetExecutingAssembly ().CodeBase;
60                         Url u = new Url (codebase);
61                         ApplicationDirectory adir = new ApplicationDirectory (codebase);
62
63                         e.AddHost (u);
64                         Assert.IsFalse (ad.Check (e), "Check (url-host)"); // not enough
65                         e.AddAssembly (adir);
66                         Assert.IsFalse (ad.Check (e), "Check (url-host+adir-assembly)");
67
68                         e = new Evidence ();
69                         e.AddHost (adir);
70                         Assert.IsFalse (ad.Check (e), "Check (adir-host)"); // not enough
71                         e.AddAssembly (u);
72                         Assert.IsFalse (ad.Check (e), "Check (url-assembly+adir-host)");
73
74                         e = new Evidence ();
75                         e.AddHost (u);
76                         e.AddHost (adir);
77                         Assert.IsTrue (ad.Check (e), "Check (url+adir host)"); // both!!
78                 }
79
80                 [Test]
81                 public void Copy ()
82                 {
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");
87                 }
88
89                 [Test]
90                 public void Equals ()
91                 {
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)");
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void FromXml_Null ()
103                 {
104                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
105                         ad.FromXml (null);
106                 }
107
108                 [Test]
109                 public void FromXml ()
110                 {
111                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
112                         SecurityElement se = ad.ToXml ();
113                         ad.FromXml (se);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentException))]
118                 public void FromXml_InvalidTag ()
119                 {
120                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
121                         SecurityElement se = ad.ToXml ();
122                         se.Tag = "IMonoship";
123                         ad.FromXml (se);
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentException))]
128                 public void FromXml_WrongTagCase ()
129                 {
130                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
131                         SecurityElement se = ad.ToXml ();
132                         se.Tag = "IMEMBERSHIPCONDITION"; // instead of IMembershipCondition
133                         ad.FromXml (se);
134                 }
135
136                 [Test]
137                 public void FromXml_InvalidClass ()
138                 {
139                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
140                         SecurityElement se = ad.ToXml ();
141                         se.Attributes ["class"] = "Hello world";
142                         ad.FromXml (se);
143                 }
144
145                 [Test]
146                 public void FromXml_NoClass ()
147                 {
148                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
149                         SecurityElement se = ad.ToXml ();
150
151                         SecurityElement w = new SecurityElement (se.Tag);
152                         w.AddAttribute ("version", se.Attribute ("version"));
153                         ad.FromXml (w);
154                         // doesn't even care of the class attribute presence
155                 }
156
157                 [Test]
158                 public void FromXml_InvalidVersion ()
159                 {
160                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
161                         SecurityElement se = ad.ToXml ();
162
163                         SecurityElement w = new SecurityElement (se.Tag);
164                         w.AddAttribute ("class", se.Attribute ("class"));
165                         w.AddAttribute ("version", "2");
166                         ad.FromXml (w);
167                         // doesn't seems to care about the version number!
168                 }
169
170                 [Test]
171                 public void FromXml_NoVersion ()
172                 {
173                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
174                         SecurityElement se = ad.ToXml ();
175
176                         SecurityElement w = new SecurityElement (se.Tag);
177                         w.AddAttribute ("class", se.Attribute ("class"));
178                         ad.FromXml (w);
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (ArgumentNullException))]
183                 public void FromXml_SecurityElementNull ()
184                 {
185                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
186                         ad.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
187                 }
188
189                 [Test]
190                 public void FromXml_PolicyLevelNull ()
191                 {
192                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
193                         SecurityElement se = ad.ToXml ();
194                         ad.FromXml (se, null);
195                 }
196
197                 [Test]
198                 public void GetHashCode_ ()
199                 {
200                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
201                         ApplicationDirectoryMembershipCondition copy = (ApplicationDirectoryMembershipCondition)ad.Copy ();
202                         Assert.AreEqual (ad.GetHashCode (), copy.GetHashCode ());
203                 }
204
205                 [Test]
206                 public void ToString_ ()
207                 {
208                         ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
209                         Assert.AreEqual ("ApplicationDirectory", ad.ToString ());
210                 }
211
212                 [Test]
213                 public void ToXml ()
214                 {
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)");
222                 }
223         }
224 }