2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Security.Policy / GacMembershipConditionTest.cs
1 //
2 // GacMembershipConditionTest.cs - NUnit Test Cases for GacMembershipCondition
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 #if NET_2_0
30
31 using NUnit.Framework;
32 using System;
33 using System.Security;
34 using System.Security.Policy;
35
36 namespace MonoTests.System.Security.Policy {
37
38         [TestFixture]
39         public class GacMembershipConditionTest {
40
41                 [Test]
42                 public void Constructor ()
43                 {
44                         GacMembershipCondition gac = new GacMembershipCondition ();
45                         Assert.IsNotNull (gac);
46                 }
47
48                 [Test]
49                 public void Check ()
50                 {
51                         GacMembershipCondition gac = new GacMembershipCondition ();
52                         Evidence e = null;
53                         Assert.IsFalse (gac.Check (e), "Check (null)");
54                         e = new Evidence ();
55                         Assert.IsFalse (gac.Check (e), "Check (empty)");
56                         e.AddHost (new Zone (SecurityZone.MyComputer));
57                         Assert.IsFalse (gac.Check (e), "Check (zone)");
58                         Gac g = new Gac ();
59                         e.AddAssembly (g);
60                         Assert.IsFalse (gac.Check (e), "Check (gac-assembly)");
61                         e.AddHost (g);
62                         Assert.IsTrue (gac.Check (e), "Check (gac-host)");
63                 }
64
65                 [Test]
66                 public void Copy ()
67                 {
68                         GacMembershipCondition gac = new GacMembershipCondition ();
69                         GacMembershipCondition copy = (GacMembershipCondition) gac.Copy ();
70                         Assert.AreEqual (gac, copy, "Equals");
71                         Assert.IsFalse (Object.ReferenceEquals (gac, copy), "ReferenceEquals");
72                 }
73
74                 [Test]
75                 public void Equals ()
76                 {
77                         GacMembershipCondition gac = new GacMembershipCondition ();
78                         Assert.IsFalse (gac.Equals (null), "Equals(null)");
79                         GacMembershipCondition g2 = new GacMembershipCondition ();
80                         Assert.IsTrue (gac.Equals (g2), "Equals(g2)");
81                         Assert.IsTrue (g2.Equals (gac), "Equals(gac)");
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentNullException))]
86                 public void FromXml_Null () 
87                 {
88                         GacMembershipCondition gac = new GacMembershipCondition ();
89                         gac.FromXml (null);
90                 }
91
92                 [Test]
93                 public void FromXml ()
94                 {
95                         GacMembershipCondition gac = new GacMembershipCondition ();
96                         SecurityElement se = gac.ToXml ();
97                         gac.FromXml (se);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentException))]
102                 public void FromXml_InvalidTag ()
103                 {
104                         GacMembershipCondition gac = new GacMembershipCondition ();
105                         SecurityElement se = gac.ToXml ();
106                         se.Tag = "IMonoship";
107                         gac.FromXml (se);
108                 }
109
110                 [Test]
111                 public void FromXml_InvalidClass ()
112                 {
113                         GacMembershipCondition gac = new GacMembershipCondition ();
114                         SecurityElement se = gac.ToXml ();
115                         se.Attributes ["class"] = "Hello world";
116                         gac.FromXml (se);
117                 }
118
119                 [Test]
120                 public void FromXml_NoClass ()
121                 {
122                         GacMembershipCondition gac = new GacMembershipCondition ();
123                         SecurityElement se = gac.ToXml ();
124
125                         SecurityElement w = new SecurityElement (se.Tag);
126                         w.AddAttribute ("version", se.Attribute ("version"));
127                         gac.FromXml (w);
128                         // doesn't even care of the class attribute presence
129                 }
130
131                 [Test]
132                 public void FromXml_InvalidVersion ()
133                 {
134                         GacMembershipCondition gac = new GacMembershipCondition ();
135                         SecurityElement se = gac.ToXml ();
136
137                         SecurityElement w = new SecurityElement (se.Tag);
138                         w.AddAttribute ("class", se.Attribute ("class"));
139                         w.AddAttribute ("version", "2");
140                         gac.FromXml (w);
141                 }
142
143                 [Test]
144                 public void FromXml_NoVersion ()
145                 {
146                         GacMembershipCondition gac = new GacMembershipCondition ();
147                         SecurityElement se = gac.ToXml ();
148
149                         SecurityElement w = new SecurityElement (se.Tag);
150                         w.AddAttribute ("class", se.Attribute ("class"));
151                         gac.FromXml (w);
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (ArgumentNullException))]
156                 public void FromXml_SecurityElementNull ()
157                 {
158                         GacMembershipCondition gac = new GacMembershipCondition ();
159                         gac.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
160                 }
161
162                 [Test]
163                 public void FromXml_PolicyLevelNull ()
164                 {
165                         GacMembershipCondition gac = new GacMembershipCondition ();
166                         SecurityElement se = gac.ToXml ();
167                         gac.FromXml (se, null);
168                 }
169
170                 [Test]
171                 public void GetHashCode_ ()
172                 {
173                         GacMembershipCondition gac = new GacMembershipCondition ();
174                         Assert.AreEqual (0, gac.GetHashCode ());
175                 }
176
177                 [Test]
178                 public void ToString_ ()
179                 {
180                         GacMembershipCondition gac = new GacMembershipCondition ();
181                         Assert.AreEqual ("GAC", gac.ToString ());
182                 }
183
184                 [Test]
185                 public void ToXml ()
186                 {
187                         GacMembershipCondition gac = new GacMembershipCondition ();
188                         SecurityElement se = gac.ToXml ();
189                         Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
190                         Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.GacMembershipCondition"), "class");
191                         Assert.AreEqual ("1", se.Attribute ("version"), "version");
192                         Assert.AreEqual (se.ToString (), gac.ToXml (null).ToString (), "ToXml(null)");
193                         Assert.AreEqual (se.ToString (), gac.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");
194                 }
195         }
196 }
197
198 #endif