2 // System.Reflection.Assembly Test Cases
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Philippe Lavoie (philippe.lavoie@cactus.ca)
7 // Sebastien Pouliot (sebastien@ximian.com)
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
34 using System.Configuration.Assemblies;
35 using System.Globalization;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.Serialization;
40 using System.Security;
42 namespace MonoTests.System.Reflection
45 public class AssemblyTest
48 public void CreateInstance()
50 Type type = typeof (AssemblyTest);
51 Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
52 Assert.IsNotNull (obj, "#01");
53 Assert.AreEqual (GetType (), obj.GetType (), "#02");
57 public void CreateInvalidInstance()
59 Type type = typeof (AssemblyTest);
60 Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
61 Assert.IsNull (obj, "#03");
66 [Category ("NotWorking")]
67 [ExpectedException (typeof (ArgumentException))]
69 [ExpectedException (typeof (TypeLoadException))]
71 public void TestGetType ()
74 typeof (int).Assembly.GetType ("&blabla", true, true);
78 public void GetEntryAssembly ()
80 // note: only available in default appdomain
81 // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
82 // Not sure we should emulate this behavior.
83 string fname = AppDomain.CurrentDomain.FriendlyName;
84 if (fname.EndsWith (".dll")) { // nunit-console
85 Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
87 Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
90 Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
92 Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
97 [Category ("NotWorking")]
100 public void Corlib ()
102 Assembly corlib = typeof (int).Assembly;
103 Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
104 Assert.IsNull (corlib.EntryPoint, "EntryPoint");
105 Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
106 Assert.IsNotNull (corlib.Evidence, "Evidence");
107 Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
109 // corlib doesn't reference anything
110 Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
112 Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
113 // not really "true" but it's even more trusted so...
114 Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
115 Assert.AreEqual (0, corlib.HostContext, "HostContext");
116 Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
117 Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
118 Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
120 Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
121 Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
122 Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
127 public void Corlib_test ()
129 Assembly corlib_test = Assembly.GetExecutingAssembly ();
130 Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
131 Assert.IsNotNull (corlib_test.Evidence, "Evidence");
132 Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
134 Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
136 Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
137 Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
138 Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
139 Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
141 Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
146 public void GetAssembly ()
148 Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
149 Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
153 [ExpectedException (typeof (ArgumentNullException))]
154 public void GetFile_Null ()
156 Assembly.GetExecutingAssembly ().GetFile (null);
160 [ExpectedException (typeof (ArgumentException))]
161 public void GetFile_Empty ()
163 Assembly.GetExecutingAssembly ().GetFile (String.Empty);
167 public void GetFiles_False ()
169 Assembly corlib = typeof (int).Assembly;
170 FileStream[] fss = corlib.GetFiles ();
171 Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
173 Assembly corlib_test = Assembly.GetExecutingAssembly ();
174 fss = corlib_test.GetFiles ();
175 Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
179 public void GetFiles_True ()
181 Assembly corlib = typeof (int).Assembly;
182 FileStream[] fss = corlib.GetFiles ();
183 Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
185 Assembly corlib_test = Assembly.GetExecutingAssembly ();
186 fss = corlib_test.GetFiles ();
187 Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
192 [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
194 public void LoadFrom_Empty_Assembly ()
196 string tempFile = Path.GetTempFileName ();
199 Assembly.LoadFrom (tempFile);
201 } catch (BadImageFormatException ex) {
202 Assert.IsNull (ex.InnerException, "#2");
204 File.Delete (tempFile);
209 public void LoadFrom_Invalid_Assembly ()
211 string tempFile = Path.GetTempFileName ();
212 using (StreamWriter sw = File.CreateText (tempFile)) {
213 sw.WriteLine ("foo");
218 Assembly.LoadFrom (tempFile);
220 } catch (BadImageFormatException ex) {
221 Assert.IsNull (ex.InnerException, "#2");
223 File.Delete (tempFile);
228 public void LoadFrom_NonExisting_Assembly ()
230 string tempFile = Path.GetTempFileName ();
231 File.Delete (tempFile);
234 Assembly.LoadFrom (tempFile);
236 } catch (FileNotFoundException ex) {
237 Assert.IsNull (ex.InnerException, "#2");
239 File.Delete (tempFile);
244 public void LoadWithPartialName ()
246 string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
248 foreach (string s in names)
249 if (Assembly.LoadWithPartialName (s) != null)
251 Assertion.Fail ("Was not able to load any corlib test");
255 [ExpectedException (typeof (ArgumentNullException))]
256 public void GetObjectData_Null ()
258 Assembly corlib = typeof (int).Assembly;
259 corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
263 public void GetReferencedAssemblies ()
265 Assembly corlib_test = Assembly.GetExecutingAssembly ();
266 AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
267 foreach (AssemblyName an in names) {
268 Assert.IsNull (an.CodeBase, "CodeBase");
269 Assert.IsNotNull (an.CultureInfo, "CultureInfo");
270 Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
271 Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
272 Assert.IsNotNull (an.FullName, "FullName");
273 Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
274 Assert.IsNull (an.KeyPair, "KeyPair");
275 Assert.IsNotNull (an.Name, "Name");
276 Assert.IsNotNull (an.Version, "Version");
277 Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
278 an.VersionCompatibility, "VersionCompatibility");
283 public void Location_Empty() {
284 string assemblyFileName = Path.Combine (
285 Path.GetTempPath (), "AssemblyLocation.dll");
288 AssemblyName assemblyName = new AssemblyName ();
289 assemblyName.Name = "AssemblyLocation";
291 AssemblyBuilder ab = AppDomain.CurrentDomain
292 .DefineDynamicAssembly (assemblyName,
293 AssemblyBuilderAccess.Save,
295 AppDomain.CurrentDomain.Evidence);
296 ab.Save (Path.GetFileName (assemblyFileName));
298 using (FileStream fs = File.OpenRead (assemblyFileName)) {
299 byte[] buffer = new byte[fs.Length];
300 fs.Read (buffer, 0, buffer.Length);
301 Assembly assembly = Assembly.Load (buffer);
302 Assert.AreEqual (string.Empty, assembly.Location);
306 File.Delete (assemblyFileName);
311 [Category ("NotWorking")]
312 public void bug78464 ()
314 string assemblyFileName = Path.Combine (
315 Path.GetTempPath (), "bug78464.dll");
318 // execute test in separate appdomain to allow assembly to be unloaded
319 AppDomain testDomain = CreateTestDomain ();
320 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
322 crossDomainTester.bug78464 (assemblyFileName);
324 AppDomain.Unload (testDomain);
327 File.Delete (assemblyFileName);
332 [Category ("NotWorking")]
333 public void bug78465 ()
335 string assemblyFileName = Path.Combine (
336 Path.GetTempPath (), "bug78465.dll");
339 AssemblyName assemblyName = new AssemblyName ();
340 assemblyName.Name = "bug78465";
342 AssemblyBuilder ab = AppDomain.CurrentDomain
343 .DefineDynamicAssembly (assemblyName,
344 AssemblyBuilderAccess.Save,
345 Path.GetDirectoryName (assemblyFileName),
346 AppDomain.CurrentDomain.Evidence);
347 ab.Save (Path.GetFileName (assemblyFileName));
349 using (FileStream fs = File.OpenRead (assemblyFileName)) {
350 byte[] buffer = new byte[fs.Length];
351 fs.Read (buffer, 0, buffer.Length);
352 Assembly assembly = Assembly.Load (buffer);
353 Assert.AreEqual (string.Empty, assembly.Location, "#1");
357 AppDomain testDomain = CreateTestDomain ();
358 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
360 crossDomainTester.bug78465 (assemblyFileName);
362 AppDomain.Unload (testDomain);
365 File.Delete (assemblyFileName);
370 public void bug78468 ()
372 string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
374 string resourceFileName = Path.Combine (Path.GetTempPath (),
377 using (StreamWriter sw = File.CreateText (resourceFileName)) {
378 sw.WriteLine ("FOO");
383 AssemblyName assemblyName = new AssemblyName ();
384 assemblyName.Name = "bug78468a";
386 AssemblyBuilder ab = AppDomain.CurrentDomain
387 .DefineDynamicAssembly (assemblyName,
388 AssemblyBuilderAccess.Save,
390 AppDomain.CurrentDomain.Evidence);
391 ab.AddResourceFile ("read", "readme.txt");
392 ab.Save (Path.GetFileName (assemblyFileNameA));
396 using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
397 byte[] buffer = new byte[fs.Length];
398 fs.Read (buffer, 0, buffer.Length);
399 assembly = Assembly.Load (buffer);
403 Assert.AreEqual (string.Empty, assembly.Location, "#A1");
404 string[] resNames = assembly.GetManifestResourceNames ();
405 Assert.IsNotNull (resNames, "#A2");
406 Assert.AreEqual (1, resNames.Length, "#A3");
407 Assert.AreEqual ("read", resNames[0], "#A4");
408 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
409 Assert.IsNotNull (resInfo, "#A5");
410 Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
411 Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
412 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
413 Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
415 assembly.GetFile ("readme.txt");
416 Assert.Fail ("#A10");
417 } catch (FileNotFoundException) {
420 string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
423 AppDomain testDomain = CreateTestDomain ();
424 CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
426 crossDomainTester.bug78468 (assemblyFileNameB);
428 AppDomain.Unload (testDomain);
429 File.Delete (assemblyFileNameB);
432 File.Delete (assemblyFileNameA);
433 File.Delete (resourceFileName);
439 [Category ("NotWorking")]
440 public void ReflectionOnlyLoad ()
442 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
444 Assert.IsNotNull (assembly);
445 Assert.IsTrue (assembly.ReflectionOnly);
449 public void ReflectionOnlyLoadFrom ()
451 string loc = typeof (AssemblyTest).Assembly.Location;
452 string filename = Path.GetFileName (loc);
453 Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
455 Assert.IsNotNull (assembly);
456 Assert.IsTrue (assembly.ReflectionOnly);
460 [ExpectedException (typeof (ArgumentException))]
461 public void CreateInstanceOnRefOnly ()
463 Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
464 assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
468 private static AppDomain CreateTestDomain ()
470 return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
471 AppDomain.CurrentDomain.SetupInformation);
474 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
476 Type testerType = typeof (CrossDomainTester);
478 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
479 testerType.Assembly.FullName, testerType.FullName, false,
480 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
481 CultureInfo.InvariantCulture, new object[0], domain.Evidence);
484 private class CrossDomainTester : MarshalByRefObject
486 public void bug78464 (string assemblyFileName)
488 AssemblyName assemblyName = new AssemblyName ();
489 assemblyName.Name = "bug78464";
491 AssemblyBuilder ab = AppDomain.CurrentDomain
492 .DefineDynamicAssembly (assemblyName,
493 AssemblyBuilderAccess.Save,
494 Path.GetDirectoryName (assemblyFileName),
495 AppDomain.CurrentDomain.Evidence);
496 ab.Save (Path.GetFileName (assemblyFileName));
500 using (FileStream fs = File.OpenRead (assemblyFileName)) {
501 byte[] buffer = new byte[fs.Length];
502 fs.Read (buffer, 0, buffer.Length);
503 assembly = Assembly.Load (buffer);
507 Assert.AreEqual (string.Empty, assembly.Location, "#1");
509 assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
510 Assert.IsFalse (assembly.Location == string.Empty, "#2");
511 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
512 // note: we cannot check if directory names match, as MS.NET seems to
513 // convert directory part of assembly location to lowercase
514 Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
517 public void bug78465 (string assemblyFileName)
519 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
520 Assert.IsFalse (assembly.Location == string.Empty, "#2");
521 Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
522 // note: we cannot check if directory names match, as MS.NET seems to
523 // convert directory part of assembly location to lowercase
524 Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
527 public void bug78468 (string assemblyFileName)
529 AssemblyName assemblyName = new AssemblyName ();
530 assemblyName.Name = "bug78468b";
532 AssemblyBuilder ab = AppDomain.CurrentDomain
533 .DefineDynamicAssembly (assemblyName,
534 AssemblyBuilderAccess.Save,
535 Path.GetDirectoryName (assemblyFileName),
536 AppDomain.CurrentDomain.Evidence);
537 ab.AddResourceFile ("read", "readme.txt");
538 ab.Save (Path.GetFileName (assemblyFileName));
540 Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
541 Assert.IsTrue (assembly.Location != string.Empty, "#B1");
542 string[] resNames = assembly.GetManifestResourceNames ();
543 Assert.IsNotNull (resNames, "#B2");
544 Assert.AreEqual (1, resNames.Length, "#B3");
545 Assert.AreEqual ("read", resNames[0], "#B4");
546 ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
547 Assert.IsNotNull (resInfo, "#B5");
548 Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
549 Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
550 Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
551 Stream s = assembly.GetManifestResourceStream ("read");
552 Assert.IsNotNull (s, "#B9");
554 s = assembly.GetFile ("readme.txt");
555 Assert.IsNotNull (s, "#B10");