1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
//
// ZoneIdentityPermissionTest.cs - NUnit Test Cases for ZoneIdentityPermission
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Security;
using System.Security.Permissions;
namespace MonoTests.System.Security.Permissions {
[TestFixture]
public class ZoneIdentityPermissionTest {
[Test]
public void PermissionStateNone ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
Assert.AreEqual (SecurityZone.NoZone, zip.SecurityZone);
}
#if NET_2_0
[Test]
[Category ("NotWorking")]
public void PermissionStateUnrestricted ()
{
// In 2.0 Unrestricted are permitted for identity permissions
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.Unrestricted);
Assert.AreEqual (SecurityZone.NoZone, zip.SecurityZone);
SecurityElement se = zip.ToXml ();
Assert.AreEqual (5, se.Children.Count, "Count");
// and they aren't equals to None
Assert.IsFalse (zip.Equals (new ZoneIdentityPermission (PermissionState.None)));
}
#else
[Test]
[ExpectedException (typeof (ArgumentException))]
public void PermissionStateUnrestricted ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.Unrestricted);
}
#endif
[Test]
[ExpectedException (typeof (ArgumentException))]
public void PermissionStateInvalid ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission ((PermissionState)2);
}
private bool Same (ZoneIdentityPermission zip1, ZoneIdentityPermission zip2)
{
#if NET_2_0
return zip1.Equals (zip2);
#else
return (zip1.SecurityZone == zip2.SecurityZone);
#endif
}
private ZoneIdentityPermission BasicTestZone (SecurityZone zone, bool special)
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (zone);
Assert.AreEqual (zone, zip.SecurityZone, "SecurityZone");
ZoneIdentityPermission copy = (ZoneIdentityPermission) zip.Copy ();
Assert.IsTrue (Same (zip, copy), "Equals-Copy");
Assert.IsTrue (zip.IsSubsetOf (copy), "IsSubset-1");
Assert.IsTrue (copy.IsSubsetOf (zip), "IsSubset-2");
if (special) {
Assert.IsFalse (zip.IsSubsetOf (null), "IsSubset-Null");
}
IPermission intersect = zip.Intersect (copy);
if (special) {
Assert.IsTrue (intersect.IsSubsetOf (zip), "IsSubset-3");
Assert.IsFalse (Object.ReferenceEquals (zip, intersect), "!ReferenceEquals1");
Assert.IsTrue (intersect.IsSubsetOf (copy), "IsSubset-4");
Assert.IsFalse (Object.ReferenceEquals (copy, intersect), "!ReferenceEquals2");
}
Assert.IsNull (zip.Intersect (null), "Intersect with null");
intersect = zip.Intersect (new ZoneIdentityPermission (PermissionState.None));
Assert.IsNull (intersect, "Intersect with PS.None");
// note: can't be tested with PermissionState.Unrestricted
// XML roundtrip
SecurityElement se = zip.ToXml ();
copy.FromXml (se);
Assert.IsTrue (Same (zip, copy), "Equals-Xml");
return zip;
}
[Test]
public void SecurityZone_Internet ()
{
BasicTestZone (SecurityZone.Internet, true);
}
[Test]
public void SecurityZone_Intranet ()
{
BasicTestZone (SecurityZone.Intranet, true);
}
[Test]
public void SecurityZone_MyComputer ()
{
BasicTestZone (SecurityZone.MyComputer, true);
}
[Test]
public void SecurityZone_NoZone ()
{
ZoneIdentityPermission zip = BasicTestZone (SecurityZone.NoZone, false);
Assert.IsNull (zip.ToXml ().Attribute ("Zone"), "Zone Attribute");
Assert.IsTrue (zip.IsSubsetOf (null), "IsSubset-Null");
IPermission intersect = zip.Intersect (zip);
Assert.IsNull (intersect, "Intersect with No Zone");
// NoZone is special as it is a subset of all zones
ZoneIdentityPermission ss = new ZoneIdentityPermission (SecurityZone.Internet);
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Internet");
ss.SecurityZone = SecurityZone.Intranet;
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Intranet");
ss.SecurityZone = SecurityZone.MyComputer;
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-MyComputer");
ss.SecurityZone = SecurityZone.NoZone;
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-NoZone");
ss.SecurityZone = SecurityZone.Trusted;
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Trusted");
ss.SecurityZone = SecurityZone.Untrusted;
Assert.IsTrue (zip.IsSubsetOf (ss), "IsSubset-Untrusted");
}
[Test]
public void SecurityZone_Trusted ()
{
BasicTestZone (SecurityZone.Trusted, true);
}
[Test]
public void SecurityZone_Untrusted ()
{
BasicTestZone (SecurityZone.Untrusted, true);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void SecurityZone_Invalid ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission ((SecurityZone)128);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Intersect_DifferentPermissions ()
{
ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
SecurityPermission b = new SecurityPermission (PermissionState.None);
a.Intersect (b);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void IsSubsetOf_DifferentPermissions ()
{
ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
SecurityPermission b = new SecurityPermission (PermissionState.None);
a.IsSubsetOf (b);
}
[Test]
public void Union ()
{
ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
ZoneIdentityPermission z = (ZoneIdentityPermission) a.Union (null);
Assert.IsTrue (Same (a, z), "Trusted+null");
Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals1");
z = (ZoneIdentityPermission) a.Union (new ZoneIdentityPermission (PermissionState.None));
Assert.IsTrue (Same (a, z), "Trusted+PS.None");
Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals2");
// note: can't be tested with PermissionState.Unrestricted
ZoneIdentityPermission n = new ZoneIdentityPermission (SecurityZone.NoZone);
z = (ZoneIdentityPermission) a.Union (n);
Assert.IsTrue (Same (a, z), "Trusted+NoZone");
Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals3");
z = (ZoneIdentityPermission) n.Union (a);
Assert.IsTrue (Same (a, z), "NoZone+Trusted");
Assert.IsFalse (Object.ReferenceEquals (a, z), "!ReferenceEquals4");
}
#if NET_2_0
[Category ("NotWorking")]
#endif
[Test]
public void Union_DifferentIdentities ()
{
ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
ZoneIdentityPermission b = new ZoneIdentityPermission (SecurityZone.Untrusted);
IPermission result = a.Union (b);
#if NET_2_0
Assert.IsNotNull (result, "Union");
// new XML format is used to contain more than one site
SecurityElement se = result.ToXml ();
Assert.AreEqual (2, se.Children.Count, "Childs");
Assert.AreEqual (a.SecurityZone.ToString (), (se.Children [0] as SecurityElement).Attribute ("Zone"), "Zone#1");
Assert.AreEqual (b.SecurityZone.ToString (), (se.Children [1] as SecurityElement).Attribute ("Zone"), "Zone#2");
// strangely it is still versioned as 'version="1"'.
Assert.AreEqual ("1", se.Attribute ("version"), "Version");
#else
Assert.IsNull (result);
#endif
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Union_DifferentPermissions ()
{
ZoneIdentityPermission a = new ZoneIdentityPermission (SecurityZone.Trusted);
SecurityPermission b = new SecurityPermission (PermissionState.None);
a.Union (b);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void FromXml_Null ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
zip.FromXml (null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void FromXml_WrongTag ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
se.Tag = "IMono"; // instead of IPermission
zip.FromXml (se);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void FromXml_WrongTagCase ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
se.Tag = "IPERMISSION"; // instead of IPermission
zip.FromXml (se);
}
[Test]
public void FromXml_WrongClass ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
SecurityElement w = new SecurityElement (se.Tag);
w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
w.AddAttribute ("version", se.Attribute ("version"));
zip.FromXml (w);
// doesn't care of the class name at that stage
// anyway the class has already be created so...
}
[Test]
public void FromXml_NoClass ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
SecurityElement w = new SecurityElement (se.Tag);
w.AddAttribute ("version", se.Attribute ("version"));
zip.FromXml (w);
// doesn't even care of the class attribute presence
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void FromXml_WrongVersion ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
SecurityElement w = new SecurityElement (se.Tag);
w.AddAttribute ("class", se.Attribute ("class"));
w.AddAttribute ("version", "2");
zip.FromXml (w);
}
[Test]
public void FromXml_NoVersion ()
{
ZoneIdentityPermission zip = new ZoneIdentityPermission (PermissionState.None);
SecurityElement se = zip.ToXml ();
SecurityElement w = new SecurityElement (se.Tag);
w.AddAttribute ("class", se.Attribute ("class"));
zip.FromXml (w);
}
}
}
|