コード例 #1
0
        public void TestComponentLoadFailureWithPreviousErrorWriter()
        {
            IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(
                (HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); }));

            using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
                       HostPolicyMock.Mock_corehost_set_error_writer(previousWriter))
            {
                using (HostPolicyMock.MockValues_corehost_resolve_componet_dependencies resolverMock =
                           HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                               134,
                               "",
                               "",
                               ""))
                {
                    Assert.Throws <InvalidOperationException>(() =>
                    {
                        AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                            Path.Combine(TestBasePath, _componentAssemblyPath));
                    });

                    // After everything is done, the error writer should be reset to the original value.
                    Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr);
                }
            }
        }
コード例 #2
0
 protected override void Initialize()
 {
     HostPolicyMock.Initialize(TestBasePath, CoreRoot);
     _componentDirectory = Path.Combine(TestBasePath, $"TestComponent_{Guid.NewGuid().ToString().Substring(0, 8)}");
     Directory.CreateDirectory(_componentDirectory);
     _componentAssemblyPath = CreateMockAssembly("TestComponent.dll");
 }
コード例 #3
0
        public void TestAssembly()
        {
            string assemblyDependencyPath = CreateMockAssembly("AssemblyDependency.dll");

            IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(
                (HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); }));

            using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
                       HostPolicyMock.Mock_corehost_set_error_writer(previousWriter))
            {
                using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                           0,
                           assemblyDependencyPath,
                           "",
                           ""))
                {
                    AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                        Path.Combine(TestBasePath, _componentAssemblyPath));

                    Assert.Equal(
                        assemblyDependencyPath,
                        resolver.ResolveAssemblyToPath(new AssemblyName("AssemblyDependency")));

                    // After everything is done, the error writer should be reset to the original value.
                    Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr);
                }
            }
        }
コード例 #4
0
        private void ValidateNativeLibraryResolutions(
            string nativeLibraryPaths,
            string expectedResolvedFilePath,
            string lookupName,
            OS resolvesOnOSes)
        {
            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       "",
                       $"{nativeLibraryPaths}",
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                string result = resolver.ResolveUnmanagedDllToPath(lookupName);
                if (OperatingSystem.IsWindows())
                {
                    if (resolvesOnOSes.HasFlag(OS.Windows))
                    {
                        Assert.Equal(expectedResolvedFilePath, result);
                    }
                    else
                    {
                        Assert.Null(result);
                    }
                }
                else if (OperatingSystem.IsMacOS())
                {
                    if (resolvesOnOSes.HasFlag(OS.OSX))
                    {
                        Assert.Equal(expectedResolvedFilePath, result);
                    }
                    else
                    {
                        Assert.Null(result);
                    }
                }
                else
                {
                    if (resolvesOnOSes.HasFlag(OS.Linux))
                    {
                        Assert.Equal(expectedResolvedFilePath, result);
                    }
                    else
                    {
                        Assert.Null(result);
                    }
                }
            }
        }
コード例 #5
0
        public void TestAssemblyWithMissingFile()
        {
            // Even if the .deps.json can resolve the request, if the file is not present
            // the resolution should still return null.
            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       Path.Combine(_componentDirectory, "NonExistingAssembly.dll"),
                       "",
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Null(resolver.ResolveAssemblyToPath(new AssemblyName("NonExistingAssembly")));
            }
        }
コード例 #6
0
        public void TestAssemblyWithNoRecord()
        {
            // If the reqest is for assembly which is not listed in .deps.json
            // the resolver should return null.
            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       "",
                       "",
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Null(resolver.ResolveAssemblyToPath(new AssemblyName("AssemblyWithNoRecord")));
            }
        }
コード例 #7
0
        public void TestSingleNativeDependency()
        {
            string nativeLibraryPath = CreateMockStandardNativeLibrary("native", "Single");

            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       "",
                       Path.GetDirectoryName(nativeLibraryPath),
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Equal(
                    nativeLibraryPath,
                    resolver.ResolveUnmanagedDllToPath("Single"));
            }
        }
コード例 #8
0
        public void TestAssemblyWithNeutralCulture()
        {
            string neutralAssemblyPath = CreateMockAssembly("NeutralAssembly.dll");

            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       neutralAssemblyPath,
                       "",
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Equal(
                    neutralAssemblyPath,
                    resolver.ResolveAssemblyToPath(new AssemblyName("NeutralAssembly, Culture=neutral")));
            }
        }
