Skip to content

Commit

Permalink
Merge pull request #199 from lindend/master
Browse files Browse the repository at this point in the history
Implementation of MockFile GetAccessControl and SetAccessControl
  • Loading branch information
tathamoddie committed Jan 17, 2017
2 parents 5a55b71 + 7f3ed99 commit 4ce9e62
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 3 deletions.
70 changes: 70 additions & 0 deletions TestHelpers.Tests/MockFileGetAccessControlTests.cs
@@ -0,0 +1,70 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
using XFS = MockUnixSupport;

[TestFixture]
public class MockFileGetAccessControlTests
{
[TestCase(" ")]
[TestCase(" ")]
public void MockFile_GetAccessControl_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
var fileSystem = new MockFileSystem();

// Act
TestDelegate action = () => fileSystem.File.GetAccessControl(path);

// Assert
var exception = Assert.Throws<ArgumentException>(action);
Assert.That(exception.ParamName, Is.EqualTo("path"));
}

[Test]
public void MockFile_GetAccessControl_ShouldThrowFileNotFoundExceptionIfFileDoesNotExistInMockData()
{
// Arrange
var fileSystem = new MockFileSystem();
var expectedFileName = XFS.Path(@"c:\a.txt");

// Act
TestDelegate action = () => fileSystem.File.GetAccessControl(expectedFileName);

// Assert
var exception = Assert.Throws<FileNotFoundException>(action);
Assert.That(exception.FileName, Is.EqualTo(expectedFileName));
}

[Test]
public void MockFile_GetAccessControl_ShouldReturnAccessControlOfFileData()
{
// Arrange
var expectedFileSecurity = new FileSecurity();
expectedFileSecurity.SetAccessRuleProtection(false, false);

var filePath = XFS.Path(@"c:\a.txt");
var fileData = new MockFileData("Test content")
{
AccessControl = expectedFileSecurity,
};

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ filePath, fileData }
});

// Act
var fileSecurity = fileSystem.File.GetAccessControl(filePath);

// Assert
Assert.That(fileSecurity, Is.EqualTo(expectedFileSecurity));
}
}
}
69 changes: 69 additions & 0 deletions TestHelpers.Tests/MockFileSetAccessControlTests.cs
@@ -0,0 +1,69 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
using Security.AccessControl;
using XFS = MockUnixSupport;

[TestFixture]
public class MockFileSetAccessControlTests
{
[TestCase(" ")]
[TestCase(" ")]
public void MockFile_SetAccessControl_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
var fileSystem = new MockFileSystem();
var fileSecurity = new FileSecurity();

// Act
TestDelegate action = () => fileSystem.File.SetAccessControl(path, fileSecurity);

// Assert
var exception = Assert.Throws<ArgumentException>(action);
Assert.That(exception.ParamName, Is.EqualTo("path"));
}

[Test]
public void MockFile_SetAccessControl_ShouldThrowFileNotFoundExceptionIfFileDoesNotExistInMockData()
{
// Arrange
var fileSystem = new MockFileSystem();
var expectedFileName = XFS.Path(@"c:\a.txt");
var fileSecurity = new FileSecurity();

// Act
TestDelegate action = () => fileSystem.File.SetAccessControl(expectedFileName, fileSecurity);

// Assert
var exception = Assert.Throws<FileNotFoundException>(action);
Assert.That(exception.FileName, Is.EqualTo(expectedFileName));
}

[Test]
public void MockFile_SetAccessControl_ShouldReturnAccessControlOfFileData()
{
// Arrange
var filePath = XFS.Path(@"c:\a.txt");
var fileData = new MockFileData("Test content");

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()
{
{ filePath, fileData }
});

// Act
var expectedAccessControl = new FileSecurity();
expectedAccessControl.SetAccessRuleProtection(false, false);
fileSystem.File.SetAccessControl(filePath, expectedAccessControl);

// Assert
var accessControl = fileSystem.File.GetAccessControl(filePath);
Assert.That(accessControl, Is.EqualTo(expectedAccessControl));
}
}
}
2 changes: 2 additions & 0 deletions TestHelpers.Tests/TestHelpers.Tests.csproj
Expand Up @@ -94,6 +94,7 @@
<Compile Include="MockFileCopyTests.cs" />
<Compile Include="MockFileCreateTests.cs" />
<Compile Include="MockFileExistsTests.cs" />
<Compile Include="MockFileGetAccessControlTests.cs" />
<Compile Include="MockFileGetLastAccessTimeUtcTests.cs" />
<Compile Include="MockFileGetLastAccessTimeTests.cs" />
<Compile Include="MockFileGetCreationTimeUtcTests.cs" />
Expand All @@ -104,6 +105,7 @@
<Compile Include="MockFileOpenTests.cs" />
<Compile Include="MockFileReadAllLinesTests.cs" />
<Compile Include="MockFileReadLinesTests.cs" />
<Compile Include="MockFileSetAccessControlTests.cs" />
<Compile Include="MockFileStreamTests.cs" />
<Compile Include="MockFileSystemTests.cs" />
<Compile Include="MockFileTests.cs" />
Expand Down
22 changes: 19 additions & 3 deletions TestingHelpers/MockFile.cs
Expand Up @@ -194,12 +194,20 @@ public override bool Exists(string path)

public override FileSecurity GetAccessControl(string path)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

if (!mockFileDataAccessor.FileExists(path))
{
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path), path);
}

var fileData = mockFileDataAccessor.GetFile(path);
return fileData.AccessControl;
}

public override FileSecurity GetAccessControl(string path, AccessControlSections includeSections)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
return GetAccessControl(path);
}

/// <summary>
Expand Down Expand Up @@ -494,7 +502,15 @@ public override void Replace(string sourceFileName, string destinationFileName,

public override void SetAccessControl(string path, FileSecurity fileSecurity)
{
throw new NotImplementedException(Properties.Resources.NOT_IMPLEMENTED_EXCEPTION);
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

if (!mockFileDataAccessor.FileExists(path))
{
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path), path);
}

var fileData = mockFileDataAccessor.GetFile(path);
fileData.AccessControl = fileSecurity;
}

public override void SetAttributes(string path, FileAttributes fileAttributes)
Expand Down
16 changes: 16 additions & 0 deletions TestingHelpers/MockFileData.cs
@@ -1,4 +1,5 @@
using System.Linq;
using System.Security.AccessControl;
using System.Text;

namespace System.IO.Abstractions.TestingHelpers
Expand Down Expand Up @@ -55,6 +56,12 @@ public class MockFileData
/// </summary>
private FileAttributes attributes = FileAttributes.Normal;

/// <summary>
/// The access control of the <see cref="MockFileData"/>.
/// </summary>
[NonSerialized]
private FileSecurity accessControl = new FileSecurity();

/// <summary>
/// Gets a value indicating whether the <see cref="MockFileData"/> is a directory or not.
/// </summary>
Expand Down Expand Up @@ -168,5 +175,14 @@ public FileAttributes Attributes
get { return attributes; }
set { attributes = value; }
}

/// <summary>
/// Gets or sets <see cref="FileSecurity"/> of the <see cref="MockFileData"/>. This is the object that is returned for this <see cref="MockFileData"/> when calling <see cref="FileBase.GetAccessControl(string)"/>.
/// </summary>
public FileSecurity AccessControl
{
get { return accessControl; }
set { accessControl = value; }
}
}
}

0 comments on commit 4ce9e62

Please sign in to comment.