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