diff --git a/test/Templates.Test/Helpers/AddFirewallExclusion.cs b/test/Templates.Test/Helpers/AddFirewallExclusion.cs new file mode 100644 index 0000000000..5387548a11 --- /dev/null +++ b/test/Templates.Test/Helpers/AddFirewallExclusion.cs @@ -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; + } + } + } +} diff --git a/test/Templates.Test/Helpers/AspNetProcess.cs b/test/Templates.Test/Helpers/AspNetProcess.cs index de24d4aa70..7dff3eb486 100644 --- a/test/Templates.Test/Helpers/AspNetProcess.cs +++ b/test/Templates.Test/Helpers/AspNetProcess.cs @@ -74,15 +74,24 @@ namespace Templates.Test.Helpers { var dllPath = publish ? $"{projectName}.dll" : $"bin/Debug/{framework}/{projectName}.dll"; _process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), $"exec {dllPath}", envVars: envVars); + _listeningUri = GetListeningUri(output); } else { var exeFullPath = publish ? Path.Combine(workingDirectory, $"{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 output.WriteLine("Waiting until ASP.NET application is accepting connections..."); var listeningMessage = _process @@ -99,8 +108,7 @@ namespace Templates.Test.Helpers listeningUrlString.Substring(listeningUrlString.LastIndexOf(':')); output.WriteLine("Sending requests to " + listeningUrlString); - - _listeningUri = new Uri(listeningUrlString, UriKind.Absolute); + return new Uri(listeningUrlString, UriKind.Absolute); } public void AssertOk(string requestUrl)