1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.DynamicProxy
.Tests
18 using NUnit
.Framework
;
19 using System
.Reflection
.Emit
;
21 using System
.Reflection
;
24 public class ModuleScopeTestCase
27 public void ModuleScopeStoresModuleBuilder ()
29 ModuleScope scope
= new ModuleScope ();
30 ModuleBuilder one
= scope
.ObtainDynamicModuleWithStrongName ();
31 ModuleBuilder two
= scope
.ObtainDynamicModuleWithStrongName ();
33 Assert
.AreSame (one
, two
);
34 Assert
.AreSame (one
.Assembly
, two
.Assembly
);
38 public void ModuleScopeCanHandleSignedAndUnsignedInParallel ()
40 ModuleScope scope
= new ModuleScope ();
41 Assert
.IsNull (scope
.StrongNamedModule
);
42 Assert
.IsNull (scope
.WeakNamedModule
);
44 ModuleBuilder one
= scope
.ObtainDynamicModuleWithStrongName ();
45 Assert
.IsNotNull (scope
.StrongNamedModule
);
46 Assert
.IsNull (scope
.WeakNamedModule
);
47 Assert
.AreSame (one
, scope
.StrongNamedModule
);
49 ModuleBuilder two
= scope
.ObtainDynamicModuleWithWeakName ();
50 Assert
.IsNotNull (scope
.StrongNamedModule
);
51 Assert
.IsNotNull (scope
.WeakNamedModule
);
52 Assert
.AreSame (two
, scope
.WeakNamedModule
);
54 Assert
.AreNotSame (one
, two
);
55 Assert
.AreNotSame (one
.Assembly
, two
.Assembly
);
57 ModuleBuilder three
= scope
.ObtainDynamicModuleWithStrongName ();
58 ModuleBuilder four
= scope
.ObtainDynamicModuleWithWeakName ();
60 Assert
.AreSame (one
, three
);
61 Assert
.AreSame (two
, four
);
67 public void ImplicitModulePaths ()
69 ModuleScope scope
= new ModuleScope (true);
70 Assert
.AreEqual (ModuleScope
.DEFAULT_FILE_NAME
, scope
.StrongNamedModuleName
);
71 Assert
.AreEqual (Path
.Combine (Environment
.CurrentDirectory
, ModuleScope
.DEFAULT_FILE_NAME
),
72 scope
.ObtainDynamicModuleWithStrongName ().FullyQualifiedName
);
73 Assert
.IsNull (scope
.StrongNamedModuleDirectory
);
75 Assert
.AreEqual (ModuleScope
.DEFAULT_FILE_NAME
, scope
.WeakNamedModuleName
);
76 Assert
.AreEqual (Path
.Combine (Environment
.CurrentDirectory
, ModuleScope
.DEFAULT_FILE_NAME
),
77 scope
.ObtainDynamicModuleWithWeakName ().FullyQualifiedName
);
78 Assert
.IsNull (scope
.WeakNamedModuleDirectory
);
82 public void ExplicitModulePaths ()
84 ModuleScope scope
= new ModuleScope (true, "Strong", "StrongModule.dll", "Weak", "WeakModule.dll");
85 Assert
.AreEqual ("StrongModule.dll", scope
.StrongNamedModuleName
);
86 Assert
.AreEqual (Path
.Combine (Environment
.CurrentDirectory
, "StrongModule.dll"), scope
.ObtainDynamicModuleWithStrongName ().FullyQualifiedName
);
87 Assert
.IsNull (scope
.StrongNamedModuleDirectory
);
89 Assert
.AreEqual ("WeakModule.dll", scope
.WeakNamedModuleName
);
90 Assert
.AreEqual (Path
.Combine (Environment
.CurrentDirectory
, "WeakModule.dll"), scope
.ObtainDynamicModuleWithWeakName ().FullyQualifiedName
);
91 Assert
.IsNull (scope
.WeakNamedModuleDirectory
);
93 scope
= new ModuleScope (true, "Strong", @"c:\Foo\StrongModule.dll", "Weak", @"d:\Bar\WeakModule.dll");
94 Assert
.AreEqual ("StrongModule.dll", scope
.StrongNamedModuleName
);
95 Assert
.AreEqual (@"c:\Foo\StrongModule.dll", scope
.ObtainDynamicModuleWithStrongName ().FullyQualifiedName
);
96 Assert
.AreEqual (@"c:\Foo", scope
.StrongNamedModuleDirectory
);
98 Assert
.AreEqual ("WeakModule.dll", scope
.WeakNamedModuleName
);
99 Assert
.AreEqual (@"d:\Bar\WeakModule.dll", scope
.ObtainDynamicModuleWithWeakName ().FullyQualifiedName
);
100 Assert
.AreEqual (@"d:\Bar", scope
.WeakNamedModuleDirectory
);
105 private static void CheckSignedSavedAssembly (string path
)
107 Assert
.IsTrue (File
.Exists (path
));
109 AssemblyName assemblyName
= AssemblyName
.GetAssemblyName (path
);
110 Assert
.AreEqual (ModuleScope
.DEFAULT_ASSEMBLY_NAME
, assemblyName
.Name
);
112 byte[] keyPairBytes
= ModuleScope
.GetKeyPair ();
113 StrongNameKeyPair keyPair
= new StrongNameKeyPair (keyPairBytes
);
114 byte[] loadedPublicKey
= assemblyName
.GetPublicKey ();
116 Assert
.AreEqual (keyPair
.PublicKey
.Length
, loadedPublicKey
.Length
);
117 for (int i
= 0; i
< keyPair
.PublicKey
.Length
; ++i
)
118 Assert
.AreEqual (keyPair
.PublicKey
[i
], loadedPublicKey
[i
]);
122 public void SaveSigned ()
124 ModuleScope scope
= new ModuleScope (true);
125 scope
.ObtainDynamicModuleWithStrongName ();
127 string path
= ModuleScope
.DEFAULT_FILE_NAME
;
128 if (File
.Exists (path
))
131 Assert
.IsFalse (File
.Exists (path
));
132 string savedPath
= scope
.SaveAssembly ();
134 Assert
.AreEqual (savedPath
, Path
.GetFullPath (path
));
136 CheckSignedSavedAssembly(path
);
141 public void SaveUnsigned ()
143 ModuleScope scope
= new ModuleScope (true);
144 scope
.ObtainDynamicModuleWithWeakName ();
146 string path
= ModuleScope
.DEFAULT_FILE_NAME
;
147 if (File
.Exists (path
))
150 Assert
.IsFalse (File
.Exists (path
));
151 string savedPath
= scope
.SaveAssembly ();
153 Assert
.AreEqual (savedPath
, Path
.GetFullPath (path
));
155 CheckUnsignedSavedAssembly(path
);
160 public void SaveWithPath ()
162 string strongModulePath
= Path
.GetTempFileName ();
163 string weakModulePath
= Path
.GetTempFileName ();
165 File
.Delete (strongModulePath
);
166 File
.Delete (weakModulePath
);
168 Assert
.IsFalse (File
.Exists (strongModulePath
));
169 Assert
.IsFalse (File
.Exists (weakModulePath
));
171 ModuleScope scope
= new ModuleScope (true, "Strong", strongModulePath
, "Weak", weakModulePath
);
172 scope
.ObtainDynamicModuleWithStrongName ();
173 scope
.ObtainDynamicModuleWithWeakName ();
175 scope
.SaveAssembly (true);
176 scope
.SaveAssembly (false);
178 Assert
.IsTrue (File
.Exists (strongModulePath
));
179 Assert
.IsTrue (File
.Exists (weakModulePath
));
181 File
.Delete (strongModulePath
);
182 File
.Delete (weakModulePath
);
185 private static void CheckUnsignedSavedAssembly (string path
)
187 Assert
.IsTrue (File
.Exists (path
));
189 AssemblyName assemblyName
= AssemblyName
.GetAssemblyName (path
);
190 Assert
.AreEqual (ModuleScope
.DEFAULT_ASSEMBLY_NAME
, assemblyName
.Name
);
192 byte[] loadedPublicKey
= assemblyName
.GetPublicKey ();
193 Assert
.IsNull (loadedPublicKey
);
197 public void SaveReturnsNullWhenNoModuleObtained ()
199 ModuleScope scope
= new ModuleScope (true);
200 Assert
.IsNull (scope
.SaveAssembly ());
204 [ExpectedException (typeof (InvalidOperationException
))]
205 public void SaveThrowsWhenMultipleAssembliesGenerated ()
207 ModuleScope scope
= new ModuleScope (true);
208 scope
.ObtainDynamicModuleWithStrongName ();
209 scope
.ObtainDynamicModuleWithWeakName ();
211 scope
.SaveAssembly ();
215 public void SaveWithFlagFalseDoesntThrowsWhenMultipleAssembliesGenerated ()
217 ModuleScope scope
= new ModuleScope (false);
218 scope
.ObtainDynamicModuleWithStrongName ();
219 scope
.ObtainDynamicModuleWithWeakName ();
221 scope
.SaveAssembly ();
225 public void ExplicitSaveWorksEvenWhenMultipleAssembliesGenerated ()
227 ModuleScope scope
= new ModuleScope (true);
228 scope
.ObtainDynamicModuleWithStrongName ();
229 scope
.ObtainDynamicModuleWithWeakName ();
231 scope
.SaveAssembly (true);
232 CheckSignedSavedAssembly (ModuleScope
.DEFAULT_FILE_NAME
);
234 scope
.SaveAssembly (false);
235 CheckUnsignedSavedAssembly (ModuleScope
.DEFAULT_FILE_NAME
);
237 File
.Delete (ModuleScope
.DEFAULT_FILE_NAME
);
241 [ExpectedException (typeof (InvalidOperationException
))]
242 public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedWeakName ()
244 ModuleScope scope
= new ModuleScope (true);
245 scope
.ObtainDynamicModuleWithStrongName ();
247 scope
.SaveAssembly (false);
251 [ExpectedException (typeof (InvalidOperationException
))]
252 public void ExplicitSaveThrowsWhenSpecifiedAssemblyNotGeneratedStrongName ()
254 ModuleScope scope
= new ModuleScope (true);
255 scope
.ObtainDynamicModuleWithWeakName ();
257 scope
.SaveAssembly (true);
261 public void GeneratedAssembliesDefaultName ()
263 ModuleScope scope
= new ModuleScope ();
264 ModuleBuilder strong
= scope
.ObtainDynamicModuleWithStrongName ();
265 ModuleBuilder weak
= scope
.ObtainDynamicModuleWithWeakName ();
267 Assert
.AreEqual (ModuleScope
.DEFAULT_ASSEMBLY_NAME
, strong
.Assembly
.GetName ().Name
);
268 Assert
.AreEqual (ModuleScope
.DEFAULT_ASSEMBLY_NAME
, weak
.Assembly
.GetName ().Name
);
272 public void GeneratedAssembliesWithCustomName ()
274 ModuleScope scope
= new ModuleScope (false, "Strong", "Module1.dll", "Weak", "Module2,dll");
275 ModuleBuilder strong
= scope
.ObtainDynamicModuleWithStrongName ();
276 ModuleBuilder weak
= scope
.ObtainDynamicModuleWithWeakName ();
278 Assert
.AreEqual ("Strong", strong
.Assembly
.GetName ().Name
);
279 Assert
.AreEqual ("Weak", weak
.Assembly
.GetName ().Name
);
283 public void ModuleScopeDoesntTryToDeleteFromCurrentDirectory ()
285 string moduleDirectory
= Path
.Combine (Environment
.CurrentDirectory
, "GeneratedDlls");
286 if (Directory
.Exists (moduleDirectory
))
287 Directory
.Delete (moduleDirectory
, true);
289 string strongModulePath
= Path
.Combine (moduleDirectory
, "Strong.dll");
290 string weakModulePath
= Path
.Combine (moduleDirectory
, "Weak.dll");
292 Directory
.CreateDirectory(moduleDirectory
);
293 ModuleScope scope
= new ModuleScope (true, "Strong", strongModulePath
, "Weak", weakModulePath
);
295 using (File
.Create (Path
.Combine (Environment
.CurrentDirectory
, "Strong.dll")))
297 scope
.ObtainDynamicModuleWithStrongName ();
298 scope
.SaveAssembly (true); // this will throw if SaveAssembly tries to delete from the current directory
301 using (File
.Create (Path
.Combine (Environment
.CurrentDirectory
, "Weak.dll")))
303 scope
.ObtainDynamicModuleWithWeakName ();
304 scope
.SaveAssembly (false); // this will throw if SaveAssembly tries to delete from the current directory
309 public void DefaultProxyBuilderWithSpecificScope ()
311 ModuleScope scope
= new ModuleScope (false);
312 DefaultProxyBuilder builder
= new DefaultProxyBuilder (scope
);
313 Assert
.AreSame (scope
, builder
.ModuleScope
);