AssemblyTest.cs 36 KB

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