Пример #1
0
        public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName()
        {
            var scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithWeakName();

            Assert.Throws <InvalidOperationException>(() => scope.SaveAssembly(true));
        }
Пример #2
0
        public static string[] SaveAssemblies(ModuleScope scope)
        {
            List <string> paths = new List <string> ();

            if (scope.StrongNamedModule != null)
            {
                scope.SaveAssembly(true);
                paths.Add(scope.StrongNamedModule.FullyQualifiedName);
            }

            if (scope.WeakNamedModule != null)
            {
                scope.SaveAssembly(false);
                paths.Add(scope.WeakNamedModule.FullyQualifiedName);
            }
            return(paths.ToArray());
        }
        public void SaveThrowsWhenMultipleAssembliesGenerated()
        {
            ModuleScope scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithStrongName();
            scope.ObtainDynamicModuleWithWeakName();

            scope.SaveAssembly();
        }
Пример #4
0
        public void SaveThrowsWhenMultipleAssembliesGenerated()
        {
            var scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithStrongName();
            scope.ObtainDynamicModuleWithWeakName();

            Assert.Throws <InvalidOperationException>(() => scope.SaveAssembly());
        }
Пример #5
0
        public void SaveWithFlagFalseDoesntThrowsWhenMultipleAssembliesGenerated()
        {
            var scope = new ModuleScope(false);

            scope.ObtainDynamicModuleWithStrongName();
            scope.ObtainDynamicModuleWithWeakName();

            scope.SaveAssembly();
        }
Пример #6
0
        public void LoadAssemblyIntoCache_DifferentGenerationOptions()
        {
            var savedScope = new ModuleScope(true);
            var builder    = new DefaultProxyBuilder(savedScope);

            var options1 = new ProxyGenerationOptions();

            options1.AddMixinInstance(new DateTime());
            var options2 = ProxyGenerationOptions.Default;

            var cp1 = builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options1);
            var cp2 = builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options2);

            Assert.AreNotSame(cp1, cp2);
            Assert.AreSame(
                cp1,
                builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options1)
                );
            Assert.AreSame(
                cp2,
                builder.CreateClassProxyType(typeof(object), Type.EmptyTypes, options2)
                );

            var path = savedScope.SaveAssembly();

            CrossAppDomainCaller.RunInOtherAppDomain(
                delegate(object[] args)
            {
                var newScope   = new ModuleScope(false);
                var newBuilder = new DefaultProxyBuilder(newScope);

                var assembly = Assembly.LoadFrom((string)args[0]);
                newScope.LoadAssemblyIntoCache(assembly);

                var newOptions1 = new ProxyGenerationOptions();
                newOptions1.AddMixinInstance(new DateTime());
                var newOptions2 = ProxyGenerationOptions.Default;

                var loadedCP1 = newBuilder.CreateClassProxyType(
                    typeof(object),
                    Type.EmptyTypes,
                    newOptions1
                    );
                var loadedCP2 = newBuilder.CreateClassProxyType(
                    typeof(object),
                    Type.EmptyTypes,
                    newOptions2
                    );
                Assert.AreNotSame(loadedCP1, loadedCP2);
                Assert.AreEqual(assembly, loadedCP1.Assembly);
                Assert.AreEqual(assembly, loadedCP2.Assembly);
            },
                path
                );

            File.Delete(path);
        }
Пример #7
0
        public void ModuleScopeDoesntTryToDeleteFromCurrentDirectory()
        {
            var moduleDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GeneratedDlls");

            if (Directory.Exists(moduleDirectory))
            {
                Directory.Delete(moduleDirectory, true);
            }

            var strongModulePath = Path.Combine(moduleDirectory, "Strong.dll");
            var weakModulePath   = Path.Combine(moduleDirectory, "Weak.dll");

            Directory.CreateDirectory(moduleDirectory);
            var scope = new ModuleScope(
                true,
                false,
                "Strong",
                strongModulePath,
                "Weak",
                weakModulePath
                );

            using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll")))
            {
                scope.ObtainDynamicModuleWithStrongName();
                scope.SaveAssembly(true); // this will throw if SaveAssembly tries to delete from the current directory
            }

            using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll")))
            {
                scope.ObtainDynamicModuleWithWeakName();
                scope.SaveAssembly(false); // this will throw if SaveAssembly tries to delete from the current directory
            }

            // Clean up the generated DLLs because the FileStreams are now closed
            Directory.Delete(moduleDirectory, true);
            File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll"));
            File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll"));
        }
