File: ObjectSecurityTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (276 lines) | stat: -rw-r--r-- 9,586 bytes parent folder | download | duplicates (9)
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
// ObjectSecurityTest.cs - NUnit Test Cases for ObjectSecurity
//
// Authors:
//	James Bellinger  <jfb@zer7.com>
//
// Copyright (C) 2012 James Bellinger

using System;
using System.Collections.Generic;
using System.Security.AccessControl;
using System.Security.Principal;
using NUnit.Framework;

namespace MonoTests.System.Security.AccessControl
{
	[TestFixture]
	public class ObjectSecurityTest
	{
		[Test]
		public void Defaults ()
		{
			TestSecurity security = new TestSecurity ();
			Assert.IsTrue (security.AreAccessRulesCanonical);
			Assert.IsTrue (security.AreAuditRulesCanonical);
			Assert.IsFalse (security.AreAccessRulesProtected);
			Assert.IsFalse (security.AreAuditRulesProtected);
			Assert.IsNull (security.GetGroup (typeof (SecurityIdentifier)));
			Assert.IsNull (security.GetOwner (typeof (SecurityIdentifier)));
		}

		[Test]
		public void DefaultsForSddlAndBinary ()
		{
			TestSecurity security = new TestSecurity ();
			Assert.AreEqual ("D:", security.GetSecurityDescriptorSddlForm (AccessControlSections.All));
			Assert.AreEqual (28, security.GetSecurityDescriptorBinaryForm ().Length);
		}

