FileSystem.AccessControl tests not cleaning files properly (#47074)
authorCarlos Sanchez <1175054+carlossanlop@users.noreply.github.com>
Tue, 9 Feb 2021 21:11:29 +0000 (13:11 -0800)
committerGitHub <noreply@github.com>
Tue, 9 Feb 2021 21:11:29 +0000 (13:11 -0800)
commit08be96571bddc04876d989c88129324be3433b3b
tree4af3f03bbc2f628b51ef58640c87f758a393c4f2
parent91aefdd625fa6616ccc7538f689f9fad6fad41fc
FileSystem.AccessControl tests not cleaning files properly (#47074)

* FileSystem.AccessControl tests not cleaning files properly

The TempDirectory helper class is disposable. The finalizer calls a function that deletes the folders left behind, without throwing exceptions on fail.

This protective try catch hid some additional errors that were not caught when these unit tests were first written, so I'm fixing them:

- I created a class that inherits from TempDirectory to override the folder deleting method
- In the finalizer, I iterate through all the files and folders created by the test, ensure their ACLs give me full control (in case the tests prevent deletion), then attempt to delete the tree.
- To verify my changes, I did not put the finalizer code in a try catch, so I could see the errors thrown when attempting to delete the tree.
- The deletion exceptions helped me find that GetAccessControl needs to be called with the AccessControlSections.Access argument, otherwise they throw "PrivilegeNotHeldException: The process does not possess the 'SeSecurityPrivilege' privilege which is required for this operation."
- I avoided creating files and directories using a FileSecurity or DirectorySecurity that did not have its access rules defined. Files and folders created this way could not be deleted.
- I avoided testing AccessControlType.Deny. This would also cause deletion exceptions.
- Moved some repeated code into common methods.

* Temp commit to verify files and dirs deleted without try catch

* Set directory security first, then file security, in DeleteDirectory. Bring back InlineData for test, but only when a Read is included.

* Restore try catch before merging.

* Address suggestions.

* Bring back tests that consume the parameterless security constructors. Bring back all FileSystemRights with annotations on why some need to be skipped. Ensure DeleteDirectory can successfully reset permissions before deleting all files and folders deleted by the unit test.

* NotFound test

* FileShare.None

* InlineData FileMode

* Rename method that creates per platform. Bring back inline data.

* Address suggestions

* Address suggestions

* Address suggestions

* Reverting Verify_FileSecurity_CreateFile to the original arguments

Co-authored-by: carlossanlop <carlossanlop@users.noreply.github.com>
src/libraries/Common/tests/System/IO/TempDirectory.cs
src/libraries/System.IO.FileSystem.AccessControl/tests/FileSystemAclExtensionsTests.cs
src/libraries/System.IO.FileSystem.AccessControl/tests/System.IO.FileSystem.AccessControl.Tests.csproj
src/libraries/System.IO.FileSystem.AccessControl/tests/TempAclDirectory.cs [new file with mode: 0644]