Пример #8
0
        public void GenerateTypes()
        {
            const string assemblyName     = "Castle.Core.Tests.CustomModifiersTestCaseDynamicAssembly";
            const string assemblyFileName =
                "Castle.Core.Tests.CustomModifiersTestCaseDynamicAssembly.dll";

            var moduleScope = new ModuleScope(
                savePhysicalAssembly: true,
                disableSignedModule: false,
                namingScope: new Generators.NamingScope(),
                strongAssemblyName: assemblyName,
                strongModulePath: assemblyFileName,
                weakAssemblyName: assemblyName,
                weakModulePath: assemblyFileName
                );

            foreach (var partialTypeName in CustomModifiersTestCase.customModifiers.Keys)
            {
                var customModifiers = CustomModifiersTestCase.customModifiers[partialTypeName];
                this.AddTypeWithCustomModifiersAsModoptOnParamType(
                    moduleScope,
                    partialTypeName,
                    customModifiers
                    );
                this.AddTypeWithCustomModifiersAsModreqOnParamType(
                    moduleScope,
                    partialTypeName,
                    customModifiers
                    );
                this.AddTypeWithCustomModifiersAsModoptOnReturnType(
                    moduleScope,
                    partialTypeName,
                    customModifiers
                    );
                this.AddTypeWithCustomModifiersAsModreqOnReturnType(
                    moduleScope,
                    partialTypeName,
                    customModifiers
                    );
            }
#if FEATURE_ASSEMBLYBUILDER_SAVE
            if (IsVerificationPossible)
            {
                // Let's persist and PE-verify the dynamic assembly before it gets used in tests:
                var assemblyPath = moduleScope.SaveAssembly();
                RunPEVerifyOnGeneratedAssembly(assemblyPath);
            }
#endif
        }
        public void SavedAssemblyHasCacheMappings()
        {
            ModuleScope scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithWeakName();

            string savedPath = scope.SaveAssembly();

            CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args)
            {
                Assembly assembly = Assembly.LoadFrom((string)args[0]);
                Assert.IsTrue(assembly.IsDefined(typeof(CacheMappingsAttribute), false));
            },
                                                     savedPath);

            File.Delete(savedPath);
        }
        public void LoadAssemblyIntoCache_InvalidCacheAfterTwoLoadAssemblyIntoCacheThatContainsSameClass()
        {
            //
            // Step 1 - Save an assembly with 1 class proxy
            //
            var proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "5", "ProxyCache5.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "5", "ProxyCache5.dll");
            var proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            var generator    = new ProxyGenerator(proxyBuilder);

            generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface1) }, new DoNothingInterceptor());
            proxyGeneratorModuleScope.SaveAssembly();

            //
            // Step 2 - Save another assembly with 1 class proxy
            // note : to reproduce the problem, must load previously saved assembly (in cache) before saving this assembly.
            //
            proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "6", "ProxyCache6.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "6", "ProxyCache6.dll");
            proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            generator    = new ProxyGenerator(proxyBuilder);

            var proxyAssembly = Assembly.LoadFrom("ProxyCache5.dll");

            proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly);

            generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface2) }, new DoNothingInterceptor());
            proxyGeneratorModuleScope.SaveAssembly();

            //
            // Step 3 - Load the last proxy assembly and try to create the first class proxy (created in step 1)
            // note : Creating proxy from step 2 works.
            // issue : returns the wrong proxy (the one from step 2)
            //
            proxyGeneratorModuleScope = new ModuleScope(true);
            proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            generator    = new ProxyGenerator(proxyBuilder);

            proxyAssembly = Assembly.LoadFrom("ProxyCache6.dll");
            proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly);

            var invalidProxy = generator.CreateClassProxy(typeof(EmptyClass), new[] { typeof(IInterface1) }, new DoNothingInterceptor());

            if (invalidProxy as IInterface1 == null)
            {
                Assert.Fail();
            }
        }
        // Warning: Do not modify, this is called through reflection
        public static void GenerateAssembly(
            Assembly proxyAssembly,
            string newAssemblyName,
            bool ignoreAttributes,
            List <string> targetAssemblies,
            List <string> templates,
            string @namespace)
        {
            newAssemblyName += ".dll";
            ModuleScope scope = CreateModuleScope(newAssemblyName, newAssemblyName);

            foreach (Type type in proxyAssembly.GetTypes())
            {
                GenerateType(scope, @namespace, type);
            }

            scope.SaveAssembly();
        }
        public void LoadAssemblyIntoCache_InvalidCacheAfterTwoLoadAssemblyIntoCacheThatContainsSameGeneric()
        {
            //
            // Step 1 - Save an assembly with 1 generic proxy
            //
            var proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "1", "ProxyCache1.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "1", "ProxyCache1.dll");
            var proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            var generator    = new ProxyGenerator(proxyBuilder);

            generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface <IInterface1>), new Class1(), new DoNothingInterceptor());
            proxyGeneratorModuleScope.SaveAssembly();

            //
            // Step 2 - Save another assembly with 1 generic proxy
            // note : to reproduce the problem, must load previously saved assembly (in cache) before saving this assembly.
            //
            proxyGeneratorModuleScope = new ModuleScope(true, true, ModuleScope.DEFAULT_ASSEMBLY_NAME + "2", "ProxyCache2.dll", ModuleScope.DEFAULT_ASSEMBLY_NAME + "2", "ProxyCache2.dll");
            proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            generator    = new ProxyGenerator(proxyBuilder);

            var proxyAssembly = Assembly.LoadFrom("ProxyCache1.dll");

            proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly);

            generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface <IInterface2>), new Class2(), new DoNothingInterceptor());
            proxyGeneratorModuleScope.SaveAssembly();

            //
            // Step 3 - Load the last proxy assembly and try to create the first generic proxy (created in step 1)
            // note : Creating proxy from step 2 works.
            // exception : Missing method exception, it returns the wrong proxy and the constructor used doesnt match the arguments passed.
            //
            proxyGeneratorModuleScope = new ModuleScope(true);
            proxyBuilder = new DefaultProxyBuilder(proxyGeneratorModuleScope);
            generator    = new ProxyGenerator(proxyBuilder);

            proxyAssembly = Assembly.LoadFrom("ProxyCache2.dll");
            proxyGeneratorModuleScope.LoadAssemblyIntoCache(proxyAssembly);

            generator.CreateInterfaceProxyWithTargetInterface(typeof(IGenericInterface <IInterface1>), new Class1(), new DoNothingInterceptor());
        }
