AssemblyTest.cs 35 KB

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