AssemblyTest.cs 29 KB

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