Skip to content

Commit

Permalink
Merge pull request #99 from manne/respect_hidden_files
Browse files Browse the repository at this point in the history
added throwing exception when writing on a hidden file
  • Loading branch information
tathamoddie committed Jan 25, 2015
2 parents a43a24c + 5d39677 commit 95d5965
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
36 changes: 36 additions & 0 deletions TestHelpers.Tests/MockFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,24 @@ public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem()
fileSystem.GetFile(path).Contents);
}

[Test]
public void MockFile_WriteAllBytes_ShouldThrowAnUnauthorizedAccessExceptionIfFileIsHidden()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("this is hidden") },
});
fileSystem.File.SetAttributes(path, FileAttributes.Hidden);

// Act
TestDelegate action = () => fileSystem.File.WriteAllBytes(path, new byte[] { 123 });

// Assert
Assert.Throws<UnauthorizedAccessException>(action, "Access to the path '{0}' is denied.", path);
}

[Test]
public void MockFile_WriteAllText_ShouldWriteTextFileToMemoryFileSystem()
{
Expand Down Expand Up @@ -602,6 +620,24 @@ public void MockFile_WriteAllText_ShouldOverriteAnExistingFile()
Assert.AreEqual("bar", fileSystem.GetFile(path).TextContents);
}

[Test]
public void MockFile_WriteAllText_ShouldThrowAnUnauthorizedAccessExceptionIfFileIsHidden()
{
// Arrange
string path = XFS.Path(@"c:\something\demo.txt");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData("this is hidden") },
});
fileSystem.File.SetAttributes(path, FileAttributes.Hidden);

// Act
TestDelegate action = () => fileSystem.File.WriteAllText(path, "hello world");

// Assert
Assert.Throws<UnauthorizedAccessException>(action, "Access to the path '{0}' is denied.", path);
}

private IEnumerable<Encoding> GetEncodings()
{
return new List<Encoding>()
Expand Down
2 changes: 0 additions & 2 deletions TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ public override void WriteAllText(string path, string contents, Encoding encodin

private void WriteAllText(string path, MockFileData mockFileData)
{
if (mockFileDataAccessor.FileExists(path))
mockFileDataAccessor.RemoveFile(path);
mockFileDataAccessor.AddFile(path, mockFileData);
}
}
Expand Down
22 changes: 15 additions & 7 deletions TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MockFileSystem() : this(null) { }
public MockFileSystem(IDictionary<string, MockFileData> files, string currentDirectory = "")
{
if (String.IsNullOrEmpty(currentDirectory))
currentDirectory = System.IO.Path.GetTempPath();
currentDirectory = IO.Path.GetTempPath();

this.files = new Dictionary<string, MockFileData>(StringComparer.OrdinalIgnoreCase);
pathField = new MockPath(this);
Expand Down Expand Up @@ -73,17 +73,25 @@ public MockFileData GetFile(string path, bool returnNullObject = false)
lock (files)
return FileExists(path) ? files[path] : returnNullObject ? MockFileData.NullObject : null;
}

public void AddFile(string path, MockFileData mockFile)
{
var fixedPath = FixPath(path);
if (FileExists(fixedPath) && (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));

var directoryPath = Path.GetDirectoryName(fixedPath);

lock (files)
{
if (FileExists(fixedPath))
{
var isReadOnly = (files[fixedPath].Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
var isHidden = (files[fixedPath].Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;

if (isReadOnly || isHidden)
{
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));
}
}

var directoryPath = Path.GetDirectoryName(fixedPath);

if (!directory.Exists(directoryPath))
{
AddDirectory(directoryPath);
Expand Down

0 comments on commit 95d5965

Please sign in to comment.