File: DirectoryObjectSecurityTest.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 (240 lines) | stat: -rw-r--r-- 8,112 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
// DirectoryObjectSecurityTest.cs - NUnit Test Cases for DirectoryObjectSecurity
//
// 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 DirectoryObjectSecurityTest
	{
		[Test]
		public void Defaults ()
		{
			TestSecurity security = new TestSecurity ();
			Assert.IsTrue (security.IsContainerTest);
			Assert.IsTrue (security.IsDSTest);
		}

		[Test, ExpectedExceptionAttribute (typeof (ArgumentOutOfRangeException))]
		public void ChecksAccessControlModificationRange ()
		{
			bool modifiedRet, modifiedOut;
			TestSecurity security = new TestSecurity ();

			SecurityIdentifier sid = new SecurityIdentifier ("WD");
			TestAccessRule rule = new TestAccessRule
				(sid, 1, false, InheritanceFlags.None, PropagationFlags.None,
				 Guid.Empty, Guid.Empty, AccessControlType.Allow);

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

		[Test]
		public void IgnoresResetOnAuditAndReturnsTrue ()
		{
			bool modifiedRet, modifiedOut;
			TestSecurity security = new TestSecurity ();

			SecurityIdentifier sid = new SecurityIdentifier ("WD");
			TestAuditRule rule = new TestAuditRule
				(sid, 1, false, InheritanceFlags.None, PropagationFlags.None,
				 Guid.Empty, Guid.Empty, AuditFlags.Success);

			modifiedRet = security.ModifyAuditRule (AccessControlModification.Reset,
			                           		rule, out modifiedOut);
			Assert.IsTrue (modifiedRet);
		}

		[Test, ExpectedException (typeof (ArgumentNullException))]
		public void ConstructorFailsOnNullDescriptor ()
		{
			new TestSecurity (null);
		}

		[Test]
		public void ConstructorLetsFalseDSThrough ()
		{
			CommonSecurityDescriptor descriptor = new CommonSecurityDescriptor
				(false, false, ControlFlags.None, null, null, null, null);

			TestSecurity security = new TestSecurity (descriptor);
			Assert.IsFalse (security.IsContainerTest);
			Assert.IsFalse (security.IsDSTest);
		}

		[Test]
		public void ObjectSecurityJustWrapsCommonSecurityDescriptor ()
		{
			CommonSecurityDescriptor descriptor = new CommonSecurityDescriptor
				(false, false, ControlFlags.None, null, null, null, null);

			TestSecurity security = new TestSecurity (descriptor);
			Assert.IsNull (security.GetOwner (typeof(SecurityIdentifier)));
			SecurityIdentifier sid = new SecurityIdentifier ("WD");

			descriptor.Owner = sid; // Not virtual, so the conclusion in the test's title.
			Assert.IsFalse (security.OwnerModifiedTest);
			Assert.AreSame (sid, security.GetOwner (typeof(SecurityIdentifier)));

			security.SetOwner (sid);
			Assert.IsTrue (security.OwnerModifiedTest);
			Assert.AreSame (sid, security.GetOwner (typeof(SecurityIdentifier)));
		}

		[Test, ExpectedExceptionAttribute (typeof (InvalidOperationException))]
		public void LocksAreEnforced ()
		{
			TestSecurity security = new TestSecurity ();
			bool value = security.OwnerModifiedTestWithoutLock;
		}

		[Test]
		[Category ("NotWorking")] // Mono does not have a working CustomAce implementation yet.
		public void ObjectSecurityRemovesWhatItCannotCreate ()
		{
			RawAcl acl = new RawAcl (GenericAcl.AclRevision, 1);
			acl.InsertAce (0, new CustomAce ((AceType)255, AceFlags.None, new byte[4]));

			DiscretionaryAcl dacl = new DiscretionaryAcl (true, true, acl);
			Assert.AreEqual (1, dacl.Count);

			CommonSecurityDescriptor descriptor = new CommonSecurityDescriptor
				(true, true, ControlFlags.None, null, null, null, dacl);

			TestSecurity security = new TestSecurity (descriptor);
			AuthorizationRuleCollection rules = security.GetAccessRules (true, true, typeof (SecurityIdentifier));
			Assert.AreEqual (0, rules.Count);
		}

		[Test]
		public void FactoryWithoutGuidsCalledWhenNotObjectAce ()
		{
			TestSecurity security = FactoryCallTest (false);
			Assert.IsTrue (security.access_factory_called);
		}

		[Test, ExpectedExceptionAttribute (typeof (NotImplementedException))]
		public void FactoryWithGuidsThrowsNotImplementedByDefault ()
		{
			FactoryCallTest (true);
		}

		TestSecurity FactoryCallTest (bool objectAce)
		{
			SecurityIdentifier sid = new SecurityIdentifier ("WD");
			DiscretionaryAcl dacl = new DiscretionaryAcl (true, true, 1);
			dacl.AddAccess (AccessControlType.Allow, sid, 1,
			                InheritanceFlags.None, PropagationFlags.None,
			                objectAce ? ObjectAceFlags.ObjectAceTypePresent : ObjectAceFlags.None,
			                Guid.NewGuid (), Guid.Empty);

			CommonSecurityDescriptor descriptor = new CommonSecurityDescriptor
				(true, true, ControlFlags.None, null, null, null, dacl);

			TestSecurity security = new TestSecurity (descriptor);
			security.GetAccessRules (true, true, typeof (SecurityIdentifier));
			return security;
		}

		class TestAccessRule : ObjectAccessRule
		{
			public TestAccessRule(IdentityReference identity, int accessMask, bool isInherited,
			                      InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags,
			                      Guid objectType, Guid inheritedObjectType,
			                      AccessControlType type)
				: base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags,
				       objectType, inheritedObjectType, type)
			{
			}
		}

		class TestAuditRule : ObjectAuditRule
		{
			public TestAuditRule(IdentityReference identity, int accessMask, bool isInherited,
			                     InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags,
			                     Guid objectType, Guid inheritedObjectType,
			                     AuditFlags flags)
				: base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags,
				       objectType, inheritedObjectType, flags)
			{
			}
		}

		class TestSecurity : DirectoryObjectSecurity
		{
			internal bool access_factory_called;

			public TestSecurity ()
			{
			}

			public TestSecurity (CommonSecurityDescriptor descriptor)
				: base (descriptor)
			{
			}

			public bool IsContainerTest {
				get { return IsContainer; }
			}

			public bool IsDSTest {
				get { return IsDS; }
			}

			public bool OwnerModifiedTest {
				get { ReadLock (); bool value = OwnerModified; ReadUnlock (); return value; }
				set { WriteLock (); OwnerModified = value; WriteUnlock (); }
			}

			public bool OwnerModifiedTestWithoutLock {
				get { return OwnerModified; }
			}

			public override AccessRule AccessRuleFactory (IdentityReference identityReference,
			                                              int accessMask, bool isInherited,
			                                              InheritanceFlags inheritanceFlags,
			                                              PropagationFlags propagationFlags,
			                                              AccessControlType type)
			{
				access_factory_called = true;
				return new TestAccessRule (identityReference, accessMask,
				                           isInherited, inheritanceFlags, propagationFlags,
				                           Guid.Empty, Guid.Empty, type);
			}

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

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

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

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