Ejemplo n.º 1
0
        static void ClassNotRegistered(bool builtInComDisabled)
        {
            Console.WriteLine($"Running {nameof(ClassNotRegistered)}...");

            Action action = () =>
            {
                var CLSID_NotRegistered = new Guid("328FF83E-3F6C-4BE9-A742-752562032925");     // Random GUID
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotRegistered,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = @"C:\foo.dll"
                };
                ComActivator.GetClassFactoryForType(cxt);
            };

            if (!builtInComDisabled)
            {
                COMException e = Assert.Throws <COMException>(action, "Class should not be found");
                const int    CLASS_E_CLASSNOTAVAILABLE = unchecked ((int)0x80040111);
                Assert.AreEqual(CLASS_E_CLASSNOTAVAILABLE, e.HResult, "Unexpected HRESULT");
            }
            else
            {
                Assert.Throws <NotSupportedException>(action, "Built-in COM has been disabled via a feature switch");
            }
        }
Ejemplo n.º 2
0
 static void InvalidInterfaceRequest()
 {
     Assert.Throws <NotSupportedException>(
         () =>
     {
         var notIClassFactory = new Guid("ED53F949-63E4-43B5-A13D-5655478AADD5");
         var cxt = new ComActivationContext()
         {
             InterfaceId = notIClassFactory
         };
         ComActivator.GetClassFactoryForType(cxt);
     },
         "Non-IClassFactory request should fail");
 }
Ejemplo n.º 3
0
        static void InvalidInterfaceRequest()
        {
            Console.WriteLine($"Running {nameof(InvalidInterfaceRequest)}...");

            Assert.Throws <NotSupportedException>(
                () =>
            {
                var notIClassFactory = new Guid("ED53F949-63E4-43B5-A13D-5655478AADD5");
                var cxt = new ComActivationContext()
                {
                    InterfaceId = notIClassFactory
                };
                ComActivator.GetClassFactoryForType(cxt);
            });
        }
Ejemplo n.º 4
0
        static void NonrootedAssemblyPath()
        {
            Console.WriteLine($"Running {nameof(NonrootedAssemblyPath)}...");

            ArgumentException e = Assert.Throws <ArgumentException>(
                () =>
            {
                var cxt = new ComActivationContext()
                {
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = "foo.dll"
                };
                ComActivator.GetClassFactoryForType(cxt);
            },
                "Non-root assembly path should not be valid");
        }
Ejemplo n.º 5
0
        static void ClassNotRegistered()
        {
            COMException e = Assert.Throws <COMException>(
                () =>
            {
                var CLSID_NotRegistered = new Guid("328FF83E-3F6C-4BE9-A742-752562032925");     // Random GUID
                var IID_IClassFactory   = new Guid("00000001-0000-0000-C000-000000000046");
                var cxt = new ComActivationContext()
                {
                    ClassId     = CLSID_NotRegistered,
                    InterfaceId = IID_IClassFactory
                };
                ComActivator.GetClassFactoryForType(cxt);
            },
                "Class should not be found");

            const int CLASS_E_CLASSNOTAVAILABLE = unchecked ((int)0x80040111);

            Assert.AreEqual(CLASS_E_CLASSNOTAVAILABLE, e.HResult, "Unexpected HRESULT");
        }
Ejemplo n.º 6
0
        static void ClassNotRegistered()
        {
            Console.WriteLine($"Running {nameof(ClassNotRegistered)}...");

            COMException e = Assert.Throws <COMException>(
                () =>
            {
                var CLSID_NotRegistered = new Guid("328FF83E-3F6C-4BE9-A742-752562032925");     // Random GUID
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotRegistered,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = @"C:\foo.dll"
                };
                ComActivator.GetClassFactoryForType(cxt);
            },
                "Class should not be found");

            const int CLASS_E_CLASSNOTAVAILABLE = unchecked ((int)0x80040111);

            Assert.AreEqual(CLASS_E_CLASSNOTAVAILABLE, e.HResult, "Unexpected HRESULT");
        }
Ejemplo n.º 7
0
        static void NonrootedAssemblyPath(bool builtInComDisabled)
        {
            Console.WriteLine($"Running {nameof(NonrootedAssemblyPath)}...");

            Action action = () =>
            {
                var cxt = new ComActivationContext()
                {
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = "foo.dll"
                };
                ComActivator.GetClassFactoryForType(cxt);
            };

            if (!builtInComDisabled)
            {
                Assert.Throws <ArgumentException>(action, "Non-root assembly path should not be valid");
            }
            else
            {
                Assert.Throws <NotSupportedException>(action, "Built-in COM has been disabled via a feature switch");
            }
        }
Ejemplo n.º 8
0
        static void NonrootedAssemblyPath(bool builtInComDisabled)
        {
            Console.WriteLine($"Running {nameof(NonrootedAssemblyPath)}...");

            Action action = () =>
            {
                var cxt = new ComActivationContext()
                {
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = "foo.dll"
                };
                ComActivator.GetClassFactoryForType(cxt);
            };

            if (!builtInComDisabled)
            {
                Assert.Throws <ArgumentException>(action);
            }
            else
            {
                Assert.Throws <NotSupportedException>(action);
            }
        }
Ejemplo n.º 9
0
        static void ValidateAssemblyIsolation()
        {
            Console.WriteLine($"Running {nameof(ValidateAssemblyIsolation)}...");

            string assemblySubPath = Path.Combine(Environment.CurrentDirectory, "Servers");
            string assemblyAPath   = Path.Combine(assemblySubPath, "AssemblyA.dll");
            string assemblyBPath   = Path.Combine(assemblySubPath, "AssemblyB.dll");
            string assemblyCPath   = Path.Combine(assemblySubPath, "AssemblyC.dll");
            string assemblyPaths   = $"{assemblyAPath}{Path.PathSeparator}{assemblyBPath}{Path.PathSeparator}{assemblyCPath}";

            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);

            var  CLSID_NotUsed = Guid.Empty; // During this phase of activation the GUID is not used.
            Guid iid           = typeof(IGetTypeFromC).GUID;
            Type typeCFromAssemblyA;
            Type typeCFromAssemblyB;

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotUsed,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = assemblyAPath,
                    AssemblyName = "AssemblyA",
                    TypeName     = "ClassFromA"
                };

                var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                object svr;
                factory.CreateInstance(null, ref iid, out svr);
                typeCFromAssemblyA = (Type)((IGetTypeFromC)svr).GetTypeFromC();
            }

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotUsed,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = assemblyBPath,
                    AssemblyName = "AssemblyB",
                    TypeName     = "ClassFromB"
                };

                var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                object svr;
                factory.CreateInstance(null, ref iid, out svr);
                typeCFromAssemblyB = (Type)((IGetTypeFromC)svr).GetTypeFromC();
            }

            Assert.AreNotEqual(typeCFromAssemblyA, typeCFromAssemblyB, "Types should be from different AssemblyLoadContexts");
        }
Ejemplo n.º 10
0
        static void ValidateUserDefinedRegistrationCallbacks()
        {
            Console.WriteLine($"Running {nameof(ValidateUserDefinedRegistrationCallbacks)}...");

            string assemblySubPath = Path.Combine(Environment.CurrentDirectory, "Servers");
            string assemblyAPath   = Path.Combine(assemblySubPath, "AssemblyA.dll");
            string assemblyBPath   = Path.Combine(assemblySubPath, "AssemblyB.dll");
            string assemblyCPath   = Path.Combine(assemblySubPath, "AssemblyC.dll");
            string assemblyPaths   = $"{assemblyAPath}{Path.PathSeparator}{assemblyBPath}{Path.PathSeparator}{assemblyCPath}";

            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);

            var  CLSID_NotUsed = Guid.Empty; // During this phase of activation the GUID is not used.
            Guid iid           = typeof(IValidateRegistrationCallbacks).GUID;

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                foreach (string typename in new[] { "ValidRegistrationTypeCallbacks", "ValidRegistrationStringCallbacks" })
                {
                    Console.WriteLine($"Validating {typename}...");

                    var cxt = new ComActivationContext()
                    {
                        ClassId      = CLSID_NotUsed,
                        InterfaceId  = typeof(IClassFactory).GUID,
                        AssemblyPath = assemblyAPath,
                        AssemblyName = "AssemblyA",
                        TypeName     = typename
                    };

                    var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                    object svr;
                    factory.CreateInstance(null, ref iid, out svr);

                    var inst = (IValidateRegistrationCallbacks)svr;
                    Assert.IsFalse(inst.DidRegister());
                    Assert.IsFalse(inst.DidUnregister());

                    cxt.InterfaceId = Guid.Empty;
                    ComActivator.ClassRegistrationScenarioForType(cxt, register: true);
                    ComActivator.ClassRegistrationScenarioForType(cxt, register: false);

                    Assert.IsTrue(inst.DidRegister());
                    Assert.IsTrue(inst.DidUnregister());
                }
            }

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                foreach (string typename in new[] { "NoRegistrationCallbacks", "InvalidArgRegistrationCallbacks", "InvalidInstanceRegistrationCallbacks", "MultipleRegistrationCallbacks" })
                {
                    Console.WriteLine($"Validating {typename}...");

                    var cxt = new ComActivationContext()
                    {
                        ClassId      = CLSID_NotUsed,
                        InterfaceId  = typeof(IClassFactory).GUID,
                        AssemblyPath = assemblyAPath,
                        AssemblyName = "AssemblyA",
                        TypeName     = typename
                    };

                    var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                    object svr;
                    factory.CreateInstance(null, ref iid, out svr);

                    var inst = (IValidateRegistrationCallbacks)svr;
                    cxt.InterfaceId = Guid.Empty;
                    bool exceptionThrown = false;
                    try
                    {
                        ComActivator.ClassRegistrationScenarioForType(cxt, register: true);
                    }
                    catch
                    {
                        exceptionThrown = true;
                    }

                    Assert.IsTrue(exceptionThrown || !inst.DidRegister());

                    exceptionThrown = false;
                    try
                    {
                        ComActivator.ClassRegistrationScenarioForType(cxt, register: false);
                    }
                    catch
                    {
                        exceptionThrown = true;
                    }

                    Assert.IsTrue(exceptionThrown || !inst.DidUnregister());
                }
            }
        }
Ejemplo n.º 11
0
        static void ValidateAssemblyIsolation(bool builtInComDisabled)
        {
            Console.WriteLine($"Running {nameof(ValidateAssemblyIsolation)}...");

            string assemblySubPath = Path.Combine(Environment.CurrentDirectory, "Servers");
            string assemblyAPath   = Path.Combine(assemblySubPath, "AssemblyA.dll");
            string assemblyBPath   = Path.Combine(assemblySubPath, "AssemblyB.dll");
            string assemblyCPath   = Path.Combine(assemblySubPath, "AssemblyC.dll");
            string assemblyPaths   = $"{assemblyAPath}{Path.PathSeparator}{assemblyBPath}{Path.PathSeparator}{assemblyCPath}";

            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);

            var  CLSID_NotUsed = Guid.Empty; // During this phase of activation the GUID is not used.
            Guid iid           = typeof(IGetTypeFromC).GUID;
            Type typeCFromAssemblyA;
            Type typeCFromAssemblyB;

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotUsed,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = assemblyAPath,
                    AssemblyName = "AssemblyA",
                    TypeName     = "ClassFromA"
                };

                if (builtInComDisabled)
                {
                    Assert.Throws <NotSupportedException>(
                        () => ComActivator.GetClassFactoryForType(cxt), "Built-in COM has been disabled via a feature switch");
                    return;
                }

                var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                IntPtr svrRaw;
                factory.CreateInstance(null, ref iid, out svrRaw);
                var svr = (IGetTypeFromC)Marshal.GetObjectForIUnknown(svrRaw);
                Marshal.Release(svrRaw);
                typeCFromAssemblyA = (Type)svr.GetTypeFromC();
            }

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       assemblyPaths,
                       string.Empty,
                       string.Empty))
            {
                var cxt = new ComActivationContext()
                {
                    ClassId      = CLSID_NotUsed,
                    InterfaceId  = typeof(IClassFactory).GUID,
                    AssemblyPath = assemblyBPath,
                    AssemblyName = "AssemblyB",
                    TypeName     = "ClassFromB"
                };

                var factory = (IClassFactory)ComActivator.GetClassFactoryForType(cxt);

                IntPtr svrRaw;
                factory.CreateInstance(null, ref iid, out svrRaw);
                var svr = (IGetTypeFromC)Marshal.GetObjectForIUnknown(svrRaw);
                Marshal.Release(svrRaw);
                typeCFromAssemblyB = (Type)svr.GetTypeFromC();
            }

            Assert.AreNotEqual(typeCFromAssemblyA, typeCFromAssemblyB, "Types should be from different AssemblyLoadContexts");
        }