Пример #13
0
        public void SaveUnsigned()
        {
            var scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithWeakName();

            var path = ModuleScope.DEFAULT_FILE_NAME;

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            Assert.IsFalse(File.Exists(path));
            var savedPath = scope.SaveAssembly();

            Assert.AreEqual(savedPath, Path.GetFullPath(path));

            CheckUnsignedSavedAssembly(path);
            File.Delete(path);
        }
        public void SaveSigned()
        {
            ModuleScope scope = new ModuleScope(true);

            scope.ObtainDynamicModuleWithStrongName();

            string path = ModuleScope.DEFAULT_FILE_NAME;

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            Assert.IsFalse(File.Exists(path));
            string savedPath = scope.SaveAssembly();

            Assert.AreEqual(savedPath, Path.GetFullPath(path));

            CheckSignedSavedAssembly(path);
            File.Delete(path);
        }
        public void RunPEVerify()
        {
#if false
            if (module.SaveAssembly())
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments = ModuleScope.FILE_NAME;
                // Hack. Should it find in the path?
                // I thought it would.
                process.StartInfo.FileName = @"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\PEVerify.exe";
                process.Start();
                process.WaitForExit(Int32.MaxValue);
                if (process.ExitCode != 0)
                {
                    Assert.Fail(process.StandardOutput.ReadToEnd());
                }
            }
#endif
        }
Пример #16
0
        // ========================================
        // method
        // ========================================
        // === IEntityPersister ==========
        // --- proxy assembly ---
        public void SaveProxyAssembly(Assembly[] assems)
        {
            Contract.Requires(assems != null);

            var service = TypeService.Instance;

            foreach (var assem in assems)
            {
                var types = assem.GetTypes();
                foreach (var type in types)
                {
                    if (service.IsEntityDefined(type))
                    {
                        /// proxyをとりあえず作る
                        CreateTransientEntity(type);
                    }
                }
            }

            PathUtil.EnsureDirectoryExists(_scope.StrongNamedModuleDirectory);
            _scope.SaveAssembly(true);
        }
