| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- //
- // 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.Runtime.Serialization;
- using System.Security;
- namespace MonoTests.System.Reflection
- {
- [TestFixture]
- public class AssemblyTest
- {
- [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]
- #if NET_2_0
- [Category ("NotWorking")]
- [ExpectedException (typeof (ArgumentException))]
- #else
- [ExpectedException (typeof (TypeLoadException))]
- #endif
- public void TestGetType ()
- {
- // Bug #49114
- typeof (int).Assembly.GetType ("&blabla", true, true);
- }
- [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 // 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]
- [ExpectedException (typeof (ArgumentNullException))]
- public void GetFile_Null ()
- {
- Assembly.GetExecutingAssembly ().GetFile (null);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void GetFile_Empty ()
- {
- Assembly.GetExecutingAssembly ().GetFile (String.Empty);
- }
- [Test]
- 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]
- 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] // 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]
- [Category ("NotWorking")]
- 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
- [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;
- }
- }
- }
|