		[Test]
		public void SetSddlForm ()
		{
			TestSecurity security = new TestSecurity ();

			SecurityIdentifier groupSid = new SecurityIdentifier ("WD");
			SecurityIdentifier userSid = new SecurityIdentifier ("SY");

			security.SetGroup (groupSid);
			security.SetOwner (userSid);
			Assert.AreEqual ("G:WD", security.GetSecurityDescriptorSddlForm (AccessControlSections.Group));
			Assert.AreEqual ("O:SY", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
			security.SetSecurityDescriptorSddlForm ("O:BG", AccessControlSections.Owner);
			Assert.AreEqual ("O:BG", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
			Assert.AreEqual (new SecurityIdentifier ("BG"), security.GetOwner (typeof (SecurityIdentifier)));
		}

		[Test]
		public void SetSddlFormAllowsFlags ()
		{
			TestSecurity security = new TestSecurity ();
			security.SetSecurityDescriptorSddlForm ("G:BA", AccessControlSections.Group | AccessControlSections.Owner);
			Assert.AreEqual ("", security.GetSecurityDescriptorSddlForm (AccessControlSections.Owner));
			Assert.AreEqual ("G:BA", security.GetSecurityDescriptorSddlForm (AccessControlSections.Group));
		}

		[Test, ExpectedException (typeof (ArgumentNullException))]
		public void SetGroupThrowsOnNull ()
		{
			TestSecurity security = new TestSecurity ();
			security.SetGroup (null);
		}

		[Test, ExpectedException (typeof (ArgumentNullException))]
		public void SetOwnerThrowsOnNull ()
		{
			TestSecurity security = new TestSecurity ();
			security.SetOwner (null);
		}

		[Test, ExpectedException (typeof (ArgumentNullException))]
		public void PurgeThrowsOnNull ()
		{
			TestSecurity security = new TestSecurity ();
			security.PurgeAccessRules (null);
		}

		[Test]
		public void AllTypesAcceptedOnGetGroupOwnerUntilTheyAreSet ()
		{
			TestSecurity security = new TestSecurity ();
			Assert.IsNull (security.GetGroup (typeof (void)));
			Assert.IsNull (security.GetOwner (typeof (int)));

			SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
			security.SetOwner (everyoneSid);

			bool throwsOnInt = false;
			try { security.GetOwner (typeof (int)); } catch (ArgumentException) { throwsOnInt = true; }
			Assert.IsTrue (throwsOnInt);

			bool throwsOnSuperclass = false;
			try { security.GetOwner (typeof (IdentityReference)); } catch (ArgumentException) { throwsOnSuperclass = true; }
			Assert.IsTrue (throwsOnSuperclass);

			Assert.IsNull (security.GetGroup (typeof (void)));
			Assert.IsInstanceOfType (typeof (SecurityIdentifier), security.GetOwner (typeof (SecurityIdentifier)));
		}

		[Test]
		public void ModifyAccessRuleAllowsDerivedTypeAndCallsModifyAccessButNothingChanges ()
		{
			bool modifiedRet, modifiedOut;
			SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
			TestSecurity security = new TestSecurity ();

			DerivedAccessRule rule = new DerivedAccessRule (everyoneSid, TestRights.One, AccessControlType.Allow);

			modifiedRet = security.ModifyAccessRule (AccessControlModification.Add, rule, out modifiedOut);
			Assert.AreEqual (modifiedRet, modifiedOut);
			Assert.IsTrue (modifiedRet);

			Assert.IsTrue (security.modify_access_called);
			Assert.AreEqual ("D:", security.GetSecurityDescriptorSddlForm (AccessControlSections.All));

			// (1) There is no external abstract/virtual 'get collection',
			// (2) The overrides in this test call this base class, which does not change it, and
			// (3) There are methods based on the collection value such as GetSecurityDescriptorSddlForm.
			// Conclusion: Collection is internal and manipulated by derived classes.
		}

		[Test, ExpectedException (typeof (ArgumentException))]
		public void ModifyAccessRuleThrowsOnWrongType ()
		{
			bool modified;
			SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
			TestSecurity security = new TestSecurity ();

			FileSystemAccessRule rule = new FileSystemAccessRule
				(everyoneSid, FileSystemRights.FullControl, AccessControlType.Allow);

			security.ModifyAccessRule (AccessControlModification.Add, rule, out modified);
		}

		[Test]
		public void Reset ()
		{
			bool modifiedRet, modifiedOut;
			SecurityIdentifier everyoneSid = new SecurityIdentifier ("WD");
			TestSecurity security = new TestSecurity ();

			TestAccessRule rule = new TestAccessRule
				(everyoneSid, TestRights.One, AccessControlType.Allow);

			modifiedRet = security.ModifyAccessRule (AccessControlModification.Reset, rule, out modifiedOut);
		}

		[Test]
		public void Protection ()
		{
			TestSecurity security = new TestSecurity ();

			security.SetAccessRuleProtection (true, true);
			Assert.IsTrue (security.AreAccessRulesProtected);
			Assert.IsFalse (security.AreAuditRulesProtected);

			security.SetAuditRuleProtection (true, false);
			Assert.IsTrue (security.AreAccessRulesProtected);
			Assert.IsTrue (security.AreAuditRulesProtected);

			security.SetAccessRuleProtection (false, false);
			Assert.IsFalse (security.AreAccessRulesProtected);
			Assert.IsTrue (security.AreAuditRulesProtected);

			security.SetAuditRuleProtection (false, true);
			Assert.IsFalse (security.AreAccessRulesProtected);
			Assert.IsFalse (security.AreAuditRulesProtected);
		}

		enum TestRights
		{
			One = 1
		}

		class DerivedAccessRule : TestAccessRule
		{
			public DerivedAccessRule (IdentityReference identity, TestRights rights, AccessControlType type)
				: base (identity, rights, type)
			{
			}
		}

		class TestAccessRule : AccessRule
		{
			public TestAccessRule (IdentityReference identity, TestRights rights, AccessControlType type)
				: this (identity, rights, false, InheritanceFlags.None, PropagationFlags.None, type)
			{
			}

			public TestAccessRule (IdentityReference identity,
			                       TestRights rights, bool isInherited,
			                       InheritanceFlags inheritanceFlags,
			                       PropagationFlags propagationFlags,
			                       AccessControlType type)
				: base (identity, (int)rights, isInherited, inheritanceFlags, propagationFlags, type)
			{
			}
		}

		class TestAuditRule : AuditRule
		{
			public TestAuditRule (IdentityReference identity,
				              TestRights rights, bool isInherited,
				              InheritanceFlags inheritanceFlags,
			                      PropagationFlags propagationFlags,
			                      AuditFlags flags)
				: base (identity, (int)rights, isInherited, inheritanceFlags, propagationFlags, flags)
			{
			}
		}

		class TestSecurity : ObjectSecurity
		{
			internal bool modify_access_called;

			public TestSecurity () : base (false, false)
			{
			}

			public override AccessRule AccessRuleFactory (IdentityReference identityReference,
			                                              int accessMask, bool isInherited,
			                                              InheritanceFlags inheritanceFlags,
			                                              PropagationFlags propagationFlags,
			                                              AccessControlType type)
			{
				return new TestAccessRule (identityReference, (TestRights)accessMask, isInherited,
				                           inheritanceFlags, propagationFlags, type);
			}

			public override AuditRule AuditRuleFactory (IdentityReference identityReference,
				                                    int accessMask, bool isInherited,
				                                    InheritanceFlags inheritanceFlags,
			                                            PropagationFlags propagationFlags,
			                                            AuditFlags flags)
			{
				return new TestAuditRule (identityReference, (TestRights)accessMask, isInherited,
				                          inheritanceFlags, propagationFlags, flags);
			}

			protected override bool ModifyAccess (AccessControlModification modification, 
			                                      AccessRule rule, out bool modified)
			{
				modify_access_called = true;
				modified = true; return modified;
			}

			protected override bool ModifyAudit (AccessControlModification modification,
			                                     AuditRule rule, out bool modified)
			{
				modified = false; return modified;
			}

			public override Type AccessRightType {
				get { return typeof (TestRights); }
			}

			public override Type AccessRuleType {
				get { return typeof (TestAccessRule); }
			}

			public override Type AuditRuleType {
				get { return typeof (TestAuditRule); }
			}
		}
	}
}