Repository navigation
Expand file tree
/
Copy pathPersistentProcessRuleMatcherTests.cs
More file actions
152 lines (117 loc) Β· 5.57 KB
/
Copy pathPersistentProcessRuleMatcherTests.cs
File metadata and controls
152 lines (117 loc) Β· 5.57 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
150
151
152
/*
* ThreadPilot - persistent process rule matcher tests.
*/
namespace ThreadPilot.Core.Tests
{
using ThreadPilot.Models;
using ThreadPilot.Services;
public sealed class PersistentProcessRuleMatcherTests
{
private readonly PersistentProcessRuleMatcher matcher = new();
[Fact]
public void IsMatch_WithProcessName_MatchesCaseInsensitive()
{
var rule = CreateRule(processName: "GAME.EXE");
var process = CreateProcess(name: "game.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
[Fact]
public void IsMatch_WithExecutablePath_MatchesCaseInsensitive()
{
var rule = CreateRule(executablePath: @"C:\Games\App\Game.exe");
var process = CreateProcess(executablePath: @"c:\games\app\game.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
[Fact]
public void IsMatch_WithNameAndPath_UsesExecutablePathPriority()
{
var rule = CreateRule(processName: "game.exe", executablePath: @"C:\Games\App\Game.exe");
var process = CreateProcess(name: "game.exe", executablePath: @"C:\Other\Game.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.False(result);
}
[Fact]
public void IsMatch_WithDisabledRule_ReturnsFalse()
{
var rule = CreateRule(processName: "game.exe") with { IsEnabled = false };
var process = CreateProcess(name: "game.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.False(result);
}
[Fact]
public void IsMatch_WithProcessWithoutExecutablePath_CanMatchProcessName()
{
var rule = CreateRule(processName: "game.exe");
var process = CreateProcess(name: "GAME.EXE", executablePath: string.Empty);
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
[Fact]
public void IsMatch_WithNullPaths_DoesNotThrow()
{
var rule = CreateRule(processName: null, executablePath: null);
var process = CreateProcess(name: "game.exe", executablePath: null);
var exception = Record.Exception(() => this.matcher.IsMatch(rule, process));
Assert.Null(exception);
}
[Fact]
public void IsMatch_WithWildcardExecutablePath_MatchesPattern()
{
var rule = CreateRule(executablePath: @"*Raycast*\backend\node.exe");
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
[Fact]
public void IsMatch_WithWildcardExecutablePath_RejectsNonMatchingPath()
{
var rule = CreateRule(executablePath: @"*Raycast*\backend\node.exe");
var process = CreateProcess(name: "node.exe", executablePath: @"C:\nvm4w\nodejs\node.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.False(result);
}
[Fact]
public void IsMatch_WithWildcardExecutablePath_MatchesVersionGlob()
{
var rule = CreateRule(executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_*_x64__*\Raycast\backend\node.exe");
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
[Fact]
public void IsMatch_WithQuestionMarkWildcard_MatchesSingleCharacter()
{
var rule = CreateRule(executablePath: @"C:\Games\App?\Game.exe");
var matchProcess = CreateProcess(name: "game.exe", executablePath: @"C:\Games\App1\Game.exe");
var noMatchProcess = CreateProcess(name: "game.exe", executablePath: @"C:\Games\App12\Game.exe");
Assert.True(this.matcher.IsMatch(rule, matchProcess));
Assert.False(this.matcher.IsMatch(rule, noMatchProcess));
}
[Fact]
public void IsMatch_WithForwardSlashWildcard_MatchesNormalizedBackslashes()
{
var rule = CreateRule(executablePath: @"*/Raycast/backend/node.exe");
var process = CreateProcess(name: "node.exe", executablePath: @"C:\Program Files\WindowsApps\Raycast.Raycast_2.5.3.0_x64__qypenmj9wpt2a\Raycast\backend\node.exe");
var result = this.matcher.IsMatch(rule, process);
Assert.True(result);
}
private static PersistentProcessRule CreateRule(string? processName = null, string? executablePath = null) =>
new()
{
Id = Guid.NewGuid().ToString("N"),
Name = "Rule",
IsEnabled = true,
ProcessName = processName,
ExecutablePath = executablePath,
};
private static ProcessModel CreateProcess(string name = "game.exe", string? executablePath = @"C:\Games\Game.exe") =>
new()
{
ProcessId = 42,
Name = name,
ExecutablePath = executablePath ?? string.Empty,
};
}
}