[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / ApplicationMembershipConditionTest.cs
1 //
2 // ApplicationMembershipConditionTest.cs -
3 //      NUnit Test Cases for ApplicationMembershipCondition
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
31 using NUnit.Framework;
32 using System;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Policy;
36
37 namespace MonoTests.System.Security.Policy {
38
39         [TestFixture]
40         public class ApplicationMembershipConditionTest {
41
42                 [Test]
43                 public void Constructor ()
44                 {
45                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
46                         Assert.IsNotNull (app);
47                 }
48
49                 [Test]
50                 public void Check ()
51                 {
52                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
53                         Evidence e = null;
54                         Assert.IsFalse (app.Check (e), "Check (null)");
55                         e = new Evidence ();
56                         Assert.IsFalse (app.Check (e), "Check (empty)");
57                         e.AddHost (new Zone (SecurityZone.MyComputer));
58                         Assert.IsFalse (app.Check (e), "Check (zone)");
59
60                         // TODO - more (non failing ;) tests
61                 }
62
63                 [Test]
64                 public void Copy ()
65                 {
66                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
67                         ApplicationMembershipCondition copy = (ApplicationMembershipCondition)app.Copy ();
68                         Assert.AreEqual (app, copy, "Equals");
69                         Assert.IsFalse (Object.ReferenceEquals (app, copy), "ReferenceEquals");
70                 }
71
72                 [Test]
73                 public void Equals ()
74                 {
75                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
76                         Assert.IsFalse (app.Equals (null), "Equals(null)");
77                         ApplicationMembershipCondition g2 = new ApplicationMembershipCondition ();
78                         Assert.IsTrue (app.Equals (g2), "Equals(g2)");
79                         Assert.IsTrue (g2.Equals (app), "Equals(app)");
80                         Assert.IsFalse (app.Equals (new object ()), "Equals (object)");
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentNullException))]
85                 public void FromXml_Null ()
86                 {
87                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
88                         app.FromXml (null);
89                 }
90
91                 [Test]
92                 public void FromXml ()
93                 {
94                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
95                         SecurityElement se = app.ToXml ();
96                         app.FromXml (se);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentException))]
101                 public void FromXml_InvalidTag ()
102                 {
103                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
104                         SecurityElement se = app.ToXml ();
105                         se.Tag = "IMonoship";
106                         app.FromXml (se);
107                 }
108
109                 [Test]
110                 [ExpectedException (typeof (ArgumentException))]
111                 public void FromXml_WrongTagCase ()
112                 {
113                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
114                         SecurityElement se = app.ToXml ();
115                         se.Tag = "IMEMBERSHIPCONDITION"; // insteapp of IMembershipCondition
116                         app.FromXml (se);
117                 }
118
119                 [Test]
120                 public void FromXml_InvalidClass ()
121                 {
122                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
123                         SecurityElement se = app.ToXml ();
124                         se.Attributes ["class"] = "Hello world";
125                         app.FromXml (se);
126                 }
127
128                 [Test]
129                 public void FromXml_NoClass ()
130                 {
131                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
132                         SecurityElement se = app.ToXml ();
133
134                         SecurityElement w = new SecurityElement (se.Tag);
135                         w.AddAttribute ("version", se.Attribute ("version"));
136                         app.FromXml (w);
137                         // doesn't even care of the class attribute presence
138                 }
139
140                 [Test]
141                 public void FromXml_InvalidVersion ()
142                 {
143                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
144                         SecurityElement se = app.ToXml ();
145
146                         SecurityElement w = new SecurityElement (se.Tag);
147                         w.AddAttribute ("class", se.Attribute ("class"));
148                         w.AddAttribute ("version", "2");
149                         app.FromXml (w);
150                         // doesn't seems to care about the version number!
151                 }
152
153                 [Test]
154                 public void FromXml_NoVersion ()
155                 {
156                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
157                         SecurityElement se = app.ToXml ();
158
159                         SecurityElement w = new SecurityElement (se.Tag);
160                         w.AddAttribute ("class", se.Attribute ("class"));
161                         app.FromXml (w);
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (ArgumentNullException))]
166                 public void FromXml_SecurityElementNull ()
167                 {
168                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
169                         app.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
170                 }
171
172                 [Test]
173                 public void FromXml_NonBooleanLookAtDir ()
174                 {
175                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
176                         SecurityElement se = app.ToXml ();
177
178                         SecurityElement w = new SecurityElement (se.Tag);
179                         w.AddAttribute ("class", se.Attribute ("class"));
180                         w.AddAttribute ("version", se.Attribute ("version"));
181                         w.AddAttribute ("LookAtDir", "Maybe"); // not (generally) a boolean ;)
182
183                         ApplicationMembershipCondition app2 = new ApplicationMembershipCondition ();
184                         app2.FromXml (w);
185
186                         se = app2.ToXml ();
187                         Assert.IsNull (se.Attribute ("LookAtDir"), "LookAtDir");
188                         // LookAtDir isn't part of the Equals computation
189                         Assert.IsTrue (app2.Equals (app), "Equals-1");
190                         Assert.IsTrue (app.Equals (app2), "Equals-2");
191
192                         ApplicationMembershipCondition app3 = (ApplicationMembershipCondition)app2.Copy ();
193                         se = app3.ToXml ();
194                         // LookAtDir isn't copied either
195                         Assert.AreEqual ("true", se.Attribute ("LookAtDir"), "Copy-LookAtDir");
196                 }
197
198                 [Test]
199                 public void FromXml_PolicyLevelNull ()
200                 {
201                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
202                         SecurityElement se = app.ToXml ();
203                         app.FromXml (se, null);
204                 }
205
206                 [Test]
207                 public void GetHashCode_ ()
208                 {
209                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
210                         Assert.AreEqual (-1, app.GetHashCode ());
211                         ApplicationMembershipCondition copy = (ApplicationMembershipCondition)app.Copy ();
212                         Assert.AreEqual (app.GetHashCode (), copy.GetHashCode ());
213                 }
214
215                 [Test]
216                 public void ToString_ ()
217                 {
218                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
219                         Assert.AreEqual ("Application", app.ToString ());
220                 }
221
222                 [Test]
223                 public void ToXml ()
224                 {
225                         ApplicationMembershipCondition app = new ApplicationMembershipCondition ();
226                         SecurityElement se = app.ToXml ();
227                         Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
228                         Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.ApplicationMembershipCondition"), "class");
229                         Assert.AreEqual ("1", se.Attribute ("version"), "version");
230                         Assert.AreEqual ("true", se.Attribute ("LookAtDir"), "LookAtDir");
231                         Assert.AreEqual (se.ToString (), app.ToXml (null).ToString (), "ToXml(null)");
232                         Assert.AreEqual (se.ToString (), app.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");
233                 }
234         }
235 }
236