AssemblyTest.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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.Runtime.Serialization;
  41. using System.Security;
  42. namespace MonoTests.System.Reflection
  43. {
  44. [TestFixture]
  45. public class AssemblyTest
  46. {
  47. [Test]
  48. public void CreateInstance()
  49. {
  50. Type type = typeof (AssemblyTest);
  51. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  52. Assert.IsNotNull (obj, "#01");
  53. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  54. }
  55. [Test]
  56. public void CreateInvalidInstance()
  57. {
  58. Type type = typeof (AssemblyTest);
  59. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  60. Assert.IsNull (obj, "#03");
  61. }
  62. [Test]
  63. #if NET_2_0
  64. [Category ("NotWorking")]
  65. [ExpectedException (typeof (ArgumentException))]
  66. #else
  67. [ExpectedException (typeof (TypeLoadException))]
  68. #endif
  69. public void TestGetType ()
  70. {
  71. // Bug #49114
  72. typeof (int).Assembly.GetType ("&blabla", true, true);
  73. }
  74. [Test]
  75. public void GetEntryAssembly ()
  76. {
  77. // note: only available in default appdomain
  78. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  79. // Not sure we should emulate this behavior.
  80. string fname = AppDomain.CurrentDomain.FriendlyName;
  81. if (fname.EndsWith (".dll")) { // nunit-console
  82. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  83. #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
  84. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  85. #endif
  86. } else { // gnunit
  87. Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  88. #if NET_2_0 && !TARGET_JVM // IsDefaultAppDomain not supported for TARGET_JVM
  89. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  90. #endif
  91. }
  92. }
  93. #if !TARGET_JVM // ManifestModule not supported under TARGET_JVM.
  94. #if NET_2_0
  95. [Category ("NotWorking")]
  96. #endif
  97. [Test]
  98. public void Corlib ()
  99. {
  100. Assembly corlib = typeof (int).Assembly;
  101. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  102. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  103. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  104. Assert.IsNotNull (corlib.Evidence, "Evidence");
  105. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  106. // corlib doesn't reference anything
  107. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  108. #if NET_2_0
  109. Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  110. // not really "true" but it's even more trusted so...
  111. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  112. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  113. Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  114. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  115. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  116. #elif NET_1_1
  117. Assert.IsFalse (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  118. Assert.AreEqual ("mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  119. Assert.AreEqual ("v1.1.4322", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  120. #endif
  121. }
  122. [Test]
  123. public void Corlib_test ()
  124. {
  125. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  126. Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
  127. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  128. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  129. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  130. #if NET_2_0
  131. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  132. Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  133. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  134. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  135. #elif NET_1_1
  136. Assert.AreEqual ("v1.1.4322", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  137. #endif
  138. }
  139. #endif
  140. [Test]
  141. public void GetAssembly ()
  142. {
  143. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  144. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  145. }
  146. [Test]
  147. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  148. [ExpectedException (typeof (ArgumentNullException))]
  149. public void GetFile_Null ()
  150. {
  151. Assembly.GetExecutingAssembly ().GetFile (null);
  152. }
  153. [Test]
  154. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  155. [ExpectedException (typeof (ArgumentException))]
  156. public void GetFile_Empty ()
  157. {
  158. Assembly.GetExecutingAssembly ().GetFile (String.Empty);
  159. }
  160. [Test]
  161. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  162. public void GetFiles_False ()
  163. {
  164. Assembly corlib = typeof (int).Assembly;
  165. FileStream[] fss = corlib.GetFiles ();
  166. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  167. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  168. fss = corlib_test.GetFiles ();
  169. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  170. }
  171. [Test]
  172. [Category("TargetJvmNotWorking")] // Not yet supported for TARGET_JVM
  173. public void GetFiles_True ()
  174. {
  175. Assembly corlib = typeof (int).Assembly;
  176. FileStream[] fss = corlib.GetFiles ();
  177. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  178. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  179. fss = corlib_test.GetFiles ();
  180. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  181. }
  182. [Test] // bug #78517
  183. #if ONLY_1_1
  184. [Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
  185. #endif
  186. public void LoadFrom_Empty_Assembly ()
  187. {
  188. string tempFile = Path.GetTempFileName ();
  189. try {
  190. Assembly.LoadFrom (tempFile);
  191. Assert.Fail ("#1");
  192. } catch (BadImageFormatException ex) {
  193. Assert.IsNull (ex.InnerException, "#2");
  194. } finally {
  195. File.Delete (tempFile);
  196. }
  197. }
  198. [Test] // bug #78517
  199. public void LoadFrom_Invalid_Assembly ()
  200. {
  201. string tempFile = Path.GetTempFileName ();
  202. using (StreamWriter sw = File.CreateText (tempFile)) {
  203. sw.WriteLine ("foo");
  204. sw.Close ();
  205. }
  206. try {
  207. Assembly.LoadFrom (tempFile);
  208. Assert.Fail ("#1");
  209. } catch (BadImageFormatException ex) {
  210. Assert.IsNull (ex.InnerException, "#2");
  211. } finally {
  212. File.Delete (tempFile);
  213. }
  214. }
  215. [Test]
  216. public void LoadFrom_NonExisting_Assembly ()
  217. {
  218. string tempFile = Path.GetTempFileName ();
  219. File.Delete (tempFile);
  220. try {
  221. Assembly.LoadFrom (tempFile);
  222. Assert.Fail ("#1");
  223. } catch (FileNotFoundException ex) {
  224. Assert.IsNull (ex.InnerException, "#2");
  225. } finally {
  226. File.Delete (tempFile);
  227. }
  228. }
  229. [Test]
  230. public void LoadWithPartialName ()
  231. {
  232. string [] names = { "corlib_test_default", "corlib_test_net_2_0", "corlib_plattest" };
  233. foreach (string s in names)
  234. if (Assembly.LoadWithPartialName (s) != null)
  235. return;
  236. Assertion.Fail ("Was not able to load any corlib test");
  237. }
  238. #if !TARGET_JVM // GetObjectData currently not implemented for Assembly.
  239. [Test]
  240. [ExpectedException (typeof (ArgumentNullException))]
  241. public void GetObjectData_Null ()
  242. {
  243. Assembly corlib = typeof (int).Assembly;
  244. corlib.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
  245. }
  246. #endif // TARGET_JVM
  247. [Test]
  248. public void GetReferencedAssemblies ()
  249. {
  250. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  251. AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
  252. foreach (AssemblyName an in names) {
  253. Assert.IsNull (an.CodeBase, "CodeBase");
  254. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  255. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  256. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  257. Assert.IsNotNull (an.FullName, "FullName");
  258. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  259. Assert.IsNull (an.KeyPair, "KeyPair");
  260. Assert.IsNotNull (an.Name, "Name");
  261. Assert.IsNotNull (an.Version, "Version");
  262. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  263. an.VersionCompatibility, "VersionCompatibility");
  264. }
  265. }
  266. #if !TARGET_JVM // Reflection.Emit is not supported.
  267. [Test]
  268. public void Location_Empty() {
  269. string assemblyFileName = Path.Combine (
  270. Path.GetTempPath (), "AssemblyLocation.dll");
  271. try {
  272. AssemblyName assemblyName = new AssemblyName ();
  273. assemblyName.Name = "AssemblyLocation";
  274. AssemblyBuilder ab = AppDomain.CurrentDomain
  275. .DefineDynamicAssembly (assemblyName,
  276. AssemblyBuilderAccess.Save,
  277. Path.GetTempPath (),
  278. AppDomain.CurrentDomain.Evidence);
  279. ab.Save (Path.GetFileName (assemblyFileName));
  280. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  281. byte[] buffer = new byte[fs.Length];
  282. fs.Read (buffer, 0, buffer.Length);
  283. Assembly assembly = Assembly.Load (buffer);
  284. Assert.AreEqual (string.Empty, assembly.Location);
  285. fs.Close ();
  286. }
  287. } finally {
  288. File.Delete (assemblyFileName);
  289. }
  290. }
  291. [Test]
  292. [Category ("NotWorking")]
  293. public void bug78464 ()
  294. {
  295. string assemblyFileName = Path.Combine (
  296. Path.GetTempPath (), "bug78464.dll");
  297. try {
  298. // execute test in separate appdomain to allow assembly to be unloaded
  299. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  300. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  301. try {
  302. crossDomainTester.bug78464 (assemblyFileName);
  303. } finally {
  304. AppDomain.Unload (testDomain);
  305. }
  306. } finally {
  307. File.Delete (assemblyFileName);
  308. }
  309. }
  310. [Test]
  311. [Category ("NotWorking")]
  312. public void bug78465 ()
  313. {
  314. string assemblyFileName = Path.Combine (
  315. Path.GetTempPath (), "bug78465.dll");
  316. try {
  317. AssemblyName assemblyName = new AssemblyName ();
  318. assemblyName.Name = "bug78465";
  319. AssemblyBuilder ab = AppDomain.CurrentDomain
  320. .DefineDynamicAssembly (assemblyName,
  321. AssemblyBuilderAccess.Save,
  322. Path.GetDirectoryName (assemblyFileName),
  323. AppDomain.CurrentDomain.Evidence);
  324. ab.Save (Path.GetFileName (assemblyFileName));
  325. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  326. byte[] buffer = new byte[fs.Length];
  327. fs.Read (buffer, 0, buffer.Length);
  328. Assembly assembly = Assembly.Load (buffer);
  329. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  330. fs.Close ();
  331. }
  332. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  333. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  334. try {
  335. crossDomainTester.bug78465 (assemblyFileName);
  336. } finally {
  337. AppDomain.Unload (testDomain);
  338. }
  339. } finally {
  340. File.Delete (assemblyFileName);
  341. }
  342. }
  343. [Test]
  344. public void bug78468 ()
  345. {
  346. string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
  347. "bug78468a.dll");
  348. string resourceFileName = Path.Combine (Path.GetTempPath (),
  349. "readme.txt");
  350. using (StreamWriter sw = File.CreateText (resourceFileName)) {
  351. sw.WriteLine ("FOO");
  352. sw.Close ();
  353. }
  354. try {
  355. AssemblyName assemblyName = new AssemblyName ();
  356. assemblyName.Name = "bug78468a";
  357. AssemblyBuilder ab = AppDomain.CurrentDomain
  358. .DefineDynamicAssembly (assemblyName,
  359. AssemblyBuilderAccess.Save,
  360. Path.GetTempPath (),
  361. AppDomain.CurrentDomain.Evidence);
  362. ab.AddResourceFile ("read", "readme.txt");
  363. ab.Save (Path.GetFileName (assemblyFileNameA));
  364. Assembly assembly;
  365. using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
  366. byte[] buffer = new byte[fs.Length];
  367. fs.Read (buffer, 0, buffer.Length);
  368. assembly = Assembly.Load (buffer);
  369. fs.Close ();
  370. }
  371. Assert.AreEqual (string.Empty, assembly.Location, "#A1");
  372. string[] resNames = assembly.GetManifestResourceNames ();
  373. Assert.IsNotNull (resNames, "#A2");
  374. Assert.AreEqual (1, resNames.Length, "#A3");
  375. Assert.AreEqual ("read", resNames[0], "#A4");
  376. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  377. Assert.IsNotNull (resInfo, "#A5");
  378. Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
  379. Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
  380. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
  381. #if NET_2_0
  382. try {
  383. assembly.GetManifestResourceStream ("read");
  384. Assert.Fail ("#A9");
  385. } catch (FileNotFoundException) {
  386. }
  387. #else
  388. Assert.IsNull (assembly.GetManifestResourceStream ("read"), "#A9");
  389. #endif
  390. try {
  391. assembly.GetFile ("readme.txt");
  392. Assert.Fail ("#A10");
  393. } catch (FileNotFoundException) {
  394. }
  395. string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
  396. "bug78468b.dll");
  397. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  398. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  399. try {
  400. crossDomainTester.bug78468 (assemblyFileNameB);
  401. } finally {
  402. AppDomain.Unload (testDomain);
  403. File.Delete (assemblyFileNameB);
  404. }
  405. } finally {
  406. File.Delete (assemblyFileNameA);
  407. File.Delete (resourceFileName);
  408. }
  409. }
  410. #if NET_2_0
  411. [Test]
  412. [Category ("NotWorking")]
  413. public void ReflectionOnlyLoad ()
  414. {
  415. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  416. Assert.IsNotNull (assembly);
  417. Assert.IsTrue (assembly.ReflectionOnly);
  418. }
  419. [Test]
  420. public void ReflectionOnlyLoadFrom ()
  421. {
  422. string loc = typeof (AssemblyTest).Assembly.Location;
  423. string filename = Path.GetFileName (loc);
  424. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
  425. Assert.IsNotNull (assembly);
  426. Assert.IsTrue (assembly.ReflectionOnly);
  427. }
  428. [Test]
  429. [ExpectedException (typeof (ArgumentException))]
  430. public void CreateInstanceOnRefOnly ()
  431. {
  432. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  433. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  434. }
  435. #endif
  436. [Test]
  437. [Category ("NotWorking")] // patch for bug #79720 must be committed first
  438. public void Load_Culture ()
  439. {
  440. string tempDir = Path.Combine (Path.GetTempPath (),
  441. "MonoTests.System.Reflection.AssemblyTest");
  442. string cultureTempDir = Path.Combine (tempDir, "nl-BE");
  443. if (!Directory.Exists (cultureTempDir))
  444. Directory.CreateDirectory (cultureTempDir);
  445. cultureTempDir = Path.Combine (tempDir, "en-US");
  446. if (!Directory.Exists (cultureTempDir))
  447. Directory.CreateDirectory (cultureTempDir);
  448. AppDomain ad = CreateTestDomain (tempDir, true);
  449. try {
  450. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  451. // PART A
  452. AssemblyName aname = new AssemblyName ();
  453. aname.Name = "culturea";
  454. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
  455. aname = new AssemblyName ();
  456. aname.Name = "culturea";
  457. Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
  458. aname = new AssemblyName ();
  459. aname.Name = "culturea";
  460. aname.CultureInfo = new CultureInfo ("nl-BE");
  461. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
  462. aname = new AssemblyName ();
  463. aname.Name = "culturea";
  464. aname.CultureInfo = CultureInfo.InvariantCulture;
  465. Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
  466. // PART B
  467. aname = new AssemblyName ();
  468. aname.Name = "cultureb";
  469. aname.CultureInfo = new CultureInfo ("nl-BE");
  470. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
  471. aname = new AssemblyName ();
  472. aname.Name = "cultureb";
  473. aname.CultureInfo = new CultureInfo ("nl-BE");
  474. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  475. aname = new AssemblyName ();
  476. aname.Name = "cultureb";
  477. Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
  478. aname = new AssemblyName ();
  479. aname.Name = "cultureb";
  480. aname.CultureInfo = new CultureInfo ("en-US");
  481. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
  482. // PART C
  483. aname = new AssemblyName ();
  484. aname.Name = "culturec";
  485. aname.CultureInfo = new CultureInfo ("nl-BE");
  486. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
  487. aname = new AssemblyName ();
  488. aname.Name = "culturec";
  489. aname.CultureInfo = new CultureInfo ("nl-BE");
  490. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  491. aname = new AssemblyName ();
  492. aname.Name = "culturec";
  493. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
  494. aname = new AssemblyName ();
  495. aname.Name = "culturec";
  496. aname.CultureInfo = CultureInfo.InvariantCulture;
  497. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
  498. // PART D
  499. aname = new AssemblyName ();
  500. aname.Name = "cultured";
  501. aname.CultureInfo = new CultureInfo ("nl-BE");
  502. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
  503. aname = new AssemblyName ();
  504. aname.Name = "cultured";
  505. aname.CultureInfo = new CultureInfo ("nl-BE");
  506. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
  507. aname = new AssemblyName ();
  508. aname.Name = "cultured";
  509. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
  510. aname = new AssemblyName ();
  511. aname.Name = "cultured";
  512. aname.CultureInfo = CultureInfo.InvariantCulture;
  513. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
  514. } finally {
  515. AppDomain.Unload (ad);
  516. if (Directory.Exists (tempDir))
  517. Directory.Delete (tempDir, true);
  518. }
  519. }
  520. [Test] // bug #79712
  521. #if NET_2_0
  522. [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
  523. #else
  524. [Category ("NotWorking")]
  525. #endif
  526. public void Load_Culture_Mismatch ()
  527. {
  528. string tempDir = Path.Combine (Path.GetTempPath (),
  529. "MonoTests.System.Reflection.AssemblyTest");
  530. string cultureTempDir = Path.Combine (tempDir, "en-US");
  531. if (!Directory.Exists (cultureTempDir))
  532. Directory.CreateDirectory (cultureTempDir);
  533. AppDomain ad = CreateTestDomain (tempDir, true);
  534. try {
  535. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  536. // PART A
  537. AssemblyName aname = new AssemblyName ();
  538. aname.Name = "bug79712a";
  539. aname.CultureInfo = new CultureInfo ("nl-BE");
  540. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
  541. aname = new AssemblyName ();
  542. aname.Name = "bug79712a";
  543. aname.CultureInfo = CultureInfo.InvariantCulture;
  544. #if NET_2_0
  545. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
  546. #else
  547. Assert.IsTrue (cdt.AssertFileLoadException (aname), "#A2");
  548. #endif
  549. // PART B
  550. aname = new AssemblyName ();
  551. aname.Name = "bug79712b";
  552. aname.CultureInfo = new CultureInfo ("nl-BE");
  553. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
  554. aname = new AssemblyName ();
  555. aname.Name = "bug79712b";
  556. aname.CultureInfo = new CultureInfo ("en-US");
  557. #if NET_2_0
  558. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  559. #else
  560. Assert.IsTrue (cdt.AssertFileLoadException (aname), "#B2");
  561. #endif
  562. } finally {
  563. AppDomain.Unload (ad);
  564. if (Directory.Exists (tempDir))
  565. Directory.Delete (tempDir, true);
  566. }
  567. }
  568. [Test] // bug #79715
  569. public void Load_PartialVersion ()
  570. {
  571. string tempDir = Path.Combine (Path.GetTempPath (),
  572. "MonoTests.System.Reflection.AssemblyTest");
  573. if (!Directory.Exists (tempDir))
  574. Directory.CreateDirectory (tempDir);
  575. AppDomain ad = CreateTestDomain (tempDir, true);
  576. try {
  577. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  578. AssemblyName aname = new AssemblyName ();
  579. aname.Name = "bug79715";
  580. aname.Version = new Version (1, 2, 3, 4);
  581. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
  582. aname = new AssemblyName ();
  583. aname.Name = "bug79715";
  584. aname.Version = new Version (1, 2);
  585. Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
  586. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
  587. aname = new AssemblyName ();
  588. aname.Name = "bug79715";
  589. aname.Version = new Version (1, 2, 3);
  590. Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
  591. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
  592. aname = new AssemblyName ();
  593. aname.Name = "bug79715";
  594. aname.Version = new Version (1, 2, 3, 4);
  595. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  596. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
  597. } finally {
  598. AppDomain.Unload (ad);
  599. if (Directory.Exists (tempDir))
  600. Directory.Delete (tempDir, true);
  601. }
  602. }
  603. private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
  604. {
  605. AppDomainSetup setup = new AppDomainSetup ();
  606. setup.ApplicationBase = baseDirectory;
  607. setup.ApplicationName = "testdomain";
  608. AppDomain ad = AppDomain.CreateDomain ("testdomain",
  609. AppDomain.CurrentDomain.Evidence, setup);
  610. if (assemblyResolver) {
  611. Assembly ea = Assembly.GetExecutingAssembly ();
  612. ad.CreateInstanceFrom (ea.CodeBase,
  613. typeof (AssemblyResolveHandler).FullName,
  614. false,
  615. BindingFlags.Public | BindingFlags.Instance,
  616. null,
  617. new object [] { ea.Location, ea.FullName },
  618. CultureInfo.InvariantCulture,
  619. null,
  620. null);
  621. }
  622. return ad;
  623. }
  624. private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
  625. {
  626. Type testerType = typeof (CrossDomainTester);
  627. return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
  628. testerType.Assembly.FullName, testerType.FullName, false,
  629. BindingFlags.Public | BindingFlags.Instance, null, new object[0],
  630. CultureInfo.InvariantCulture, new object[0], null);
  631. }
  632. private class CrossDomainTester : MarshalByRefObject
  633. {
  634. public void GenerateAssembly (AssemblyName aname, string path)
  635. {
  636. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  637. aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
  638. ab.Save (Path.GetFileName (path));
  639. }
  640. public void Load (AssemblyName assemblyRef)
  641. {
  642. Assembly.Load (assemblyRef);
  643. }
  644. public void LoadFrom (string assemblyFile)
  645. {
  646. Assembly.LoadFrom (assemblyFile);
  647. }
  648. public bool AssertLoad (AssemblyName assemblyRef)
  649. {
  650. try {
  651. Assembly.Load (assemblyRef);
  652. return true;
  653. } catch {
  654. return false;
  655. }
  656. }
  657. public bool AssertLoad (string assemblyString)
  658. {
  659. try {
  660. Assembly.Load (assemblyString);
  661. return true;
  662. } catch {
  663. return false;
  664. }
  665. }
  666. public bool AssertFileLoadException (AssemblyName assemblyRef)
  667. {
  668. try {
  669. Assembly.Load (assemblyRef);
  670. return false;
  671. } catch (FileLoadException) {
  672. return true;
  673. }
  674. }
  675. public bool AssertFileNotFoundException (AssemblyName assemblyRef)
  676. {
  677. try {
  678. Assembly.Load (assemblyRef);
  679. return false;
  680. } catch (FileNotFoundException) {
  681. return true;
  682. }
  683. }
  684. public void bug78464 (string assemblyFileName)
  685. {
  686. AssemblyName assemblyName = new AssemblyName ();
  687. assemblyName.Name = "bug78464";
  688. AssemblyBuilder ab = AppDomain.CurrentDomain
  689. .DefineDynamicAssembly (assemblyName,
  690. AssemblyBuilderAccess.Save,
  691. Path.GetDirectoryName (assemblyFileName),
  692. AppDomain.CurrentDomain.Evidence);
  693. ab.Save (Path.GetFileName (assemblyFileName));
  694. Assembly assembly;
  695. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  696. byte[] buffer = new byte[fs.Length];
  697. fs.Read (buffer, 0, buffer.Length);
  698. assembly = Assembly.Load (buffer);
  699. fs.Close ();
  700. }
  701. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  702. assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  703. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  704. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
  705. // note: we cannot check if directory names match, as MS.NET seems to
  706. // convert directory part of assembly location to lowercase
  707. Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
  708. }
  709. public void bug78465 (string assemblyFileName)
  710. {
  711. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  712. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  713. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
  714. // note: we cannot check if directory names match, as MS.NET seems to
  715. // convert directory part of assembly location to lowercase
  716. Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
  717. }
  718. public void bug78468 (string assemblyFileName)
  719. {
  720. AssemblyName assemblyName = new AssemblyName ();
  721. assemblyName.Name = "bug78468b";
  722. AssemblyBuilder ab = AppDomain.CurrentDomain
  723. .DefineDynamicAssembly (assemblyName,
  724. AssemblyBuilderAccess.Save,
  725. Path.GetDirectoryName (assemblyFileName),
  726. AppDomain.CurrentDomain.Evidence);
  727. ab.AddResourceFile ("read", "readme.txt");
  728. ab.Save (Path.GetFileName (assemblyFileName));
  729. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  730. Assert.IsTrue (assembly.Location != string.Empty, "#B1");
  731. string[] resNames = assembly.GetManifestResourceNames ();
  732. Assert.IsNotNull (resNames, "#B2");
  733. Assert.AreEqual (1, resNames.Length, "#B3");
  734. Assert.AreEqual ("read", resNames[0], "#B4");
  735. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  736. Assert.IsNotNull (resInfo, "#B5");
  737. Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
  738. Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
  739. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
  740. Stream s = assembly.GetManifestResourceStream ("read");
  741. Assert.IsNotNull (s, "#B9");
  742. s.Close ();
  743. s = assembly.GetFile ("readme.txt");
  744. Assert.IsNotNull (s, "#B10");
  745. s.Close ();
  746. }
  747. }
  748. [Test]
  749. public void bug79872 ()
  750. {
  751. Random rnd = new Random ();
  752. string outdir;
  753. int tries = 0;
  754. retry:
  755. outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
  756. if (Directory.Exists (outdir)) {
  757. try {
  758. Directory.Delete (outdir, true);
  759. } catch {
  760. if (++tries <= 100)
  761. goto retry;
  762. }
  763. }
  764. Directory.CreateDirectory (outdir);
  765. AssemblyName an = new AssemblyName ();
  766. an.Name = "bug79872";
  767. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
  768. string dllname = "bug79872.dll";
  769. ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
  770. string netmodule = "bug79872.netmodule";
  771. ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
  772. TypeBuilder a1 = mb2.DefineType ("A");
  773. a1.CreateType ();
  774. ab.Save (dllname);
  775. bool ok = true;
  776. try {
  777. Assembly.LoadFrom (Path.Combine (outdir, dllname));
  778. } catch {
  779. ok = false;
  780. }
  781. Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
  782. ok = false;
  783. try {
  784. Assembly.LoadFrom (Path.Combine (outdir, netmodule));
  785. } catch (BadImageFormatException) {
  786. ok = true; // mono and .net 2.0 throw this
  787. } catch (FileLoadException) {
  788. ok = true; // .net 1.1 throws this
  789. } catch {
  790. // swallow the rest
  791. }
  792. Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
  793. Directory.Delete (outdir, true);
  794. }
  795. #endif // TARGET_JVM
  796. [Serializable ()]
  797. private class AssemblyResolveHandler
  798. {
  799. public AssemblyResolveHandler (string assemblyFile, string assemblyName)
  800. {
  801. _assemblyFile = assemblyFile;
  802. _assemblyName = assemblyName;
  803. AppDomain.CurrentDomain.AssemblyResolve +=
  804. new ResolveEventHandler (ResolveAssembly);
  805. }
  806. private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
  807. {
  808. if (args.Name == _assemblyName)
  809. return Assembly.LoadFrom (_assemblyFile);
  810. return null;
  811. }
  812. private readonly string _assemblyFile;
  813. private readonly string _assemblyName;
  814. }
  815. }
  816. }