Skip to content

Commit fb8e29f

Browse files
committed
Add EnvDump for test
1 parent c1f612c commit fb8e29f

6 files changed

Lines changed: 181 additions & 5 deletions

File tree

BrowserGuard.Tests/EnvDumpTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using BrowserGuard;
6+
using Xunit;
7+
8+
namespace BrowserGuard.Tests
9+
{
10+
public class EnvDumpTests : IDisposable
11+
{
12+
readonly string tempDir;
13+
14+
public EnvDumpTests()
15+
{
16+
tempDir = Path.Combine(Path.GetTempPath(), "browserguard-envdump-" + Guid.NewGuid().ToString("N"));
17+
Directory.CreateDirectory(tempDir);
18+
}
19+
20+
public void Dispose()
21+
{
22+
try { Directory.Delete(tempDir, true); } catch { }
23+
}
24+
25+
// Built next to the test assembly only when the whole solution is built,
26+
// so the check is skipped rather than failing a host-only build.
27+
static string? FindEnvDump()
28+
{
29+
var repoRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", ".."));
30+
var candidates = new[] { "Debug", "Release" }
31+
.Select(c => Path.Combine(repoRoot, "tools", "EnvDump", "bin", c, "net8.0", "EnvDump.exe"));
32+
return candidates.FirstOrDefault(File.Exists);
33+
}
34+
35+
[Fact]
36+
public void StartsTheProgramWithTheConfiguredDirectoryAndEnvironment()
37+
{
38+
var envDump = FindEnvDump();
39+
if (envDump is null)
40+
{
41+
return;
42+
}
43+
44+
var config = new StartupLauncherConfig
45+
{
46+
Enabled = true,
47+
Programs =
48+
[
49+
new StartupProgramConfig
50+
{
51+
Path = envDump,
52+
WorkingDirectory = tempDir,
53+
EnvironmentVariables = new Dictionary<string, string>
54+
{
55+
["BROWSERGUARD_STARTUP_TEST"] = "from-config",
56+
},
57+
},
58+
],
59+
};
60+
61+
Assert.Null(StartupLauncher.Run(config));
62+
63+
var report = Path.Combine(tempDir, "envdump.txt");
64+
Assert.True(WaitForFile(report), "the program did not write its report");
65+
66+
var lines = File.ReadAllLines(report);
67+
Assert.Contains($"WorkingDirectory={tempDir}", lines);
68+
Assert.Contains("BROWSERGUARD_STARTUP_TEST=from-config", lines);
69+
// The configured variables are added to the inherited environment.
70+
Assert.Contains(lines, line => line.StartsWith("PATH=", StringComparison.OrdinalIgnoreCase));
71+
}
72+
73+
static bool WaitForFile(string path)
74+
{
75+
for (var i = 0; i < 100 && !File.Exists(path); i++)
76+
{
77+
System.Threading.Thread.Sleep(100);
78+
}
79+
return File.Exists(path);
80+
}
81+
}
82+
}

BrowserGuard.Tests/StartupLauncherTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class StartupLauncherTests : IDisposable
1616

1717
public StartupLauncherTests()
1818
{
19-
tempDir = Path.Combine(Path.GetTempPath(), "bg-startup-" + Guid.NewGuid().ToString("N"));
19+
tempDir = Path.Combine(Path.GetTempPath(), "browserguard-startup-" + Guid.NewGuid().ToString("N"));
2020
Directory.CreateDirectory(tempDir);
2121
}
2222

@@ -111,15 +111,15 @@ public void BuildsTheStartInfoFromTheConfiguration()
111111
Path = cmd,
112112
Arguments = ["/c", "echo hello world"],
113113
WorkingDirectory = tempDir,
114-
EnvironmentVariables = new Dictionary<string, string> { ["BG_TEST"] = "1" },
114+
EnvironmentVariables = new Dictionary<string, string> { ["BROWSERGUARD_TEST"] = "1" },
115115
};
116116

117117
var info = StartupLauncher.BuildStartInfo(program);
118118

