Exemple #1
0
        public void TestNET2Caller_AppDomain3_CreateCom1()
        {
            Assembly net2Caller = Assembly.LoadFrom(TestAssemblyRelativePath);

            AppDomainSetup setup = new AppDomainSetup()
            {
                PrivateBinPath      = net2Caller.CodeBase,
                ApplicationBase     = net2Caller.CodeBase,
                ApplicationName     = "TestNET2Caller",
                SandboxInterop      = true,
                ShadowCopyFiles     = Boolean.TrueString,
                TargetFrameworkName = ".NETFramework,Version=v2.0"
            };

            System.Security.Policy.Evidence evidence = new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence);
            evidence.AddAssemblyEvidence(new System.Security.Policy.ApplicationDirectory(@"..\..\..\]BET[.Playground.Interop.COM.NET2Caller\bin\Debug\"));

            // create domain
            AppDomain net2CallerDomain = AppDomain.CreateDomain(
                "TestNET4Caller",
                evidence,
                setup
                );

            try
            {
                var handle = net2CallerDomain.CreateComInstanceFrom(net2Caller.ManifestModule.FullyQualifiedName, net2Caller.GetType().FullName);
                var prg    = handle.Unwrap();

                var callCom = prg.GetType().GetMethod("CallCom");
                var result  = callCom.Invoke(prg, null) as string;

                Assert.AreEqual <string>(ErrorMsg, result); // fail
                Assert.Inconclusive(InconclusiveMsg);
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2146233054)
                {
                    Assert.Fail($"Expected Fail 1: {ex.Message}");
                }
                if (ex.HResult == -2147024894)
                {
                    Assert.Fail($"Expected Fail 2: {ex.Message}");
                }
                if (ex.HResult == -2147024773)
                {
                    Assert.Fail($"Expected Fail 3: {ex.Message}");
                }

                Assert.Fail($"Unknown Fail: {ex.Message}");
            }
            finally
            {
                AppDomain.Unload(net2CallerDomain);
            }
        }
 static void CreateComInstance(string typeName)
 {
     try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         string    assemblyName  = currentDomain.FriendlyName;
         currentDomain.CreateComInstanceFrom(assemblyName, typeName);
     } catch (Exception e) {
         Console.WriteLine(e.Message);
     }
 }
        public virtual Object CreateInstanceWithSecurity(int dwFlag,
                                                         int dwZone,
                                                         string pURL,
                                                         string uniqueIdString,
                                                         string link,
                                                         string licenses)
        {
            if (pURL == null || Manager.UrlIs(pURL, Manager.URL_IS.URLIS_FILEURL))
            {
                throw new ArgumentException("pURL");
            }
            if ((dwFlag & CORIESECURITY_ZONE) == CORIESECURITY_ZONE && dwZone == 0)
            {
                throw new SecurityException();
            }
            new PermissionSet(PermissionState.Unrestricted).Assert();
            AppDomain proxy  = null;
            Object    result = null;
            string    CodeBase;
            string    Application = null;

            try {
                Manager.CanonizeURL(link, Manager.URL_CANONFLAGS.URL_WININET_COMPATIBILITY);
                if (!Manager.IsValidURL(pURL))
                {
                    throw new ArgumentException("pURL");
                }

                Manager.Log(this, true, "Creating instance of the object in the correct domain", "");
                Manager.Log(this, true, "pUrl = " + pURL, "");
                Manager.Log(this, true, "id = " + uniqueIdString, "");
                Manager.Log(this, true, "link = " + link, "");
                Manager.Log(this, true, "licenses = " + licenses, "");


                // Hack to convert unique id back into byte array;
                byte[] uniqueId = Manager.DecodeDomainId(uniqueIdString);

                if (uniqueId == null)
                {
                    Manager.Log(this, true, "No unqiue id was sent", "");
                    return(null);
                }

                // Get the codebase from the URL, it will be a protocol or a file location. File locations
                // do not have CodeBase set
                bool fHasCodeBase = Manager.GetCodeBase(pURL, out CodeBase, out Application);
                if (CodeBase != null)
                {
                    CodeBase = Manager.CanonizeURL(CodeBase, Manager.URL_CANONFLAGS.URL_UNESCAPE);
                }

                if (Application != null)
                {
                    Application = Application.Replace('\\', '/');
                }

                Manager.Log(this,
                            CodeBase != null,
                            "URL codeBase: " + CodeBase,
                            "URL codeBase: <null>");
                Manager.Log(this,
                            Application != null,
                            "URL application: " + Application,
                            "URL application: <null>");

                // If we have a link tag then get the code base from that.
                string configSite              = null; // Site where the configuration file is located
                string configLocation          = null; // Path from the site to the configuration file
                string configFile              = null; // Name of the configuration file
                bool   fFoundConfigurationSite = false;

                // If there is no link tag then the Application base is always the site.
                // When there is a link tag then the Application base is where the config
                // file is relative to the page.
                if (licenses != null && licenses.Length > 0)
                {
                    licenses = Manager.MakeFullLink(licenses, CodeBase, Application);
                }


                if (link != null && link.Length > 0)
                {
                    link = Manager.MakeFullLink(link, CodeBase, Application);

                    if (!Manager.IsValidURL(link))
                    {
                        throw new ArgumentException("link");
                    }

                    Manager.Log(this, true, "final link = " + link, "");

                    fFoundConfigurationSite = Manager.GetCodeBase(link,
                                                                  out configSite,
                                                                  out configLocation);
                    if (configSite != null)
                    {
                        configSite = Manager.CanonizeURL(configSite, Manager.URL_CANONFLAGS.URL_UNESCAPE);
                    }

                    Manager.GetConfigurationFile(link, out configLocation, out configFile);

                    Manager.Log(this,
                                configSite != null,
                                "Configuration site: " + configSite,
                                "Configuration site: <null>");

                    Manager.Log(this,
                                configLocation != null,
                                "Configuration location: " + configLocation,
                                "Configuration location: <null>");

                    Manager.Log(this,
                                configFile != null,
                                "Configuration fileName: " + configFile,
                                "Configuration fileName: <null>");

                    // We have a configuration site then it must match the pages site
                    if (configSite != null)
                    {
                        if (CodeBase == null ||
                            !Manager.AreTheSame(CodeBase, configSite))
                        {
                            throw new TypeLoadException(_typeName);
                        }
                    }

                    // If they put a site on the configuration file then the code base is set to the configFile
                    // location.
                    //                    if(fFoundConfigurationSite)
                    CodeBase = configLocation;
                    //                    else {
                    //                        StringBuilder sb = new StringBuilder();
                    //                        if(fHasCodeBase) {
                    //                            sb.Append(CodeBase);
                    //                            sb.Append("/");
                    //                        }
                    //                        sb.Append(Application);
                    //                        sb.Append("/");
                    //                        sb.Append(configLocation);
                    //                        CodeBase = sb.ToString();
                    //                    }
                }
                else if (!fHasCodeBase)
                {
                    CodeBase = Application;
                }

                // Rules:
                // 1. If we have a directory associated with the configuration file
                //    then application base is the codebase plus the directory
                string domainName = null;
                Manager.Log(this, true,
                            "Locating domain for " + CodeBase,
                            "");
                domainName = CodeBase;

                proxy = _host.LocateDomain(uniqueId, domainName);
                if (proxy == null)
                {
                    lock (_host) {
                        proxy = _host.LocateDomain(uniqueId, domainName);
                        if (proxy == null)
                        {
                            proxy = CreateProxy(dwZone,
                                                dwFlag,
                                                domainName,
                                                fHasCodeBase,
                                                CodeBase,
                                                pURL,
                                                configFile,
                                                uniqueId,
                                                licenses);
                        }
                        else
                        {
                            Manager.Log(this, true, "SOME ONE CREATED THE DOMAIN BEFORE I HAD a CHANCE", "");
                        }
                    }
                }
                else
                {
                    Manager.Log(this, true, "Do not have to create new domain", "");
                    Manager.Log(this, true, "Existing Domain:", "");
                    Manager.Log(this, true, proxy.ToString(), "");
                }

                // The assembly is assigned the attributes stored with this factory.
                // There is a new factory for each assembly downloaded from the web
                // so the attributes are unique to a specific assembly.

                Manager.Log(this, true, "Trying to create instance of type " + _assemblyName.CodeBase + "#" + _typeName, "");
                AssemblyHashAlgorithm id = AssemblyHashAlgorithm.SHA1;
                result = proxy.CreateComInstanceFrom(_assemblyName.CodeBase,
                                                     _typeName,
                                                     _fileHash,
                                                     id);

                Manager.Log(this, result == null, "Unable to create instance of type " + _assemblyName + "::" + _typeName,
                            "Created instance of type " + _assemblyName.CodeBase + "::" + _typeName);
                // @BUGBUG We need to do this why does it not work???
                if (result == null)
                {
                    throw new TypeLoadException(_typeName);
                }


#if _DEBUG
                if (configFile != null)
                {
                    String s = "Configuration file name " + proxy.SetupInformation.ConfigurationFile;
                    Manager.Log(this, true, s, "");
                }
#endif
            }
            catch (Exception e) {
                Manager.Log(this, true, e.ToString(), "");
                string entry = null;
                if (_assemblyName.CodeBase != null)
                {
                    int k = _assemblyName.CodeBase.LastIndexOf('/');
                    if (k != -1)
                    {
                        entry = _assemblyName.CodeBase.Substring(k + 1);
                    }
                }
                LogException(e, entry);
                throw e;
            }
            return(result);
        }