Пример #17
0
        public void CacheMappingsHoldTypes()
        {
            var scope   = new ModuleScope(true);
            var builder = new DefaultProxyBuilder(scope);
            var cp      = builder.CreateClassProxyType(
                typeof(object),
                Type.EmptyTypes,
                ProxyGenerationOptions.Default
                );

            var savedPath = scope.SaveAssembly();

            CrossAppDomainCaller.RunInOtherAppDomain(
                delegate(object[] args)
            {
                var assembly  = Assembly.LoadFrom((string)args[0]);
                var attribute = (CacheMappingsAttribute)assembly.GetCustomAttributes(
                    typeof(CacheMappingsAttribute),
                    false
                    )[0];
                var entries = attribute.GetDeserializedMappings();
                Assert.AreEqual(1, entries.Count);

                var key = new CacheKey(
                    typeof(object),
                    new Type[0],
                    ProxyGenerationOptions.Default
                    );
                Assert.IsTrue(entries.ContainsKey(key));
                Assert.AreEqual(args[1], entries[key]);
            },
                savedPath,
                cp.FullName
                );

            File.Delete(savedPath);
        }
Пример #18
0
    public void ModuleScopeDoesntTryToDeleteFromCurrentDirectory ()
    {
      string moduleDirectory = Path.Combine (Environment.CurrentDirectory, "GeneratedDlls");
      if (Directory.Exists (moduleDirectory))
        Directory.Delete (moduleDirectory, true);

      string strongModulePath = Path.Combine (moduleDirectory, "Strong.dll");
      string weakModulePath = Path.Combine (moduleDirectory, "Weak.dll");
      
      Directory.CreateDirectory(moduleDirectory);
      ModuleScope scope = new ModuleScope (true, "Strong", strongModulePath, "Weak", weakModulePath);

      using (File.Create (Path.Combine (Environment.CurrentDirectory, "Strong.dll")))
      {
        scope.ObtainDynamicModuleWithStrongName ();
        scope.SaveAssembly (true); // this will throw if SaveAssembly tries to delete from the current directory
      }

      using (File.Create (Path.Combine (Environment.CurrentDirectory, "Weak.dll")))
      {
        scope.ObtainDynamicModuleWithWeakName ();
        scope.SaveAssembly (false);  // this will throw if SaveAssembly tries to delete from the current directory
      }
    }
Пример #19
0
		public void LoadAssemblyIntoCache_DifferentGenerationOptions()
		{
			var savedScope = new ModuleScope(true);
			var builder = new DefaultProxyBuilder(savedScope);

			var options1 = new ProxyGenerationOptions();
			options1.AddMixinInstance(new DateTime());
			var options2 = ProxyGenerationOptions.Default;

			var cp1 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1);
			var cp2 = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2);
			Assert.AreNotSame(cp1, cp2);
			Assert.AreSame(cp1, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options1));
			Assert.AreSame(cp2, builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, options2));

			var path = savedScope.SaveAssembly();

			CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args)
			                                         	{
			                                         		var newScope = new ModuleScope(false);
			                                         		var newBuilder = new DefaultProxyBuilder(newScope);

			                                         		var assembly = Assembly.LoadFrom((string) args[0]);
			                                         		newScope.LoadAssemblyIntoCache(assembly);

			                                         		var newOptions1 = new ProxyGenerationOptions();
			                                         		newOptions1.AddMixinInstance(new DateTime());
			                                         		var newOptions2 = ProxyGenerationOptions.Default;

			                                         		var loadedCP1 = newBuilder.CreateClassProxyType(typeof (object),
			                                         		                                                Type.EmptyTypes,
			                                         		                                                newOptions1);
			                                         		var loadedCP2 = newBuilder.CreateClassProxyType(typeof (object),
			                                         		                                                Type.EmptyTypes,
			                                         		                                                newOptions2);
			                                         		Assert.AreNotSame(loadedCP1, loadedCP2);
			                                         		Assert.AreEqual(assembly, loadedCP1.Assembly);
			                                         		Assert.AreEqual(assembly, loadedCP2.Assembly);
			                                         	}, path);

			File.Delete(path);
		}
