AssemblyTest.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. // Aleksey Kliger ([email protected])
  9. //
  10. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  11. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  12. // Copyright (C) 2015 Xamarin, Inc. (http://www.xamarin.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using NUnit.Framework;
  34. using System;
  35. using System.Configuration.Assemblies;
  36. using System.Globalization;
  37. using System.IO;
  38. using System.Reflection;
  39. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  40. using System.Reflection.Emit;
  41. #endif
  42. using System.Threading;
  43. using System.Runtime.Serialization;
  44. using System.Runtime.CompilerServices;
  45. using System.Security;
  46. using System.Linq;
  47. using System.Resources;
  48. // Used by GetType_TypeForwarder_Nested ()
  49. [assembly: System.Runtime.CompilerServices.TypeForwardedToAttribute(typeof(System.Globalization.CultureInfo))]
  50. namespace MonoTests.System.Reflection
  51. {
  52. [TestFixture]
  53. public class AssemblyTest
  54. {
  55. static string BaseTempFolder = Path.Combine (Path.GetTempPath (),
  56. "MonoTests.System.Reflection.AssemblyTest");
  57. static string TempFolder;
  58. [TestFixtureSetUp]
  59. public void FixtureSetUp ()
  60. {
  61. try {
  62. // Try to cleanup from any previous NUnit run.
  63. Directory.Delete (BaseTempFolder, true);
  64. } catch (Exception) {
  65. }
  66. }
  67. [SetUp]
  68. public void SetUp ()
  69. {
  70. int i = 0;
  71. do {
  72. TempFolder = Path.Combine (BaseTempFolder, (++i).ToString());
  73. } while (Directory.Exists (TempFolder));
  74. Directory.CreateDirectory (TempFolder);
  75. }
  76. [TearDown]
  77. public void TearDown ()
  78. {
  79. try {
  80. // This throws an exception under MS.NET and Mono on Windows,
  81. // since the directory contains loaded assemblies.
  82. Directory.Delete (TempFolder, true);
  83. } catch (Exception) {
  84. }
  85. }
  86. [Test]
  87. public void CreateInstance()
  88. {
  89. Type type = typeof (AssemblyTest);
  90. Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  91. Assert.IsNotNull (obj, "#01");
  92. Assert.AreEqual (GetType (), obj.GetType (), "#02");
  93. }
  94. [Test]
  95. public void CreateInvalidInstance()
  96. {
  97. Type type = typeof (AssemblyTest);
  98. Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
  99. Assert.IsNull (obj, "#03");
  100. }
  101. [Test] // bug #49114
  102. [ExpectedException (typeof (ArgumentException))]
  103. public void GetType_TypeName_Invalid ()
  104. {
  105. typeof (int).Assembly.GetType ("&blabla", true, true);
  106. }
  107. [Test] // bug #17571
  108. [ExpectedException (typeof (ArgumentException))]
  109. public void GetType_Invalid_RefPtr () {
  110. typeof (int).Assembly.GetType ("System.Int32&*", true, true);
  111. }
  112. [Test]
  113. [ExpectedException (typeof (ArgumentException))]
  114. public void GetType_Invalid_RefArray () {
  115. typeof (int).Assembly.GetType ("System.Int32&[]", true, true);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void GetType_Invalid_RefGeneric () {
  120. typeof (int).Assembly.GetType ("System.Tuple`1&[System.Int32]", true, true);
  121. }
  122. [Test]
  123. [ExpectedException (typeof (ArgumentException))]
  124. public void GetType_Invalid_PtrGeneric () {
  125. typeof (int).Assembly.GetType ("System.Tuple`1*[System.Int32]", true, true);
  126. }
  127. [Test]
  128. public void GetType_ComposeModifiers () {
  129. var a = typeof(int).Assembly;
  130. var e1 = typeof (Int32).MakePointerType().MakeByRefType();
  131. var t1 = a.GetType ("System.Int32*&", true, true);
  132. Assert.AreEqual (e1, t1, "#1");
  133. var e2 = typeof (Int32).MakeArrayType(2).MakeByRefType();
  134. var t2 = a.GetType ("System.Int32[,]&", true, true);
  135. Assert.AreEqual (e2, t2, "#2");
  136. var e3 = typeof (Int32).MakePointerType().MakeArrayType();
  137. var t3 = a.GetType ("System.Int32*[]", true, true);
  138. Assert.AreEqual (e3, t3, "#3");
  139. var e4 = typeof (Int32).MakeArrayType().MakePointerType().MakePointerType().MakeArrayType().MakePointerType().MakeByRefType();
  140. var t4 = a.GetType ("System.Int32[]**[]*&", true, true);
  141. Assert.AreEqual (e4, t4, "#4");
  142. }
  143. [Test] // bug #334203
  144. public void GetType_TypeName_AssemblyName ()
  145. {
  146. Assembly a = typeof (int).Assembly;
  147. string typeName = typeof (string).AssemblyQualifiedName;
  148. try {
  149. a.GetType (typeName, true, false);
  150. Assert.Fail ("#A1");
  151. } catch (ArgumentException ex) {
  152. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  153. Assert.IsNull (ex.InnerException, "#A3");
  154. Assert.IsNotNull (ex.Message, "#A4");
  155. Assert.IsNull (ex.ParamName, "#A5");
  156. }
  157. Type type = a.GetType (typeName, false);
  158. Assert.IsNull (type, "#B1");
  159. type = a.GetType (typeName, false, true);
  160. Assert.IsNull (type, "#B2");
  161. }
  162. [Test]
  163. public void GetType_TypeForwarder_Nested () {
  164. // System.Globalization is a PCL assembly
  165. Type t = typeof (AssemblyTest).Assembly.GetType ("System.Globalization.CultureInfo/Data");
  166. Assert.IsNotNull (t);
  167. Assert.AreEqual ("System.Globalization.CultureInfo+Data", t.FullName);
  168. }
  169. [Test]
  170. public void GetEntryAssembly ()
  171. {
  172. // note: only available in default appdomain
  173. // http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
  174. // Not sure we should emulate this behavior.
  175. #if __WATCHOS__
  176. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  177. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  178. #elif !MONODROID
  179. string fname = AppDomain.CurrentDomain.FriendlyName;
  180. if (fname.EndsWith (".dll")) { // nunit-console
  181. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  182. Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  183. } else { // gnunit
  184. Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  185. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  186. }
  187. #else
  188. Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
  189. Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
  190. #endif
  191. }
  192. #if !MONOTOUCH && !FULL_AOT_RUNTIME // Reflection.Emit is not supported.
  193. [Test]
  194. public void GetModules_MissingFile ()
  195. {
  196. AssemblyName newName = new AssemblyName ();
  197. newName.Name = "AssemblyTest";
  198. AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  199. ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", false);
  200. ab.Save ("test_assembly.dll");
  201. File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
  202. Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
  203. try {
  204. ass.GetModules ();
  205. Assert.Fail ();
  206. } catch (FileNotFoundException ex) {
  207. Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
  208. }
  209. }
  210. #endif
  211. [Category ("NotWorking")]
  212. [Test]
  213. public void Corlib ()
  214. {
  215. Assembly corlib = typeof (int).Assembly;
  216. Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
  217. Assert.IsNull (corlib.EntryPoint, "EntryPoint");
  218. Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
  219. Assert.IsNotNull (corlib.Evidence, "Evidence");
  220. Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
  221. // corlib doesn't reference anything
  222. Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
  223. Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
  224. // not really "true" but it's even more trusted so...
  225. Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
  226. Assert.AreEqual (0, corlib.HostContext, "HostContext");
  227. Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
  228. Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
  229. Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
  230. }
  231. [Test]
  232. public void Corlib_test ()
  233. {
  234. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  235. #if MOBILE
  236. Assert.IsNull (corlib_test.Evidence, "Evidence");
  237. #else
  238. Assert.IsNotNull (corlib_test.Evidence, "Evidence");
  239. #endif
  240. Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
  241. Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
  242. Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
  243. Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
  244. Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
  245. Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
  246. }
  247. [Test]
  248. public void GetAssembly ()
  249. {
  250. Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
  251. Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
  252. }
  253. [Test]
  254. public void GetFile_Null ()
  255. {
  256. try {
  257. Assembly.GetExecutingAssembly ().GetFile (null);
  258. Assert.Fail ("#1");
  259. } catch (ArgumentNullException ex) {
  260. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  261. Assert.IsNull (ex.InnerException, "#3");
  262. Assert.IsNotNull (ex.Message, "#4");
  263. Assert.IsNull (ex.ParamName, "#5");
  264. }
  265. }
  266. [Test]
  267. public void GetFile_Empty ()
  268. {
  269. try {
  270. Assembly.GetExecutingAssembly ().GetFile (
  271. String.Empty);
  272. Assert.Fail ("#1");
  273. } catch (ArgumentException ex) {
  274. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  275. Assert.IsNull (ex.InnerException, "#3");
  276. Assert.IsNotNull (ex.Message, "#4");
  277. Assert.IsNull (ex.ParamName, "#5");
  278. }
  279. }
  280. [Test]
  281. [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
  282. [Category ("StaticLinkedAotNotWorking")] // Can't find .dll files when bundled in .exe
  283. public void GetFiles_False ()
  284. {
  285. Assembly corlib = typeof (int).Assembly;
  286. FileStream[] fss = corlib.GetFiles ();
  287. Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
  288. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  289. fss = corlib_test.GetFiles ();
  290. Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
  291. }
  292. [Test]
  293. [Category ("AndroidNotWorking")] // Assemblies in Xamarin.Android cannot be accessed as FileStream
  294. [Category ("StaticLinkedAotNotWorking")] // Can't find .dll files when bundled in .exe
  295. public void GetFiles_True ()
  296. {
  297. Assembly corlib = typeof (int).Assembly;
  298. FileStream[] fss = corlib.GetFiles ();
  299. Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
  300. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  301. fss = corlib_test.GetFiles ();
  302. Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
  303. }
  304. [Test]
  305. public void GetManifestResourceStream_Name_Empty ()
  306. {
  307. Assembly corlib = typeof (int).Assembly;
  308. try {
  309. corlib.GetManifestResourceStream (string.Empty);
  310. Assert.Fail ("#A1");
  311. } catch (ArgumentException ex) {
  312. // String cannot have zero length
  313. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  314. Assert.IsNull (ex.InnerException, "#A3");
  315. Assert.IsNotNull (ex.Message, "#A4");
  316. }
  317. corlib.GetManifestResourceStream (typeof (int), string.Empty);
  318. try {
  319. corlib.GetManifestResourceStream ((Type) null, string.Empty);
  320. Assert.Fail ("#B1");
  321. } catch (ArgumentException ex) {
  322. // String cannot have zero length
  323. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  324. Assert.IsNull (ex.InnerException, "#B3");
  325. Assert.IsNotNull (ex.Message, "#B4");
  326. }
  327. }
  328. [Test]
  329. public void GetManifestResourceStream_Name_Null ()
  330. {
  331. Assembly corlib = typeof (int).Assembly;
  332. try {
  333. corlib.GetManifestResourceStream ((string) null);
  334. Assert.Fail ("#A1");
  335. } catch (ArgumentNullException ex) {
  336. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  337. Assert.IsNull (ex.InnerException, "#A3");
  338. Assert.IsNotNull (ex.Message, "#A4");
  339. }
  340. corlib.GetManifestResourceStream (typeof (int), (string) null);
  341. try {
  342. corlib.GetManifestResourceStream ((Type) null, (string) null);
  343. Assert.Fail ("#B1");
  344. } catch (ArgumentNullException ex) {
  345. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  346. Assert.IsNull (ex.InnerException, "#B3");
  347. Assert.IsNotNull (ex.Message, "#B4");
  348. Assert.IsNotNull (ex.ParamName, "#B5");
  349. Assert.AreEqual ("type", ex.ParamName, "#B6");
  350. }
  351. }
  352. [Test]
  353. public void IsDefined_AttributeType_Null ()
  354. {
  355. Assembly corlib = typeof (int).Assembly;
  356. try {
  357. corlib.IsDefined ((Type) null, false);
  358. Assert.Fail ("#1");
  359. } catch (ArgumentNullException ex) {
  360. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  361. Assert.IsNull (ex.InnerException, "#3");
  362. Assert.IsNotNull (ex.Message, "#4");
  363. Assert.IsNotNull (ex.ParamName, "#5");
  364. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  365. }
  366. }
  367. [Test] // bug #78517
  368. public void LoadFrom_Empty_Assembly ()
  369. {
  370. string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
  371. File.CreateText (tempFile).Close ();
  372. try {
  373. Assembly.LoadFrom (tempFile);
  374. Assert.Fail ("#1");
  375. } catch (BadImageFormatException ex) {
  376. Assert.IsNull (ex.InnerException, "#2");
  377. }
  378. }
  379. [Test] // bug #78517
  380. public void LoadFrom_Invalid_Assembly ()
  381. {
  382. string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
  383. using (StreamWriter sw = File.CreateText (tempFile)) {
  384. sw.WriteLine ("foo");
  385. sw.Close ();
  386. }
  387. try {
  388. Assembly.LoadFrom (tempFile);
  389. Assert.Fail ("#1");
  390. } catch (BadImageFormatException ex) {
  391. Assert.IsNull (ex.InnerException, "#2");
  392. }
  393. }
  394. [Test]
  395. public void LoadFrom_NonExisting_Assembly ()
  396. {
  397. string tempFile = Path.Combine (TempFolder, Path.GetRandomFileName ());
  398. try {
  399. Assembly.LoadFrom (tempFile);
  400. Assert.Fail ("#1");
  401. } catch (FileNotFoundException ex) {
  402. Assert.IsNull (ex.InnerException, "#2");
  403. }
  404. }
  405. [Test]
  406. public void LoadWithPartialName ()
  407. {
  408. // FIXME?
  409. // So the problem here is that if we load an assembly
  410. // in a fully aot mode and then cannot load the
  411. // dylib, we will assert. This test is incompatible
  412. // with the semantics of aot'ed assembly loading, as
  413. // aot may assert when loading. This assumes that it's
  414. // safe to greedly load everything.
  415. var names = new string[] { Assembly.GetCallingAssembly ().GetName ().Name };
  416. foreach (string s in names)
  417. if (Assembly.LoadWithPartialName (s) != null)
  418. return;
  419. Assert.Fail ("Was not able to load any corlib test");
  420. }
  421. [Test]
  422. public void GetObjectData_Info_Null ()
  423. {
  424. Assembly corlib = typeof (int).Assembly;
  425. try {
  426. corlib.GetObjectData (null, new StreamingContext (
  427. StreamingContextStates.All));
  428. Assert.Fail ("#1");
  429. } catch (ArgumentNullException ex) {
  430. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  431. Assert.IsNull (ex.InnerException, "#3");
  432. Assert.IsNotNull (ex.Message, "#4");
  433. Assert.IsNotNull (ex.ParamName, "#5");
  434. Assert.AreEqual ("info", ex.ParamName, "#6");
  435. }
  436. }
  437. [Test]
  438. public void GetReferencedAssemblies ()
  439. {
  440. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  441. AssemblyName an = corlib_test.GetReferencedAssemblies ().First (l => l.Name == "mscorlib");
  442. Assert.IsNull (an.CodeBase, "CodeBase");
  443. Assert.IsNotNull (an.CultureInfo, "CultureInfo");
  444. Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
  445. Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
  446. Assert.IsNotNull (an.FullName, "FullName");
  447. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
  448. Assert.IsNull (an.KeyPair, "KeyPair");
  449. Assert.IsNotNull (an.Name, "Name");
  450. Assert.IsNotNull (an.Version, "Version");
  451. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, an.VersionCompatibility, "VersionCompatibility");
  452. }
  453. #if !MONOTOUCH && !FULL_AOT_RUNTIME // Reflection.Emit is not supported.
  454. [Test]
  455. public void Location_Empty() {
  456. string assemblyFileName = Path.Combine (
  457. TempFolder, "AssemblyLocation.dll");
  458. AssemblyName assemblyName = new AssemblyName ();
  459. assemblyName.Name = "AssemblyLocation";
  460. AssemblyBuilder ab = AppDomain.CurrentDomain
  461. .DefineDynamicAssembly (assemblyName,
  462. AssemblyBuilderAccess.Save,
  463. TempFolder,
  464. AppDomain.CurrentDomain.Evidence);
  465. ab.Save (Path.GetFileName (assemblyFileName));
  466. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  467. byte[] buffer = new byte[fs.Length];
  468. fs.Read (buffer, 0, buffer.Length);
  469. Assembly assembly = Assembly.Load (buffer);
  470. Assert.AreEqual (string.Empty, assembly.Location);
  471. fs.Close ();
  472. }
  473. }
  474. [Test]
  475. public void SateliteAssemblyForInMemoryAssembly ()
  476. {
  477. string assemblyFileName = Path.Combine (
  478. TempFolder, "AssemblyLocation1.dll");
  479. AssemblyName assemblyName = new AssemblyName ();
  480. assemblyName.Name = "AssemblyLocation1";
  481. AssemblyBuilder ab = AppDomain.CurrentDomain
  482. .DefineDynamicAssembly (assemblyName,
  483. AssemblyBuilderAccess.Save,
  484. TempFolder);
  485. ModuleBuilder moduleBuilder = ab.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
  486. TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);
  487. MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
  488. ILGenerator gen = methodBuilder.GetILGenerator ();
  489. //
  490. // var resourceManager = new ResourceManager (typeof (Program));
  491. // resourceManager.GetString ("test");
  492. //
  493. gen.Emit (OpCodes.Ldtoken, typeBuilder);
  494. gen.Emit (OpCodes.Call, typeof(Type).GetMethod ("GetTypeFromHandle"));
  495. gen.Emit (OpCodes.Newobj, typeof(ResourceManager).GetConstructor (new Type[] { typeof(Type) }));
  496. gen.Emit (OpCodes.Ldstr, "test");
  497. gen.Emit (OpCodes.Callvirt, typeof(ResourceManager).GetMethod ("GetString", new Type[] { typeof(string) }));
  498. gen.Emit (OpCodes.Pop);
  499. gen.Emit (OpCodes.Ret);
  500. typeBuilder.CreateType ();
  501. ab.Save (Path.GetFileName (assemblyFileName));
  502. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  503. byte[] buffer = new byte[fs.Length];
  504. fs.Read (buffer, 0, buffer.Length);
  505. Assembly assembly = Assembly.Load (buffer);
  506. var mm = assembly.GetType ("Program").GetMethod ("TestCall");
  507. try {
  508. mm.Invoke (null, null);
  509. Assert.Fail ();
  510. } catch (TargetInvocationException e) {
  511. Assert.IsTrue (e.InnerException is MissingManifestResourceException);
  512. }
  513. fs.Close ();
  514. }
  515. }
  516. [Test]
  517. [Category ("NotWorking")]
  518. public void bug78464 ()
  519. {
  520. string assemblyFileName = Path.Combine (
  521. TempFolder, "bug78464.dll");
  522. // execute test in separate appdomain to allow assembly to be unloaded
  523. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  524. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  525. try {
  526. crossDomainTester.bug78464 (assemblyFileName);
  527. } finally {
  528. AppDomain.Unload (testDomain);
  529. }
  530. }
  531. [Test]
  532. [Category("MobileNotWorking")]
  533. public void bug78465 ()
  534. {
  535. string assemblyFileName = Path.Combine (
  536. TempFolder, "bug78465.dll");
  537. AssemblyName assemblyName = new AssemblyName ();
  538. assemblyName.Name = "bug78465";
  539. AssemblyBuilder ab = AppDomain.CurrentDomain
  540. .DefineDynamicAssembly (assemblyName,
  541. AssemblyBuilderAccess.Save,
  542. Path.GetDirectoryName (assemblyFileName),
  543. AppDomain.CurrentDomain.Evidence);
  544. ab.Save (Path.GetFileName (assemblyFileName));
  545. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  546. byte[] buffer = new byte[fs.Length];
  547. fs.Read (buffer, 0, buffer.Length);
  548. Assembly assembly = Assembly.Load (buffer);
  549. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  550. fs.Close ();
  551. }
  552. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  553. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  554. try {
  555. crossDomainTester.bug78465 (assemblyFileName);
  556. } finally {
  557. AppDomain.Unload (testDomain);
  558. }
  559. }
  560. [Test]
  561. [Category("MobileNotWorking")]
  562. public void bug78468 ()
  563. {
  564. string assemblyFileNameA = Path.Combine (TempFolder,
  565. "bug78468a.dll");
  566. string resourceFileName = Path.Combine (TempFolder,
  567. "readme.txt");
  568. using (StreamWriter sw = File.CreateText (resourceFileName)) {
  569. sw.WriteLine ("FOO");
  570. sw.Close ();
  571. }
  572. AssemblyName assemblyName = new AssemblyName ();
  573. assemblyName.Name = "bug78468a";
  574. AssemblyBuilder ab = AppDomain.CurrentDomain
  575. .DefineDynamicAssembly (assemblyName,
  576. AssemblyBuilderAccess.Save,
  577. TempFolder,
  578. AppDomain.CurrentDomain.Evidence);
  579. ab.AddResourceFile ("read", "readme.txt");
  580. ab.Save (Path.GetFileName (assemblyFileNameA));
  581. Assembly assembly;
  582. using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
  583. byte[] buffer = new byte[fs.Length];
  584. fs.Read (buffer, 0, buffer.Length);
  585. assembly = Assembly.Load (buffer);
  586. fs.Close ();
  587. }
  588. Assert.AreEqual (string.Empty, assembly.Location, "#A1");
  589. string[] resNames = assembly.GetManifestResourceNames ();
  590. Assert.IsNotNull (resNames, "#A2");
  591. Assert.AreEqual (1, resNames.Length, "#A3");
  592. Assert.AreEqual ("read", resNames[0], "#A4");
  593. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  594. Assert.IsNotNull (resInfo, "#A5");
  595. Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
  596. Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
  597. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
  598. try {
  599. assembly.GetManifestResourceStream ("read");
  600. Assert.Fail ("#A9");
  601. } catch (FileNotFoundException) {
  602. }
  603. try {
  604. assembly.GetFile ("readme.txt");
  605. Assert.Fail ("#A10");
  606. } catch (FileNotFoundException) {
  607. }
  608. string assemblyFileNameB = Path.Combine (TempFolder,
  609. "bug78468b.dll");
  610. AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
  611. CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
  612. try {
  613. crossDomainTester.bug78468 (assemblyFileNameB);
  614. } finally {
  615. AppDomain.Unload (testDomain);
  616. }
  617. }
  618. [Test]
  619. [Category ("NotWorking")]
  620. public void ReflectionOnlyLoad ()
  621. {
  622. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  623. Assert.IsNotNull (assembly);
  624. Assert.IsTrue (assembly.ReflectionOnly);
  625. }
  626. [Test]
  627. [Category ("AndroidNotWorking")] // Xamarin.Android assemblies are bundled so they don't exist in the file system.
  628. public void ReflectionOnlyLoadFrom ()
  629. {
  630. string loc = typeof (AssemblyTest).Assembly.Location;
  631. string filename = Path.GetFileName (loc);
  632. Assembly assembly = Assembly.ReflectionOnlyLoadFrom (loc);
  633. Assert.IsNotNull (assembly);
  634. Assert.IsTrue (assembly.ReflectionOnly);
  635. }
  636. [Test]
  637. [ExpectedException (typeof (ArgumentException))]
  638. public void CreateInstanceOnRefOnly ()
  639. {
  640. Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
  641. assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
  642. }
  643. [Test]
  644. [Category ("NotWorking")] // patch for bug #79720 must be committed first
  645. public void Load_Culture ()
  646. {
  647. string tempDir = TempFolder;
  648. string cultureTempDir = Path.Combine (tempDir, "nl-BE");
  649. if (!Directory.Exists (cultureTempDir))
  650. Directory.CreateDirectory (cultureTempDir);
  651. cultureTempDir = Path.Combine (tempDir, "en-US");
  652. if (!Directory.Exists (cultureTempDir))
  653. Directory.CreateDirectory (cultureTempDir);
  654. AppDomain ad = CreateTestDomain (tempDir, true);
  655. try {
  656. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  657. // PART A
  658. AssemblyName aname = new AssemblyName ();
  659. aname.Name = "culturea";
  660. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
  661. aname = new AssemblyName ();
  662. aname.Name = "culturea";
  663. Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
  664. aname = new AssemblyName ();
  665. aname.Name = "culturea";
  666. aname.CultureInfo = new CultureInfo ("nl-BE");
  667. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
  668. aname = new AssemblyName ();
  669. aname.Name = "culturea";
  670. aname.CultureInfo = CultureInfo.InvariantCulture;
  671. Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
  672. // PART B
  673. aname = new AssemblyName ();
  674. aname.Name = "cultureb";
  675. aname.CultureInfo = new CultureInfo ("nl-BE");
  676. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
  677. aname = new AssemblyName ();
  678. aname.Name = "cultureb";
  679. aname.CultureInfo = new CultureInfo ("nl-BE");
  680. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  681. aname = new AssemblyName ();
  682. aname.Name = "cultureb";
  683. Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
  684. aname = new AssemblyName ();
  685. aname.Name = "cultureb";
  686. aname.CultureInfo = new CultureInfo ("en-US");
  687. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
  688. // PART C
  689. aname = new AssemblyName ();
  690. aname.Name = "culturec";
  691. aname.CultureInfo = new CultureInfo ("nl-BE");
  692. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
  693. aname = new AssemblyName ();
  694. aname.Name = "culturec";
  695. aname.CultureInfo = new CultureInfo ("nl-BE");
  696. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  697. aname = new AssemblyName ();
  698. aname.Name = "culturec";
  699. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
  700. aname = new AssemblyName ();
  701. aname.Name = "culturec";
  702. aname.CultureInfo = CultureInfo.InvariantCulture;
  703. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
  704. // PART D
  705. aname = new AssemblyName ();
  706. aname.Name = "cultured";
  707. aname.CultureInfo = new CultureInfo ("nl-BE");
  708. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
  709. aname = new AssemblyName ();
  710. aname.Name = "cultured";
  711. aname.CultureInfo = new CultureInfo ("nl-BE");
  712. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
  713. aname = new AssemblyName ();
  714. aname.Name = "cultured";
  715. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
  716. aname = new AssemblyName ();
  717. aname.Name = "cultured";
  718. aname.CultureInfo = CultureInfo.InvariantCulture;
  719. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
  720. } finally {
  721. AppDomain.Unload (ad);
  722. }
  723. }
  724. [Test] // bug #79712
  725. [Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
  726. public void Load_Culture_Mismatch ()
  727. {
  728. string tempDir = TempFolder;
  729. string cultureTempDir = Path.Combine (tempDir, "en-US");
  730. if (!Directory.Exists (cultureTempDir))
  731. Directory.CreateDirectory (cultureTempDir);
  732. AppDomain ad = CreateTestDomain (tempDir, true);
  733. try {
  734. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  735. // PART A
  736. AssemblyName aname = new AssemblyName ();
  737. aname.Name = "bug79712a";
  738. aname.CultureInfo = new CultureInfo ("nl-BE");
  739. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
  740. aname = new AssemblyName ();
  741. aname.Name = "bug79712a";
  742. aname.CultureInfo = CultureInfo.InvariantCulture;
  743. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
  744. // PART B
  745. aname = new AssemblyName ();
  746. aname.Name = "bug79712b";
  747. aname.CultureInfo = new CultureInfo ("nl-BE");
  748. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
  749. aname = new AssemblyName ();
  750. aname.Name = "bug79712b";
  751. aname.CultureInfo = new CultureInfo ("en-US");
  752. Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
  753. } finally {
  754. AppDomain.Unload (ad);
  755. }
  756. }
  757. [Test] // bug #79715
  758. [Category("MobileNotWorking")]
  759. public void Load_PartialVersion ()
  760. {
  761. string tempDir = TempFolder;
  762. AppDomain ad = CreateTestDomain (tempDir, true);
  763. try {
  764. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  765. AssemblyName aname = new AssemblyName ();
  766. aname.Name = "bug79715";
  767. aname.Version = new Version (1, 2, 3, 4);
  768. cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
  769. aname = new AssemblyName ();
  770. aname.Name = "bug79715";
  771. aname.Version = new Version (1, 2);
  772. Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
  773. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
  774. aname = new AssemblyName ();
  775. aname.Name = "bug79715";
  776. aname.Version = new Version (1, 2, 3);
  777. Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
  778. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
  779. aname = new AssemblyName ();
  780. aname.Name = "bug79715";
  781. aname.Version = new Version (1, 2, 3, 4);
  782. Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
  783. Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
  784. } finally {
  785. AppDomain.Unload (ad);
  786. }
  787. }
  788. private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
  789. {
  790. AppDomainSetup setup = new AppDomainSetup ();
  791. setup.ApplicationBase = baseDirectory;
  792. setup.ApplicationName = "testdomain";
  793. AppDomain ad = AppDomain.CreateDomain ("testdomain",
  794. AppDomain.CurrentDomain.Evidence, setup);
  795. if (assemblyResolver) {
  796. Assembly ea = Assembly.GetExecutingAssembly ();
  797. ad.CreateInstanceFrom (ea.CodeBase,
  798. typeof (AssemblyResolveHandler).FullName,
  799. false,
  800. BindingFlags.Public | BindingFlags.Instance,
  801. null,
  802. new object [] { ea.Location, ea.FullName },
  803. CultureInfo.InvariantCulture,
  804. null,
  805. null);
  806. }
  807. return ad;
  808. }
  809. private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
  810. {
  811. Type testerType = typeof (CrossDomainTester);
  812. return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
  813. testerType.Assembly.FullName, testerType.FullName, false,
  814. BindingFlags.Public | BindingFlags.Instance, null, new object[0],
  815. CultureInfo.InvariantCulture, new object[0], null);
  816. }
  817. private class CrossDomainTester : MarshalByRefObject
  818. {
  819. public void GenerateAssembly (AssemblyName aname, string path)
  820. {
  821. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  822. aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
  823. ab.Save (Path.GetFileName (path));
  824. }
  825. public void Load (AssemblyName assemblyRef)
  826. {
  827. Assembly.Load (assemblyRef);
  828. }
  829. public void LoadFrom (string assemblyFile)
  830. {
  831. Assembly.LoadFrom (assemblyFile);
  832. }
  833. public bool AssertLoad (AssemblyName assemblyRef)
  834. {
  835. try {
  836. Assembly.Load (assemblyRef);
  837. return true;
  838. } catch {
  839. return false;
  840. }
  841. }
  842. public bool AssertLoad (string assemblyString)
  843. {
  844. try {
  845. Assembly.Load (assemblyString);
  846. return true;
  847. } catch {
  848. return false;
  849. }
  850. }
  851. public bool AssertFileLoadException (AssemblyName assemblyRef)
  852. {
  853. try {
  854. Assembly.Load (assemblyRef);
  855. return false;
  856. } catch (FileLoadException) {
  857. return true;
  858. }
  859. }
  860. public bool AssertFileNotFoundException (AssemblyName assemblyRef)
  861. {
  862. try {
  863. Assembly.Load (assemblyRef);
  864. return false;
  865. } catch (FileNotFoundException) {
  866. return true;
  867. }
  868. }
  869. public void bug78464 (string assemblyFileName)
  870. {
  871. AssemblyName assemblyName = new AssemblyName ();
  872. assemblyName.Name = "bug78464";
  873. AssemblyBuilder ab = AppDomain.CurrentDomain
  874. .DefineDynamicAssembly (assemblyName,
  875. AssemblyBuilderAccess.Save,
  876. Path.GetDirectoryName (assemblyFileName),
  877. AppDomain.CurrentDomain.Evidence);
  878. ab.Save (Path.GetFileName (assemblyFileName));
  879. Assembly assembly;
  880. using (FileStream fs = File.OpenRead (assemblyFileName)) {
  881. byte[] buffer = new byte[fs.Length];
  882. fs.Read (buffer, 0, buffer.Length);
  883. assembly = Assembly.Load (buffer);
  884. fs.Close ();
  885. }
  886. Assert.AreEqual (string.Empty, assembly.Location, "#1");
  887. assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  888. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  889. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
  890. // note: we cannot check if directory names match, as MS.NET seems to
  891. // convert directory part of assembly location to lowercase
  892. Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
  893. }
  894. public void bug78465 (string assemblyFileName)
  895. {
  896. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  897. Assert.IsFalse (assembly.Location == string.Empty, "#2");
  898. Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
  899. // note: we cannot check if directory names match, as MS.NET seems to
  900. // convert directory part of assembly location to lowercase
  901. Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
  902. }
  903. public void bug78468 (string assemblyFileName)
  904. {
  905. AssemblyName assemblyName = new AssemblyName ();
  906. assemblyName.Name = "bug78468b";
  907. AssemblyBuilder ab = AppDomain.CurrentDomain
  908. .DefineDynamicAssembly (assemblyName,
  909. AssemblyBuilderAccess.Save,
  910. Path.GetDirectoryName (assemblyFileName),
  911. AppDomain.CurrentDomain.Evidence);
  912. ab.AddResourceFile ("read", "readme.txt");
  913. ab.Save (Path.GetFileName (assemblyFileName));
  914. Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
  915. Assert.IsTrue (assembly.Location != string.Empty, "#B1");
  916. string[] resNames = assembly.GetManifestResourceNames ();
  917. Assert.IsNotNull (resNames, "#B2");
  918. Assert.AreEqual (1, resNames.Length, "#B3");
  919. Assert.AreEqual ("read", resNames[0], "#B4");
  920. ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
  921. Assert.IsNotNull (resInfo, "#B5");
  922. Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
  923. Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
  924. Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
  925. Stream s = assembly.GetManifestResourceStream ("read");
  926. Assert.IsNotNull (s, "#B9");
  927. s.Close ();
  928. s = assembly.GetFile ("readme.txt");
  929. Assert.IsNotNull (s, "#B10");
  930. s.Close ();
  931. }
  932. }
  933. [Test]
  934. public void bug79872 ()
  935. {
  936. string outdir = TempFolder;
  937. AssemblyName an = new AssemblyName ();
  938. an.Name = "bug79872";
  939. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
  940. string dllname = "bug79872.dll";
  941. ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
  942. string netmodule = "bug79872.netmodule";
  943. ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
  944. TypeBuilder a1 = mb2.DefineType ("A");
  945. a1.CreateType ();
  946. ab.Save (dllname);
  947. bool ok = true;
  948. try {
  949. Assembly.LoadFrom (Path.Combine (outdir, dllname));
  950. } catch {
  951. ok = false;
  952. }
  953. Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
  954. ok = false;
  955. try {
  956. Assembly.LoadFrom (Path.Combine (outdir, netmodule));
  957. } catch (BadImageFormatException) {
  958. ok = true; // mono and .net 2.0 throw this
  959. } catch (FileLoadException) {
  960. ok = true; // .net 1.1 throws this
  961. } catch {
  962. // swallow the rest
  963. }
  964. Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
  965. }
  966. #endif
  967. [Test]
  968. public void ManifestModule ()
  969. {
  970. Assembly assembly = typeof (int).Assembly;
  971. Module module = assembly.ManifestModule;
  972. Assert.IsNotNull (module, "#1");
  973. Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
  974. #if !MONOTOUCH && !FULL_AOT_RUNTIME
  975. Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
  976. #endif
  977. Assert.IsFalse (module.IsResource (), "#4");
  978. Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
  979. Assert.AreSame (module, assembly.GetModules () [0], "#6");
  980. Assert.AreSame (module, assembly.ManifestModule, "#7");
  981. }
  982. [Serializable ()]
  983. private class AssemblyResolveHandler
  984. {
  985. public AssemblyResolveHandler (string assemblyFile, string assemblyName)
  986. {
  987. _assemblyFile = assemblyFile;
  988. _assemblyName = assemblyName;
  989. AppDomain.CurrentDomain.AssemblyResolve +=
  990. new ResolveEventHandler (ResolveAssembly);
  991. }
  992. private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
  993. {
  994. if (args.Name == _assemblyName)
  995. return Assembly.LoadFrom (_assemblyFile);
  996. return null;
  997. }
  998. private readonly string _assemblyFile;
  999. private readonly string _assemblyName;
  1000. }
  1001. protected internal class Bug328812_NestedFamORAssem { };
  1002. [Test]
  1003. public void bug328812 ()
  1004. {
  1005. Assembly corlib_test = Assembly.GetExecutingAssembly ();
  1006. Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
  1007. // Just a sanity check, in case the above passed for some other reason
  1008. Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
  1009. }
  1010. [Test]
  1011. public void GetCustomAttributes_AttributeType_Null ()
  1012. {
  1013. Assembly a = typeof (int).Assembly;
  1014. try {
  1015. a.GetCustomAttributes (null, false);
  1016. Assert.Fail ("#1");
  1017. } catch (ArgumentNullException ex) {
  1018. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  1019. Assert.IsNull (ex.InnerException, "#3");
  1020. Assert.IsNotNull (ex.Message, "#4");
  1021. Assert.IsNotNull (ex.ParamName, "#5");
  1022. Assert.AreEqual ("attributeType", ex.ParamName, "#6");
  1023. }
  1024. }
  1025. [Test]
  1026. public void GetTypeWithEmptyStringShouldThrow ()
  1027. {
  1028. try {
  1029. typeof (string).Assembly.GetType ("");
  1030. Assert.Fail ("#1");
  1031. } catch (ArgumentException) {}
  1032. }
  1033. class GetCallingAssemblyCallee {
  1034. static int _dummy;
  1035. static void sideEffect () {
  1036. _dummy++;
  1037. }
  1038. // GetCallingAssembly may see an unpredictable
  1039. // view of the stack if it's called in tail
  1040. // position, or if its caller or the caller's
  1041. // caller is inlined. So we put in a side
  1042. // effect to get out of tail position, and we
  1043. // tag the methods NoInlining to discourage
  1044. // the inliner.
  1045. [MethodImplAttribute (MethodImplOptions.NoInlining)]
  1046. public static Assembly Leaf () {
  1047. var a = Assembly.GetCallingAssembly ();
  1048. sideEffect();
  1049. return a;
  1050. }
  1051. [MethodImplAttribute (MethodImplOptions.NoInlining)]
  1052. public static Assembly DirectCall () {
  1053. var a = Leaf();
  1054. sideEffect();
  1055. return a;
  1056. }
  1057. [MethodImplAttribute (MethodImplOptions.NoInlining)]
  1058. public static Assembly InvokeCall () {
  1059. var ty = typeof (GetCallingAssemblyCallee);
  1060. var mi = ty.GetMethod("Leaf");
  1061. var o = mi.Invoke(null, null);
  1062. sideEffect();
  1063. return (Assembly)o;
  1064. }
  1065. }
  1066. [Test]
  1067. public void GetCallingAssembly_Direct() {
  1068. var a = GetCallingAssemblyCallee.DirectCall ();
  1069. Assert.IsNotNull (a);
  1070. Assert.AreEqual (GetType().Assembly, a);
  1071. }
  1072. [Test]
  1073. public void GetCallingAssembly_SkipsReflection () {
  1074. // check that the calling assembly is this
  1075. // one, not mscorlib (aka, the reflection
  1076. // API).
  1077. var a = GetCallingAssemblyCallee.InvokeCall ();
  1078. Assert.IsNotNull (a);
  1079. var invokeAssembly =
  1080. typeof (MethodInfo).Assembly;
  1081. Assert.AreNotEqual (invokeAssembly, a);
  1082. Assert.AreEqual (GetType().Assembly, a);
  1083. }
  1084. [Test]
  1085. public void DefinedTypes_Equality ()
  1086. {
  1087. var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
  1088. var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
  1089. Assert.AreSame (x1, x2, "#1");
  1090. }
  1091. class MyAssembly : Assembly { }
  1092. [Test]
  1093. public void CustomAssemblyImplThrows ()
  1094. {
  1095. var ma = new MyAssembly();
  1096. try {
  1097. ma.GetName ();
  1098. Assert.Fail ("must throw");
  1099. } catch (NotImplementedException){
  1100. }
  1101. }
  1102. [Test]
  1103. [ExpectedException (typeof (ArgumentException))]
  1104. public void LoadFileRelativeThrows ()
  1105. {
  1106. Assembly.LoadFile (Path.Combine ("non-existent", "relative", "path.dll"));
  1107. }
  1108. [Test]
  1109. [ExpectedException (typeof (FileNotFoundException))]
  1110. public void LoadFileAbsoluteNotFoundThrows ()
  1111. {
  1112. // have to use GetFullPath so we get C:\... on Windows
  1113. var abspath = Path.GetFullPath (Path.Combine ("non-existent", "absolute", "path.dll"));
  1114. Assembly.LoadFile (abspath);
  1115. }
  1116. }
  1117. public class TestDefinedTypes
  1118. {
  1119. }
  1120. }