public void SdkMultilevelLookup_RegistryAccess() { // The purpose of this test is to verify that the product uses correct code to access // the registry to extract the path to search for SDKs. // Most of our tests rely on a shortcut which is to set _DOTNET_TEST_SDK_SELF_REGISTERED_DIR env variable // which will skip the registry reading code in the product and simply use the specified value. // This test is different since it actually runs the registry reading code. // Normally the reg key the product uses is in HKEY_LOCAL_MACHINE which is only writable as admin // so we would require the tests to run as admin to modify that key (and it may introduce races with other code running on the machine). // So instead the tests use _DOTENT_TEST_SDK_REGISTRY_PATH env variable to point to the produce to use // different registry key, inside the HKEY_CURRENT_USER hive which is writable without admin. // Note that the test creates a unique key (based on PID) for every run, to avoid collisions between parallel running tests. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Multi-level lookup is only supported on Windows. return; } using (var regKeyOverride = new RegisteredInstallKeyOverride()) { regKeyOverride.SetInstallLocation(_regDir, RepoDirectories.BuildArchitecture); // Add SDK versions AddAvailableSdkVersions(_regSdkBaseDir, "9999.0.4"); // Specified SDK version: none // Cwd: empty // User: empty // Exe: empty // Reg: 9999.0.4 // Expected: 9999.0.4 from reg dir DotNet.Exec("help") .WorkingDirectory(_currentWorkingDir) .WithUserProfile(_userDir) .Environment(s_DefaultEnvironment) .EnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "1") .EnvironmentVariable(Constants.TestOnlyEnvironmentVariables.RegistryPath, regKeyOverride.KeyPath) .CaptureStdOut() .CaptureStdErr() .Execute() .Should().Pass() .And.HaveStdErrContaining(Path.Combine(_regSelectedMessage, "9999.0.4", _dotnetSdkDllMessageTerminator)); } }
public void Framework_Dependent_AppHost_From_Global_Registry_Location_Succeeds() { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { return; } var fixture = sharedTestState.PortableAppFixture_Published .Copy(); // Since SDK doesn't support building framework dependent apphost yet, emulate that behavior // by creating the executable from apphost.exe var appExe = fixture.TestProject.AppExe; var appDllName = Path.GetFileName(fixture.TestProject.AppDll); string hostExeName = RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("apphost"); string builtAppHost = Path.Combine(sharedTestState.RepoDirectories.HostArtifacts, hostExeName); string appDir = Path.GetDirectoryName(appExe); string appDirHostExe = Path.Combine(appDir, hostExeName); // Make a copy of apphost first, replace hash and overwrite app.exe, rather than // overwrite app.exe and edit in place, because the file is opened as "write" for // the replacement -- the test fails with ETXTBSY (exit code: 26) in Linux when // executing a file opened in "write" mode. File.Copy(builtAppHost, appDirHostExe, true); using (var sha256 = SHA256.Create()) { // Replace the hash with the managed DLL name. var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes("foobar")); var hashStr = BitConverter.ToString(hash).Replace("-", "").ToLower(); AppHostExtensions.SearchAndReplace(appDirHostExe, Encoding.UTF8.GetBytes(hashStr), Encoding.UTF8.GetBytes(appDllName), true); } File.Copy(appDirHostExe, appExe, true); // Get the framework location that was built string builtDotnet = fixture.BuiltDotnet.BinPath; using (var regKeyOverride = new RegisteredInstallKeyOverride()) { string architecture = fixture.CurrentRid.Split('-')[1]; regKeyOverride.SetInstallLocation(builtDotnet, architecture); // Verify running with the default working directory Command.Create(appExe) .CaptureStdErr() .CaptureStdOut() .EnvironmentVariable(Constants.TestOnlyEnvironmentVariables.RegistryPath, regKeyOverride.KeyPath) .Execute() .Should().Pass() .And.HaveStdOutContaining("Hello World") .And.HaveStdOutContaining($"Framework Version:{sharedTestState.RepoDirectories.MicrosoftNETCoreAppVersion}"); // Verify running from within the working directory Command.Create(appExe) .WorkingDirectory(fixture.TestProject.OutputDirectory) .EnvironmentVariable(Constants.TestOnlyEnvironmentVariables.RegistryPath, regKeyOverride.KeyPath) .CaptureStdErr() .CaptureStdOut() .Execute() .Should().Pass() .And.HaveStdOutContaining("Hello World") .And.HaveStdOutContaining($"Framework Version:{sharedTestState.RepoDirectories.MicrosoftNETCoreAppVersion}"); } }