Пример #20
0
		public void CacheMappingsHoldTypes()
		{
			var scope = new ModuleScope(true);
			var builder = new DefaultProxyBuilder(scope);
			var cp = builder.CreateClassProxyType(typeof (object), Type.EmptyTypes, ProxyGenerationOptions.Default);

			var savedPath = scope.SaveAssembly();

			CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args)
			                                         	{
			                                         		var assembly = Assembly.LoadFrom((string) args[0]);
			                                         		var attribute =
			                                         			(CacheMappingsAttribute)
			                                         			assembly.GetCustomAttributes(typeof (CacheMappingsAttribute), false)[0];
			                                         		var entries = attribute.GetDeserializedMappings();
			                                         		Assert.AreEqual(1, entries.Count);

			                                         		var key = new CacheKey(typeof (object), new Type[0],
			                                         		                       ProxyGenerationOptions.Default);
			                                         		Assert.IsTrue(entries.ContainsKey(key));
			                                         		Assert.AreEqual(args[1], entries[key]);
			                                         	},
			                                         savedPath, cp.FullName);

			File.Delete(savedPath);
		}
        private void SaveAndVerifyModuleScopeAssembly(ModuleScope moduleScope, bool strongNamed)
        {
            var path = moduleScope.SaveAssembly(strongNamed);

            PEVerifier.CreateDefault().VerifyPEFile(path);
        }
Пример #22
0
		public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithWeakName();

			scope.SaveAssembly(true);
		}
Пример #23
0
		public void SavedAssemblyHasCacheMappings()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithWeakName();

			var savedPath = scope.SaveAssembly();

			CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args)
			                                         	{
			                                         		var assembly = Assembly.LoadFrom((string) args[0]);
			                                         		Assert.IsTrue(assembly.IsDefined(typeof (CacheMappingsAttribute), false));
			                                         	},
			                                         savedPath);

			File.Delete(savedPath);
		}
Пример #24
0
        protected virtual GenerateProxiesResult GenerateProxies(Configuration nhibernateConfiguration, string modulePath)
        {
            ModuleScope moduleScope = new ModuleScope(true, ModuleScope.DEFAULT_ASSEMBLY_NAME, modulePath, ModuleScope.DEFAULT_ASSEMBLY_NAME, modulePath );
            IDictionary proxies = new Hashtable();

            try
            {
                CastleProxyFactoryFactory.ProxyFactory = new CastleProxyFactory(new DefaultProxyBuilder(moduleScope), proxies);
                using (nhibernateConfiguration.BuildSessionFactory())
                {
                }
            }
            finally
            {
                CastleProxyFactoryFactory.ProxyFactory = null;
            }

            moduleScope.SaveAssembly();
            moduleScope = null;

            AssemblyName proxyAssemblyName = new AssemblyName(ModuleScope.DEFAULT_ASSEMBLY_NAME);
            proxyAssemblyName.CodeBase = modulePath;

            Assembly proxyAssembly = Assembly.Load(proxyAssemblyName);

            return new GenerateProxiesResult(proxies, proxyAssembly);
        }
Пример #25
0
		public void ModuleScopeDoesntTryToDeleteFromCurrentDirectory()
		{
			var moduleDirectory = Path.Combine(Directory.GetCurrentDirectory(), "GeneratedDlls");
			if (Directory.Exists(moduleDirectory))
				Directory.Delete(moduleDirectory, true);

			var strongModulePath = Path.Combine(moduleDirectory, "Strong.dll");
			var weakModulePath = Path.Combine(moduleDirectory, "Weak.dll");

			Directory.CreateDirectory(moduleDirectory);
			var scope = new ModuleScope(true, false, "Strong", strongModulePath, "Weak", weakModulePath);

			using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll")))
			{
				scope.ObtainDynamicModuleWithStrongName();
				scope.SaveAssembly(true); // this will throw if SaveAssembly tries to delete from the current directory
			}

			using (File.Create(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll")))
			{
				scope.ObtainDynamicModuleWithWeakName();
				scope.SaveAssembly(false); // this will throw if SaveAssembly tries to delete from the current directory
			}

			// Clean up the generated DLLs because the FileStreams are now closed
			Directory.Delete(moduleDirectory, true);
			File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Strong.dll"));
			File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "Weak.dll"));
		}
Пример #26
0
		public void SaveUnsigned()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithWeakName();

			var path = ModuleScope.DEFAULT_FILE_NAME;
			if (File.Exists(path))
				File.Delete(path);

			Assert.IsFalse(File.Exists(path));
			var savedPath = scope.SaveAssembly();

			Assert.AreEqual(savedPath, Path.GetFullPath(path));

			CheckUnsignedSavedAssembly(path);
			File.Delete(path);
		}
