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