Exemple #1
0
        public void SetAccessControl_DirectoryInfo_DirectorySecurity_Success()
        {
            using var directory = new TempAclDirectory();
            var directoryInfo     = new DirectoryInfo(directory.Path);
            var directorySecurity = new DirectorySecurity();

            directoryInfo.SetAccessControl(directorySecurity);
        }
Exemple #2
0
        public void SetAccessControl_FileInfo_FileSecurity_InvalidArguments()
        {
            using var directory = new TempAclDirectory();
            using var file      = new TempFile(Path.Combine(directory.Path, "file.txt"));
            var fileInfo = new FileInfo(file.Path);

            AssertExtensions.Throws <ArgumentNullException>("fileSecurity", () => fileInfo.SetAccessControl(fileSecurity: null));
        }
Exemple #3
0
        public void GetAccessControl_DirectoryInfo_ReturnsValidObject()
        {
            using var directory = new TempAclDirectory();
            var directoryInfo = new DirectoryInfo(directory.Path);
            DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();

            Assert.NotNull(directorySecurity);
            Assert.Equal(typeof(FileSystemRights), directorySecurity.AccessRightType);
        }
Exemple #4
0
        public void SetAccessControl_FileStream_FileSecurity_Success()
        {
            using var directory         = new TempAclDirectory();
            using var file              = new TempFile(Path.Combine(directory.Path, "file.txt"));
            using FileStream fileStream = File.Open(file.Path, FileMode.Append, FileAccess.Write, FileShare.None);
            var fileSecurity = new FileSecurity();

            FileSystemAclExtensions.SetAccessControl(fileStream, fileSecurity);
        }
Exemple #5
0
        public void SetAccessControl_FileInfo_FileSecurity_Success()
        {
            using var directory = new TempAclDirectory();
            using var file      = new TempFile(Path.Combine(directory.Path, "file.txt"));
            var fileInfo     = new FileInfo(file.Path);
            var fileSecurity = new FileSecurity();

            fileInfo.SetAccessControl(fileSecurity);
        }
Exemple #6
0
        public void GetAccessControl_Filestream_ReturnValidObject()
        {
            using var directory         = new TempAclDirectory();
            using var file              = new TempFile(Path.Combine(directory.Path, "file.txt"));
            using FileStream fileStream = File.Open(file.Path, FileMode.Append, FileAccess.Write, FileShare.None);
            FileSecurity fileSecurity = FileSystemAclExtensions.GetAccessControl(fileStream);

            Assert.NotNull(fileSecurity);
            Assert.Equal(typeof(FileSystemRights), fileSecurity.AccessRightType);
        }
Exemple #7
0
        public void GetAccessControl_FileInfo_ReturnsValidObject()
        {
            using var directory = new TempAclDirectory();
            using var file      = new TempFile(Path.Combine(directory.Path, "file.txt"));
            var          fileInfo     = new FileInfo(file.Path);
            FileSecurity fileSecurity = fileInfo.GetAccessControl();

            Assert.NotNull(fileSecurity);
            Assert.Equal(typeof(FileSystemRights), fileSecurity.AccessRightType);
        }
Exemple #8
0
        public void FileInfo_Create_DirectoryNotFound()
        {
            using var tempRootDir = new TempAclDirectory();
            string path     = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "file.txt");
            var    info     = new FileInfo(path);
            var    security = new FileSecurity();

            Assert.Throws <DirectoryNotFoundException>(() =>
                                                       info.Create(FileMode.CreateNew, FileSystemRights.FullControl, FileShare.None, DefaultBufferSize, FileOptions.None, security));
        }
Exemple #9
0
        public void DirectoryInfo_Create_NotFound_FullControl()
        {
            using var tempRootDir = new TempAclDirectory();
            string dirPath = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "ParentDoesNotExist");

            var dirInfo  = new DirectoryInfo(dirPath);
            var security = GetDirectorySecurity(FileSystemRights.FullControl);

            // Succeeds because it creates the missing parent folder
            CreateDirectoryWithSecurity(dirInfo, security);
        }
Exemple #10
0
        public void DirectoryInfo_Create_NotFound()
        {
            using var tempRootDir = new TempAclDirectory();
            string dirPath = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString(), "ParentDoesNotExist");

            var dirInfo  = new DirectoryInfo(dirPath);
            var security = new DirectorySecurity();

            // Fails because the DirectorySecurity lacks any rights to create parent folder
            Assert.Throws <UnauthorizedAccessException>(() => CreateDirectoryWithSecurity(dirInfo, security));
        }
