| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118 |
- //
- // System.Reflection.Assembly Test Cases
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- // Philippe Lavoie ([email protected])
- // Sebastien Pouliot ([email protected])
- //
- // (c) 2003 Ximian, Inc. (http://www.ximian.com)
- // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using NUnit.Framework;
- using System;
- using System.Configuration.Assemblies;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- #if !TARGET_JVM
- using System.Reflection.Emit;
- #endif
- using System.Threading;
- using System.Runtime.Serialization;
- using System.Security;
- namespace MonoTests.System.Reflection
- {
- [TestFixture]
- public class AssemblyTest
- {
- static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.AssemblyTest");
- [SetUp]
- public void SetUp ()
- {
- while (Directory.Exists (TempFolder))
- TempFolder = Path.Combine (TempFolder, "2");
- Directory.CreateDirectory (TempFolder);
- }
- [TearDown]
- public void TearDown ()
- {
- try {
- // This throws an exception under MS.NET, since the directory contains loaded
- // assemblies.
- Directory.Delete (TempFolder, true);
- } catch (Exception) {
- }
- }
- [Test]
- public void CreateInstance()
- {
- Type type = typeof (AssemblyTest);
- Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
- Assert.IsNotNull (obj, "#01");
- Assert.AreEqual (GetType (), obj.GetType (), "#02");
- }
- [Test]
- public void CreateInvalidInstance()
- {
- Type type = typeof (AssemblyTest);
- Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
- Assert.IsNull (obj, "#03");
- }
- [Test] // bug #49114
- #if NET_2_0
- [Category ("NotWorking")]
- [ExpectedException (typeof (ArgumentException))]
- #else
- [ExpectedException (typeof (TypeLoadException))]
- #endif
- public void GetType_TypeName_Invalid ()
- {
- typeof (int).Assembly.GetType ("&blabla", true, true);
- }
- [Test] // bug #334203
- public void GetType_TypeName_AssemblyName ()
- {
- Assembly a = typeof (int).Assembly;
- string typeName = typeof (string).AssemblyQualifiedName;
- #if NET_2_0
- try {
- a.GetType (typeName, true, false);
- Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.IsNull (ex.ParamName, "#A5");
- }
- #else
- try {
- a.GetType (typeName, true, false);
- Assert.Fail ("#A1");
- } catch (TypeLoadException ex) {
- Assert.AreEqual (typeof (TypeLoadException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.IsTrue (ex.Message.IndexOf (typeName) != -1, "#A5");
- }
- #endif
- Type type = a.GetType (typeName, false);
- Assert.IsNull (type, "#B1");
- type = a.GetType (typeName, false, true);
- Assert.IsNull (type, "#B2");
- }
- [Test]
- public void GetEntryAssembly ()
- {
- // note: only available in default appdomain
- // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
- // Not sure we should emulate this behavior.
- string fname = AppDomain.CurrentDomain.FriendlyName;
- if (fname.EndsWith (".dll")) { // nunit-console
- Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
- #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
- Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
- #endif
- } else { // gnunit
- Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
- #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
- Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
- #endif
- }
- }
- #if !TARGET_JVM // Reflection.Emit is not supported.
- [Test]
- public void GetModules_MissingFile ()
- {
- AssemblyName newName = new AssemblyName ();
- newName.Name = "AssemblyTest";
- AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
- ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
- ab.Save ("test_assembly.dll");
- File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
- Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
- try {
- ass.GetModules ();
- Assert.Fail ();
- }
- catch (FileNotFoundException ex) {
- Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
- }
- }
- #endif
- #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
- #if NET_2_0
- [Category ("NotWorking")]
- #endif
- [Test]
- public void Corlib ()
- {
- Assembly corlib = typeof (int).Assembly;
- Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
- Assert.IsNull (corlib.EntryPoint, "EntryPoint");
- Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
- Assert.IsNotNull (corlib.Evidence, "Evidence");
- Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
- // corlib doesn't reference anything
- Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
- #if NET_2_0
- Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
- // not really "true" but it's even more trusted so...
- Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
- Assert.AreEqual (0, corlib.HostContext, "HostContext");
- Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
- Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
- Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
- #elif NET_1_1
- Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
- Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
- Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
- #endif
- }
- [Test]
- public void Corlib_test ()
- {
- Assembly corlib_test = Assembly.GetExecutingAssembly ();
- Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
- Assert.IsNotNull (corlib_test.Evidence, "Evidence");
- Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
- Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
- #if NET_2_0
- Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
- Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
- Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
- Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
- #elif NET_1_1
- Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
- #endif
- }
- #endif
- [Test]
- public void GetAssembly ()
- {
- Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
- Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
- }
- [Test]
- [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
- [ExpectedException (typeof (ArgumentNullException))]
- public void GetFile_Null ()
- {
- Assembly.GetExecutingAssembly ().GetFile (null);
- }
- [Test]
- [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
- [ExpectedException (typeof (ArgumentException))]
- public void GetFile_Empty ()
- {
- Assembly.GetExecutingAssembly ().GetFile (String.Empty);
- }
- [Test]
- [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
- public void GetFiles_False ()
- {
- Assembly corlib = typeof (int).Assembly;
- FileStream[] fss = corlib.GetFiles ();
- Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
- Assembly corlib_test = Assembly.GetExecutingAssembly ();
- fss = corlib_test.GetFiles ();
- Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
- }
- [Test]
- [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
- public void GetFiles_True ()
- {
- Assembly corlib = typeof (int).Assembly;
- FileStream[] fss = corlib.GetFiles ();
- Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
- Assembly corlib_test = Assembly.GetExecutingAssembly ();
- fss = corlib_test.GetFiles ();
- Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
- }
- [Test]
- public void GetManifestResourceStream_Name_Empty ()
- {
- Assembly corlib = typeof (int).Assembly;
- try {
- corlib.GetManifestResourceStream (string.Empty);
- Assert.Fail ("#A1");
- } catch (ArgumentException ex) {
- // String cannot have zero length
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- }
- corlib.GetManifestResourceStream (typeof (int), string.Empty);
- try {
- corlib.GetManifestResourceStream ((Type) null, string.Empty);
- Assert.Fail ("#B1");
- } catch (ArgumentException ex) {
- // String cannot have zero length
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- }
- }
- [Test]
- public void GetManifestResourceStream_Name_Null ()
- {
- Assembly corlib = typeof (int).Assembly;
- try {
- corlib.GetManifestResourceStream ((string) null);
- Assert.Fail ("#A1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- }
- corlib.GetManifestResourceStream (typeof (int), (string) null);
- try {
- corlib.GetManifestResourceStream ((Type) null, (string) null);
- Assert.Fail ("#B1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.IsNotNull (ex.ParamName, "#B5");
- Assert.AreEqual ("type", ex.ParamName, "#B6");
- }
- }
- [Test]
- public void IsDefined_AttributeType_Null ()
- {
- Assembly corlib = typeof (int).Assembly;
- try {
- corlib.IsDefined ((Type) null, false);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNotNull (ex.ParamName, "#5");
- Assert.AreEqual ("attributeType", ex.ParamName, "#6");
- }
- }
- [Test] // bug #78517
- #if ONLY_1_1
- [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
- #endif
- public void LoadFrom_Empty_Assembly ()
- {
- string tempFile = Path.GetTempFileName ();
- try {
- Assembly.LoadFrom (tempFile);
- Assert.Fail ("#1");
- } catch (BadImageFormatException ex) {
- Assert.IsNull (ex.InnerException, "#2");
- } finally {
- File.Delete (tempFile);
- }
- }
- [Test] // bug #78517
- public void LoadFrom_Invalid_Assembly ()
- {
- string tempFile = Path.GetTempFileName ();
- using (StreamWriter sw = File.CreateText (tempFile)) {
- sw.WriteLine ("foo");
- sw.Close ();
- }
- try {
- Assembly.LoadFrom (tempFile);
- Assert.Fail ("#1");
- } catch (BadImageFormatException ex) {
- Assert.IsNull (ex.InnerException, "#2");
- } finally {
- File.Delete (tempFile);
- }
- }
- [Test]
- public void LoadFrom_NonExisting_Assembly ()
- {
- string tempFile = Path.GetTempFileName ();
- File.Delete (tempFile);
- try {
- Assembly.LoadFrom (tempFile);
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.IsNull (ex.InnerException, "#2");
- } finally {
- File.Delete (tempFile);
- }
- }
- [Test]
- public void LoadWithPartialName ()
- {
- string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
- foreach (string s in names)
- if (Assembly.LoadWithPartialName (s) != null)
- return;
- Assertion.Fail ("Was not able to load any corlib test");
- }
- #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void GetObjectData_Null ()
- {
- Assembly corlib = typeof (int).Assembly;
- corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
- }
- #endif // TARGET_JVM
- [Test]
- public void GetReferencedAssemblies ()
- {
- Assembly corlib_test = Assembly.GetExecutingAssembly ();
- AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
- foreach (AssemblyName an in names) {
- Assert.IsNull (an.CodeBase, "CodeBase");
- Assert.IsNotNull (an.CultureInfo, "CultureInfo");
- Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
- Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
- Assert.IsNotNull (an.FullName, "FullName");
- Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
- Assert.IsNull (an.KeyPair, "KeyPair");
- Assert.IsNotNull (an.Name, "Name");
- Assert.IsNotNull (an.Version, "Version");
- Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
- an.VersionCompatibility, "VersionCompatibility");
- }
- }
- #if !TARGET_JVM // Reflection.Emit is not supported.
- [Test]
- public void Location_Empty() {
- string assemblyFileName = Path.Combine (
- Path.GetTempPath (), "AssemblyLocation.dll");
- try {
- AssemblyName assemblyName = new AssemblyName ();
- assemblyName.Name = "AssemblyLocation";
- AssemblyBuilder ab = AppDomain.CurrentDomain
- .DefineDynamicAssembly (assemblyName,
- AssemblyBuilderAccess.Save,
- Path.GetTempPath (),
- AppDomain.CurrentDomain.Evidence);
- ab.Save (Path.GetFileName (assemblyFileName));
- using (FileStream fs = File.OpenRead (assemblyFileName)) {
- byte[] buffer = new byte[fs.Length];
- fs.Read (buffer, 0, buffer.Length);
- Assembly assembly = Assembly.Load (buffer);
- Assert.AreEqual (string.Empty, assembly.Location);
- fs.Close ();
- }
- } finally {
- File.Delete (assemblyFileName);
- }
- }
- [Test]
- [Category ("NotWorking")]
- public void bug78464 ()
- {
- string assemblyFileName = Path.Combine (
- Path.GetTempPath (), "bug78464.dll");
- try {
- // execute test in separate appdomain to allow assembly to be unloaded
- AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
- CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
- try {
- crossDomainTester.bug78464 (assemblyFileName);
- } finally {
- AppDomain.Unload (testDomain);
- }
- } finally {
- File.Delete (assemblyFileName);
- }
- }
- [Test]
- public void bug78465 ()
- {
- string assemblyFileName = Path.Combine (
- Path.GetTempPath (), "bug78465.dll");
- try {
- AssemblyName assemblyName = new AssemblyName ();
- assemblyName.Name = "bug78465";
- AssemblyBuilder ab = AppDomain.CurrentDomain
- .DefineDynamicAssembly (assemblyName,
- AssemblyBuilderAccess.Save,
- Path.GetDirectoryName (assemblyFileName),
- AppDomain.CurrentDomain.Evidence);
- ab.Save (Path.GetFileName (assemblyFileName));
- using (FileStream fs = File.OpenRead (assemblyFileName)) {
- byte[] buffer = new byte[fs.Length];
- fs.Read (buffer, 0, buffer.Length);
- Assembly assembly = Assembly.Load (buffer);
- Assert.AreEqual (string.Empty, assembly.Location, "#1");
- fs.Close ();
- }
- AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
- CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
- try {
- crossDomainTester.bug78465 (assemblyFileName);
- } finally {
- AppDomain.Unload (testDomain);
- }
- } finally {
- File.Delete (assemblyFileName);
- }
- }
- [Test]
- public void bug78468 ()
- {
- string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
- "bug78468a.dll");
- string resourceFileName = Path.Combine (Path.GetTempPath (),
- "readme.txt");
- using (StreamWriter sw = File.CreateText (resourceFileName)) {
- sw.WriteLine ("FOO");
- sw.Close ();
- }
- try {
- AssemblyName assemblyName = new AssemblyName ();
- assemblyName.Name = "bug78468a";
- AssemblyBuilder ab = AppDomain.CurrentDomain
- .DefineDynamicAssembly (assemblyName,
- AssemblyBuilderAccess.Save,
- Path.GetTempPath (),
- AppDomain.CurrentDomain.Evidence);
- ab.AddResourceFile ("read", "readme.txt");
- ab.Save (Path.GetFileName (assemblyFileNameA));
- Assembly assembly;
- using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
- byte[] buffer = new byte[fs.Length];
- fs.Read (buffer, 0, buffer.Length);
- assembly = Assembly.Load (buffer);
- fs.Close ();
- }
- Assert.AreEqual (string.Empty, assembly.Location, "#A1");
- string[] resNames = assembly.GetManifestResourceNames ();
- Assert.IsNotNull (resNames, "#A2");
- Assert.AreEqual (1, resNames.Length, "#A3");
- Assert.AreEqual ("read", resNames[0], "#A4");
- ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
- Assert.IsNotNull (resInfo, "#A5");
- Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
- Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
- Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
- #if NET_2_0
- try {
- assembly.GetManifestResourceStream ("read");
- Assert.Fail ("#A9");
- } catch (FileNotFoundException) {
- }
- #else
- Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
- #endif
- try {
- assembly.GetFile ("readme.txt");
- Assert.Fail ("#A10");
- } catch (FileNotFoundException) {
- }
- string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
- "bug78468b.dll");
- AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
- CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
- try {
- crossDomainTester.bug78468 (assemblyFileNameB);
- } finally {
- AppDomain.Unload (testDomain);
- File.Delete (assemblyFileNameB);
- }
- } finally {
- File.Delete (assemblyFileNameA);
- File.Delete (resourceFileName);
- }
- }
- #if NET_2_0
- [Test]
- [Category ("NotWorking")]
- public void ReflectionOnlyLoad ()
- {
- Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
-
- Assert.IsNotNull (assembly);
- Assert.IsTrue (assembly.ReflectionOnly);
- }
- [Test]
- public void ReflectionOnlyLoadFrom ()
- {
- string loc = typeof (AssemblyTest).Assembly.Location;
- string filename = Path.GetFileName (loc);
- Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
- Assert.IsNotNull (assembly);
- Assert.IsTrue (assembly.ReflectionOnly);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CreateInstanceOnRefOnly ()
- {
- Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
- assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
- }
- #endif
- [Test]
- [Category ("NotWorking")] // patch for bug #79720 must be committed first
- public void Load_Culture ()
- {
- string tempDir = Path.Combine (Path.GetTempPath (),
- "MonoTests.System.Reflection.AssemblyTest");
- string cultureTempDir = Path.Combine (tempDir, "nl-BE");
- if (!Directory.Exists (cultureTempDir))
- Directory.CreateDirectory (cultureTempDir);
- cultureTempDir = Path.Combine (tempDir, "en-US");
- if (!Directory.Exists (cultureTempDir))
- Directory.CreateDirectory (cultureTempDir);
- AppDomain ad = CreateTestDomain (tempDir, true);
- try {
- CrossDomainTester cdt = CreateCrossDomainTester (ad);
- // PART A
- AssemblyName aname = new AssemblyName ();
- aname.Name = "culturea";
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
- aname = new AssemblyName ();
- aname.Name = "culturea";
- Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
- aname = new AssemblyName ();
- aname.Name = "culturea";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
- aname = new AssemblyName ();
- aname.Name = "culturea";
- aname.CultureInfo = CultureInfo.InvariantCulture;
- Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
- // PART B
- aname = new AssemblyName ();
- aname.Name = "cultureb";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
- aname = new AssemblyName ();
- aname.Name = "cultureb";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
- aname = new AssemblyName ();
- aname.Name = "cultureb";
- Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
- aname = new AssemblyName ();
- aname.Name = "cultureb";
- aname.CultureInfo = new CultureInfo ("en-US");
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
- // PART C
- aname = new AssemblyName ();
- aname.Name = "culturec";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
- aname = new AssemblyName ();
- aname.Name = "culturec";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
- aname = new AssemblyName ();
- aname.Name = "culturec";
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
- aname = new AssemblyName ();
- aname.Name = "culturec";
- aname.CultureInfo = CultureInfo.InvariantCulture;
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
- // PART D
- aname = new AssemblyName ();
- aname.Name = "cultured";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
- aname = new AssemblyName ();
- aname.Name = "cultured";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
- aname = new AssemblyName ();
- aname.Name = "cultured";
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
- aname = new AssemblyName ();
- aname.Name = "cultured";
- aname.CultureInfo = CultureInfo.InvariantCulture;
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
- } finally {
- AppDomain.Unload (ad);
- if (Directory.Exists (tempDir))
- Directory.Delete (tempDir, true);
- }
- }
- [Test] // bug #79712
- #if NET_2_0
- [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
- #else
- [Category ("NotWorking")]
- #endif
- public void Load_Culture_Mismatch ()
- {
- string tempDir = Path.Combine (Path.GetTempPath (),
- "MonoTests.System.Reflection.AssemblyTest");
- string cultureTempDir = Path.Combine (tempDir, "en-US");
- if (!Directory.Exists (cultureTempDir))
- Directory.CreateDirectory (cultureTempDir);
- AppDomain ad = CreateTestDomain (tempDir, true);
- try {
- CrossDomainTester cdt = CreateCrossDomainTester (ad);
- // PART A
- AssemblyName aname = new AssemblyName ();
- aname.Name = "bug79712a";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
- aname = new AssemblyName ();
- aname.Name = "bug79712a";
- aname.CultureInfo = CultureInfo.InvariantCulture;
- #if NET_2_0
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
- #else
- Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
- #endif
- // PART B
- aname = new AssemblyName ();
- aname.Name = "bug79712b";
- aname.CultureInfo = new CultureInfo ("nl-BE");
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
- aname = new AssemblyName ();
- aname.Name = "bug79712b";
- aname.CultureInfo = new CultureInfo ("en-US");
- #if NET_2_0
- Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
- #else
- Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
- #endif
- } finally {
- AppDomain.Unload (ad);
- if (Directory.Exists (tempDir))
- Directory.Delete (tempDir, true);
- }
- }
- [Test] // bug #79715
- public void Load_PartialVersion ()
- {
- string tempDir = Path.Combine (Path.GetTempPath (),
- "MonoTests.System.Reflection.AssemblyTest");
- if (!Directory.Exists (tempDir))
- Directory.CreateDirectory (tempDir);
- AppDomain ad = CreateTestDomain (tempDir, true);
- try {
- CrossDomainTester cdt = CreateCrossDomainTester (ad);
- AssemblyName aname = new AssemblyName ();
- aname.Name = "bug79715";
- aname.Version = new Version (1, 2, 3, 4);
- cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
- aname = new AssemblyName ();
- aname.Name = "bug79715";
- aname.Version = new Version (1, 2);
- Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
- Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
- aname = new AssemblyName ();
- aname.Name = "bug79715";
- aname.Version = new Version (1, 2, 3);
- Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
- Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
- aname = new AssemblyName ();
- aname.Name = "bug79715";
- aname.Version = new Version (1, 2, 3, 4);
- Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
- Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
- } finally {
- AppDomain.Unload (ad);
- if (Directory.Exists (tempDir))
- Directory.Delete (tempDir, true);
- }
- }
- private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
- {
- AppDomainSetup setup = new AppDomainSetup ();
- setup.ApplicationBase = baseDirectory;
- setup.ApplicationName = "testdomain";
- AppDomain ad = AppDomain.CreateDomain ("testdomain",
- AppDomain.CurrentDomain.Evidence, setup);
- if (assemblyResolver) {
- Assembly ea = Assembly.GetExecutingAssembly ();
- ad.CreateInstanceFrom (ea.CodeBase,
- typeof (AssemblyResolveHandler).FullName,
- false,
- BindingFlags.Public | BindingFlags.Instance,
- null,
- new object [] { ea.Location, ea.FullName },
- CultureInfo.InvariantCulture,
- null,
- null);
- }
- return ad;
- }
- private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
- {
- Type testerType = typeof (CrossDomainTester);
- return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
- testerType.Assembly.FullName, testerType.FullName, false,
- BindingFlags.Public | BindingFlags.Instance, null, new object[0],
- CultureInfo.InvariantCulture, new object[0], null);
- }
- private class CrossDomainTester : MarshalByRefObject
- {
- public void GenerateAssembly (AssemblyName aname, string path)
- {
- AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
- aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
- ab.Save (Path.GetFileName (path));
- }
- public void Load (AssemblyName assemblyRef)
- {
- Assembly.Load (assemblyRef);
- }
- public void LoadFrom (string assemblyFile)
- {
- Assembly.LoadFrom (assemblyFile);
- }
- public bool AssertLoad (AssemblyName assemblyRef)
- {
- try {
- Assembly.Load (assemblyRef);
- return true;
- } catch {
- return false;
- }
- }
- public bool AssertLoad (string assemblyString)
- {
- try {
- Assembly.Load (assemblyString);
- return true;
- } catch {
- return false;
- }
- }
- public bool AssertFileLoadException (AssemblyName assemblyRef)
- {
- try {
- Assembly.Load (assemblyRef);
- return false;
- } catch (FileLoadException) {
- return true;
- }
- }
- public bool AssertFileNotFoundException (AssemblyName assemblyRef)
- {
- try {
- Assembly.Load (assemblyRef);
- return false;
- } catch (FileNotFoundException) {
- return true;
- }
- }
- public void bug78464 (string assemblyFileName)
- {
- AssemblyName assemblyName = new AssemblyName ();
- assemblyName.Name = "bug78464";
- AssemblyBuilder ab = AppDomain.CurrentDomain
- .DefineDynamicAssembly (assemblyName,
- AssemblyBuilderAccess.Save,
- Path.GetDirectoryName (assemblyFileName),
- AppDomain.CurrentDomain.Evidence);
- ab.Save (Path.GetFileName (assemblyFileName));
- Assembly assembly;
- using (FileStream fs = File.OpenRead (assemblyFileName)) {
- byte[] buffer = new byte[fs.Length];
- fs.Read (buffer, 0, buffer.Length);
- assembly = Assembly.Load (buffer);
- fs.Close ();
- }
- Assert.AreEqual (string.Empty, assembly.Location, "#1");
- assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
- Assert.IsFalse (assembly.Location == string.Empty, "#2");
- Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
- // note: we cannot check if directory names match, as MS.NET seems to
- // convert directory part of assembly location to lowercase
- Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
- }
- public void bug78465 (string assemblyFileName)
- {
- Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
- Assert.IsFalse (assembly.Location == string.Empty, "#2");
- Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
- // note: we cannot check if directory names match, as MS.NET seems to
- // convert directory part of assembly location to lowercase
- Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
- }
- public void bug78468 (string assemblyFileName)
- {
- AssemblyName assemblyName = new AssemblyName ();
- assemblyName.Name = "bug78468b";
- AssemblyBuilder ab = AppDomain.CurrentDomain
- .DefineDynamicAssembly (assemblyName,
- AssemblyBuilderAccess.Save,
- Path.GetDirectoryName (assemblyFileName),
- AppDomain.CurrentDomain.Evidence);
- ab.AddResourceFile ("read", "readme.txt");
- ab.Save (Path.GetFileName (assemblyFileName));
- Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
- Assert.IsTrue (assembly.Location != string.Empty, "#B1");
- string[] resNames = assembly.GetManifestResourceNames ();
- Assert.IsNotNull (resNames, "#B2");
- Assert.AreEqual (1, resNames.Length, "#B3");
- Assert.AreEqual ("read", resNames[0], "#B4");
- ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
- Assert.IsNotNull (resInfo, "#B5");
- Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
- Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
- Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
- Stream s = assembly.GetManifestResourceStream ("read");
- Assert.IsNotNull (s, "#B9");
- s.Close ();
- s = assembly.GetFile ("readme.txt");
- Assert.IsNotNull (s, "#B10");
- s.Close ();
- }
- }
- [Test]
- public void bug79872 ()
- {
- Random rnd = new Random ();
- string outdir;
- int tries = 0;
- retry:
- outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
- if (Directory.Exists (outdir)) {
- try {
- Directory.Delete (outdir, true);
- } catch {
- if (++tries <= 100)
- goto retry;
- }
- }
- Directory.CreateDirectory (outdir);
- AssemblyName an = new AssemblyName ();
- an.Name = "bug79872";
- AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
- string dllname = "bug79872.dll";
- ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
- string netmodule = "bug79872.netmodule";
- ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
- TypeBuilder a1 = mb2.DefineType ("A");
- a1.CreateType ();
- ab.Save (dllname);
- bool ok = true;
- try {
- Assembly.LoadFrom (Path.Combine (outdir, dllname));
- } catch {
- ok = false;
- }
- Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
- ok = false;
- try {
- Assembly.LoadFrom (Path.Combine (outdir, netmodule));
- } catch (BadImageFormatException) {
- ok = true; // mono and .net 2.0 throw this
- } catch (FileLoadException) {
- ok = true; // .net 1.1 throws this
- } catch {
- // swallow the rest
- }
- Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
- Directory.Delete (outdir, true);
- }
- #endif // TARGET_JVM
- #if NET_2_0
- [Test]
- public void ManifestModule ()
- {
- Assembly assembly = typeof (int).Assembly;
- Module module = assembly.ManifestModule;
- Assert.IsNotNull (module, "#1");
- Assert.AreEqual (typeof (Module), module.GetType (), "#2");
- Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
- Assert.IsFalse (module.IsResource (), "#4");
- Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
- Assert.AreSame (module, assembly.GetModules () [0], "#6");
- Assert.AreSame (module, assembly.ManifestModule, "#7");
- }
- #endif
- [Serializable ()]
- private class AssemblyResolveHandler
- {
- public AssemblyResolveHandler (string assemblyFile, string assemblyName)
- {
- _assemblyFile = assemblyFile;
- _assemblyName = assemblyName;
- AppDomain.CurrentDomain.AssemblyResolve +=
- new ResolveEventHandler (ResolveAssembly);
- }
- private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
- {
- if (args.Name == _assemblyName)
- return Assembly.LoadFrom (_assemblyFile);
- return null;
- }
- private readonly string _assemblyFile;
- private readonly string _assemblyName;
- }
- protected internal class Bug328812_NestedFamORAssem { };
- [Test]
- public void bug328812 ()
- {
- Assembly corlib_test = Assembly.GetExecutingAssembly ();
- Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
- // Just a sanity check, in case the above passed for some other reason
- Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
- }
- }
- }
|