コード例 #9
0
        public void TestSingleResource()
        {
            string enResourcePath = CreateMockAssembly($"en{Path.DirectorySeparatorChar}TestComponent.resources.dll");

            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       "",
                       "",
                       _componentDirectory))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Equal(
                    enResourcePath,
                    resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=en")));
            }
        }
コード例 #10
0
        protected override void Initialize()
        {
            // Make sure there's no hostpolicy available
            _officialHostPolicyPath = HostPolicyMock.DeleteExistingHostpolicy(CoreRoot);
            string hostPolicyFileName = XPlatformUtils.GetStandardNativeLibraryFileName("hostpolicy");

            _localHostPolicyPath   = Path.Combine(TestBasePath, hostPolicyFileName);
            _renamedHostPolicyPath = Path.Combine(TestBasePath, hostPolicyFileName + "_renamed");
            if (File.Exists(_renamedHostPolicyPath))
            {
                File.Delete(_renamedHostPolicyPath);
            }
            File.Move(_localHostPolicyPath, _renamedHostPolicyPath);

            _componentDirectory = Path.Combine(TestBasePath, $"InvalidHostingComponent_{Guid.NewGuid().ToString().Substring(0, 8)}");
            Directory.CreateDirectory(_componentDirectory);
            _componentAssemblyPath = Path.Combine(_componentDirectory, "InvalidHostingComponent.dll");
            File.WriteAllText(_componentAssemblyPath, "Mock assembly");
        }
コード例 #11
0
        public void TestComponentLoadFailure()
        {
            const string errorMessageFirstLine  = "First line: failure";
            const string errorMessageSecondLine = "Second line: value";

            using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
                       HostPolicyMock.Mock_corehost_set_error_writer())
            {
                using (HostPolicyMock.MockValues_corehost_resolve_componet_dependencies resolverMock =
                           HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                               134,
                               "",
                               "",
                               ""))
                {
                    // When the resolver is called, emulate error behavior
                    // which is to write to the error writer some error message.
                    resolverMock.Callback = (string componentAssemblyPath) =>
                    {
                        Assert.NotNull(errorWriterMock.LastSetErrorWriter);
                        errorWriterMock.LastSetErrorWriter(errorMessageFirstLine);
                        errorWriterMock.LastSetErrorWriter(errorMessageSecondLine);
                    };

                    string message = Assert.Throws <InvalidOperationException>(() =>
                    {
                        AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                            Path.Combine(TestBasePath, _componentAssemblyPath));
                    }).Message;

                    Assert.Contains("134", message);
                    Assert.Contains(
                        errorMessageFirstLine + Environment.NewLine + errorMessageSecondLine,
                        message);

                    // After everything is done, the error writer should be reset.
                    Assert.Null(errorWriterMock.LastSetErrorWriter);
                }
            }
        }
コード例 #12
0
        public void TestMultipleNativeDependencies()
        {
            string oneNativeLibraryPath = CreateMockStandardNativeLibrary($"native{Path.DirectorySeparatorChar}one", "One");
            string twoNativeLibraryPath = CreateMockStandardNativeLibrary($"native{Path.DirectorySeparatorChar}two", "Two");

            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       "",
                       $"{Path.GetDirectoryName(oneNativeLibraryPath)}{Path.PathSeparator}{Path.GetDirectoryName(twoNativeLibraryPath)}",
                       ""))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Equal(
                    oneNativeLibraryPath,
                    resolver.ResolveUnmanagedDllToPath("One"));
                Assert.Equal(
                    twoNativeLibraryPath,
                    resolver.ResolveUnmanagedDllToPath("Two"));
            }
        }
コード例 #13
0
        public void TestMutipleResourcesWithDifferentBasePath()
        {
            string enResourcePath = CreateMockAssembly($"en{Path.DirectorySeparatorChar}TestComponent.resources.dll");
            string frResourcePath = CreateMockAssembly($"SubComponent{Path.DirectorySeparatorChar}fr{Path.DirectorySeparatorChar}TestComponent.resources.dll");

            using (HostPolicyMock.Mock_corehost_resolve_componet_dependencies(
                       0,
                       "",
                       "",
                       $"{_componentDirectory}{Path.PathSeparator}{Path.GetDirectoryName(Path.GetDirectoryName(frResourcePath))}"))
            {
                AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
                    Path.Combine(TestBasePath, _componentAssemblyPath));

                Assert.Equal(
                    enResourcePath,
                    resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=en")));
                Assert.Equal(
                    frResourcePath,
                    resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=fr")));
            }
        }