-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathElevationHelperSelectionTests.cs
More file actions
95 lines (80 loc) · 3.5 KB
/
Copy pathElevationHelperSelectionTests.cs
File metadata and controls
95 lines (80 loc) · 3.5 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
using HostsFileEditor.Elevation;
namespace HostsFileEditor.Core.Tests;
/// <summary>
/// Coverage for <see cref="PrivilegedFileOperations.UseElevationHelper"/>'s helper-resolution branches.
/// <see cref="PrivilegedFileOperations.Current"/> is process-global, so every test restores it.
/// </summary>
[TestClass]
public sealed class ElevationHelperSelectionTests
{
private IPrivilegedFileOperations _originalCurrent = null!;
[TestInitialize]
public void Init() => _originalCurrent = PrivilegedFileOperations.Current;
[TestCleanup]
public void Cleanup() => PrivilegedFileOperations.Current = _originalCurrent;
[TestMethod]
public void UseElevationHelper_ExplicitExistingPath_SelectsHelper()
{
var helper = Path.Combine(Path.GetTempPath(), "HfeHelper_" + Guid.NewGuid().ToString("N") + ".exe");
File.WriteAllText(helper, "not a real exe");
try
{
PrivilegedFileOperations.Current = new InProcessPrivilegedFileOperations();
PrivilegedFileOperations.UseElevationHelper(helper);
PrivilegedFileOperations.Current.ShouldBeOfType<ElevatedHelperPrivilegedFileOperations>();
}
finally
{
File.Delete(helper);
}
}
[TestMethod]
public void UseElevationHelper_ExplicitMissingPath_LeavesCurrentUnchanged()
{
var inProcess = new InProcessPrivilegedFileOperations();
PrivilegedFileOperations.Current = inProcess;
PrivilegedFileOperations.UseElevationHelper(Path.Combine(Path.GetTempPath(), "no-such-helper.exe"));
PrivilegedFileOperations.Current.ShouldBeSameAs(inProcess);
}
[TestMethod]
public void UseElevationHelper_NoHelperBesideApp_StaysInProcess()
{
// No HostsFileEditor.Elevate.exe ships in the test bin. Defensively clear any stub a prior
// (interrupted) run of UseElevationHelper_HelperBesideApp_SelectsHelper may have left behind,
// so the default probe genuinely finds nothing regardless of test order.
foreach (var stray in HelperCandidatePaths())
{
if (File.Exists(stray))
{
File.Delete(stray);
}
}
var inProcess = new InProcessPrivilegedFileOperations();
PrivilegedFileOperations.Current = inProcess;
PrivilegedFileOperations.UseElevationHelper();
PrivilegedFileOperations.Current.ShouldBeSameAs(inProcess);
}
[TestMethod]
public void UseElevationHelper_HelperBesideApp_SelectsHelper()
{
// Drop a stand-in helper next to the test assembly so the default candidate probe finds it.
// The test bin never contains a real helper, so the stub is always removed afterwards.
var candidate = Path.Combine(AppContext.BaseDirectory, PrivilegedFileOperations.HelperExecutableName);
File.WriteAllText(candidate, "stub");
try
{
PrivilegedFileOperations.Current = new InProcessPrivilegedFileOperations();
PrivilegedFileOperations.UseElevationHelper();
PrivilegedFileOperations.Current.ShouldBeOfType<ElevatedHelperPrivilegedFileOperations>();
}
finally
{
File.Delete(candidate);
}
}
private static IEnumerable<string> HelperCandidatePaths() =>
[
Path.Combine(AppContext.BaseDirectory, PrivilegedFileOperations.HelperSubdirectory, PrivilegedFileOperations.HelperExecutableName),
Path.Combine(AppContext.BaseDirectory, PrivilegedFileOperations.HelperExecutableName),
];
}