-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathBaseHardLinks.FileSystem.cs
More file actions
119 lines (93 loc) · 3.84 KB
/
Copy pathBaseHardLinks.FileSystem.cs
File metadata and controls
119 lines (93 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using Xunit;
namespace System.IO.Tests
{
// Contains test methods that can be used for FileInfo or File.
[ConditionalClass(typeof(MountHelper), nameof(MountHelper.CanCreateHardLinks))]
public abstract class BaseHardLinks_FileSystem : FileSystemTest
{
public BaseHardLinks_FileSystem()
{
Assert.True(MountHelper.CanCreateHardLinks);
}
/// <summary>Creates a new file depending on the implementing class.</summary>
protected abstract void CreateFile(string path);
protected abstract void AssertLinkExists(FileSystemInfo linkInfo);
/// <summary>Calls the actual public API for creating a hard link.</summary>
protected abstract FileSystemInfo CreateHardLink(string path, string pathToTarget);
[Fact]
public void CreateHardLink_NullPathToTarget()
{
Assert.Throws<ArgumentNullException>(() => CreateHardLink(GetRandomFilePath(), pathToTarget: null));
}
[Theory]
[InlineData("")]
[InlineData("\0")]
public void CreateHardLink_InvalidPathToTarget(string pathToTarget)
{
Assert.Throws<ArgumentException>(() => CreateHardLink(GetRandomFilePath(), pathToTarget));
}
[Fact]
public void CreateHardLink_RelativePath()
{
string relativePathToTarget = GetRandomFileName();
string relativePath = GetRandomFileName();
CreateFile(relativePathToTarget);
CreateHardLink(relativePath, relativePathToTarget);
}
[Fact]
public void CreateHardLink_TargetDoesNotExist_Throws()
{
string linkPath = GetRandomFilePath();
string nonExistentTarget = GetRandomFilePath();
Assert.Throws<FileNotFoundException>(() => CreateHardLink(linkPath, nonExistentTarget));
}
[Fact]
public void CreateHardLink_LinkPathAlreadyExists_Throws()
{
string targetPath = GetRandomFilePath();
string linkPath = GetRandomFilePath();
CreateFile(targetPath);
CreateFile(linkPath);
Assert.Throws<IOException>(() => CreateHardLink(linkPath, targetPath));
}
[Fact]
public void CreateHardLink_TargetExists_Succeeds()
{
string targetPath = GetRandomFilePath();
string linkPath = GetRandomFilePath();
CreateFile(targetPath);
FileSystemInfo linkInfo = CreateHardLink(linkPath, targetPath);
AssertLinkExists(linkInfo);
// Both files should have the same content
Assert.Equal(File.ReadAllText(targetPath), File.ReadAllText(linkPath));
}
[Fact]
public void CreateHardLink_ModifyViaOneLink_VisibleViaOther()
{
string targetPath = GetRandomFilePath();
string linkPath = GetRandomFilePath();
File.WriteAllText(targetPath, "original");
CreateHardLink(linkPath, targetPath);
// Modify via link
File.WriteAllText(linkPath, "changed");
// Read via target
Assert.Equal("changed", File.ReadAllText(targetPath));
}
[Fact]
public void CreateHardLink_DeleteOneLink_FileStillAccessible()
{
string targetPath = GetRandomFilePath();
string linkPath = GetRandomFilePath();
File.WriteAllText(targetPath, "data");
CreateHardLink(linkPath, targetPath);
// Delete the original file
File.Delete(targetPath);
// The link should still exist and have the data
Assert.Equal("data", File.ReadAllText(linkPath));
}
}
}