Пример #27
0
		private void CheckLoadAssemblyIntoCache(ProxyCreator creator)
		{
			var savedScope = new ModuleScope(true);
			var builder = new DefaultProxyBuilder(savedScope);

			var cp = creator(builder);
			Assert.AreSame(cp, creator(builder));

			var path = savedScope.SaveAssembly();

			CrossAppDomainCaller.RunInOtherAppDomain(delegate(object[] args)
			                                         	{
			                                         		var newScope = new ModuleScope(false);
			                                         		var newBuilder = new DefaultProxyBuilder(newScope);

			                                         		var assembly = Assembly.LoadFrom((string) args[0]);
			                                         		newScope.LoadAssemblyIntoCache(assembly);

			                                         		var loadedCP = assembly.GetType((string) args[1]);
			                                         		Assert.AreSame(loadedCP, ((ProxyCreator) args[2])(newBuilder));
			                                         		Assert.AreEqual(assembly, ((ProxyCreator) args[2])(newBuilder).Assembly);
			                                         	}, path, cp.FullName, creator);

			File.Delete(path);
		}
Пример #28
0
		public void SaveWithPath()
		{
			var strongModulePath = Path.GetTempFileName();
			var weakModulePath = Path.GetTempFileName();

			File.Delete(strongModulePath);
			File.Delete(weakModulePath);

			Assert.IsFalse(File.Exists(strongModulePath));
			Assert.IsFalse(File.Exists(weakModulePath));

			var scope = new ModuleScope(true,false, "Strong", strongModulePath, "Weak", weakModulePath);
			scope.ObtainDynamicModuleWithStrongName();
			scope.ObtainDynamicModuleWithWeakName();

			scope.SaveAssembly(true);
			scope.SaveAssembly(false);

			Assert.IsTrue(File.Exists(strongModulePath));
			Assert.IsTrue(File.Exists(weakModulePath));

			File.Delete(strongModulePath);
			File.Delete(weakModulePath);
		}
Пример #29
0
		public void SaveReturnsNullWhenNoModuleObtained()
		{
			var scope = new ModuleScope(true);
			Assert.IsNull(scope.SaveAssembly());
		}
Пример #30
0
		public void SaveSigned()
		{
			ModuleScope scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithStrongName();

			string path = ModuleScope.DEFAULT_FILE_NAME;
			if (File.Exists(path))
				File.Delete(path);

			Assert.IsFalse(File.Exists(path));
			string savedPath = scope.SaveAssembly();

			Assert.AreEqual(savedPath, Path.GetFullPath(path));

			CheckSignedSavedAssembly(path);
			File.Delete(path);
		}
Пример #31
0
        public void GenerateDynamicAssemblyHavingModopts()
        {
            // One test below tries to proxy a type that has a method whose signature
            // involves an optional modifier (modopt). These are never produced by the
            // C# nor VB.NET compilers, but the C++/CLI compiler produces those. However,
            // if we added a C++/CLI project to this solution, it could only be compiled
            // on Windows (since the only extant C++/CLI compiler is MSVC). So what we do
            // instead to get some modopts is to generate a dynamic test assembly.
            //
            // Instead of using System.Reflection.AssemblyBuilder directly, we use
            // Castle's own ModuleScope since that seems the easiest way to get a strong-
            // named persistent assembly that can be referenced by DynamicProxy.

            const string assemblyName     = "Rhino.Mocks.CPP.Interfaces";
            const string assemblyFileName = "Rhino.Mocks.CPP.Interfaces.dll";

            var moduleScope = new ModuleScope(
                savePhysicalAssembly: true,
                disableSignedModule: false,
                namingScope: new Generators.NamingScope(),
                strongAssemblyName: assemblyName,
                strongModulePath: assemblyFileName,
                weakAssemblyName: assemblyName,
                weakModulePath: assemblyFileName);

            // This is the C++/CLI type we want to generate:
            //
            //   namespace RhinoMocksCPPInterfaces {
            //       public interface class IHaveMethodWithModOpts {
            //	         virtual void StartLiveOnSlot(long int slotNumber);
            //       };
            //   }
            //
            // which corresponds to this IL:
            //
            //   .class interface public abstract auto ansi beforefieldinit RhinoMocksCPPInterfaces.IHaveMethodWithModOpts
            //   {
            //       .method public hidebysig newslot abstract virtual instance void StartLiveOnSlot(int32 modopt([mscorlib]System.Runtime.CompilerServices.IsLong) slotNumber) cil managed
            //       { }
            //   }

            var typeBuilder = moduleScope.DefineType(
                true,
                "RhinoMocksCPPInterfaces.IHaveMethodWithModOpts",
                TypeAttributes.Class | TypeAttributes.Interface | TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit);

            var methodBuilder = typeBuilder.DefineMethod(
                "StartLiveOnSlot",
                MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Abstract | MethodAttributes.Virtual,
                returnType: typeof(void),
                returnTypeRequiredCustomModifiers: null,
                returnTypeOptionalCustomModifiers: null,
                parameterTypes: new[]
            {
                typeof(int)
            },
                parameterTypeRequiredCustomModifiers: null,
                parameterTypeOptionalCustomModifiers: new[]
            {
                new[] { typeof(IsLong) }
            },
                callingConvention: CallingConventions.Standard);

            var iHaveMethodWithModOptsType = typeBuilder.CreateType();

#if FEATURE_ASSEMBLYBUILDER_SAVE && FEATURE_TEST_PEVERIFY
            // Let's persist and PE-verify the dynamic assembly before it gets used in tests:
            var assemblyPath = moduleScope.SaveAssembly();
            base.RunPEVerifyOnGeneratedAssembly(assemblyPath);
#endif

            this.iHaveMethodWithModOptsType = iHaveMethodWithModOptsType;
        }
