AssemblyTest.cs 36 KB

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