创建未经授权访问的子目录(用于单元测试)
Create SubDirectory with unauthorized access (for unit test)
我必须为将在内部枚举文件和文件夹的 class 创建单元测试。
我当然想测试它是否正确处理未经授权访问的文件夹。
换句话说:我怀疑测试 class 中的方法之一会调用 DirectoryInfo.EnumerateDirectories,我想确保跳过未授权访问的文件夹。
所以我需要创建一个我的 TestObject 无权访问的文件夹。
[TestMethod]
[ExpectedException(typeof(SecurityException)]
public void HelpFileCollection_GetFiles_SkipsInaccessibleFolders()
{
// Test: HelpFileCollection.GetFiles() will skip Folders that are not accessible
// preparation: create a base Folder and a subfolder with limited security
DirectoryInfo baseFolder = this.CreateTestFolder();
DirectoryInfo subFolder = baseFolder.CreateDirectory(... ???
HelpFilderCollection testObject = new HelpFileCollection
{
RootFolder = baseFolder,
IncludeSubFolders = true,
}
// Test: call GetFiles(); expect SecurityException
testObject.GetFiles();
Assert.Fail("HelpFileCollection.GetFiles() unexpectedly didn't throw SecurityException");
}
我应该怎么做才能使子文件夹无法访问?
创建一个文件夹的示例,当您调用“EnumerateDirectories”时,该文件夹会显示“拒绝访问”错误:
DirectoryInfo baseFolder = new DirectoryInfo("D:\Test");
DirectoryInfo subFolder = baseFolder.CreateSubdirectory("HelloWorld");
var security = new System.Security.AccessControl.DirectorySecurity();
security.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(Environment.UserName, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Deny));
subFolder.SetAccessControl(security);
//Get Access denied error
subFolder.EnumerateDirectories();
我必须为将在内部枚举文件和文件夹的 class 创建单元测试。
我当然想测试它是否正确处理未经授权访问的文件夹。
换句话说:我怀疑测试 class 中的方法之一会调用 DirectoryInfo.EnumerateDirectories,我想确保跳过未授权访问的文件夹。
所以我需要创建一个我的 TestObject 无权访问的文件夹。
[TestMethod]
[ExpectedException(typeof(SecurityException)]
public void HelpFileCollection_GetFiles_SkipsInaccessibleFolders()
{
// Test: HelpFileCollection.GetFiles() will skip Folders that are not accessible
// preparation: create a base Folder and a subfolder with limited security
DirectoryInfo baseFolder = this.CreateTestFolder();
DirectoryInfo subFolder = baseFolder.CreateDirectory(... ???
HelpFilderCollection testObject = new HelpFileCollection
{
RootFolder = baseFolder,
IncludeSubFolders = true,
}
// Test: call GetFiles(); expect SecurityException
testObject.GetFiles();
Assert.Fail("HelpFileCollection.GetFiles() unexpectedly didn't throw SecurityException");
}
我应该怎么做才能使子文件夹无法访问?
创建一个文件夹的示例,当您调用“EnumerateDirectories”时,该文件夹会显示“拒绝访问”错误:
DirectoryInfo baseFolder = new DirectoryInfo("D:\Test");
DirectoryInfo subFolder = baseFolder.CreateSubdirectory("HelloWorld");
var security = new System.Security.AccessControl.DirectorySecurity();
security.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(Environment.UserName, System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Deny));
subFolder.SetAccessControl(security);
//Get Access denied error
subFolder.EnumerateDirectories();