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]
Constructor()40 		public void Constructor ()
41 		{
42 			AllMembershipCondition all = new AllMembershipCondition ();
43 			Assert.IsNotNull (all);
44 		}
45 
46 		[Test]
Check()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]
Copy()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]
Equals()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))]
FromXml_Null()86 		public void FromXml_Null ()
87 		{
88 			AllMembershipCondition all = new AllMembershipCondition ();
89 			all.FromXml (null);
90 		}
91 
92 		[Test]
FromXml()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))]
FromXml_InvalidTag()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))]
FromXml_WrongTagCase()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]
FromXml_InvalidClass()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]
FromXml_NoClass()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]
FromXml_InvalidVersion()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]
FromXml_NoVersion()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 #if MOBILE
167 		[Ignore]
168 #endif
169 		[ExpectedException (typeof (ArgumentNullException))]
FromXml_SecurityElementNull()170 		public void FromXml_SecurityElementNull ()
171 		{
172 			AllMembershipCondition all = new AllMembershipCondition ();
173 			all.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
174 		}
175 
176 		[Test]
FromXml_PolicyLevelNull()177 		public void FromXml_PolicyLevelNull ()
178 		{
179 			AllMembershipCondition all = new AllMembershipCondition ();
180 			SecurityElement se = all.ToXml ();
181 			all.FromXml (se, null);
182 		}
183 
184 		[Test]
GetHashCode_()185 		public void GetHashCode_ ()
186 		{
187 			AllMembershipCondition all = new AllMembershipCondition ();
188 			AllMembershipCondition copy = (AllMembershipCondition)all.Copy ();
189 			Assert.AreEqual (all.GetHashCode (), copy.GetHashCode ());
190 		}
191 
192 		[Test]
ToString_()193 		public void ToString_ ()
194 		{
195 			AllMembershipCondition all = new AllMembershipCondition ();
196 			Assert.AreEqual ("All code", all.ToString ());
197 		}
198 
199 		[Test]
200 #if MOBILE
201 		[Ignore]
202 #endif
ToXml()203 		public void ToXml ()
204 		{
205 			AllMembershipCondition all = new AllMembershipCondition ();
206 			SecurityElement se = all.ToXml ();
207 			Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
208 			Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.AllMembershipCondition"), "class");
209 			Assert.AreEqual ("1", se.Attribute ("version"), "version");
210 			Assert.AreEqual (se.ToString (), all.ToXml (null).ToString (), "ToXml(null)");
211 			Assert.AreEqual (se.ToString (), all.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");
212 		}
213 	}
214 }
215