Exemple #11
0
        private void Verify_DirectorySecurity_CreateDirectory(DirectorySecurity expectedSecurity)
        {
            using var tempRootDir = new TempAclDirectory();
            string        path    = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString());
            DirectoryInfo dirInfo = expectedSecurity.CreateDirectory(path);

            Assert.True(dirInfo.Exists);
            tempRootDir.CreatedSubdirectories.Add(dirInfo);

            var actualDirInfo = new DirectoryInfo(path);
            DirectorySecurity actualSecurity = actualDirInfo.GetAccessControl(AccessControlSections.Access);

            VerifyAccessSecurity(expectedSecurity, actualSecurity);
        }
Exemple #12
0
        private void Verify_FileSecurity_CreateFile(FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity expectedSecurity)
        {
            using var tempRootDir = new TempAclDirectory();
            string path     = Path.Combine(tempRootDir.Path, "file.txt");
            var    fileInfo = new FileInfo(path);

            fileInfo.Create(mode, rights, share, bufferSize, options, expectedSecurity).Dispose();
            Assert.True(fileInfo.Exists);
            tempRootDir.CreatedSubfiles.Add(fileInfo);

            var          actualFileInfo = new FileInfo(path);
            FileSecurity actualSecurity = actualFileInfo.GetAccessControl(AccessControlSections.Access);

            VerifyAccessSecurity(expectedSecurity, actualSecurity);
        }
Exemple #13
0
        public void DirectoryInfo_Create_AllowSpecific_AccessRules(FileSystemRights rights)
        {
            using var tempRootDir = new TempAclDirectory();
            string path    = Path.Combine(tempRootDir.Path, "directory");
            var    dirInfo = new DirectoryInfo(path);

            DirectorySecurity expectedSecurity = GetDirectorySecurity(rights);

            dirInfo.Create(expectedSecurity);
            Assert.True(dirInfo.Exists);
            tempRootDir.CreatedSubdirectories.Add(dirInfo);

            var actualInfo = new DirectoryInfo(dirInfo.FullName);
            DirectorySecurity actualSecurity = actualInfo.GetAccessControl(AccessControlSections.Access);

            VerifyAccessSecurity(expectedSecurity, actualSecurity);
        }
Exemple #14
0
        public void DirectorySecurity_CreateDirectory_DirectoryAlreadyExists()
        {
            using var tempRootDir = new TempAclDirectory();
            string path = Path.Combine(tempRootDir.Path, Guid.NewGuid().ToString());

            DirectorySecurity expectedSecurity = GetDirectorySecurity(FileSystemRights.FullControl);
            DirectoryInfo     dirInfo          = expectedSecurity.CreateDirectory(path);

            Assert.True(dirInfo.Exists);
            tempRootDir.CreatedSubdirectories.Add(dirInfo);

            var basicSecurity = new DirectorySecurity();
            // Already exists, existingDirInfo should have the original security, not the new basic security
            DirectoryInfo existingDirInfo = basicSecurity.CreateDirectory(path);

            DirectorySecurity actualSecurity = existingDirInfo.GetAccessControl(AccessControlSections.Access);

            VerifyAccessSecurity(expectedSecurity, actualSecurity);
        }
Exemple #15
0
        public void FileInfo_Create_AllowSpecific_AccessRules(FileSystemRights rights)
        {
            using var tempRootDir = new TempAclDirectory();
            string path     = Path.Combine(tempRootDir.Path, "file.txt");
            var    fileInfo = new FileInfo(path);

            FileSecurity expectedSecurity = GetFileSecurity(rights);

            using FileStream stream = fileInfo.Create(
                      FileMode.Create,
                      FileSystemRights.FullControl,
                      FileShare.ReadWrite | FileShare.Delete,
                      DefaultBufferSize,
                      FileOptions.None,
                      expectedSecurity);

            Assert.True(fileInfo.Exists);
            tempRootDir.CreatedSubfiles.Add(fileInfo);

            var          actualInfo     = new FileInfo(fileInfo.FullName);
            FileSecurity actualSecurity = actualInfo.GetAccessControl(AccessControlSections.Access);

            VerifyAccessSecurity(expectedSecurity, actualSecurity);
        }