Ignore the FileSecurityTest.EveryoneMayNotBeOwner test when running as
[mono.git] / mcs / class / corlib / Test / System.Security.AccessControl / FileSecurityTest.cs
1 // FileSecurityTest.cs - NUnit Test Cases for FileSecurity
2 //
3 // Authors:
4 //      James Bellinger (jfb@zer7.com)
5
6 using System;
7 using System.IO;
8 using System.Security.AccessControl;
9 using System.Security.Principal;
10 using NUnit.Framework;
11
12 namespace MonoTests.System.Security.AccessControl
13 {
14         [TestFixture]
15         public class FileSecurityTest
16         {
17                 [Test]
18                 public void ChangeGroupToEveryone ()
19                 {
20                         FileSecurity security;
21                         if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
22                                 Assert.Ignore ();
23                         }
24
25                         string path = Path.GetTempFileName ();
26                         try {
27                                 SecurityIdentifier worldSid = new SecurityIdentifier ("WD");
28
29                                 security = File.GetAccessControl (path);
30                                 security.SetGroup (worldSid);
31                                 File.SetAccessControl (path, security);
32
33                                 security = File.GetAccessControl (path);
34                                 Assert.AreEqual (worldSid, security.GetGroup (typeof(SecurityIdentifier)));
35                         } finally {
36                                 File.Delete (path);
37                         }
38                 }
39
40                 [Test]
41                 public void ChangeAccessRules ()
42                 {
43                         FileSecurity security;
44                         if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
45                                 Assert.Ignore ();
46                         }
47
48                         string path = Path.GetTempFileName ();
49                         try {
50                                 // Add 'Everyone' to the access list.
51                                 SecurityIdentifier worldSid = new SecurityIdentifier ("WD");
52
53                                 security = File.GetAccessControl (path);
54                                 FileSystemAccessRule rule = new FileSystemAccessRule (worldSid,
55                                                                                       FileSystemRights.FullControl,
56                                                                                       AccessControlType.Allow);
57                                 security.AddAccessRule (rule);
58                                 File.SetAccessControl (path, security);
59
60                                 // Make sure 'Everyone' is *on* the access list.
61                                 // Let's use the SafeHandle overload to check it.
62                                 AuthorizationRuleCollection rules;
63                                 using (FileStream file = File.Open (path, FileMode.Open, FileAccess.Read)) {
64                                         security = file.GetAccessControl ();
65                                         rules = security.GetAccessRules (true, false, typeof (SecurityIdentifier));
66
67                                         Assert.AreEqual (1, rules.Count);
68                                         Assert.AreEqual (worldSid, rules[0].IdentityReference);
69                                         Assert.AreEqual (InheritanceFlags.None, rules[0].InheritanceFlags);
70                                         Assert.AreEqual (PropagationFlags.None, rules[0].PropagationFlags);
71                                         Assert.IsFalse (rules[0].IsInherited);
72                                 }
73
74                                 // Remove 'Everyone' from the access list.
75                                 security.RemoveAccessRuleSpecific (rule);
76                                 File.SetAccessControl (path, security);
77
78                                 // Make sure our non-inherited access control list is now empty.
79                                 security = File.GetAccessControl (path);
80                                 rules = security.GetAccessRules (true, false, typeof (SecurityIdentifier));
81
82                                 Assert.AreEqual (0, rules.Count);
83                         } finally {
84                                 File.Delete (path);
85                         }
86                 }
87
88                 [Test, ExpectedException (typeof (InvalidOperationException))]
89                 public void EveryoneMayNotBeOwner ()
90                 {
91                         FileSecurity security;
92                         if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
93                                 Assert.Ignore ();
94                         }
95
96                         string path = Path.GetTempFileName ();
97                         try {
98                                 security = File.GetAccessControl (path);
99                                 security.SetOwner (new SecurityIdentifier ("WD"));
100                                 File.SetAccessControl (path, security);
101                                 // If we don't get an InvalidOperationException it could be that we are running
102                                 // with administrator privileges. Don't fail the test if that is the case.
103                                 WindowsIdentity identity = WindowsIdentity.GetCurrent ();
104                                 WindowsPrincipal principal = new WindowsPrincipal (identity);
105                                 if (principal.IsInRole (WindowsBuiltInRole.Administrator)) {
106                                         Assert.Ignore ("Running as Administrator");
107                                 }
108                         } finally {
109                                 File.Delete (path);
110                         }
111                 }
112
113         }
114 }
115
116