Repository navigation
Expand file tree
/
Copy pathPathValidatorTests.cs
More file actions
149 lines (133 loc) · 5.77 KB
/
Copy pathPathValidatorTests.cs
File metadata and controls
149 lines (133 loc) · 5.77 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Runtime.InteropServices;
using FolderDiffIL4DotNet.Core.IO;
using Xunit;
namespace FolderDiffIL4DotNet.Tests.Core.IO
{
[Trait("Category", "Unit")]
public class PathValidatorTests
{
[Theory]
[InlineData("MyFolder")]
[InlineData("release-v1.2.3")]
[InlineData("2024-01-01_build")]
[InlineData("a")]
public void ValidateFolderNameOrThrow_ValidNames_DoesNotThrow(string name)
{
PathValidator.ValidateFolderNameOrThrow(name);
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ValidateFolderNameOrThrow_NullOrEmpty_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData(".")]
[InlineData("..")]
public void ValidateFolderNameOrThrow_DotNames_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData("my\\folder")]
[InlineData("my/folder")]
[InlineData("my:folder")]
[InlineData("my*folder")]
[InlineData("my?folder")]
[InlineData("my\"folder")]
[InlineData("my<folder")]
[InlineData("my>folder")]
[InlineData("my|folder")]
public void ValidateFolderNameOrThrow_InvalidChars_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData("folder ")]
[InlineData("folder.")]
public void ValidateFolderNameOrThrow_TrailingSpaceOrDot_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData("CON")]
[InlineData("con")]
[InlineData("PRN")]
[InlineData("AUX")]
[InlineData("NUL")]
[InlineData("COM1")]
[InlineData("LPT1")]
public void ValidateFolderNameOrThrow_WindowsReservedNames_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Fact]
public void ValidateFolderNameOrThrow_ControlChar_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow("my\x01folder"));
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ValidateAbsolutePathLengthOrThrow_NullOrEmpty_ThrowsArgumentException(string path)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateAbsolutePathLengthOrThrow(path));
}
[Fact]
public void ValidateAbsolutePathLengthOrThrow_NormalPath_DoesNotThrow()
{
PathValidator.ValidateAbsolutePathLengthOrThrow("/usr/local/bin/test");
}
[Fact]
public void ValidateAbsolutePathLengthOrThrow_ExceedsLimit_ThrowsArgumentException()
{
int limit;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
limit = 260;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
limit = 1024;
else
limit = 4096;
var tooLongPath = "/" + new string('a', limit + 1);
Assert.Throws<ArgumentException>(() => PathValidator.ValidateAbsolutePathLengthOrThrow(tooLongPath));
}
// -----------------------------------------------------------------------
// Path traversal prevention
// -----------------------------------------------------------------------
[Theory]
[InlineData("../etc/passwd")] // forward slash already invalid
[InlineData("..\\windows\\system32")] // backslash already invalid
[InlineData("../../root")]
[InlineData("a/../../b")]
public void ValidateFolderNameOrThrow_PathTraversalAttempts_ThrowsArgumentException(string name)
{
// Path traversal via report label is blocked because '/', '\', and ".." components
// are all rejected by the character or dot-name checks.
// レポートラベルでのパストラバーサルは '/'、'\'、".." がすべて
// 文字チェックまたはドット名チェックで拒否されるためブロックされる。
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData("foo\0bar")] // null byte - control char
[InlineData("\0")] // lone null byte
public void ValidateFolderNameOrThrow_NullByteInName_ThrowsArgumentException(string name)
{
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
[Theory]
[InlineData("/etc/passwd")] // starts with / (absolute path)
[InlineData("/tmp/evil")]
public void ValidateFolderNameOrThrow_AbsolutePathAsLabel_ThrowsArgumentException(string name)
{
// An absolute-path string used as a report label would escape the Reports/ directory.
// The leading '/' (or '\') is caught by the invalid-character check.
// レポートラベルに絶対パスを使うと Reports/ ディレクトリを脱出できてしまう。
// 先頭の '/'(または '\')は不正文字チェックで捕捉される。
Assert.Throws<ArgumentException>(() => PathValidator.ValidateFolderNameOrThrow(name));
}
}
}