Exemple #4
0
        public void TestNET45Caller_AppDomain3_CreateCom2()
        {
            Assembly net45Caller = Assembly.LoadFrom(TestAssembly45RelativePath);

            AppDomainSetup setup = new AppDomainSetup()
            {
                PrivateBinPath  = net45Caller.CodeBase,
                ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                //ApplicationBase = net45Caller.CodeBase, // -> AssemblyLocator will crash
                ApplicationName     = "TestNET45Caller",
                SandboxInterop      = true,
                ShadowCopyFiles     = Boolean.TrueString,
                TargetFrameworkName = ".NETFramework,Version=v4.5",
            };

            setup.SetConfigurationBytes(System.Text.Encoding.UTF8.GetBytes(NET45Config));

            System.Security.Policy.Evidence evidence = new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence);
            evidence.AddAssemblyEvidence(new System.Security.Policy.ApplicationDirectory(@"..\..\..\]BET[.Playground.Interop.COM.NET45Caller\bin\Debug\"));

            // create domain
            AppDomain net45CallerDomain = AppDomain.CreateDomain(
                "TestNET45Caller",
                evidence,
                setup
                );

            _BET_.Playground.Core.AssemblyLocator.Init(net45CallerDomain);

            try
            {
                var handle = net45CallerDomain.CreateComInstanceFrom(net45Caller.ManifestModule.FullyQualifiedName, net45Caller.GetType().FullName);
                var prg    = handle.Unwrap();

                var callCom = prg.GetType().GetMethod("CallCom");
                var result  = callCom.Invoke(prg, null) as string;

                Assert.AreEqual <string>(ErrorMsg, result); // fail
                Assert.Inconclusive(InconclusiveMsg);
            }
            catch (Exception ex)
            {
                // Could not load type 'System.Reflection.RuntimeAssembly' from
                // assembly 'Playground.Interop.COM.NET2Caller, Version=1.0.0.0,
                // Culture =neutral, PublicKeyToken=null'
                if (ex.HResult == -2146233054)
                {
                    Assert.Fail($"Expected Fail 1: {ex.Message}");
                }
                // Could not load file or assembly 'Playground.Interop.COM.NET2Caller,
                // Version =1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of
                // its dependencies. Das System kann die angegebene Datei nicht finden.
                if (ex.HResult == -2147024894)
                {
                    Assert.Fail($"Expected Fail 2: {ex.Message}");
                }
                // Could not load file or assembly 'Playground.Interop.COM.NET2Caller,
                // Version =1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of
                // its dependencies. Die Syntax für den Dateinamen, Verzeichnisnamen
                // oder die Datenträgerbezeichnung ist falsch.
                if (ex.HResult == -2147024773)
                {
                    Assert.Fail($"Expected Fail 3: {ex.Message}");
                }

                Assert.Fail($"Unknown Fail: {ex.Message}");
            }
            finally
            {
                AppDomain.Unload(net45CallerDomain);
            }
        }
Exemple #5
0
        public void TestNET45Caller_AppDomain3_CreateCom1()
        {
            AppDomainSetup setup = new AppDomainSetup()
            {
                PrivateBinPath      = TestPath,
                ApplicationBase     = TestPath,
                ApplicationName     = "TestNET45Caller",
                SandboxInterop      = true,
                ShadowCopyFiles     = Boolean.TrueString,
                TargetFrameworkName = ".NETFramework,Version=v4.5"
            };

            System.Security.Policy.Evidence evidence = new System.Security.Policy.Evidence(AppDomain.CurrentDomain.Evidence);
            evidence.AddAssemblyEvidence(new System.Security.Policy.ApplicationDirectory(TestPath));

            // create domain
            AppDomain net45CallerDomain = AppDomain.CreateDomain(
                "TestNET45Caller",
                evidence,
                setup
                );

            Type proxy = typeof(Proxy);

            try
            {
                var handle = net45CallerDomain.CreateComInstanceFrom(proxy.Assembly.ManifestModule.FullyQualifiedName, proxy.FullName);
                var prg    = (Proxy)handle.Unwrap();

                var assembly = prg.GetAssembly(TestAssembly45RelativePath);
                var instance = Activator.CreateInstance(assembly.GetType(TestObject45));

                var call   = assembly.GetType(TestObject45).GetMethod(TestMethod);
                var result = call.Invoke(instance, null) as string;

                Assert.AreEqual <string>(ErrorMsg, result); // fail
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2146233054)
                {
                    Assert.Fail($"Expected Fail 1: {ex.Message}");
                }
                if (ex.HResult == -2147024894)
                {
                    Assert.Fail($"Expected Fail 2: {ex.Message}");
                }
                if (ex.HResult == -2147024773)
                {
                    Assert.Fail($"Expected Fail 3: {ex.Message}");
                }

                Assert.Fail($"Unknown Fail: {ex.Message}");
            }
            finally
            {
                AppDomain.Unload(net45CallerDomain);
            }

            Assert.Inconclusive(InconclusiveMsg);
        }