DirectorySecurityTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // DirectorySecurityTest.cs - NUnit Test Cases for DirectorySecurity
  2. //
  3. // Authors:
  4. // James Bellinger ([email protected])
  5. using System;
  6. using System.IO;
  7. using System.Security.AccessControl;
  8. using System.Security.Principal;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Security.AccessControl
  11. {
  12. [TestFixture]
  13. public class DirectorySecurityTest
  14. {
  15. [Test]
  16. public void InheritedPermissions ()
  17. {
  18. AuthorizationRuleCollection rules;
  19. DirectorySecurity dirSecurity; FileSecurity fileSecurity;
  20. SecurityIdentifier usersSid = new SecurityIdentifier ("BU");
  21. SecurityIdentifier worldSid = new SecurityIdentifier ("WD");
  22. FileSystemAccessRule worldDirFullControl = new FileSystemAccessRule
  23. (worldSid, FileSystemRights.FullControl,
  24. InheritanceFlags.ObjectInherit, PropagationFlags.None,
  25. AccessControlType.Allow);
  26. if (PlatformID.Win32NT != Environment.OSVersion.Platform) {
  27. Assert.Ignore (); return;
  28. }
  29. string dirpath = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ());
  30. string dirpath2 = null;
  31. string filepath = null;
  32. DirectoryInfo dirinfo = Directory.CreateDirectory (dirpath);
  33. try {
  34. // Set Full Control to Everyone.
  35. dirSecurity = dirinfo.GetAccessControl ();
  36. dirSecurity.SetGroup (usersSid);
  37. dirSecurity.AddAccessRule (worldDirFullControl);
  38. Directory.SetAccessControl (dirpath, dirSecurity);
  39. // Did the rule store on the directory?
  40. dirSecurity = Directory.GetAccessControl (dirpath);
  41. rules = dirSecurity.GetAccessRules (true, false, typeof (SecurityIdentifier ));
  42. Assert.AreEqual (usersSid, dirSecurity.GetGroup (typeof(SecurityIdentifier)));
  43. Assert.AreEqual (1, rules.Count);
  44. Assert.AreEqual (worldSid, rules[0].IdentityReference);
  45. Assert.AreEqual (InheritanceFlags.ObjectInherit, rules[0].InheritanceFlags);
  46. Assert.AreEqual (PropagationFlags.None, rules[0].PropagationFlags);
  47. Assert.IsFalse (rules[0].IsInherited);
  48. // Create a file. It will have no explicit rules.
  49. filepath = Path.Combine (dirpath, Path.GetRandomFileName ());
  50. using (FileStream file = new FileStream (filepath, FileMode.Create, FileAccess.ReadWrite)) {
  51. fileSecurity = file.GetAccessControl ();
  52. rules = fileSecurity.GetAccessRules (true, false, typeof (SecurityIdentifier));
  53. Assert.AreEqual (0, rules.Count);
  54. }
  55. // Make sure the file has inherited the Full Control access rule.
  56. FileInfo fileInfo = new FileInfo (filepath);
  57. fileSecurity = fileInfo.GetAccessControl ();
  58. rules = fileSecurity.GetAccessRules (false, true, typeof (SecurityIdentifier));
  59. bool fileInheritedRule = false;
  60. foreach (FileSystemAccessRule rule in rules) {
  61. if (rule.AccessControlType == AccessControlType.Allow &&
  62. rule.FileSystemRights == FileSystemRights.FullControl &&
  63. rule.IdentityReference == worldSid &&
  64. rule.IsInherited &&
  65. rule.InheritanceFlags == InheritanceFlags.None &&
  66. rule.PropagationFlags == PropagationFlags.None) // only containers get non-None flags
  67. fileInheritedRule = true;
  68. }
  69. Assert.IsTrue (fileInheritedRule);
  70. // ContainerInherit not being set, create a directory.
  71. // Its inherited rule will have propagation flags to indicate only its children are affected.
  72. dirpath2 = Path.Combine (dirpath, Path.GetRandomFileName ());
  73. dirinfo = Directory.CreateDirectory (dirpath2);
  74. dirSecurity = dirinfo.GetAccessControl ();
  75. rules = dirSecurity.GetAccessRules (false, true, typeof (SecurityIdentifier));
  76. bool dirInheritedRule = false;
  77. foreach (FileSystemAccessRule rule in rules) {
  78. if (rule.AccessControlType == AccessControlType.Allow &&
  79. rule.FileSystemRights == FileSystemRights.FullControl &&
  80. rule.IdentityReference == worldSid &&
  81. rule.IsInherited &&
  82. rule.InheritanceFlags == InheritanceFlags.ObjectInherit &&
  83. rule.PropagationFlags == PropagationFlags.InheritOnly) // <-- key difference
  84. dirInheritedRule = true;
  85. }
  86. Assert.IsTrue (dirInheritedRule);
  87. } finally {
  88. if (null != filepath) File.Delete (filepath);
  89. if (null != dirpath2) Directory.Delete (dirpath2);
  90. Directory.Delete (dirpath);
  91. }
  92. }
  93. }
  94. }