119119
Assert.Equal(cmd, info.FileName);
120120
Assert.Equal(["/c", "echo hello world"], info.ArgumentList);
121121
Assert.Equal(tempDir, info.WorkingDirectory);
122-
Assert.Equal("1", info.Environment["BG_TEST"]);
122+
Assert.Equal("1", info.Environment["BROWSERGUARD_TEST"]);
123123
// Required for the environment to be handed to the child.
124124
Assert.False(info.UseShellExecute);
125125
}
@@ -167,8 +167,8 @@ public void StartsTheConfiguredProgram()
167167
public void PassesTheEnvironmentToTheProgram()
168168
{
169169
var marker = Path.Combine(tempDir, "env.txt");
170-
var program = BatchProgram("env.bat", "@echo %BG_TEST% > env.txt");
171-
program.EnvironmentVariables = new Dictionary<string, string> { ["BG_TEST"] = "from-config" };
170+
var program = BatchProgram("env.bat", "@echo %BROWSERGUARD_TEST% > env.txt");
171+
program.EnvironmentVariables = new Dictionary<string, string> { ["BROWSERGUARD_TEST"] = "from-config" };
172172
var config = new StartupLauncherConfig { Enabled = true, Programs = [program] };
173173

174174
Assert.Null(StartupLauncher.Run(config));

BrowserGuard.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Project("{404DD7B1-9623-42CB-8504-B9700DC52753}") = "BrowserGuard", "BrowserGuar
77
EndProject
88
Project("{404DD7B1-9623-42CB-8504-B9700DC52753}") = "BrowserGuard.Tests", "BrowserGuard.Tests\BrowserGuard.Tests.csproj", "{CF8CEA51-3C8A-43B2-B6ED-07D74AD4ED59}"
99
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{07C2787E-EAC7-C090-1BA3-A61EC2A24D84}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnvDump", "tools\EnvDump\EnvDump.csproj", "{1914AF6E-425F-446E-8F95-1102B33E2A17}"
13+
EndProject
1014
Global
1115
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1216
Debug|Any CPU = Debug|Any CPU
@@ -41,10 +45,25 @@ Global
4145
{CF8CEA51-3C8A-43B2-B6ED-07D74AD4ED59}.Release|x64.Build.0 = Release|Any CPU
4246
{CF8CEA51-3C8A-43B2-B6ED-07D74AD4ED59}.Release|x86.ActiveCfg = Release|Any CPU
4347
{CF8CEA51-3C8A-43B2-B6ED-07D74AD4ED59}.Release|x86.Build.0 = Release|Any CPU
48+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|x64.ActiveCfg = Debug|Any CPU
51+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|x64.Build.0 = Debug|Any CPU
52+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|x86.ActiveCfg = Debug|Any CPU
53+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Debug|x86.Build.0 = Debug|Any CPU
54+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|Any CPU.ActiveCfg = Release|Any CPU
55+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|x64.ActiveCfg = Release|Any CPU
57+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|x64.Build.0 = Release|Any CPU
58+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|x86.ActiveCfg = Release|Any CPU
59+
{1914AF6E-425F-446E-8F95-1102B33E2A17}.Release|x86.Build.0 = Release|Any CPU
4460
EndGlobalSection
4561
GlobalSection(SolutionProperties) = preSolution
4662
HideSolutionNode = FALSE
4763
EndGlobalSection
64+
GlobalSection(NestedProjects) = preSolution
65+
{1914AF6E-425F-446E-8F95-1102B33E2A17} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84}
66+
EndGlobalSection
4867
GlobalSection(ExtensibilityGlobals) = postSolution
4968
SolutionGuid = {CFE10506-DF38-4A2A-83BA-5115D7FC91FF}
5069
EndGlobalSection
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"NetLogger": {
3+
"Enabled": false,
4+
"Endpoint": "",
5+
"UrlAccess": false,
6+
"Browsing": false,
7+
"Upload": false,
8+
"Download": false,
9+
"Auth": false,
10+
"Print": false
11+
},
12+
"UploadGuard": {
13+
"Enabled": false,
14+
"BlockedExtensions": [".exe", ".bat", ".cmd", ".js", ".vbs"],
15+
"AllowedExtensions": [".pdf", ".docx", ".xlsx"],
16+
"AllowedPaths": ["^C:\\\\Users\\\\[^\\\\]+\\\\Documents\\\\", "^D:\\\\Share\\\\"],
17+
"BlockedPaths": ["\\\\Confidential\\\\"]
18+
},
19+
"SettingPageFilter": {
20+
"Enabled": false,
21+
"NotifyOnBlocked": true,
22+
"BlockedPrefixes": ["edge://settings/"]
23+
},
24+
"StartupLauncher": {
25+
"Enabled": false,
26+
"Programs": [
27+
{
28+
"Path": "C:\\Tools\\EnvDump.exe",
29+
"Arguments": [],
30+
"WorkingDirectory": "C:\\Temp\\envdump",
31+
"EnvironmentVariables": { "BROWSERGUARD_STARTUP_TEST": "from-config" },
32+
"Sha256": ""
33+
}
34+
]
35+
}
36+
}

tools/EnvDump/EnvDump.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

tools/EnvDump/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections;
2+
using System.Text;
3+
4+
const string OutputName = "envdump.txt";
5+
6+
var output = new StringBuilder();
7+
output.AppendLine($"WorkingDirectory={Directory.GetCurrentDirectory()}");
8+
output.AppendLine();
9+
output.AppendLine("[Environment]");
10+
11+
var variables = Environment.GetEnvironmentVariables()
12+
.Cast<DictionaryEntry>()
13+
.OrderBy(entry => (string)entry.Key, StringComparer.OrdinalIgnoreCase);
14+
15+
foreach (var variable in variables)
16+
{
17+
output.AppendLine($"{variable.Key}={variable.Value}");
18+
}
19+
20+
try
21+
{
22+
File.WriteAllText(OutputName, output.ToString(), new UTF8Encoding(false));
23+
return 0;
24+
}
25+
catch (Exception ex)
26+
{
27+
Console.Error.WriteLine($"cannot write {OutputName}: {ex.Message}");
28+
return 1;
29+
}

0 commit comments

Comments
 (0)