コード例 #1
0
ファイル: Comhost.cs プロジェクト: directhex/core-setup
            public SharedTestState()
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // COM activation is only supported on Windows
                    return;
                }

                ComLibraryFixture = new TestProjectFixture("ComLibrary", RepoDirectories)
                                    .EnsureRestored(RepoDirectories.CorehostPackages)
                                    .BuildProject();

                ComHostPath = Path.Combine(
                    ComLibraryFixture.TestProject.BuiltApp.Location,
                    $"{ ComLibraryFixture.TestProject.AssemblyName }.comhost.dll");

                File.Copy(Path.Combine(RepoDirectories.CorehostPackages, "comhost.dll"), ComHostPath);

                RuntimeConfig.FromFile(ComLibraryFixture.TestProject.RuntimeConfigJson)
                .WithFramework(new RuntimeConfig.Framework("Microsoft.NETCore.App", RepoDirectories.MicrosoftNETCoreAppVersion))
                .Save();

                JObject clsidMap = new JObject()
                {
                    {
                        ClsidString,
                        new JObject()
                        {
                            { "assembly", "ComLibrary" }, { "type", "ComLibrary.Server" }
                        }
                    }
                };

                File.WriteAllText($"{ ComHostPath }.clsidmap", clsidMap.ToString());
            }
