AssemblyTest.cs 29 KB

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