diff --git a/test/AspNetCoreModule.Test/Framework/IISConfigUtility.cs b/test/AspNetCoreModule.Test/Framework/IISConfigUtility.cs index fc4c48ab3b..86077f22db 100644 --- a/test/AspNetCoreModule.Test/Framework/IISConfigUtility.cs +++ b/test/AspNetCoreModule.Test/Framework/IISConfigUtility.cs @@ -155,7 +155,7 @@ namespace AspNetCoreModule.Test.Framework { ApppHostTemporaryBackupFileExtention = temporaryBackupFileExtenstion; break; - } + } } } @@ -263,7 +263,7 @@ namespace AspNetCoreModule.Test.Framework addElement[attribute] = value; break; } - serverManager.CommitChanges(); + serverManager.CommitChanges(); } } @@ -291,7 +291,7 @@ namespace AspNetCoreModule.Test.Framework } } - public void CreateSite(string siteName, string physicalPath, int siteId, int tcpPort, string appPoolName = "DefaultAppPool") + public void CreateSite(string siteName, string hostname, string physicalPath, int siteId, int tcpPort, string appPoolName = "DefaultAppPool") { TestUtility.LogInformation("Creating web site : " + siteName); @@ -312,7 +312,7 @@ namespace AspNetCoreModule.Test.Framework ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); bindingElement["protocol"] = @"http"; - bindingElement["bindingInformation"] = "*:" + tcpPort + ":"; + bindingElement["bindingInformation"] = "*:" + tcpPort + ":" + hostname; bindingsCollection.Add(bindingElement); ConfigurationElementCollection siteCollection = siteElement.GetCollection(); @@ -327,7 +327,6 @@ namespace AspNetCoreModule.Test.Framework applicationCollection.Add(virtualDirectoryElement); siteCollection.Add(applicationElement); sitesCollection.Add(siteElement); - serverManager.CommitChanges(); } } @@ -361,7 +360,6 @@ namespace AspNetCoreModule.Test.Framework virtualDirectoryElement["physicalPath"] = physicalPath; applicationCollection.Add(virtualDirectoryElement); siteCollection.Add(applicationElement); - serverManager.CommitChanges(); } } @@ -379,7 +377,6 @@ namespace AspNetCoreModule.Test.Framework basicAuthenticationSection["enabled"] = basic; ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", siteName); windowsAuthenticationSection["enabled"] = windows; - serverManager.CommitChanges(); } } @@ -416,7 +413,6 @@ namespace AspNetCoreModule.Test.Framework anonymousAuthenticationSection["enabled"] = false; ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", siteName); windowsAuthenticationSection["enabled"] = false; - serverManager.CommitChanges(); } } @@ -431,7 +427,6 @@ namespace AspNetCoreModule.Test.Framework ConfigurationSection urlCompressionSection = config.GetSection("system.webServer/urlCompression", siteName); urlCompressionSection["doStaticCompression"] = enabled; urlCompressionSection["doDynamicCompression"] = enabled; - serverManager.CommitChanges(); } } @@ -447,7 +442,6 @@ namespace AspNetCoreModule.Test.Framework anonymousAuthenticationSection["enabled"] = true; ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", siteName); windowsAuthenticationSection["enabled"] = false; - serverManager.CommitChanges(); } } @@ -509,41 +503,10 @@ namespace AspNetCoreModule.Test.Framework errorElement2["subStatusCode"] = subStatusCode; errorElement2["path"] = path; httpErrorsCollection.Add(errorElement2); - serverManager.CommitChanges(); } Thread.Sleep(500); } - - private static bool? _isIISInstalled = null; - public static bool? IsIISInstalled - { - get - { - if (_isIISInstalled == null) - { - _isIISInstalled = true; - if (_isIISInstalled == true && !File.Exists(Path.Combine(Strings.IIS64BitPath, "iiscore.dll"))) - { - _isIISInstalled = false; - } - if (_isIISInstalled == true && !File.Exists(Path.Combine(Strings.IIS64BitPath, "config", "applicationhost.config"))) - { - _isIISInstalled = false; - } - } - return _isIISInstalled; - } - set - { - _isIISInstalled = value; - } - } - - public static bool IsIISReady { - get; - set; - } public bool IsAncmInstalled(ServerType servertype) { @@ -646,7 +609,7 @@ namespace AspNetCoreModule.Test.Framework { modulesCollection.Remove(module); } - + serverManager.CommitChanges(); } return result; @@ -748,7 +711,6 @@ namespace AspNetCoreModule.Test.Framework { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName].ProcessModel.IdleTimeout = TimeSpan.FromMinutes(idleTimeoutMinutes); - serverManager.CommitChanges(); } } @@ -767,7 +729,6 @@ namespace AspNetCoreModule.Test.Framework { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName].ProcessModel.MaxProcesses = maxProcesses; - serverManager.CommitChanges(); } } @@ -788,7 +749,6 @@ namespace AspNetCoreModule.Test.Framework appPools[appPoolName].ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser; appPools[appPoolName].ProcessModel.UserName = userName; appPools[appPoolName].ProcessModel.Password = password; - serverManager.CommitChanges(); } } @@ -810,7 +770,6 @@ namespace AspNetCoreModule.Test.Framework { ApplicationPoolCollection appPools = serverManager.ApplicationPools; appPools[appPoolName]["startMode"] = startMode; - serverManager.CommitChanges(); } } @@ -841,11 +800,13 @@ namespace AspNetCoreModule.Test.Framework { ApplicationPoolCollection appPools = serverManager.ApplicationPools; if (start) + { appPools[appPoolName].Start(); + } else + { appPools[appPoolName].Stop(); - - serverManager.CommitChanges(); + } } } catch (Exception ex) @@ -902,8 +863,9 @@ namespace AspNetCoreModule.Test.Framework { ApplicationPoolCollection appPools = serverManager.ApplicationPools; while (appPools.Count > 0) + { appPools.RemoveAt(0); - + } serverManager.CommitChanges(); } } @@ -948,7 +910,6 @@ namespace AspNetCoreModule.Test.Framework b.SetAttributeValue("bindingInformation", bindingInfo); site.Bindings.Add(b); - serverManager.CommitChanges(); } } @@ -988,7 +949,6 @@ namespace AspNetCoreModule.Test.Framework sites[siteName].Stop(); sites[siteName].SetAttributeValue("serverAutoStart", false); } - serverManager.CommitChanges(); } } @@ -1033,7 +993,6 @@ namespace AspNetCoreModule.Test.Framework vdir.SetAttributeValue("physicalPath", physicalPath); app.VirtualDirectories.Add(vdir); - serverManager.CommitChanges(); } } @@ -1176,7 +1135,7 @@ namespace AspNetCoreModule.Test.Framework } return output; } - + public string ExportCertificateTo(string thumbPrint, string sslStoreFrom = @"Cert:\LocalMachine\My", string sslStoreTo = @"Cert:\LocalMachine\Root", string pfxPassword = null) { string toolsPath = Path.Combine(InitializeTestMachine.GetSolutionDirectory(), "tools"); @@ -1391,8 +1350,9 @@ namespace AspNetCoreModule.Test.Framework SiteCollection sites = serverManager.Sites; while (sites.Count > 0) + { sites.RemoveAt(0); - + } serverManager.CommitChanges(); } } @@ -1406,11 +1366,8 @@ namespace AspNetCoreModule.Test.Framework using (ServerManager serverManager = new ServerManager()) { Configuration config = serverManager.GetApplicationHostConfiguration(); - ConfigurationSection webLimitsSection = config.GetSection("system.applicationHost/webLimits"); webLimitsSection["dynamicRegistrationThreshold"] = threshold; - - serverManager.CommitChanges(); } } @@ -1418,6 +1375,6 @@ namespace AspNetCoreModule.Test.Framework { TestUtility.LogTrace(String.Format("#################### Changing dynamicRegistrationThreshold failed. Reason: {0} ####################", ex.Message)); } - } + } } } \ No newline at end of file diff --git a/test/AspNetCoreModule.Test/Framework/InitializeTestMachine.cs b/test/AspNetCoreModule.Test/Framework/InitializeTestMachine.cs index 09f32d0691..53b7fcd389 100644 --- a/test/AspNetCoreModule.Test/Framework/InitializeTestMachine.cs +++ b/test/AspNetCoreModule.Test/Framework/InitializeTestMachine.cs @@ -5,58 +5,41 @@ using System; using System.IO; using System.Threading; using Microsoft.Extensions.PlatformAbstractions; +using System.Security.Principal; +using System.Security.AccessControl; namespace AspNetCoreModule.Test.Framework { + public static class TestFlags + { + public const string SkipTest = "SkipTest"; + public const string UsePrivateANCM = "UsePrivateANCM"; + public const string UseIISExpress = "UseIISExpress"; + public const string UseFullIIS = "UseFullIIS"; + public const string RunAsAdministrator = "RunAsAdministrator"; + public const string MakeCertExeAvailable = "MakeCertExeAvailable"; + public const string X86Platform = "X86Platform"; + public const string Wow64BitMode = "Wow64BitMode"; + public const string RequireRunAsAdministrator = "RequireRunAsAdministrator"; + public const string Default = "Default"; + + public static bool Enabled(string flagValue) + { + return InitializeTestMachine.GlobalTestFlags.Contains(flagValue.ToLower()); + } + } + public class InitializeTestMachine : IDisposable { public const string ANCMTestFlagsEnvironmentVariable = "%ANCMTestFlags%"; - public const string ANCMTestFlagsDefaultContext = "AdminAnd64Bit"; - public const string ANCMTestFlagsTestSkipContext = "SkipTest"; - public const string ANCMTestFlagsUsePrivateAspNetCoreFileContext = "UsePrivate"; - private const string ANCMTestFlagsUseIISExpressContext = "UseIISExpress"; - - private static bool? _usePrivateAspNetCoreFile = null; - public static bool? UsePrivateAspNetCoreFile - { - get { - // - // By default, we don't use the private AspNetCore.dll that is compiled with this solution. - // In order to use the private file, you should add 'UsePrivateAspNetCoreFile' flag to the Environmnet variable %ANCMTestFlag%. - // - // Set ANCMTestFlag=%ANCMTestFlag%;UsePrivateAspNetCoreFile - // Or - // $Env:ANCMTestFlag=$Env:ANCMTestFlag + ";UsePrivateAspNetCoreFile" - // - if (_usePrivateAspNetCoreFile == null) - { - _usePrivateAspNetCoreFile = false; - var envValue = Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable); - if (envValue.ToLower().Contains(ANCMTestFlagsUsePrivateAspNetCoreFileContext.ToLower())) - { - TestUtility.LogInformation("PrivateAspNetCoreFile is set"); - _usePrivateAspNetCoreFile = true; - } - else - { - TestUtility.LogInformation("PrivateAspNetCoreFile is not set"); - } - } - return _usePrivateAspNetCoreFile; - } - set - { - _usePrivateAspNetCoreFile = value; - } - } - + public static int SiteId = 40000; public const string PrivateFileName = "aspnetcore_private.dll"; public static string FullIisAspnetcore_path = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "inetsrv", PrivateFileName); public static string FullIisAspnetcore_path_original = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "inetsrv", "aspnetcore.dll"); public static string FullIisAspnetcore_X86_path = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "syswow64", "inetsrv", PrivateFileName); - public static string IisExpressAspnetcore_path = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), "IIS Express", PrivateFileName); - public static string IisExpressAspnetcore_X86_path = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), "IIS Express", PrivateFileName); + public static string IisExpressAspnetcore_path; + public static string IisExpressAspnetcore_X86_path; public static string IisExpressAspnetcoreSchema_path = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles%"), "IIS Express", "config", "schema", "aspnetcore_schema.xml"); public static string IisExpressAspnetcoreSchema_X86_path = Path.Combine(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), "IIS Express", "config", "schema", "aspnetcore_schema.xml"); @@ -65,27 +48,225 @@ namespace AspNetCoreModule.Test.Framework private static bool _InitializeTestMachineCompleted = false; private string _setupScriptPath = null; - private bool CheckPerquisiteForANCMTest() + private static bool? _makeCertExeAvailable = null; + public static bool MakeCertExeAvailable { - bool result = true; - TestUtility.LogInformation("CheckPerquisiteForANCMTest(): Environment.Is64BitOperatingSystem: {0}, Environment.Is64BitProcess {1}", Environment.Is64BitOperatingSystem, Environment.Is64BitProcess); - TestUtility.LogInformation("%ANCMTestFlags%: {0}", Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable)); - - if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) + get { - TestUtility.LogInformation("CheckPerquisiteForANCMTest() Failed: ANCM test should be started with x64 process mode on 64 bit machine; if you run this test on Visual Studio, you should set X64 first after selecting 'Test -> Test Settings -> Default Process Architecture' menu"); - result = false; + if (_makeCertExeAvailable == null) + { + _makeCertExeAvailable = false; + try + { + string makecertExeFilePath = TestUtility.GetMakeCertPath(); + TestUtility.RunCommand(makecertExeFilePath, null, true, true); + TestUtility.LogInformation("Verified makecert.exe is available : " + makecertExeFilePath); + _makeCertExeAvailable = true; + } + catch + { + _makeCertExeAvailable = false; + } + } + return (_makeCertExeAvailable == true); } - return result; } + + public static string TestRootDirectory + { + get + { + return Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "_ANCMTest"); + } + } + + private static string _globalTestFlags = null; + public static string GlobalTestFlags + { + get + { + if (_globalTestFlags == null) + { + bool isElevated; + WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); + isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator); + + // check if this test process is started with the Run As Administrator start option + _globalTestFlags = Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable); + + // + // Check if ANCMTestFlags environment is not defined and the test program was started + // without using the Run As Administrator start option. + // In that case, we have to use the default TestFlags of UseIISExpress and UsePrivateANCM + // + if (!isElevated) + { + if (_globalTestFlags.ToLower().Contains("%" + ANCMTestFlagsEnvironmentVariable.ToLower() + "%")) + { + _globalTestFlags = TestFlags.UsePrivateANCM + ";" + TestFlags.UseIISExpress; + } + } + + // + // convert in lower case + // + _globalTestFlags = _globalTestFlags.ToLower(); + + // + // error handling: UseIISExpress and UseFullIIS can be used together. + // + if (_globalTestFlags.Contains(TestFlags.UseIISExpress.ToLower()) && _globalTestFlags.Contains(TestFlags.UseFullIIS.ToLower())) + { + _globalTestFlags = _globalTestFlags.Replace(TestFlags.UseFullIIS.ToLower(), ""); + } + + // + // adjust the default test context in run time to figure out wrong test context values + // + if (isElevated) + { + // add RunAsAdministrator + if (!_globalTestFlags.Contains(TestFlags.RunAsAdministrator.ToLower())) + { + TestUtility.LogInformation("Added test context of " + TestFlags.RunAsAdministrator); + _globalTestFlags += ";" + TestFlags.RunAsAdministrator; + } + } + else + { + // add UseIISExpress + if (!_globalTestFlags.Contains(TestFlags.UseIISExpress.ToLower())) + { + TestUtility.LogInformation("Added test context of " + TestFlags.UseIISExpress); + _globalTestFlags += ";" + TestFlags.UseIISExpress; + } + + // remove UseFullIIS + if (_globalTestFlags.Contains(TestFlags.UseFullIIS.ToLower())) + { + _globalTestFlags = _globalTestFlags.Replace(TestFlags.UseFullIIS.ToLower(), ""); + } + + // remove RunAsAdmistrator + if (_globalTestFlags.Contains(TestFlags.RunAsAdministrator.ToLower())) + { + _globalTestFlags = _globalTestFlags.Replace(TestFlags.RunAsAdministrator.ToLower(), ""); + } + } + + if (MakeCertExeAvailable) + { + // Add MakeCertExeAvailable + if (!_globalTestFlags.Contains(TestFlags.MakeCertExeAvailable.ToLower())) + { + TestUtility.LogInformation("Added test context of " + TestFlags.MakeCertExeAvailable); + _globalTestFlags += ";" + TestFlags.MakeCertExeAvailable; + } + } + + if (!Environment.Is64BitOperatingSystem) + { + // Add X86Platform + if (!_globalTestFlags.Contains(TestFlags.X86Platform.ToLower())) + { + TestUtility.LogInformation("Added test context of " + TestFlags.X86Platform); + _globalTestFlags += ";" + TestFlags.X86Platform; + } + } + + if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) + { + // Add Wow64bitMode + if (!_globalTestFlags.Contains(TestFlags.Wow64BitMode.ToLower())) + { + TestUtility.LogInformation("Added test context of " + TestFlags.Wow64BitMode); + _globalTestFlags += ";" + TestFlags.Wow64BitMode; + } + + // remove X86Platform + if (_globalTestFlags.Contains(TestFlags.X86Platform.ToLower())) + { + _globalTestFlags = _globalTestFlags.Replace(TestFlags.X86Platform.ToLower(), ""); + } + } + + _globalTestFlags = _globalTestFlags.ToLower(); + } + + return _globalTestFlags; + } + } + + public void InitializeIISServer() + { + // Check if IIS server is installed or not + bool isIISInstalled = true; + if (!File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "iiscore.dll"))) + { + isIISInstalled = false; + } + + if (!File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "config", "applicationhost.config"))) + { + isIISInstalled = false; + } + + if (!isIISInstalled) + { + throw new System.ApplicationException("IIS server is not installed"); + } + + // Check websocket is installed + if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "iiswsock.dll"))) + { + TestUtility.LogInformation("Websocket is installed"); + } + else + { + throw new System.ApplicationException("websocket module is not installed"); + } + + // Clean up IIS worker process + TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess); + + // Reset applicationhost.config + TestUtility.LogInformation("Restoring applicationhost.config"); + IISConfigUtility.RestoreAppHostConfig(restoreFromMasterBackupFile: true); + TestUtility.StartW3svc(); + + // check w3svc is running after resetting applicationhost.config + if (IISConfigUtility.GetServiceStatus("w3svc") == "Running") + { + TestUtility.LogInformation("W3SVC service is restarted after restoring applicationhost.config"); + } + else + { + throw new System.ApplicationException("WWW service can't start"); + } + + // check URLRewrite module exists + if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "rewrite.dll"))) + { + TestUtility.LogInformation("Verified URL Rewrite module installed for IIS server"); + } + else + { + throw new System.ApplicationException("URL Rewrite module is not installed"); + } + + if (IISConfigUtility.ApppHostTemporaryBackupFileExtention == null) + { + throw new System.ApplicationException("Failed to backup applicationhost.config"); + } + } + public InitializeTestMachine() { _referenceCount++; + // This method should be called only one time if (_referenceCount == 1) { - CheckPerquisiteForANCMTest(); - TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Start"); _InitializeTestMachineCompleted = false; @@ -93,94 +274,34 @@ namespace AspNetCoreModule.Test.Framework TestUtility.LogInformation("InitializeTestMachine::Start"); if (Environment.ExpandEnvironmentVariables("%ANCMTEST_DEBUG%").ToLower() == "true") { - System.Diagnostics.Debugger.Launch(); + System.Diagnostics.Debugger.Launch(); } - - // check Makecert.exe exists - try - { - string makecertExeFilePath = TestUtility.GetMakeCertPath(); - TestUtility.RunCommand(makecertExeFilePath, null, true, true); - TestUtility.LogInformation("Verified makecert.exe is available : " + makecertExeFilePath); - } - catch (Exception ex) - { - throw new System.ApplicationException("makecert.exe is not available : " + ex.Message); - } - + + // + // Clean up IISExpress processes + // TestUtility.ResetHelper(ResetHelperMode.KillIISExpress); - // check if we can use IIS server instead of IISExpress - try + // + // Initalize IIS server + // + + if (TestFlags.Enabled(TestFlags.UseFullIIS)) { - IISConfigUtility.IsIISReady = false; - if (IISConfigUtility.IsIISInstalled == true) - { - var envValue = Environment.ExpandEnvironmentVariables(ANCMTestFlagsEnvironmentVariable); - if (envValue.ToLower().Contains(ANCMTestFlagsUseIISExpressContext.ToLower())) - { - TestUtility.LogInformation("UseIISExpress is set"); - throw new System.ApplicationException("'ANCMTestServerType' environment variable is set to 'true'"); - } - else - { - TestUtility.LogInformation("UseIISExpress is not set"); - } - - // check websocket is installed - if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "iiswsock.dll"))) - { - TestUtility.LogInformation("Websocket is installed"); - } - else - { - throw new System.ApplicationException("websocket module is not installed"); - } - - TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess); - - // Reset applicationhost.config - TestUtility.LogInformation("Restoring applicationhost.config"); - IISConfigUtility.RestoreAppHostConfig(restoreFromMasterBackupFile:true); - TestUtility.StartW3svc(); - - // check w3svc is running after resetting applicationhost.config - if (IISConfigUtility.GetServiceStatus("w3svc") == "Running") - { - TestUtility.LogInformation("W3SVC service is restarted after restoring applicationhost.config"); - } - else - { - throw new System.ApplicationException("WWW service can't start"); - } - - // check URLRewrite module exists - if (File.Exists(Path.Combine(IISConfigUtility.Strings.IIS64BitPath, "rewrite.dll"))) - { - TestUtility.LogInformation("Verified URL Rewrite module installed for IIS server"); - } - else - { - throw new System.ApplicationException("URL Rewrite module is not installed"); - } - - if (IISConfigUtility.ApppHostTemporaryBackupFileExtention == null) - { - throw new System.ApplicationException("Failed to backup applicationhost.config"); - } - IISConfigUtility.IsIISReady = true; - } + InitializeIISServer(); } - catch (Exception ex) - { - RollbackIISApplicationhostConfigFile(); - TestUtility.LogInformation("We will use IISExpress instead of IIS: " + ex.Message); - } - - string siteRootPath = Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "inetpub", "ANCMTest"); + + string siteRootPath = TestRootDirectory; if (!Directory.Exists(siteRootPath)) { + // + // Create a new directory and set the write permission for the SID of AuthenticatedUser + // Directory.CreateDirectory(siteRootPath); + DirectorySecurity sec = Directory.GetAccessControl(siteRootPath); + SecurityIdentifier authenticatedUser = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); + sec.AddAccessRule(new FileSystemAccessRule(authenticatedUser, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); + Directory.SetAccessControl(siteRootPath, sec); } foreach (string directory in Directory.GetDirectories(siteRootPath)) @@ -208,18 +329,12 @@ namespace AspNetCoreModule.Test.Framework } } - if (InitializeTestMachine.UsePrivateAspNetCoreFile == true) + // + // Intialize Private ANCM files for Full IIS server or IISExpress + // + if (TestFlags.Enabled(TestFlags.UsePrivateANCM)) { PreparePrivateANCMFiles(); - - // update applicationhost.config for IIS server - if (IISConfigUtility.IsIISReady) - { - using (var iisConfig = new IISConfigUtility(ServerType.IIS, null)) - { - iisConfig.AddModule("AspNetCoreModule", FullIisAspnetcore_path, null); - } - } } _InitializeTestMachineCompleted = true; @@ -231,7 +346,7 @@ namespace AspNetCoreModule.Test.Framework if (_InitializeTestMachineCompleted) { break; - } + } else { TestUtility.LogInformation("InitializeTestMachine::InitializeTestMachine() Waiting..."); @@ -312,12 +427,24 @@ namespace AspNetCoreModule.Test.Framework { throw new ApplicationException("aspnetcore.dll is not available; check if there is any build issue!!!"); } - - // create an extra private copy of the private file on IIS directory - if (InitializeTestMachine.UsePrivateAspNetCoreFile == true) - { - bool updateSuccess = false; + // + // NOTE: + // ANCM schema file can't be overwritten here + // If there is any schema change, that should be updated with installing setup or manually copied with the new schema file. + // + + if (TestFlags.Enabled(TestFlags.UseIISExpress)) + { + // + // Initialize 32 bit IisExpressAspnetcore_path + // + IisExpressAspnetcore_path = Path.Combine(outputPath, "x64", "aspnetcore.dll"); + IisExpressAspnetcore_X86_path = Path.Combine(outputPath, "Win32", "aspnetcore.dll"); + } + else // if use Full IIS server + { + bool updateSuccess = false; for (int i = 0; i < 3; i++) { updateSuccess = false; @@ -326,28 +453,16 @@ namespace AspNetCoreModule.Test.Framework TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess); TestUtility.ResetHelper(ResetHelperMode.StopW3svcStartW3svc); Thread.Sleep(1000); - - string from = Path.Combine(outputPath, "x64", "aspnetcore.dll"); - TestUtility.FileCopy(from, FullIisAspnetcore_path, overWrite:true, ignoreExceptionWhileDeletingExistingFile:false); - TestUtility.FileCopy(from, IisExpressAspnetcore_path, overWrite: true, ignoreExceptionWhileDeletingExistingFile: false); - - // NOTE: schema file can't be overwritten, if there is any schema change, that should be updated manually - from = Path.Combine(outputPath, "x64", "aspnetcore_schema.xml"); - TestUtility.FileCopy(from, FullIisAspnetcoreSchema_path, overWrite: false, ignoreExceptionWhileDeletingExistingFile: false); - TestUtility.FileCopy(from, IisExpressAspnetcoreSchema_path, overWrite: false, ignoreExceptionWhileDeletingExistingFile: false); - + + // Copy private file on Inetsrv directory + TestUtility.FileCopy(Path.Combine(outputPath, "x64", "aspnetcore.dll"), FullIisAspnetcore_path, overWrite: true, ignoreExceptionWhileDeletingExistingFile: false); + if (TestUtility.IsOSAmd64) { - from = Path.Combine(outputPath, "Win32", "aspnetcore.dll"); - TestUtility.FileCopy(from, FullIisAspnetcore_X86_path, overWrite: true, ignoreExceptionWhileDeletingExistingFile: false); - TestUtility.FileCopy(from, IisExpressAspnetcore_X86_path, overWrite: true, ignoreExceptionWhileDeletingExistingFile: false); - - // NOTE: schema file can't be overwritten, if there is any schema change, that should be updated manually - from = Path.Combine(outputPath, "Win32", "aspnetcore_schema.xml"); - TestUtility.FileCopy(from, IisExpressAspnetcoreSchema_X86_path, overWrite: false, ignoreExceptionWhileDeletingExistingFile: false); + + // Copy 32bit private file on Inetsrv directory + TestUtility.FileCopy(Path.Combine(outputPath, "Win32", "aspnetcore.dll"), FullIisAspnetcore_X86_path, overWrite: true, ignoreExceptionWhileDeletingExistingFile: false); } - - updateSuccess = true; } catch @@ -363,6 +478,15 @@ namespace AspNetCoreModule.Test.Framework { throw new System.ApplicationException("Failed to update aspnetcore.dll"); } + + // update applicationhost.config for IIS server with the new private ASPNET Core file name + if (TestFlags.Enabled(TestFlags.UseFullIIS)) + { + using (var iisConfig = new IISConfigUtility(ServerType.IIS, null)) + { + iisConfig.AddModule("AspNetCoreModule", FullIisAspnetcore_path, null); + } + } } } diff --git a/test/AspNetCoreModule.Test/Framework/TestUtility.cs b/test/AspNetCoreModule.Test/Framework/TestUtility.cs index 0241427759..8238933fa7 100644 --- a/test/AspNetCoreModule.Test/Framework/TestUtility.cs +++ b/test/AspNetCoreModule.Test/Framework/TestUtility.cs @@ -287,7 +287,7 @@ namespace AspNetCoreModule.Test.Framework } if (File.Exists(from)) { - if (File.Exists(to) && overWrite == false) + if (File.Exists(to) && !overWrite) { return; } @@ -326,7 +326,7 @@ namespace AspNetCoreModule.Test.Framework if (File.Exists(from)) { - if (File.Exists(to) && overWrite == false) + if (File.Exists(to) && !overWrite) { return; } diff --git a/test/AspNetCoreModule.Test/Framework/TestWebSite.cs b/test/AspNetCoreModule.Test/Framework/TestWebSite.cs index 5f52097e94..79b4ab65a3 100644 --- a/test/AspNetCoreModule.Test/Framework/TestWebSite.cs +++ b/test/AspNetCoreModule.Test/Framework/TestWebSite.cs @@ -130,6 +130,21 @@ namespace AspNetCoreModule.Test.Framework } } + public string HostNameBinding + { + get + { + if (IisServerType == ServerType.IISExpress) + { + return "localhost"; + } + else + { + return ""; + } + } + } + public ServerType IisServerType { get; set; } public string IisExpressConfigPath { get; set; } private int _siteId { get; set; } @@ -138,15 +153,36 @@ namespace AspNetCoreModule.Test.Framework public TestWebSite(IISConfigUtility.AppPoolBitness appPoolBitness, string loggerPrefix = "ANCMTest", bool startIISExpress = true, bool copyAllPublishedFiles = false, bool attachAppVerifier = false) { _appPoolBitness = appPoolBitness; - + // - // Default server type is IISExpress. we, however, should use IIS server instead if IIS server is ready to use. + // Initialize IisServerType // - IisServerType = ServerType.IISExpress; - if (IISConfigUtility.IsIISReady) + if (TestFlags.Enabled(TestFlags.UseFullIIS)) { IisServerType = ServerType.IIS; } + else + { + IisServerType = ServerType.IISExpress; + } + + // + // Use localhost hostname for IISExpress + // + + + if (IisServerType == ServerType.IISExpress + && TestFlags.Enabled(TestFlags.Wow64BitMode)) + { + // + // In Wow64/IISExpress test context, always use 32 bit worker process + // + if (_appPoolBitness == IISConfigUtility.AppPoolBitness.noChange) + { + TestUtility.LogInformation("Warning!!! In Wow64, _appPoolBitness should be set with enable32bit"); + _appPoolBitness = IISConfigUtility.AppPoolBitness.enable32Bit; + } + } TestUtility.LogInformation("TestWebSite::TestWebSite() Start"); @@ -177,7 +213,7 @@ namespace AspNetCoreModule.Test.Framework { postfix = Path.GetRandomFileName(); siteName = loggerPrefix.Replace(" ", "") + "_" + postfix; - siteRootPath = Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "inetpub", "ANCMTest", siteName); + siteRootPath = Path.Combine(InitializeTestMachine.TestRootDirectory, siteName); if (!Directory.Exists(siteRootPath)) { break; @@ -199,14 +235,14 @@ namespace AspNetCoreModule.Test.Framework // Currently we use DotnetCore v2.0 // string publishPath = Path.Combine(srcPath, "bin", "Debug", "netcoreapp2.0", "publish"); - string publishPathOutput = Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%") + @"\", "inetpub", "ANCMTest", "publishPathOutput"); + string publishPathOutput = Path.Combine(InitializeTestMachine.TestRootDirectory, "publishPathOutput"); // // Publish aspnetcore app // if (_publishedAspnetCoreApp != true) { - string argumentForDotNet = "publish " + srcPath; + string argumentForDotNet = "publish " + srcPath + " --framework netcoreapp2.0"; TestUtility.LogInformation("TestWebSite::TestWebSite() StandardTestApp is not published, trying to publish on the fly: dotnet.exe " + argumentForDotNet); TestUtility.DeleteDirectory(publishPath); TestUtility.RunCommand("dotnet", argumentForDotNet); @@ -227,7 +263,7 @@ namespace AspNetCoreModule.Test.Framework { argumentFileName = "AspNetCoreModule.TestSites.Standard.dll"; } - iisConfig.CreateSite(tempSiteName, publishPathOutput, tempId, tempId); + iisConfig.CreateSite(tempSiteName, HostNameBinding, publishPathOutput, tempId, tempId); iisConfig.SetANCMConfig(tempSiteName, "/", "arguments", Path.Combine(publishPathOutput, argumentFileName)); iisConfig.DeleteSite(tempSiteName); } @@ -306,12 +342,26 @@ namespace AspNetCoreModule.Test.Framework } } - if (InitializeTestMachine.UsePrivateAspNetCoreFile == true && IisServerType == ServerType.IISExpress) + if (TestFlags.Enabled(TestFlags.UsePrivateANCM) && IisServerType == ServerType.IISExpress) { - iisConfig.AddModule("AspNetCoreModule", ("%IIS_BIN%\\" + InitializeTestMachine.PrivateFileName), null); + if (TestUtility.IsOSAmd64) + { + if (_appPoolBitness == IISConfigUtility.AppPoolBitness.enable32Bit) + { + iisConfig.AddModule("AspNetCoreModule", (InitializeTestMachine.IisExpressAspnetcore_X86_path), null); + } + else + { + iisConfig.AddModule("AspNetCoreModule", (InitializeTestMachine.IisExpressAspnetcore_path), null); + } + } + else + { + iisConfig.AddModule("AspNetCoreModule", (InitializeTestMachine.IisExpressAspnetcore_path), null); + } } - iisConfig.CreateSite(siteName, RootAppContext.PhysicalPath, _siteId, TcpPort, appPoolName); + iisConfig.CreateSite(siteName, HostNameBinding, RootAppContext.PhysicalPath, _siteId, TcpPort, appPoolName); iisConfig.CreateApp(siteName, AspNetCoreApp.Name, AspNetCoreApp.PhysicalPath, appPoolName); iisConfig.CreateApp(siteName, WebSocketApp.Name, WebSocketApp.PhysicalPath, appPoolName); iisConfig.CreateApp(siteName, URLRewriteApp.Name, URLRewriteApp.PhysicalPath, appPoolName); diff --git a/test/AspNetCoreModule.Test/FunctionalTest.cs b/test/AspNetCoreModule.Test/FunctionalTest.cs index 5f764cb801..01c41eb85e 100644 --- a/test/AspNetCoreModule.Test/FunctionalTest.cs +++ b/test/AspNetCoreModule.Test/FunctionalTest.cs @@ -12,7 +12,7 @@ namespace AspNetCoreModule.Test public class FunctionalTest : FunctionalTestHelper, IClassFixture { [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.Default)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.noChange)] @@ -23,7 +23,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.noChange, 5)] @@ -36,7 +36,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, 25, 19, false)] @@ -57,7 +57,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, 10)] @@ -70,7 +70,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -81,7 +81,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -92,7 +92,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -103,7 +103,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -114,7 +114,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -125,7 +125,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData("ANCMTestBar", "bar", "bar", IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -142,7 +142,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -153,7 +153,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -164,7 +164,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.Default)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789")] @@ -175,7 +175,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.Default)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -186,7 +186,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, 10)] @@ -197,7 +197,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, "00:02:00")] @@ -210,7 +210,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -221,7 +221,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, "dotnet.exe", "./")] @@ -234,7 +234,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, true)] @@ -247,7 +247,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, true, true)] @@ -260,7 +260,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -271,7 +271,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -282,7 +282,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, "MS-ASPNETCORE", "f")] @@ -297,7 +297,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit, true)] @@ -309,7 +309,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.noChange, false, DoAppVerifierTest_StartUpMode.UseGracefulShutdown, DoAppVerifierTest_ShutDownMode.RecycleAppPool, 1)] @@ -325,7 +325,7 @@ namespace AspNetCoreModule.Test ////////////////////////////////////////////////////////// [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [InlineData(IISConfigUtility.AppPoolBitness.enable32Bit)] @@ -339,7 +339,7 @@ namespace AspNetCoreModule.Test // NOTE: below test scenarios are not valid for Win7 OS ////////////////////////////////////////////////////////// [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "IIS does not support Websocket on Win7")] @@ -353,7 +353,7 @@ namespace AspNetCoreModule.Test } [ConditionalTheory] - [ANCMTestSkipCondition("%ANCMTestFlags%")] + [ANCMTestFlags(TestFlags.RequireRunAsAdministrator)] [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, SkipReason = "WAS does not handle private memory limitation with Job object on Win7")] @@ -363,5 +363,5 @@ namespace AspNetCoreModule.Test { return DoRecylingAppPoolTest(appPoolBitness); } - } -} + } +} \ No newline at end of file diff --git a/test/AspNetCoreModule.Test/FunctionalTestHelper.cs b/test/AspNetCoreModule.Test/FunctionalTestHelper.cs index b2e6f126d7..d66c850f57 100644 --- a/test/AspNetCoreModule.Test/FunctionalTestHelper.cs +++ b/test/AspNetCoreModule.Test/FunctionalTestHelper.cs @@ -23,68 +23,31 @@ using Microsoft.AspNetCore.Testing.xunit; namespace AspNetCoreModule.Test { [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] - public class ANCMTestSkipCondition : Attribute, ITestCondition + public class ANCMTestFlags : Attribute, ITestCondition { - private readonly string _environmentVariableName; - public ANCMTestSkipCondition(string environmentVariableName) + private readonly string _attributeValue; + public ANCMTestFlags(string attributeValue) { - _environmentVariableName = environmentVariableName; + _attributeValue = attributeValue.ToString(); } public bool IsMet { get { - bool result = true; - if (_environmentVariableName == InitializeTestMachine.ANCMTestFlagsEnvironmentVariable) + if (InitializeTestMachine.GlobalTestFlags.Contains(TestFlags.SkipTest)) { - var envValue = Environment.ExpandEnvironmentVariables(_environmentVariableName); - if (string.IsNullOrEmpty(envValue)) - { - envValue = InitializeTestMachine.ANCMTestFlagsDefaultContext; - } - else - { - envValue += ";" + InitializeTestMachine.ANCMTestFlagsDefaultContext; - } - - // split tokens with ';' - var tokens = envValue.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); - foreach (string token in tokens) - { - if (token.Equals(InitializeTestMachine.ANCMTestFlagsDefaultContext, StringComparison.InvariantCultureIgnoreCase)) - { - try - { - if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) - { - throw new System.InvalidOperationException("this should be started with x64 process mode on 64 bit machine"); - } - - bool isElevated; - WindowsIdentity identity = WindowsIdentity.GetCurrent(); - WindowsPrincipal principal = new WindowsPrincipal(identity); - isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator); - if (!isElevated) - { - throw new System.ApplicationException("this should be started as an administrator"); - } - } - catch (Exception ex) - { - AdditionalInfo = ex.Message; - - result = false; - } - } - if (token.Equals(InitializeTestMachine.ANCMTestFlagsTestSkipContext, StringComparison.InvariantCultureIgnoreCase)) - { - AdditionalInfo = InitializeTestMachine.ANCMTestFlagsTestSkipContext + " is set"; - result = false; - } - } + AdditionalInfo = TestFlags.SkipTest + " is set"; + return false; } - return result; + + if (_attributeValue == TestFlags.RequireRunAsAdministrator + && !InitializeTestMachine.GlobalTestFlags.Contains(TestFlags.RunAsAdministrator)) + { + AdditionalInfo = _attributeValue + " is not belong to the given global test context(" + InitializeTestMachine.GlobalTestFlags + ")"; + return false; + } + return true; } } @@ -92,7 +55,7 @@ namespace AspNetCoreModule.Test { get { - return $"Skip condition: {_environmentVariableName}: this test case is skipped becauset {AdditionalInfo}."; + return $"Skip condition: ANCMTestFlags: this test case is skipped becauset {AdditionalInfo}."; } } @@ -998,7 +961,7 @@ namespace AspNetCoreModule.Test } int y = Convert.ToInt32(TestUtility.GetProcessWMIAttributeValue("w3wp.exe", "Handle", userName)); - Assert.True(x == y && foundVSJit == false, "worker process is not recycled after 30 seconds"); + Assert.True(x == y && !foundVSJit, "worker process is not recycled after 30 seconds"); string backupPocessIdBackendProcess = await GetResponse(testSite.AspNetCoreApp.GetUri("GetProcessId"), HttpStatusCode.OK); string newPocessIdBackendProcess = backupPocessIdBackendProcess; @@ -1322,7 +1285,7 @@ namespace AspNetCoreModule.Test string publicKey = iisConfig.GetCertificatePublicKey(thumbPrintForClientAuthentication, @"Cert:\CurrentUser\My"); bool setPasswordSeperately = false; - if (testSite.IisServerType == ServerType.IISExpress && IISConfigUtility.IsIISInstalled == true) + if (testSite.IisServerType == ServerType.IISExpress) { setPasswordSeperately = true; iisConfig.EnableOneToOneClientCertificateMapping(testSite.SiteName, ".\\" + userName, null, publicKey); @@ -1934,7 +1897,7 @@ namespace AspNetCoreModule.Test TestUtility.ResetHelper(ResetHelperMode.KillWorkerProcess); // cleanup windbg process incase it is still running - if (testResult == false) + if (!testResult) { TestUtility.RunPowershellScript("stop-process -Name windbg -Force -Confirm:$false 2> $null"); } @@ -1984,7 +1947,7 @@ namespace AspNetCoreModule.Test // Verify test result for (int i = 0; i < 3; i++) { - if (DoVerifyDataSentAndReceived(websocketClient) == false) + if (!DoVerifyDataSentAndReceived(websocketClient)) { // retrying after 1 second sleeping Thread.Sleep(1000); @@ -2210,7 +2173,7 @@ namespace AspNetCoreModule.Test result = reader.ReadToEnd(); outputStream.Close(); } - } + } } else {