Пример #32
0
		public void SaveUnsigned ()
		{
			ModuleScope scope = new ModuleScope (true);
			scope.ObtainDynamicModuleWithWeakName ();

			string path = ModuleScope.DEFAULT_FILE_NAME;
			if (File.Exists (path))
				File.Delete (path);

			Assert.IsFalse (File.Exists (path));
			scope.SaveAssembly ();

			CheckUnsignedSavedAssembly(path);
			File.Delete (path);
		}
Пример #33
0
        public void SaveReturnsNullWhenNoModuleObtained()
        {
            var scope = new ModuleScope(true);

            Assert.IsNull(scope.SaveAssembly());
        }
Пример #34
0
		public void ExplicitSaveWorksEvenWhenMultipleAssembliesGenerated()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithStrongName();
			scope.ObtainDynamicModuleWithWeakName();

			scope.SaveAssembly(true);
			CheckSignedSavedAssembly(ModuleScope.DEFAULT_FILE_NAME);

			scope.SaveAssembly(false);
			CheckUnsignedSavedAssembly(ModuleScope.DEFAULT_FILE_NAME);

			File.Delete(ModuleScope.DEFAULT_FILE_NAME);
		}
Пример #35
0
		public void SaveThrowsWhenNoModuleObtained ()
		{
			ModuleScope scope = new ModuleScope (true);
			scope.SaveAssembly ();
		}
Пример #36
0
		public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithWeakName();

			Assert.Throws<InvalidOperationException>(() => scope.SaveAssembly(true));
		}
Пример #37
0
		public void SaveThrowsWhenMultipleAssembliesGenerated()
		{
			var scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithStrongName();
			scope.ObtainDynamicModuleWithWeakName();

			Assert.Throws<InvalidOperationException>(() => scope.SaveAssembly());
		}
Пример #38
0
		public void SaveThrowsWhenMultipleAssembliesGenerated()
		{
			ModuleScope scope = new ModuleScope(true);
			scope.ObtainDynamicModuleWithStrongName();
			scope.ObtainDynamicModuleWithWeakName();

			scope.SaveAssembly();
		}
Пример #39
0
 /// <summary>
 ///   Saves the generated assembly to a physical file. Note that this renders the <see cref = "PersistentProxyBuilder" /> unusable.
 /// </summary>
 /// <returns>The path of the generated assembly file, or null if no assembly has been generated.</returns>
 /// <remarks>
 ///   This method does not support saving multiple files. If both a signed and an unsigned module have been generated, use the
 ///   respective methods of the <see cref = "ModuleScope" />.
 /// </remarks>
 public string SaveAssembly()
 {
     return(ModuleScope.SaveAssembly());
 }
Пример #40
0
		public void SaveWithFlagFalseDoesntThrowsWhenMultipleAssembliesGenerated()
		{
			var scope = new ModuleScope(false);
			scope.ObtainDynamicModuleWithStrongName();
			scope.ObtainDynamicModuleWithWeakName();

			scope.SaveAssembly();
		}