コード例 #2
0
            public SharedTestState()
            {
                var dotNet = new DotNetBuilder(BaseDirectory, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "mockRuntime")
                             .AddMicrosoftNETCoreAppFrameworkMockCoreClr(NetCoreAppVersion)
                             .Build();

                DotNetRoot = dotNet.BinPath;

                HostFxrPath = Path.Combine(
                    dotNet.GreatestVersionHostFxrPath,
                    RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));

                string appDir = Path.Combine(BaseDirectory, "app");

                Directory.CreateDirectory(appDir);
                AppPath = Path.Combine(appDir, "App.dll");
                File.WriteAllText(AppPath, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(appDir, "App.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(AppPropertyName, AppPropertyValue)
                .Save();

                AppPath_MultiProperty = Path.Combine(appDir, "App_MultiProperty.dll");
                File.WriteAllText(AppPath_MultiProperty, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(appDir, "App_MultiProperty.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(AppPropertyName, AppPropertyValue)
                .WithProperty(AppMultiPropertyName, AppMultiPropertyValue)
                .Save();

                string configDir = Path.Combine(BaseDirectory, "config");

                Directory.CreateDirectory(configDir);
                RuntimeConfigPath = Path.Combine(configDir, "Component.runtimeconfig.json");
                RuntimeConfig.FromFile(RuntimeConfigPath)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(ConfigPropertyName, ConfigPropertyValue)
                .Save();

                RuntimeConfigPath_MultiProperty = Path.Combine(configDir, "Component_MultiProperty.runtimeconfig.json");
                RuntimeConfig.FromFile(RuntimeConfigPath_MultiProperty)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(ConfigPropertyName, ConfigPropertyValue)
                .WithProperty(ConfigMultiPropertyName, ConfigMultiPropertyValue)
                .Save();

                string secondaryDir = Path.Combine(BaseDirectory, "secondary");

                Directory.CreateDirectory(secondaryDir);
                SecondaryRuntimeConfigPath = Path.Combine(secondaryDir, "Secondary.runtimeconfig.json");
                RuntimeConfig.FromFile(SecondaryRuntimeConfigPath)
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .WithProperty(SecondaryConfigPropertyName, SecondaryConfigPropertyValue)
                .Save();
            }
コード例 #3
0
            public SharedTestState()
            {
                DotNet = new DotNetBuilder(BaseDirectory, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "mockRuntime")
                         .AddMicrosoftNETCoreAppFrameworkMockCoreClr(NetCoreAppVersion)
                         .Build();

                HostFxrPath = Path.Combine(
                    DotNet.GreatestVersionHostFxrPath,
                    RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr"));

                AppDirectory = Path.Combine(BaseDirectory, "app");
                Directory.CreateDirectory(AppDirectory);
                AppPath = Path.Combine(AppDirectory, "App.dll");
                File.WriteAllText(AppPath, string.Empty);

                RuntimeConfig.FromFile(Path.Combine(AppDirectory, "App.runtimeconfig.json"))
                .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, NetCoreAppVersion))
                .Save();
            }
コード例 #4
0
            public void CreateSelfContainedApp(DotNetCli dotNet, string name, out string appPath, out string hostFxrPath, out string configPath)
            {
                string selfContainedDir = Path.Combine(BaseDirectory, name);

                Directory.CreateDirectory(selfContainedDir);
                appPath = Path.Combine(selfContainedDir, name + ".dll");
                File.WriteAllText(appPath, string.Empty);
                var toCopy = Directory.GetFiles(dotNet.GreatestVersionSharedFxPath)
                             .Concat(Directory.GetFiles(dotNet.GreatestVersionHostFxrPath));

                foreach (string file in toCopy)
                {
                    File.Copy(file, Path.Combine(selfContainedDir, Path.GetFileName(file)));
                }

                hostFxrPath = Path.Combine(selfContainedDir, Path.GetFileName(dotNet.GreatestVersionHostFxrFilePath));
                configPath  = Path.Combine(selfContainedDir, name + ".runtimeconfig.json");
                RuntimeConfig.FromFile(configPath)
                .WithProperty(AppPropertyName, AppPropertyValue)
                .Save();
            }
コード例 #5
0
        protected override void RunTest(Action <RuntimeConfig> runtimeConfigCustomizer, string testAssemblyName, string appAsmVersion, string appFileVersion, string frameworkWins)
        {
            var component = SharedState.CreateComponentWithNoDependencies(b => b
                                                                          .WithPackage(TestVersionsPackage, "1.0.0", lib => lib
                                                                                       .WithAssemblyGroup(null, g => g
                                                                                                          .WithAsset(testAssemblyName + ".dll", rf => rf
                                                                                                                     .WithVersion(appAsmVersion, appFileVersion)))));

            if (runtimeConfigCustomizer is not null)
            {
                var runtimeConfig = RuntimeConfig.FromFile(component.RuntimeConfigJson);
                runtimeConfigCustomizer(runtimeConfig);
                runtimeConfig.Save();
            }

            // For component dependency resolution, frameworks are not considered, so the assembly from the component always wins
            string expectedTestAssemblyPath = Path.Combine(component.Location, testAssemblyName + ".dll");

            SharedState.RunComponentResolutionTest(component)
            .Should().Pass()
            .And.HaveSuccessfullyResolvedComponentDependencies()
            .And.HaveResolvedComponentDependencyAssembly($"{component.AppDll};{expectedTestAssemblyPath}");
        }
コード例 #6
0
        public void CompatibilityCheck_Properties(string scenario, bool hasMultipleProperties, PropertyTestData[] properties)
        {
            if (scenario != Scenario.ConfigMultiple && scenario != Scenario.Mixed && scenario != Scenario.NonContextMixed)
            {
                throw new Exception($"Unexpected scenario: {scenario}");
            }

            string propertyCompatConfig = Path.Combine(sharedState.BaseDirectory, "propertyCompat.runtimeconfig.json");
            var    config = RuntimeConfig.FromFile(propertyCompatConfig)
                            .WithFramework(new RuntimeConfig.Framework(Constants.MicrosoftNETCoreApp, SharedTestState.NetCoreAppVersion));

            foreach (var kv in properties)
            {
                config.WithProperty(kv.Name, kv.NewValue);
            }

            config.Save();

            string appOrConfigPath = scenario == Scenario.ConfigMultiple
                ? hasMultipleProperties ? sharedState.RuntimeConfigPath_MultiProperty : sharedState.RuntimeConfigPath
                : hasMultipleProperties ? sharedState.AppPath_MultiProperty : sharedState.AppPath;

            string[] args =
            {
                HostContextArg,
                scenario,
                CheckProperties.None,
                sharedState.HostFxrPath,
                appOrConfigPath,
                propertyCompatConfig
            };

            CommandResult result;

            try
            {
                result = Command.Create(sharedState.NativeHostPath, args)
                         .CaptureStdErr()
                         .CaptureStdOut()
                         .EnvironmentVariable("COREHOST_TRACE", "1")
                         .EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
                         .EnvironmentVariable("DOTNET_ROOT", sharedState.DotNetRoot)
                         .EnvironmentVariable("DOTNET_ROOT(x86)", sharedState.DotNetRoot)
                         .EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
                         .EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
                         .Execute();
            }
            finally
            {
                File.Delete(propertyCompatConfig);
            }

            result.Should().Pass()
            .And.CreateDelegateMock_InMemoryAssembly();

            switch (scenario)
            {
            case Scenario.ConfigMultiple:
                result.Should()
                .InitializeContextForConfig(appOrConfigPath)
                .And.CreateDelegateMock_COM();
                break;

            case Scenario.Mixed:
                result.Should()
                .InitializeContextForApp(appOrConfigPath)
                .And.ExecuteAssemblyMock(appOrConfigPath, new string[0]);
                break;

            case Scenario.NonContextMixed:
                result.Should()
                .ExecuteAssemblyMock(appOrConfigPath, new string[0]);
                break;
            }

            bool shouldHaveDifferentProperties = false;

            foreach (var prop in properties)
            {
                if (prop.ExistingValue == null)
                {
                    shouldHaveDifferentProperties = true;
                    result.Should()
                    .HaveStdErrContaining($"The property [{prop.Name}] is not present in the previously loaded runtime");
                }
                else if (!prop.ExistingValue.Equals(prop.NewValue))
                {
                    shouldHaveDifferentProperties = true;
                    result.Should()
                    .InitializeSecondaryContext(propertyCompatConfig, Success_DifferentRuntimeProperties)
                    .And.HaveStdErrContaining($"The property [{prop.Name}] has a different value [{prop.NewValue}] from that in the previously loaded runtime [{prop.ExistingValue}]");
                }
            }

            if (shouldHaveDifferentProperties)
            {
                result.Should()
                .InitializeSecondaryContext(propertyCompatConfig, Success_DifferentRuntimeProperties);
            }
            else
            {
                result.Should()
                .InitializeSecondaryContext(propertyCompatConfig, Success_HostAlreadyInitialized);

                if (properties.Length > 0)
                {
                    result.Should()
                    .HaveStdErrContaining("All specified properties match those in the previously loaded runtime");
                }
            }
        }
コード例 #7
0
        public void CompatibilityCheck_Frameworks(string scenario, string frameworkName, string version, string rollForward, bool?isCompatibleVersion)
        {
            if (scenario != Scenario.ConfigMultiple && scenario != Scenario.Mixed && scenario != Scenario.NonContextMixed)
            {
                throw new Exception($"Unexpected scenario: {scenario}");
            }

            string frameworkCompatConfig = Path.Combine(sharedState.BaseDirectory, "frameworkCompat.runtimeconfig.json");

            RuntimeConfig.FromFile(frameworkCompatConfig)
            .WithFramework(new RuntimeConfig.Framework(frameworkName, version))
            .WithRollForward(rollForward)
            .Save();

            string appOrConfigPath = scenario == Scenario.ConfigMultiple ? sharedState.RuntimeConfigPath : sharedState.AppPath;

            string[] args =
            {
                HostContextArg,
                scenario,
                CheckProperties.None,
                sharedState.HostFxrPath,
                appOrConfigPath,
                frameworkCompatConfig
            };

            CommandResult result;

            try
            {
                result = Command.Create(sharedState.NativeHostPath, args)
                         .CaptureStdErr()
                         .CaptureStdOut()
                         .EnvironmentVariable("COREHOST_TRACE", "1")
                         .EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
                         .EnvironmentVariable("DOTNET_ROOT", sharedState.DotNetRoot)
                         .EnvironmentVariable("DOTNET_ROOT(x86)", sharedState.DotNetRoot)
                         .EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
                         .EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
                         .Execute();
            }
            finally
            {
                File.Delete(frameworkCompatConfig);
            }

            switch (scenario)
            {
            case Scenario.ConfigMultiple:
                result.Should()
                .InitializeContextForConfig(appOrConfigPath)
                .And.CreateDelegateMock_COM();
                break;

            case Scenario.Mixed:
                result.Should()
                .InitializeContextForApp(appOrConfigPath)
                .And.ExecuteAssemblyMock(appOrConfigPath, new string[0]);
                break;

            case Scenario.NonContextMixed:
                result.Should()
                .ExecuteAssemblyMock(appOrConfigPath, new string[0]);
                break;
            }

            if (isCompatibleVersion.HasValue)
            {
                if (isCompatibleVersion.Value)
                {
                    result.Should().Pass()
                    .And.InitializeSecondaryContext(frameworkCompatConfig, Success_HostAlreadyInitialized)
                    .And.CreateDelegateMock_InMemoryAssembly();
                }
                else
                {
                    result.Should().Fail()
                    .And.FailToInitializeContextForConfig(CoreHostIncompatibleConfig)
                    .And.HaveStdErrMatching($".*The specified framework '{frameworkName}', version '{version}', apply_patches=[0-1], version_compatibility_range=[^ ]* is incompatible with the previously loaded version '{SharedTestState.NetCoreAppVersion}'.*");
                }
            }
            else
            {
                result.Should().Fail()
                .And.FailToInitializeContextForConfig(CoreHostIncompatibleConfig)
                .And.HaveStdErrContaining($"The specified framework '{frameworkName}' is not present in the previously loaded runtime");
            }
        }
コード例 #8
0
        public void CompatibilityCheck_Frameworks(string scenario, FrameworkCompatibilityTestData testData)
        {
            if (scenario != Scenario.ConfigMultiple && scenario != Scenario.Mixed && scenario != Scenario.NonContextMixedAppHost && scenario != Scenario.NonContextMixedDotnet)
            {
                throw new Exception($"Unexpected scenario: {scenario}");
            }

            string frameworkName         = testData.Name;
            string version               = testData.Version;
            string frameworkCompatConfig = Path.Combine(sharedState.BaseDirectory, "frameworkCompat.runtimeconfig.json");

            RuntimeConfig.FromFile(frameworkCompatConfig)
            .WithFramework(new RuntimeConfig.Framework(frameworkName, version))
            .WithRollForward(testData.RollForward)
            .Save();

            string appOrConfigPath = scenario == Scenario.ConfigMultiple
                ? sharedState.RuntimeConfigPath
                : testData.ExistingContext switch
            {
                ExistingContextType.FrameworkDependent => sharedState.AppPath,
                ExistingContextType.SelfContained_NoIncludedFrameworks => sharedState.SelfContainedAppPath,
                ExistingContextType.SelfContained_WithIncludedFrameworks => sharedState.SelfContainedWithIncludedFrameworksAppPath,
                _ => throw new Exception($"Unexpected test data {nameof(testData.ExistingContext)}: {testData.ExistingContext}")
            };

            string hostfxrPath = scenario == Scenario.NonContextMixedDotnet
                ? sharedState.HostFxrPath // Imitating dotnet - always use the non-self-contained hostfxr
                : testData.ExistingContext switch
            {
                ExistingContextType.FrameworkDependent => sharedState.HostFxrPath,
                ExistingContextType.SelfContained_NoIncludedFrameworks => sharedState.SelfContainedHostFxrPath,
                ExistingContextType.SelfContained_WithIncludedFrameworks => sharedState.SelfContainedWithIncludedFrameworksHostFxrPath,
                _ => throw new Exception($"Unexpected test data {nameof(testData.ExistingContext)}: {testData.ExistingContext}")
            };

            string[] args =
            {
                HostContextArg,
                scenario,
                CheckProperties.None,
                hostfxrPath,
                appOrConfigPath,
                frameworkCompatConfig
            };

            CommandResult result;

            try
            {
                result = sharedState.CreateNativeHostCommand(args, sharedState.DotNetRoot)
                         .EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
                         .EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
                         .EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
                         .Execute();
            }
            finally
            {
                File.Delete(frameworkCompatConfig);
            }

            switch (scenario)
            {
            case Scenario.ConfigMultiple:
                result.Should()
                .InitializeContextForConfig(appOrConfigPath)
                .And.CreateDelegateMock_COM();
                break;

            case Scenario.Mixed:
                result.Should()
                .InitializeContextForApp(appOrConfigPath)
                .And.ExecuteAssemblyMock(appOrConfigPath, new string[0]);
                break;

            case Scenario.NonContextMixedAppHost:
            case Scenario.NonContextMixedDotnet:
                result.Should()
                .ExecuteAssemblyMock(appOrConfigPath, new string[0])
                .And.HaveStdErrContaining($"Mode: {(scenario == Scenario.NonContextMixedAppHost ? "apphost" : "muxer")}");
                break;
            }

            bool?isCompatibleVersion = testData.IsCompatible;

            if (isCompatibleVersion.HasValue)
            {
                if (isCompatibleVersion.Value)
                {
                    result.Should().Pass()
                    .And.InitializeSecondaryContext(frameworkCompatConfig, Success_HostAlreadyInitialized)
                    .And.CreateDelegateMock_InMemoryAssembly();
                }
                else
                {
                    result.Should().Fail()
                    .And.FailToInitializeContextForConfig(CoreHostIncompatibleConfig)
                    .And.HaveStdErrMatching($".*The specified framework '{frameworkName}', version '{version}', apply_patches=[0-1], version_compatibility_range=[^ ]* is incompatible with the previously loaded version '{SharedTestState.NetCoreAppVersion}'.*");
                }
            }
            else
            {
                result.Should().Fail()
                .And.FailToInitializeContextForConfig(CoreHostIncompatibleConfig)
                .And.HaveStdErrContaining($"The specified framework '{frameworkName}' is not present in the previously loaded runtime");
            }
        }