Add firewall exclusions for test executables (#244)
This commit is contained in:
parent
d3c8231ed3
commit
1669e39735
|
|
@ -0,0 +1,65 @@
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Templates.Test.Helpers
|
||||||
|
{
|
||||||
|
internal class AddFirewallExclusion : IDisposable
|
||||||
|
{
|
||||||
|
private bool _disposedValue = false;
|
||||||
|
private readonly string _exclusionPath;
|
||||||
|
|
||||||
|
public AddFirewallExclusion(string exclusionPath)
|
||||||
|
{
|
||||||
|
if (!File.Exists(exclusionPath))
|
||||||
|
{
|
||||||
|
throw new FileNotFoundException($"File {exclusionPath} was not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
_exclusionPath = exclusionPath;
|
||||||
|
var startInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
RedirectStandardError = true,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
CreateNoWindow = true,
|
||||||
|
FileName = "cmd.exe",
|
||||||
|
Arguments = $"/c netsh advfirewall firewall add rule name=\"Allow {exclusionPath}\" dir=in action=allow program=\"{exclusionPath}\"",
|
||||||
|
UseShellExecute = false,
|
||||||
|
Verb = "runas",
|
||||||
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
|
};
|
||||||
|
|
||||||
|
Process.Start(startInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!_disposedValue)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
var startInfo = new ProcessStartInfo
|
||||||
|
{
|
||||||
|
RedirectStandardError = true,
|
||||||
|
RedirectStandardOutput = true,
|
||||||
|
CreateNoWindow = true,
|
||||||
|
FileName = "cmd.exe",
|
||||||
|
Arguments = $"/c netsh advfirewall firewall delete rule name=\"Allow {_exclusionPath}\"",
|
||||||
|
UseShellExecute = false,
|
||||||
|
Verb = "runas",
|
||||||
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
|
};
|
||||||
|
|
||||||
|
Process.Start(startInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
_disposedValue = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -74,15 +74,24 @@ namespace Templates.Test.Helpers
|
||||||
{
|
{
|
||||||
var dllPath = publish ? $"{projectName}.dll" : $"bin/Debug/{framework}/{projectName}.dll";
|
var dllPath = publish ? $"{projectName}.dll" : $"bin/Debug/{framework}/{projectName}.dll";
|
||||||
_process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), $"exec {dllPath}", envVars: envVars);
|
_process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), $"exec {dllPath}", envVars: envVars);
|
||||||
|
_listeningUri = GetListeningUri(output);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var exeFullPath = publish
|
var exeFullPath = publish
|
||||||
? Path.Combine(workingDirectory, $"{projectName}.exe")
|
? Path.Combine(workingDirectory, $"{projectName}.exe")
|
||||||
: Path.Combine(workingDirectory, "bin", "Debug", framework, $"{projectName}.exe");
|
: Path.Combine(workingDirectory, "bin", "Debug", framework, $"{projectName}.exe");
|
||||||
_process = ProcessEx.Run(output, workingDirectory, exeFullPath, envVars: envVars);
|
using (new AddFirewallExclusion(exeFullPath))
|
||||||
|
{
|
||||||
|
_process = ProcessEx.Run(output, workingDirectory, exeFullPath, envVars: envVars);
|
||||||
|
_listeningUri = GetListeningUri(output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Uri GetListeningUri(ITestOutputHelper output)
|
||||||
|
{
|
||||||
// Wait until the app is accepting HTTP requests
|
// Wait until the app is accepting HTTP requests
|
||||||
output.WriteLine("Waiting until ASP.NET application is accepting connections...");
|
output.WriteLine("Waiting until ASP.NET application is accepting connections...");
|
||||||
var listeningMessage = _process
|
var listeningMessage = _process
|
||||||
|
|
@ -99,8 +108,7 @@ namespace Templates.Test.Helpers
|
||||||
listeningUrlString.Substring(listeningUrlString.LastIndexOf(':'));
|
listeningUrlString.Substring(listeningUrlString.LastIndexOf(':'));
|
||||||
|
|
||||||
output.WriteLine("Sending requests to " + listeningUrlString);
|
output.WriteLine("Sending requests to " + listeningUrlString);
|
||||||
|
return new Uri(listeningUrlString, UriKind.Absolute);
|
||||||
_listeningUri = new Uri(listeningUrlString, UriKind.Absolute);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AssertOk(string requestUrl)
|
public void AssertOk(string requestUrl)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue