Beispiel #1
0
        static int Main(string[] doNotUse)
        {
            // RegFree COM is not supported on Windows Nano
            if (Utilities.IsWindowsNanoServer)
            {
                return(100);
            }

            // Initialize CoreShim and hostpolicymock
            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_ASSEMBLYNAME", "NETServer");
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_TYPENAME", "ConsumeNETServerTesting");

            try
            {
                using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                           0,
                           string.Empty,
                           string.Empty,
                           string.Empty))
                {
                    Validate_Activation();
                    Validate_CCW_Wasnt_Unwrapped();
                    Validate_Client_CCW_RCW();
                    Validate_Server_CCW_RCW();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Test Failure: {e}");
                return(101);
            }

            return(100);
        }
Beispiel #2
0
        public void Run()
        {
            Console.WriteLine($"Running {nameof(NETServerTest)}");

            // Initialize CoreShim and hostpolicymock
            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_ASSEMBLYNAME", "NETServer");
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_TYPENAME", "ConsumeNETServerTesting");

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                       0,
                       string.Empty,
                       string.Empty,
                       string.Empty))
            {
                Type    t   = Type.GetTypeFromCLSID(Guid.Parse(Server.Contract.Guids.ConsumeNETServerTesting));
                dynamic obj = Activator.CreateInstance(t);

                try
                {
                    Assert.True(obj.EqualByCCW(obj));
                    Assert.True(obj.NotEqualByRCW(obj));
                }
                finally
                {
                    obj.ReleaseResources();
                }
            }
        }
        private static void ValidateManagedServerActivation()
        {
            bool returnValid = !GlobalComWrappers.Instance.ReturnInvalid;

            // Initialize CoreShim and hostpolicymock
            HostPolicyMock.Initialize(Environment.CurrentDirectory, null);
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_ASSEMBLYNAME", "NETServer");
            Environment.SetEnvironmentVariable("CORESHIM_COMACT_TYPENAME", ManagedServerTypeName);

            using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(0, string.Empty, string.Empty, string.Empty))
            {
                Type t      = Type.GetTypeFromCLSID(Guid.Parse(Server.Contract.Guids.ConsumeNETServerTesting));
                var  server = Activator.CreateInstance(t);
                Assert.AreEqual(returnValid, server is FakeWrapper, $"Should{(returnValid ? string.Empty : "not")} have returned {nameof(FakeWrapper)} instance");
                object serverUnwrapped = GlobalComWrappers.Instance.LastComputeVtablesObject;
                Assert.AreEqual(ManagedServerTypeName, serverUnwrapped.GetType().Name);

                IntPtr ptr = Marshal.GetIUnknownForObject(server);
                var    obj = Marshal.GetObjectForIUnknown(ptr);
                Assert.AreEqual(server, obj);
                Assert.AreEqual(returnValid, obj is FakeWrapper, $"Should{(returnValid ? string.Empty : "not")} have returned {nameof(FakeWrapper)} instance");
                serverUnwrapped.GetType().GetMethod("NotEqualByRCW").Invoke(serverUnwrapped, new object[] { obj });
            }
        }
        unsafe static int Main(string[] args)
        {
            // Disable running on Windows 7 until IJW activation work is complete.
            if (Environment.OSVersion.Platform != PlatformID.Win32NT || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1))
            {
                return(100);
            }

            try
            {
                HostPolicyMock.Initialize(Environment.CurrentDirectory, null);

                Console.WriteLine("Verify that we can load an IJW assembly from native code.");
                string ijwModulePath   = Path.Combine(Environment.CurrentDirectory, "IjwNativeCallingManagedDll.dll");
                IntPtr ijwNativeHandle = NativeLibrary.Load(ijwModulePath);

                using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
                           0,
                           ijwModulePath,
                           string.Empty,
                           string.Empty))
                    fixed(char *path = ijwModulePath)
                    {
                        InMemoryAssemblyLoader.LoadInMemoryAssembly(ijwNativeHandle, (IntPtr)path);
                    }

                NativeEntryPointDelegate nativeEntryPoint = Marshal.GetDelegateForFunctionPointer <NativeEntryPointDelegate>(NativeLibrary.GetExport(ijwNativeHandle, "NativeEntryPoint"));

                Assert.AreEqual(100, nativeEntryPoint());

                Console.WriteLine("Test calls from managed to native to managed when an IJW assembly was first loaded via native.");

                Assembly   ijwAssemblyManaged = Assembly.Load("IjwNativeCallingManagedDll");
                Type       testType           = ijwAssemblyManaged.GetType("TestClass");
                object     testInstance       = Activator.CreateInstance(testType);
                MethodInfo testMethod         = testType.GetMethod("ManagedEntryPoint");

                Assert.AreEqual(100, (int)testMethod.Invoke(testInstance, null));

                MethodInfo changeReturnedValueMethod = testType.GetMethod("ChangeReturnedValue");
                MethodInfo getReturnValueMethod      = testType.GetMethod("GetReturnValue");

                int newValue = 42;
                changeReturnedValueMethod.Invoke(null, new object[] { newValue });

                Assert.AreEqual(newValue, (int)getReturnValueMethod.Invoke(null, null));

                // Native images are only loaded into memory once. As a result, the stubs in the vtfixup table
                // will always point to JIT stubs that exist in the first ALC that the module was loaded into.
                // As a result, if an IJW module is loaded into two different ALCs, or if the module is
                // first loaded via a native call and then loaded via the managed loader, the call stack can change ALCs when
                // jumping from managed->native->managed code within the IJW module.
                Assert.AreEqual(100, (int)testMethod.Invoke(testInstance, null));
                return(100);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                return(101);
            }
        }
Beispiel #5
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");
        }
Beispiel #6
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());
                }
            }
        }
Beispiel #7
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");
        }