AssemblyTest.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. //
  2. // System.Reflection.Assembly Test Cases
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Philippe Lavoie ([email protected])
  7. // Sebastien Pouliot ([email protected])
  8. //
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using NUnit.Framework;
  32. using System;
  33. using System.Configuration.Assemblies;
  34. using System.Globalization;
  35. using System.IO;
  36. using System.Reflection;
  37. #if !TARGET_JVM && !MONOTOUCH
  38. using System.Reflection.Emit;
  39. #endif
  40. using System.Threading;
  41. using System.Runtime.Serialization;
  42. using System.Security;
  43. namespace MonoTests.System.Reflection
  44. {
  45. [TestFixture]
  46. public class AssemblyTest
  47. {
  48. static string TempFolder = Path.Combine (Path.GetTempPath (),
  49. "MonoTests.System.Reflection.AssemblyTest");
  50. [SetUp]
  51. public void SetUp ()
  52. {
  53. while (Directory.Exists (TempFolder))
  54. TempFolder = Path.Combine (TempFolder, "2");
  55. Directory.CreateDirectory (TempFolder);
  56. }
  57. [TearDown]
  58. public void TearDown ()
  59. {
  60. try {
  61. // This throws an exception under MS.NET, since the directory contains loaded
  62. // assemblies.
  63. Directory.Delete (TempFolder, true);
  64. } catch (Exception) {
  65. }
  66. }
  67. [Test]
  68. public void CreateInstance()
  69. {
  70. Type type = typeof (AssemblyTest);
  71. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  72. Assert.IsNotNull (obj, "#01");
  73. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  74. }
  75. [Test]
  76. public void CreateInvalidInstance()
  77. {
  78. Type type = typeof (AssemblyTest);
  79. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  80. Assert.IsNull (obj, "#03");
  81. }
  82. [Test] // bug #49114
  83. #if NET_2_0
  84. [Category ("NotWorking")]
  85. [ExpectedException (typeof (ArgumentException))]
  86. #else
  87. [ExpectedException (typeof (TypeLoadException))]
  88. #endif
  89. public void GetType_TypeName_Invalid ()
  90. {
  91. typeof (int).Assembly.GetType ("&blabla", true, true);
  92. }
  93. [Test] // bug #334203
  94. public void GetType_TypeName_AssemblyName ()
  95. {
  96. Assembly a = typeof (int).Assembly;
  97. string typeName = typeof (string).AssemblyQualifiedName;
  98. #if NET_2_0
  99. try {
  100. a.GetType (typeName, true, false);
  101. Assert.Fail ("#A1");
  102. } catch (ArgumentException ex) {
  103. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  104. Assert.IsNull (ex.InnerException, "#A3");
  105. Assert.IsNotNull (ex.Message, "#A4");
  106. Assert.IsNull (ex.ParamName, "#A5");
  107. }
  108. #else
  109. try {
  110. a.GetType (typeName, true, false);
  111. Assert.Fail ("#A1");
  112. } catch (TypeLoadException ex) {
  113. Assert.AreEqual (typeof (TypeLoadException), ex.GetType (), "#A2");
  114. Assert.IsNull (ex.InnerException, "#A3");
  115. Assert.IsNotNull (ex.Message, "#A4");
  116. Assert.IsTrue (ex.Message.IndexOf (typeName) != -1, "#A5");
  117. }
  118. #endif
  119. Type type = a.GetType (typeName, false);
  120. Assert.IsNull (type, "#B1");
  121. type = a.GetType (typeName, false, true);
  122. Assert.IsNull (type, "#B2");
  123. }
  124. [Test]
  125. public void GetEntryAssembly ()
  126. {
  127. // note: only available in default appdomain
  128. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  129. // Not sure we should emulate this behavior.
  130. string fname = AppDomain.CurrentDomain.FriendlyName;
  131. if (fname.EndsWith (".dll")) { // nunit-console
  132. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  133. #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
  134. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  135. #endif
  136. } else { // gnunit
  137. Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  138. #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
  139. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  140. #endif
  141. }
  142. }
  143. #if !TARGET_JVM && !MONOTOUCH // Reflection.Emit is not supported.
  144. [Test]
  145. public void GetModules_MissingFile ()
  146. {
  147. AssemblyName newName = new AssemblyName ();
  148. newName.Name = "AssemblyTest";
  149. AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  150. ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
  151. ab.Save ("test_assembly.dll");
  152. File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
  153. Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
  154. try {
  155. ass.GetModules ();
  156. Assert.Fail ();
  157. } catch (FileNotFoundException ex) {
  158. Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
  159. }
  160. }
  161. #endif
  162. #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
  163. [Category ("NotWorking")]
  164. [Test]
  165. public void Corlib ()
  166. {
  167. Assembly corlib = typeof (int).Assembly;
  168. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  169. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  170. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  171. Assert.IsNotNull (corlib.Evidence, "Evidence");
  172. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  173. // corlib doesn't reference anything
  174. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  175. Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  176. // not really "true" but it's even more trusted so...
  177. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  178. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  179. Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  180. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  181. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  182. }
  183. [Test]
  184. public void Corlib_test ()
  185. {
  186. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  187. #if MOBILE
  188. Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
  189. Assert.IsNull (corlib_test.Evidence, "Evidence");
  190. #else
  191. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  192. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  193. #endif
  194. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  195. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  196. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  197. #if NET_4_0 && !MOBILE
  198. Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  199. #else
  200. Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  201. #endif
  202. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  203. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  204. }
  205. #endif
  206. [Test]
  207. public void GetAssembly ()
  208. {
  209. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  210. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  211. }
  212. [Test]
  213. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  214. public void GetFile_Null ()
  215. {
  216. try {
  217. Assembly.GetExecutingAssembly ().GetFile (null);
  218. Assert.Fail ("#1");
  219. } catch (ArgumentNullException ex) {
  220. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  221. Assert.IsNull (ex.InnerException, "#3");
  222. Assert.IsNotNull (ex.Message, "#4");
  223. Assert.IsNull (ex.ParamName, "#5");
  224. }
  225. }
  226. [Test]
  227. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  228. public void GetFile_Empty ()
  229. {
  230. try {
  231. Assembly.GetExecutingAssembly ().GetFile (
  232. String.Empty);
  233. Assert.Fail ("#1");
  234. } catch (ArgumentException ex) {
  235. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  236. Assert.IsNull (ex.InnerException, "#3");
  237. Assert.IsNotNull (ex.Message, "#4");
  238. Assert.IsNull (ex.ParamName, "#5");
  239. }
  240. }
  241. [Test]
  242. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  243. public void GetFiles_False ()
  244. {
  245. Assembly corlib = typeof (int).Assembly;
  246. FileStream[] fss = corlib.GetFiles ();
  247. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  248. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  249. fss = corlib_test.GetFiles ();
  250. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  251. }
  252. [Test]
  253. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  254. public void GetFiles_True ()
  255. {
  256. Assembly corlib = typeof (int).Assembly;
  257. FileStream[] fss = corlib.GetFiles ();
  258. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  259. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  260. fss = corlib_test.GetFiles ();
  261. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  262. }
  263. [Test]
  264. public void GetManifestResourceStream_Name_Empty ()
  265. {
  266. Assembly corlib = typeof (int).Assembly;
  267. try {
  268. corlib.GetManifestResourceStream (string.Empty);
  269. Assert.Fail ("#A1");
  270. } catch (ArgumentException ex) {
  271. // String cannot have zero length
  272. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  273. Assert.IsNull (ex.InnerException, "#A3");
  274. Assert.IsNotNull (ex.Message, "#A4");
  275. }
  276. corlib.GetManifestResourceStream (typeof (int), string.Empty);
  277. try {
  278. corlib.GetManifestResourceStream ((Type) null, string.Empty);
  279. Assert.Fail ("#B1");
  280. } catch (ArgumentException ex) {
  281. // String cannot have zero length
  282. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  283. Assert.IsNull (ex.InnerException, "#B3");
  284. Assert.IsNotNull (ex.Message, "#B4");
  285. }
  286. }
  287. [Test]
  288. public void GetManifestResourceStream_Name_Null ()
  289. {
  290. Assembly corlib = typeof (int).Assembly;
  291. try {
  292. corlib.GetManifestResourceStream ((string) null);
  293. Assert.Fail ("#A1");
  294. } catch (ArgumentNullException ex) {
  295. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  296. Assert.IsNull (ex.InnerException, "#A3");
  297. Assert.IsNotNull (ex.Message, "#A4");
  298. }
  299. corlib.GetManifestResourceStream (typeof (int), (string) null);
  300. try {
  301. corlib.GetManifestResourceStream ((Type) null, (string) null);
  302. Assert.Fail ("#B1");
  303. } catch (ArgumentNullException ex) {
  304. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  305. Assert.IsNull (ex.InnerException, "#B3");
  306. Assert.IsNotNull (ex.Message, "#B4");
  307. Assert.IsNotNull (ex.ParamName, "#B5");
  308. Assert.AreEqual ("type", ex.ParamName, "#B6");
  309. }
  310. }
  311. [Test]
  312. public void IsDefined_AttributeType_Null ()
  313. {
  314. Assembly corlib = typeof (int).Assembly;
  315. try {
  316. corlib.IsDefined ((Type) null, false);
  317. Assert.Fail ("#1");
  318. } catch (ArgumentNullException ex) {
  319. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  320. Assert.IsNull (ex.InnerException, "#3");
  321. Assert.IsNotNull (ex.Message, "#4");
  322. Assert.IsNotNull (ex.ParamName, "#5");
  323. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  324. }
  325. }
  326. [Test] // bug #78517
  327. #if ONLY_1_1
  328. [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
  329. #endif
  330. public void LoadFrom_Empty_Assembly ()
  331. {
  332. string tempFile = Path.GetTempFileName ();
  333. try {
  334. Assembly.LoadFrom (tempFile);
  335. Assert.Fail ("#1");
  336. } catch (BadImageFormatException ex) {
  337. Assert.IsNull (ex.InnerException, "#2");
  338. } finally {
  339. File.Delete (tempFile);
  340. }
  341. }
  342. [Test] // bug #78517
  343. public void LoadFrom_Invalid_Assembly ()
  344. {
  345. string tempFile = Path.GetTempFileName ();
  346. using (StreamWriter sw = File.CreateText (tempFile)) {
  347. sw.WriteLine ("foo");
  348. sw.Close ();
  349. }
  350. try {
  351. Assembly.LoadFrom (tempFile);
  352. Assert.Fail ("#1");
  353. } catch (BadImageFormatException ex) {
  354. Assert.IsNull (ex.InnerException, "#2");
  355. } finally {
  356. File.Delete (tempFile);
  357. }
  358. }
  359. [Test]
  360. public void LoadFrom_NonExisting_Assembly ()
  361. {
  362. string tempFile = Path.GetTempFileName ();
  363. File.Delete (tempFile);
  364. try {
  365. Assembly.LoadFrom (tempFile);
  366. Assert.Fail ("#1");
  367. } catch (FileNotFoundException ex) {
  368. Assert.IsNull (ex.InnerException, "#2");
  369. } finally {
  370. File.Delete (tempFile);
  371. }
  372. }
  373. [Test]
  374. public void LoadWithPartialName ()
  375. {
  376. string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_plattest", "mscorlibtests" };
  377. foreach (string s in names)
  378. if (Assembly.LoadWithPartialName (s) != null)
  379. return;
  380. Assert.Fail ("Was not able to load any corlib test");
  381. }
  382. #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
  383. [Test]
  384. public void GetObjectData_Info_Null ()
  385. {
  386. Assembly corlib = typeof (int).Assembly;
  387. try {
  388. corlib.GetObjectData (null, new StreamingContext (
  389. StreamingContextStates.All));
  390. Assert.Fail ("#1");
  391. } catch (ArgumentNullException ex) {
  392. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  393. Assert.IsNull (ex.InnerException, "#3");
  394. Assert.IsNotNull (ex.Message, "#4");
  395. Assert.IsNotNull (ex.ParamName, "#5");
  396. Assert.AreEqual ("info", ex.ParamName, "#6");
  397. }
  398. }
  399. #endif // TARGET_JVM
  400. [Test]
  401. public void GetReferencedAssemblies ()
  402. {
  403. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  404. AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
  405. foreach (AssemblyName an in names) {
  406. Assert.IsNull (an.CodeBase, "CodeBase");
  407. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  408. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  409. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  410. Assert.IsNotNull (an.FullName, "FullName");
  411. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  412. Assert.IsNull (an.KeyPair, "KeyPair");
  413. Assert.IsNotNull (an.Name, "Name");
  414. Assert.IsNotNull (an.Version, "Version");
  415. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  416. an.VersionCompatibility, "VersionCompatibility");
  417. }
  418. }
  419. #if !TARGET_JVM && !MONOTOUCH // Reflection.Emit is not supported.
  420. [Test]
  421. public void Location_Empty() {
  422. string assemblyFileName = Path.Combine (
  423. Path.GetTempPath (), "AssemblyLocation.dll");
  424. try {
  425. AssemblyName assemblyName = new AssemblyName ();
  426. assemblyName.Name = "AssemblyLocation";
  427. AssemblyBuilder ab = AppDomain.CurrentDomain
  428. .DefineDynamicAssembly (assemblyName,
  429. AssemblyBuilderAccess.Save,
  430. Path.GetTempPath (),
  431. AppDomain.CurrentDomain.Evidence);
  432. ab.Save (Path.GetFileName (assemblyFileName));
  433. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  434. byte[] buffer = new byte[fs.Length];
  435. fs.Read (buffer, 0, buffer.Length);
  436. Assembly assembly = Assembly.Load (buffer);
  437. Assert.AreEqual (string.Empty, assembly.Location);
  438. fs.Close ();
  439. }
  440. } finally {
  441. File.Delete (assemblyFileName);
  442. }
  443. }
  444. [Test]
  445. [Category ("NotWorking")]
  446. public void bug78464 ()
  447. {
  448. string assemblyFileName = Path.Combine (
  449. Path.GetTempPath (), "bug78464.dll");
  450. try {
  451. // execute test in separate appdomain to allow assembly to be unloaded
  452. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  453. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  454. try {
  455. crossDomainTester.bug78464 (assemblyFileName);
  456. } finally {
  457. AppDomain.Unload (testDomain);
  458. }
  459. } finally {
  460. File.Delete (assemblyFileName);
  461. }
  462. }
  463. [Test]
  464. public void bug78465 ()
  465. {
  466. string assemblyFileName = Path.Combine (
  467. Path.GetTempPath (), "bug78465.dll");
  468. try {
  469. AssemblyName assemblyName = new AssemblyName ();
  470. assemblyName.Name = "bug78465";
  471. AssemblyBuilder ab = AppDomain.CurrentDomain
  472. .DefineDynamicAssembly (assemblyName,
  473. AssemblyBuilderAccess.Save,
  474. Path.GetDirectoryName (assemblyFileName),
  475. AppDomain.CurrentDomain.Evidence);
  476. ab.Save (Path.GetFileName (assemblyFileName));
  477. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  478. byte[] buffer = new byte[fs.Length];
  479. fs.Read (buffer, 0, buffer.Length);
  480. Assembly assembly = Assembly.Load (buffer);
  481. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  482. fs.Close ();
  483. }
  484. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  485. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  486. try {
  487. crossDomainTester.bug78465 (assemblyFileName);
  488. } finally {
  489. AppDomain.Unload (testDomain);
  490. }
  491. } finally {
  492. File.Delete (assemblyFileName);
  493. }
  494. }
  495. [Test]
  496. public void bug78468 ()
  497. {
  498. string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
  499. "bug78468a.dll");
  500. string resourceFileName = Path.Combine (Path.GetTempPath (),
  501. "readme.txt");
  502. using (StreamWriter sw = File.CreateText (resourceFileName)) {
  503. sw.WriteLine ("FOO");
  504. sw.Close ();
  505. }
  506. try {
  507. AssemblyName assemblyName = new AssemblyName ();
  508. assemblyName.Name = "bug78468a";
  509. AssemblyBuilder ab = AppDomain.CurrentDomain
  510. .DefineDynamicAssembly (assemblyName,
  511. AssemblyBuilderAccess.Save,
  512. Path.GetTempPath (),
  513. AppDomain.CurrentDomain.Evidence);
  514. ab.AddResourceFile ("read", "readme.txt");
  515. ab.Save (Path.GetFileName (assemblyFileNameA));
  516. Assembly assembly;
  517. using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
  518. byte[] buffer = new byte[fs.Length];
  519. fs.Read (buffer, 0, buffer.Length);
  520. assembly = Assembly.Load (buffer);
  521. fs.Close ();
  522. }
  523. Assert.AreEqual (string.Empty, assembly.Location, "#A1");
  524. string[] resNames = assembly.GetManifestResourceNames ();
  525. Assert.IsNotNull (resNames, "#A2");
  526. Assert.AreEqual (1, resNames.Length, "#A3");
  527. Assert.AreEqual ("read", resNames[0], "#A4");
  528. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  529. Assert.IsNotNull (resInfo, "#A5");
  530. Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
  531. Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
  532. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
  533. #if NET_2_0
  534. try {
  535. assembly.GetManifestResourceStream ("read");
  536. Assert.Fail ("#A9");
  537. } catch (FileNotFoundException) {
  538. }
  539. #else
  540. Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
  541. #endif
  542. try {
  543. assembly.GetFile ("readme.txt");
  544. Assert.Fail ("#A10");
  545. } catch (FileNotFoundException) {
  546. }
  547. string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
  548. "bug78468b.dll");
  549. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  550. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  551. try {
  552. crossDomainTester.bug78468 (assemblyFileNameB);
  553. } finally {
  554. AppDomain.Unload (testDomain);
  555. File.Delete (assemblyFileNameB);
  556. }
  557. } finally {
  558. File.Delete (assemblyFileNameA);
  559. File.Delete (resourceFileName);
  560. }
  561. }
  562. #if NET_2_0
  563. [Test]
  564. [Category ("NotWorking")]
  565. public void ReflectionOnlyLoad ()
  566. {
  567. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  568. Assert.IsNotNull (assembly);
  569. Assert.IsTrue (assembly.ReflectionOnly);
  570. }
  571. [Test]
  572. public void ReflectionOnlyLoadFrom ()
  573. {
  574. string loc = typeof (AssemblyTest).Assembly.Location;
  575. string filename = Path.GetFileName (loc);
  576. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
  577. Assert.IsNotNull (assembly);
  578. Assert.IsTrue (assembly.ReflectionOnly);
  579. }
  580. [Test]
  581. [ExpectedException (typeof (ArgumentException))]
  582. public void CreateInstanceOnRefOnly ()
  583. {
  584. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  585. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  586. }
  587. #endif
  588. [Test]
  589. [Category ("NotWorking")] // patch for bug #79720 must be committed first
  590. public void Load_Culture ()
  591. {
  592. string tempDir = Path.Combine (Path.GetTempPath (),
  593. "MonoTests.System.Reflection.AssemblyTest");
  594. string cultureTempDir = Path.Combine (tempDir, "nl-BE");
  595. if (!Directory.Exists (cultureTempDir))
  596. Directory.CreateDirectory (cultureTempDir);
  597. cultureTempDir = Path.Combine (tempDir, "en-US");
  598. if (!Directory.Exists (cultureTempDir))
  599. Directory.CreateDirectory (cultureTempDir);
  600. AppDomain ad = CreateTestDomain (tempDir, true);
  601. try {
  602. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  603. // PART A
  604. AssemblyName aname = new AssemblyName ();
  605. aname.Name = "culturea";
  606. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
  607. aname = new AssemblyName ();
  608. aname.Name = "culturea";
  609. Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
  610. aname = new AssemblyName ();
  611. aname.Name = "culturea";
  612. aname.CultureInfo = new CultureInfo ("nl-BE");
  613. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
  614. aname = new AssemblyName ();
  615. aname.Name = "culturea";
  616. aname.CultureInfo = CultureInfo.InvariantCulture;
  617. Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
  618. // PART B
  619. aname = new AssemblyName ();
  620. aname.Name = "cultureb";
  621. aname.CultureInfo = new CultureInfo ("nl-BE");
  622. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
  623. aname = new AssemblyName ();
  624. aname.Name = "cultureb";
  625. aname.CultureInfo = new CultureInfo ("nl-BE");
  626. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  627. aname = new AssemblyName ();
  628. aname.Name = "cultureb";
  629. Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
  630. aname = new AssemblyName ();
  631. aname.Name = "cultureb";
  632. aname.CultureInfo = new CultureInfo ("en-US");
  633. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
  634. // PART C
  635. aname = new AssemblyName ();
  636. aname.Name = "culturec";
  637. aname.CultureInfo = new CultureInfo ("nl-BE");
  638. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
  639. aname = new AssemblyName ();
  640. aname.Name = "culturec";
  641. aname.CultureInfo = new CultureInfo ("nl-BE");
  642. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  643. aname = new AssemblyName ();
  644. aname.Name = "culturec";
  645. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
  646. aname = new AssemblyName ();
  647. aname.Name = "culturec";
  648. aname.CultureInfo = CultureInfo.InvariantCulture;
  649. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
  650. // PART D
  651. aname = new AssemblyName ();
  652. aname.Name = "cultured";
  653. aname.CultureInfo = new CultureInfo ("nl-BE");
  654. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
  655. aname = new AssemblyName ();
  656. aname.Name = "cultured";
  657. aname.CultureInfo = new CultureInfo ("nl-BE");
  658. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
  659. aname = new AssemblyName ();
  660. aname.Name = "cultured";
  661. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
  662. aname = new AssemblyName ();
  663. aname.Name = "cultured";
  664. aname.CultureInfo = CultureInfo.InvariantCulture;
  665. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
  666. } finally {
  667. AppDomain.Unload (ad);
  668. if (Directory.Exists (tempDir))
  669. Directory.Delete (tempDir, true);
  670. }
  671. }
  672. [Test] // bug #79712
  673. #if NET_2_0
  674. [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
  675. #else
  676. [Category ("NotWorking")]
  677. #endif
  678. public void Load_Culture_Mismatch ()
  679. {
  680. string tempDir = Path.Combine (Path.GetTempPath (),
  681. "MonoTests.System.Reflection.AssemblyTest");
  682. string cultureTempDir = Path.Combine (tempDir, "en-US");
  683. if (!Directory.Exists (cultureTempDir))
  684. Directory.CreateDirectory (cultureTempDir);
  685. AppDomain ad = CreateTestDomain (tempDir, true);
  686. try {
  687. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  688. // PART A
  689. AssemblyName aname = new AssemblyName ();
  690. aname.Name = "bug79712a";
  691. aname.CultureInfo = new CultureInfo ("nl-BE");
  692. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
  693. aname = new AssemblyName ();
  694. aname.Name = "bug79712a";
  695. aname.CultureInfo = CultureInfo.InvariantCulture;
  696. #if NET_2_0
  697. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
  698. #else
  699. Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
  700. #endif
  701. // PART B
  702. aname = new AssemblyName ();
  703. aname.Name = "bug79712b";
  704. aname.CultureInfo = new CultureInfo ("nl-BE");
  705. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
  706. aname = new AssemblyName ();
  707. aname.Name = "bug79712b";
  708. aname.CultureInfo = new CultureInfo ("en-US");
  709. #if NET_2_0
  710. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  711. #else
  712. Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
  713. #endif
  714. } finally {
  715. AppDomain.Unload (ad);
  716. if (Directory.Exists (tempDir))
  717. Directory.Delete (tempDir, true);
  718. }
  719. }
  720. [Test] // bug #79715
  721. public void Load_PartialVersion ()
  722. {
  723. string tempDir = Path.Combine (Path.GetTempPath (),
  724. "MonoTests.System.Reflection.AssemblyTest");
  725. if (!Directory.Exists (tempDir))
  726. Directory.CreateDirectory (tempDir);
  727. AppDomain ad = CreateTestDomain (tempDir, true);
  728. try {
  729. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  730. AssemblyName aname = new AssemblyName ();
  731. aname.Name = "bug79715";
  732. aname.Version = new Version (1, 2, 3, 4);
  733. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
  734. aname = new AssemblyName ();
  735. aname.Name = "bug79715";
  736. aname.Version = new Version (1, 2);
  737. Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
  738. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
  739. aname = new AssemblyName ();
  740. aname.Name = "bug79715";
  741. aname.Version = new Version (1, 2, 3);
  742. Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
  743. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
  744. aname = new AssemblyName ();
  745. aname.Name = "bug79715";
  746. aname.Version = new Version (1, 2, 3, 4);
  747. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  748. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
  749. } finally {
  750. AppDomain.Unload (ad);
  751. if (Directory.Exists (tempDir))
  752. Directory.Delete (tempDir, true);
  753. }
  754. }
  755. private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
  756. {
  757. AppDomainSetup setup = new AppDomainSetup ();
  758. setup.ApplicationBase = baseDirectory;
  759. setup.ApplicationName = "testdomain";
  760. AppDomain ad = AppDomain.CreateDomain ("testdomain",
  761. AppDomain.CurrentDomain.Evidence, setup);
  762. if (assemblyResolver) {
  763. Assembly ea = Assembly.GetExecutingAssembly ();
  764. ad.CreateInstanceFrom (ea.CodeBase,
  765. typeof (AssemblyResolveHandler).FullName,
  766. false,
  767. BindingFlags.Public | BindingFlags.Instance,
  768. null,
  769. new object [] { ea.Location, ea.FullName },
  770. CultureInfo.InvariantCulture,
  771. null,
  772. null);
  773. }
  774. return ad;
  775. }
  776. private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
  777. {
  778. Type testerType = typeof (CrossDomainTester);
  779. return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
  780. testerType.Assembly.FullName, testerType.FullName, false,
  781. BindingFlags.Public | BindingFlags.Instance, null, new object[0],
  782. CultureInfo.InvariantCulture, new object[0], null);
  783. }
  784. private class CrossDomainTester : MarshalByRefObject
  785. {
  786. public void GenerateAssembly (AssemblyName aname, string path)
  787. {
  788. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  789. aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
  790. ab.Save (Path.GetFileName (path));
  791. }
  792. public void Load (AssemblyName assemblyRef)
  793. {
  794. Assembly.Load (assemblyRef);
  795. }
  796. public void LoadFrom (string assemblyFile)
  797. {
  798. Assembly.LoadFrom (assemblyFile);
  799. }
  800. public bool AssertLoad (AssemblyName assemblyRef)
  801. {
  802. try {
  803. Assembly.Load (assemblyRef);
  804. return true;
  805. } catch {
  806. return false;
  807. }
  808. }
  809. public bool AssertLoad (string assemblyString)
  810. {
  811. try {
  812. Assembly.Load (assemblyString);
  813. return true;
  814. } catch {
  815. return false;
  816. }
  817. }
  818. public bool AssertFileLoadException (AssemblyName assemblyRef)
  819. {
  820. try {
  821. Assembly.Load (assemblyRef);
  822. return false;
  823. } catch (FileLoadException) {
  824. return true;
  825. }
  826. }
  827. public bool AssertFileNotFoundException (AssemblyName assemblyRef)
  828. {
  829. try {
  830. Assembly.Load (assemblyRef);
  831. return false;
  832. } catch (FileNotFoundException) {
  833. return true;
  834. }
  835. }
  836. public void bug78464 (string assemblyFileName)
  837. {
  838. AssemblyName assemblyName = new AssemblyName ();
  839. assemblyName.Name = "bug78464";
  840. AssemblyBuilder ab = AppDomain.CurrentDomain
  841. .DefineDynamicAssembly (assemblyName,
  842. AssemblyBuilderAccess.Save,
  843. Path.GetDirectoryName (assemblyFileName),
  844. AppDomain.CurrentDomain.Evidence);
  845. ab.Save (Path.GetFileName (assemblyFileName));
  846. Assembly assembly;
  847. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  848. byte[] buffer = new byte[fs.Length];
  849. fs.Read (buffer, 0, buffer.Length);
  850. assembly = Assembly.Load (buffer);
  851. fs.Close ();
  852. }
  853. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  854. assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  855. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  856. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
  857. // note: we cannot check if directory names match, as MS.NET seems to
  858. // convert directory part of assembly location to lowercase
  859. Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
  860. }
  861. public void bug78465 (string assemblyFileName)
  862. {
  863. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  864. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  865. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
  866. // note: we cannot check if directory names match, as MS.NET seems to
  867. // convert directory part of assembly location to lowercase
  868. Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
  869. }
  870. public void bug78468 (string assemblyFileName)
  871. {
  872. AssemblyName assemblyName = new AssemblyName ();
  873. assemblyName.Name = "bug78468b";
  874. AssemblyBuilder ab = AppDomain.CurrentDomain
  875. .DefineDynamicAssembly (assemblyName,
  876. AssemblyBuilderAccess.Save,
  877. Path.GetDirectoryName (assemblyFileName),
  878. AppDomain.CurrentDomain.Evidence);
  879. ab.AddResourceFile ("read", "readme.txt");
  880. ab.Save (Path.GetFileName (assemblyFileName));
  881. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  882. Assert.IsTrue (assembly.Location != string.Empty, "#B1");
  883. string[] resNames = assembly.GetManifestResourceNames ();
  884. Assert.IsNotNull (resNames, "#B2");
  885. Assert.AreEqual (1, resNames.Length, "#B3");
  886. Assert.AreEqual ("read", resNames[0], "#B4");
  887. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  888. Assert.IsNotNull (resInfo, "#B5");
  889. Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
  890. Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
  891. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
  892. Stream s = assembly.GetManifestResourceStream ("read");
  893. Assert.IsNotNull (s, "#B9");
  894. s.Close ();
  895. s = assembly.GetFile ("readme.txt");
  896. Assert.IsNotNull (s, "#B10");
  897. s.Close ();
  898. }
  899. }
  900. [Test]
  901. public void bug79872 ()
  902. {
  903. Random rnd = new Random ();
  904. string outdir;
  905. int tries = 0;
  906. retry:
  907. outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
  908. if (Directory.Exists (outdir)) {
  909. try {
  910. Directory.Delete (outdir, true);
  911. } catch {
  912. if (++tries <= 100)
  913. goto retry;
  914. }
  915. }
  916. Directory.CreateDirectory (outdir);
  917. AssemblyName an = new AssemblyName ();
  918. an.Name = "bug79872";
  919. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
  920. string dllname = "bug79872.dll";
  921. ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
  922. string netmodule = "bug79872.netmodule";
  923. ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
  924. TypeBuilder a1 = mb2.DefineType ("A");
  925. a1.CreateType ();
  926. ab.Save (dllname);
  927. bool ok = true;
  928. try {
  929. Assembly.LoadFrom (Path.Combine (outdir, dllname));
  930. } catch {
  931. ok = false;
  932. }
  933. Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
  934. ok = false;
  935. try {
  936. Assembly.LoadFrom (Path.Combine (outdir, netmodule));
  937. } catch (BadImageFormatException) {
  938. ok = true; // mono and .net 2.0 throw this
  939. } catch (FileLoadException) {
  940. ok = true; // .net 1.1 throws this
  941. } catch {
  942. // swallow the rest
  943. }
  944. Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
  945. Directory.Delete (outdir, true);
  946. }
  947. #endif // TARGET_JVM
  948. [Test]
  949. public void ManifestModule ()
  950. {
  951. Assembly assembly = typeof (int).Assembly;
  952. Module module = assembly.ManifestModule;
  953. Assert.IsNotNull (module, "#1");
  954. #if NET_4_0
  955. Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
  956. #else
  957. Assert.AreEqual (typeof (Module), module.GetType (), "#2");
  958. #endif
  959. #if !MONOTOUCH
  960. Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
  961. #endif
  962. Assert.IsFalse (module.IsResource (), "#4");
  963. Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
  964. Assert.AreSame (module, assembly.GetModules () [0], "#6");
  965. Assert.AreSame (module, assembly.ManifestModule, "#7");
  966. }
  967. [Serializable ()]
  968. private class AssemblyResolveHandler
  969. {
  970. public AssemblyResolveHandler (string assemblyFile, string assemblyName)
  971. {
  972. _assemblyFile = assemblyFile;
  973. _assemblyName = assemblyName;
  974. AppDomain.CurrentDomain.AssemblyResolve +=
  975. new ResolveEventHandler (ResolveAssembly);
  976. }
  977. private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
  978. {
  979. if (args.Name == _assemblyName)
  980. return Assembly.LoadFrom (_assemblyFile);
  981. return null;
  982. }
  983. private readonly string _assemblyFile;
  984. private readonly string _assemblyName;
  985. }
  986. protected internal class Bug328812_NestedFamORAssem { };
  987. [Test]
  988. public void bug328812 ()
  989. {
  990. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  991. Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
  992. // Just a sanity check, in case the above passed for some other reason
  993. Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
  994. }
  995. [Test]
  996. public void GetCustomAttributes_AttributeType_Null ()
  997. {
  998. Assembly a = typeof (int).Assembly;
  999. try {
  1000. a.GetCustomAttributes (null, false);
  1001. Assert.Fail ("#1");
  1002. } catch (ArgumentNullException ex) {
  1003. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1004. Assert.IsNull (ex.InnerException, "#3");
  1005. Assert.IsNotNull (ex.Message, "#4");
  1006. Assert.IsNotNull (ex.ParamName, "#5");
  1007. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  1008. }
  1009. }
  1010. [Test]
  1011. public void GetTypeWithEmptyStringShouldThrow ()
  1012. {
  1013. try {
  1014. typeof (string).Assembly.GetType ("");
  1015. Assert.Fail ("#1");
  1016. } catch (ArgumentException) {}
  1017. }
  1018. }
  1019. }