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