AssemblyBuilderTest.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. //
  2. // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. //
  9. using System;
  10. using System.Globalization;
  11. using System.Threading;
  12. using System.Reflection;
  13. using System.Reflection.Emit;
  14. using System.IO;
  15. using System.Configuration.Assemblies;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Reflection.Emit
  18. {
  19. [TestFixture]
  20. public class AssemblyBuilderTest
  21. {
  22. [AttributeUsage (AttributeTargets.Assembly)]
  23. public sealed class FooAttribute : Attribute
  24. {
  25. public FooAttribute (string arg)
  26. {
  27. }
  28. public FooAttribute ()
  29. {
  30. }
  31. }
  32. static int nameIndex = 0;
  33. static AppDomain domain;
  34. static AssemblyBuilder ab;
  35. static ModuleBuilder mb;
  36. string tempDir = Path.Combine (Path.GetTempPath (), typeof (AssemblyBuilderTest).FullName);
  37. [SetUp]
  38. protected void SetUp ()
  39. {
  40. if (Directory.Exists (tempDir))
  41. Directory.Delete (tempDir, true);
  42. Directory.CreateDirectory (tempDir);
  43. for (int i = 1; i < 3; ++i) {
  44. string resFile = Path.Combine (tempDir, "res" + i + ".txt");
  45. using (StreamWriter sw = new StreamWriter (resFile)) {
  46. sw.WriteLine ("FOO");
  47. }
  48. }
  49. domain = Thread.GetDomain ();
  50. ab = genAssembly ();
  51. mb = ab.DefineDynamicModule ("def_module");
  52. }
  53. [TearDown]
  54. protected void TearDown ()
  55. {
  56. if (Directory.Exists (tempDir))
  57. Directory.Delete (tempDir, true);
  58. }
  59. private AssemblyName genAssemblyName ()
  60. {
  61. AssemblyName assemblyName = new AssemblyName();
  62. assemblyName.Name = typeof (AssemblyBuilderTest).FullName + (nameIndex ++);
  63. return assemblyName;
  64. }
  65. private AssemblyBuilder genAssembly ()
  66. {
  67. return domain.DefineDynamicAssembly (genAssemblyName (),
  68. AssemblyBuilderAccess.RunAndSave,
  69. tempDir);
  70. }
  71. private MethodInfo genEntryFunction (AssemblyBuilder assembly)
  72. {
  73. ModuleBuilder module = assembly.DefineDynamicModule("module1");
  74. TypeBuilder tb = module.DefineType ("A");
  75. MethodBuilder mb = tb.DefineMethod ("A",
  76. MethodAttributes.Static, typeof (void), new Type [0]);
  77. mb.GetILGenerator ().Emit (OpCodes.Ret);
  78. return mb;
  79. }
  80. #if NET_2_0
  81. [Test]
  82. [Category ("NotWorking")]
  83. public void ManifestModule ()
  84. {
  85. AssemblyName aname = new AssemblyName ("ManifestModule1");
  86. ab = domain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave,
  87. tempDir);
  88. Assert.IsNotNull (ab.ManifestModule, "#A1");
  89. Assert.AreEqual (1, ab.GetModules ().Length, "#A2");
  90. Assert.AreEqual (typeof (ModuleBuilder), ab.ManifestModule.GetType (), "#A3");
  91. ModuleBuilder mb1 = (ModuleBuilder) ab.ManifestModule;
  92. Assert.AreSame (mb1, ab.GetModules () [0], "#B1");
  93. Assert.IsFalse (mb1.IsResource (), "#B2");
  94. Assert.AreSame (mb1, ab.ManifestModule, "#B3");
  95. ab.Save ("ManifestModule.dll");
  96. ModuleBuilder mb2 = (ModuleBuilder) ab.ManifestModule;
  97. Assert.AreSame (mb2, ab.GetModules () [0], "#C1");
  98. Assert.IsFalse (mb2.IsResource (), "#C2");
  99. Assert.AreSame (mb2, ab.ManifestModule, "#C3");
  100. Assert.AreSame (mb1, mb2, "#C4");
  101. }
  102. #endif
  103. [Test]
  104. [ExpectedException (typeof (NotSupportedException))]
  105. public void TestCodeBase ()
  106. {
  107. string codebase = ab.CodeBase;
  108. }
  109. [Test]
  110. [ExpectedException (typeof (NotSupportedException))]
  111. public void TestLocation ()
  112. {
  113. string location = ab.Location;
  114. }
  115. [Test]
  116. public void TestEntryPoint ()
  117. {
  118. Assert.AreEqual (null, ab.EntryPoint, "EntryPoint defaults to null");
  119. MethodInfo mi = genEntryFunction (ab);
  120. ab.SetEntryPoint (mi);
  121. Assert.AreEqual (mi, ab.EntryPoint, "EntryPoint works");
  122. }
  123. [Test]
  124. public void TestSetEntryPoint ()
  125. {
  126. // Check invalid arguments
  127. try {
  128. ab.SetEntryPoint (null);
  129. Assert.Fail ("#A1");
  130. } catch (ArgumentNullException ex) {
  131. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  132. Assert.IsNull (ex.InnerException, "#A3");
  133. Assert.IsNotNull (ex.Message, "#A4");
  134. Assert.IsNotNull (ex.ParamName, "#A5");
  135. Assert.AreEqual ("entryMethod", ex.ParamName, "#A6");
  136. }
  137. // Check method from other assembly
  138. try {
  139. ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
  140. Assert.Fail ("#B");
  141. } catch (InvalidOperationException) {
  142. }
  143. }
  144. [Test]
  145. public void TestIsDefined ()
  146. {
  147. CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
  148. ab.SetCustomAttribute (cab);
  149. Assert.IsTrue (ab.IsDefined (typeof (FooAttribute), false),
  150. "IsDefined(FooAttribute) works");
  151. Assert.IsFalse (ab.IsDefined (typeof (AssemblyVersionAttribute), false),
  152. "!IsDefined(AssemblyVersionAttribute) works");
  153. }
  154. [Test]
  155. [ExpectedException (typeof (NotSupportedException))]
  156. public void TestGetManifestResourceNames ()
  157. {
  158. ab.GetManifestResourceNames ();
  159. }
  160. [Test]
  161. [ExpectedException (typeof (NotSupportedException))]
  162. public void TestGetManifestResourceInfo ()
  163. {
  164. ab.GetManifestResourceInfo ("foo");
  165. }
  166. [Test]
  167. [ExpectedException (typeof (NotSupportedException))]
  168. public void TestGetManifestResourceStream1 ()
  169. {
  170. ab.GetManifestResourceStream ("foo");
  171. }
  172. [Test]
  173. [ExpectedException (typeof (NotSupportedException))]
  174. public void TestGetManifestResourceStream2 ()
  175. {
  176. ab.GetManifestResourceStream (typeof (int), "foo");
  177. }
  178. [Test]
  179. [ExpectedException (typeof (NotSupportedException))]
  180. public void TestGetFiles1 ()
  181. {
  182. ab.GetFiles ();
  183. }
  184. [Test]
  185. [ExpectedException (typeof (NotSupportedException))]
  186. public void TestGetFiles2 ()
  187. {
  188. ab.GetFiles (true);
  189. }
  190. [Test]
  191. [ExpectedException (typeof (NotSupportedException))]
  192. public void TestGetFile ()
  193. {
  194. ab.GetFile ("foo");
  195. }
  196. [Test]
  197. [ExpectedException (typeof (NotSupportedException))]
  198. public void TestGetExportedTypes ()
  199. {
  200. ab.GetExportedTypes ();
  201. }
  202. [Test]
  203. public void TestGetDynamicModule_Name_Null ()
  204. {
  205. try {
  206. ab.GetDynamicModule (null);
  207. Assert.Fail ("#1");
  208. } catch (ArgumentNullException ex) {
  209. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  210. Assert.IsNull (ex.InnerException, "#3");
  211. Assert.IsNotNull (ex.Message, "#4");
  212. Assert.IsNotNull (ex.ParamName, "#5");
  213. Assert.AreEqual ("name", ex.ParamName, "#6");
  214. }
  215. }
  216. [Test]
  217. public void TestGetDynamicModule_Name_Empty ()
  218. {
  219. try {
  220. ab.GetDynamicModule (string.Empty);
  221. Assert.Fail ("#1");
  222. } catch (ArgumentException ex) {
  223. // Empty name is not legal
  224. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  225. Assert.IsNull (ex.InnerException, "#3");
  226. Assert.IsNotNull (ex.Message, "#4");
  227. Assert.IsNotNull (ex.ParamName, "#5");
  228. Assert.AreEqual ("name", ex.ParamName, "#6");
  229. }
  230. }
  231. [Test]
  232. public void TestGetDynamicModule3 ()
  233. {
  234. Assert.IsNull (ab.GetDynamicModule ("FOO2"));
  235. ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
  236. Assert.AreEqual (mb, ab.GetDynamicModule ("FOO"));
  237. Assert.IsNull (ab.GetDynamicModule ("FOO4"));
  238. }
  239. #if NET_1_1
  240. [Test]
  241. public void TestImageRuntimeVersion ()
  242. {
  243. string version = ab.ImageRuntimeVersion;
  244. Assert.IsTrue (version.Length > 0);
  245. }
  246. #endif
  247. [Test]
  248. public void TestAddResourceFile_Name_Null ()
  249. {
  250. try {
  251. ab.AddResourceFile (null, "foo.txt");
  252. Assert.Fail ("#1");
  253. } catch (ArgumentNullException ex) {
  254. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  255. Assert.IsNull (ex.InnerException, "#3");
  256. Assert.IsNotNull (ex.Message, "#4");
  257. Assert.IsNotNull (ex.ParamName, "#5");
  258. Assert.AreEqual ("name", ex.ParamName, "#6");
  259. }
  260. }
  261. [Test]
  262. public void TestAddResourceFile_Filename_Null ()
  263. {
  264. try {
  265. ab.AddResourceFile ("foo", null);
  266. Assert.Fail ("#1");
  267. } catch (ArgumentNullException ex) {
  268. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  269. Assert.IsNull (ex.InnerException, "#3");
  270. Assert.IsNotNull (ex.Message, "#4");
  271. Assert.IsNotNull (ex.ParamName, "#5");
  272. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  273. }
  274. }
  275. [Test]
  276. public void TestAddResourceFile_Name_Empty ()
  277. {
  278. try {
  279. ab.AddResourceFile (string.Empty, "foo.txt");
  280. Assert.Fail ("#1");
  281. } catch (ArgumentException ex) {
  282. // Empty name is not legal
  283. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  284. Assert.IsNull (ex.InnerException, "#3");
  285. Assert.IsNotNull (ex.Message, "#4");
  286. }
  287. }
  288. [Test]
  289. public void TestAddResourceFile_Filename_Empty ()
  290. {
  291. try {
  292. ab.AddResourceFile ("foo", string.Empty);
  293. Assert.Fail ("#1");
  294. } catch (ArgumentException ex) {
  295. // Empty file name is not legal
  296. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  297. Assert.IsNull (ex.InnerException, "#3");
  298. Assert.IsNotNull (ex.Message, "#4");
  299. }
  300. }
  301. [Test]
  302. [ExpectedException (typeof (FileNotFoundException))]
  303. public void TestAddResourceFile_FileName_DoesNotExist ()
  304. {
  305. ab.AddResourceFile ("foo", "not-existent.txt");
  306. }
  307. [Test]
  308. public void TestAddResourceFile_FileName_Duplicate ()
  309. {
  310. ab.AddResourceFile ("foo", "res1.txt");
  311. try {
  312. ab.AddResourceFile ("foo2", "res1.txt");
  313. Assert.Fail ("#1");
  314. } catch (ArgumentException ex) {
  315. // Duplicate file names
  316. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  317. Assert.IsNull (ex.InnerException, "#3");
  318. Assert.IsNotNull (ex.Message, "#4");
  319. Assert.IsNull (ex.ParamName, "#5");
  320. }
  321. }
  322. [Test]
  323. public void TestAddResourceFile_Name_Duplicate ()
  324. {
  325. ab.AddResourceFile ("foo", "res1.txt");
  326. try {
  327. ab.AddResourceFile ("foo", "res2.txt");
  328. Assert.Fail ("#1");
  329. } catch (ArgumentException ex) {
  330. // Duplicate resource name within an assembly
  331. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  332. Assert.IsNull (ex.InnerException, "#3");
  333. Assert.IsNotNull (ex.Message, "#4");
  334. Assert.IsNull (ex.ParamName, "#5");
  335. }
  336. }
  337. [Test]
  338. public void TestAddResourceFile_Filename_IncludesPath ()
  339. {
  340. try {
  341. ab.AddResourceFile ("foo", "/tmp/res1.txt");
  342. Assert.Fail ("#1");
  343. } catch (ArgumentException ex) {
  344. // The filename must not include a path specification
  345. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  346. Assert.IsNull (ex.InnerException, "#3");
  347. Assert.IsNotNull (ex.Message, "#4");
  348. Assert.IsNotNull (ex.ParamName, "#5");
  349. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  350. }
  351. }
  352. [Test]
  353. public void TestAddResourceFile ()
  354. {
  355. ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);
  356. ab.Save ("TestAddResourceFile.dll");
  357. // TODO: Test reading back
  358. }
  359. [Test]
  360. public void TestDefineResource ()
  361. {
  362. ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);
  363. ab.DefineResource ("foo2", "FOO", "foo2.txt");
  364. ab.Save ("TestDefineResource.dll");
  365. }
  366. [Test]
  367. public void TestDefineDynamicModule_Name_Null ()
  368. {
  369. try {
  370. ab.DefineDynamicModule (null, "foo.txt");
  371. Assert.Fail ("#1");
  372. } catch (ArgumentNullException ex) {
  373. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  374. Assert.IsNull (ex.InnerException, "#3");
  375. Assert.IsNotNull (ex.Message, "#4");
  376. Assert.IsNotNull (ex.ParamName, "#5");
  377. Assert.AreEqual ("name", ex.ParamName, "#6");
  378. }
  379. }
  380. [Test]
  381. public void TestDefineDynamicModule_FileName_Null ()
  382. {
  383. try {
  384. ab.DefineDynamicModule ("foo", null);
  385. Assert.Fail ("#1");
  386. } catch (ArgumentNullException ex) {
  387. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  388. Assert.IsNull (ex.InnerException, "#3");
  389. Assert.IsNotNull (ex.Message, "#4");
  390. Assert.IsNotNull (ex.ParamName, "#5");
  391. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  392. }
  393. }
  394. [Test]
  395. public void TestDefineDynamicModule_Name_Empty ()
  396. {
  397. try {
  398. ab.DefineDynamicModule (string.Empty, "foo.txt");
  399. Assert.Fail ("#1");
  400. } catch (ArgumentException ex) {
  401. // Empty name is not legal
  402. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  403. Assert.IsNull (ex.InnerException, "#3");
  404. Assert.IsNotNull (ex.Message, "#4");
  405. Assert.IsNotNull (ex.ParamName, "#5");
  406. Assert.AreEqual ("name", ex.ParamName, "#6");
  407. }
  408. }
  409. [Test]
  410. public void TestDefineDynamicModule_Filename_Empty ()
  411. {
  412. try {
  413. ab.DefineDynamicModule ("foo", string.Empty);
  414. Assert.Fail ("#1");
  415. } catch (ArgumentException ex) {
  416. // Empty file name is not legal
  417. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  418. Assert.IsNull (ex.InnerException, "#3");
  419. Assert.IsNotNull (ex.Message, "#4");
  420. Assert.IsNotNull (ex.ParamName, "#5");
  421. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  422. }
  423. }
  424. [Test]
  425. public void TestDefineDynamicModule_FileName_Duplicate ()
  426. {
  427. ab.DefineDynamicModule ("foo", "res1.txt");
  428. try {
  429. ab.DefineDynamicModule ("foo2", "res1.txt");
  430. Assert.Fail ("#1");
  431. } catch (ArgumentException ex) {
  432. // Duplicate file names
  433. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  434. Assert.IsNull (ex.InnerException, "#3");
  435. Assert.IsNotNull (ex.Message, "#4");
  436. Assert.IsNull (ex.ParamName, "#5");
  437. }
  438. }
  439. [Test]
  440. public void TestDefineDynamicModule_Name_Duplicate ()
  441. {
  442. ab.DefineDynamicModule ("foo", "res1.txt");
  443. try {
  444. ab.DefineDynamicModule ("foo", "res2.txt");
  445. Assert.Fail ("#1");
  446. } catch (ArgumentException ex) {
  447. // Duplicate dynamic module name within an assembly
  448. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  449. Assert.IsNull (ex.InnerException, "#3");
  450. Assert.IsNotNull (ex.Message, "#4");
  451. Assert.IsNull (ex.ParamName, "#5");
  452. }
  453. }
  454. [Test]
  455. public void TestDefineDynamicModule_Filename_IncludesPath ()
  456. {
  457. try {
  458. ab.DefineDynamicModule ("foo", "/tmp/res1.txt");
  459. Assert.Fail ("#1");
  460. } catch (ArgumentException ex) {
  461. // The filename must not include a path specification
  462. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  463. Assert.IsNull (ex.InnerException, "#3");
  464. Assert.IsNotNull (ex.Message, "#4");
  465. Assert.IsNotNull (ex.ParamName, "#5");
  466. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  467. }
  468. }
  469. [Test]
  470. public void TestDefineDynamicModule5_FileName_NoExtension ()
  471. {
  472. try {
  473. ab.DefineDynamicModule ("foo", "bar");
  474. Assert.Fail ("#1");
  475. } catch (ArgumentException ex) {
  476. // Module file name 'bar' must have file extension
  477. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  478. Assert.IsNull (ex.InnerException, "#3");
  479. Assert.IsNotNull (ex.Message, "#4");
  480. Assert.IsTrue (ex.Message.IndexOf ("bar") != -1, "#5");
  481. Assert.IsNull (ex.ParamName, "#6");
  482. }
  483. }
  484. [Test]
  485. [Category ("NotWorking")]
  486. public void TestDefineDynamicModule_Name_MaxLength () {
  487. string name = string.Empty;
  488. for (int i = 0; i < 259; ++i)
  489. name = name + "A";
  490. ab.DefineDynamicModule (name);
  491. name = name + "A";
  492. try {
  493. ab.DefineDynamicModule (name);
  494. Assert.Fail ("#1");
  495. } catch (ArgumentException ex) {
  496. // Value does not fall within expected range
  497. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  498. Assert.IsNull (ex.InnerException, "#3");
  499. Assert.IsNotNull (ex.Message, "#4");
  500. Assert.IsNull (ex.ParamName, "#5");
  501. }
  502. }
  503. [Test]
  504. [ExpectedException (typeof (InvalidOperationException))]
  505. public void TestDefineDynamicModule_Assembly_Saved ()
  506. {
  507. // Called when assembly was already saved
  508. ab.Save ("TestDefineDynamicModule7.dll");
  509. ab.DefineDynamicModule ("foo", "foo.dll");
  510. }
  511. [Test]
  512. [ExpectedException (typeof (NotSupportedException))]
  513. public void TestDefineDynamicModule_Access_Run ()
  514. {
  515. // Called on an assembly defined with the Run attribute
  516. AssemblyBuilder ab =
  517. domain.DefineDynamicAssembly (genAssemblyName (),
  518. AssemblyBuilderAccess.Run,
  519. tempDir);
  520. ab.DefineDynamicModule ("foo", "foo.dll");
  521. }
  522. [Test]
  523. public void TestDefineDynamicModule ()
  524. {
  525. ab.DefineDynamicModule ("foo", "foo.dll");
  526. ab.DefineDynamicModule ("foo2", true);
  527. ab.DefineDynamicModule ("foo3", "foo3.dll");
  528. ab.DefineDynamicModule ("foo4", "foo4.dll", true);
  529. }
  530. [Test] // DefineUnmanagedResource (byte [])
  531. [Category ("NotWorking")]
  532. public void TestDefineUnmanagedResource1_ResourceAlreadyDefined ()
  533. {
  534. string version_res = Path.Combine (tempDir, "version.res");
  535. using (FileStream fs = File.OpenWrite (version_res)) {
  536. fs.WriteByte (0x0a);
  537. }
  538. ab.DefineUnmanagedResource (new byte [0]);
  539. try {
  540. ab.DefineUnmanagedResource (new byte [0]);
  541. Assert.Fail ("#A1");
  542. } catch (ArgumentException ex) {
  543. // Native resource has already been defined
  544. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  545. Assert.IsNull (ex.InnerException, "#A3");
  546. Assert.IsNotNull (ex.Message, "#A4");
  547. Assert.IsNull (ex.ParamName, "#A5");
  548. }
  549. try {
  550. ab.DefineUnmanagedResource (version_res);
  551. Assert.Fail ("#B1");
  552. } catch (ArgumentException ex) {
  553. // Native resource has already been defined
  554. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  555. Assert.IsNull (ex.InnerException, "#B3");
  556. Assert.IsNotNull (ex.Message, "#B4");
  557. Assert.IsNull (ex.ParamName, "#B5");
  558. }
  559. try {
  560. ab.DefineVersionInfoResource ();
  561. Assert.Fail ("#C1");
  562. } catch (ArgumentException ex) {
  563. // Native resource has already been defined
  564. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  565. Assert.IsNull (ex.InnerException, "#C3");
  566. Assert.IsNotNull (ex.Message, "#C4");
  567. Assert.IsNull (ex.ParamName, "#C5");
  568. }
  569. try {
  570. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  571. Assert.Fail ("#D1");
  572. } catch (ArgumentException ex) {
  573. // Native resource has already been defined
  574. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  575. Assert.IsNull (ex.InnerException, "#D3");
  576. Assert.IsNotNull (ex.Message, "#D4");
  577. Assert.IsNull (ex.ParamName, "#D5");
  578. }
  579. }
  580. [Test] // DefineUnmanagedResource (byte [])
  581. public void TestDefineUnmanagedResource1_Resource_Null ()
  582. {
  583. try {
  584. ab.DefineUnmanagedResource ((byte []) null);
  585. Assert.Fail ("#1");
  586. } catch (ArgumentNullException ex) {
  587. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  588. Assert.IsNull (ex.InnerException, "#3");
  589. Assert.IsNotNull (ex.Message, "#4");
  590. Assert.IsNotNull (ex.ParamName, "#5");
  591. Assert.AreEqual ("resource", ex.ParamName, "#6");
  592. }
  593. }
  594. [Test] // DefineUnmanagedResource (String)
  595. public void TestDefineUnmanagedResource2_ResourceAlreadyDefined ()
  596. {
  597. string version_res = Path.Combine (tempDir, "version.res");
  598. using (FileStream fs = File.OpenWrite (version_res)) {
  599. fs.WriteByte (0x0a);
  600. }
  601. ab.DefineUnmanagedResource (version_res);
  602. try {
  603. ab.DefineUnmanagedResource (new byte [0]);
  604. Assert.Fail ("#A1");
  605. } catch (ArgumentException ex) {
  606. // Native resource has already been defined
  607. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  608. Assert.IsNull (ex.InnerException, "#A3");
  609. Assert.IsNotNull (ex.Message, "#A4");
  610. Assert.IsNull (ex.ParamName, "#A5");
  611. }
  612. try {
  613. ab.DefineUnmanagedResource (version_res);
  614. Assert.Fail ("#B1");
  615. } catch (ArgumentException ex) {
  616. // Native resource has already been defined
  617. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  618. Assert.IsNull (ex.InnerException, "#B3");
  619. Assert.IsNotNull (ex.Message, "#B4");
  620. Assert.IsNull (ex.ParamName, "#B5");
  621. }
  622. try {
  623. ab.DefineVersionInfoResource ();
  624. Assert.Fail ("#C1");
  625. } catch (ArgumentException ex) {
  626. // Native resource has already been defined
  627. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  628. Assert.IsNull (ex.InnerException, "#C3");
  629. Assert.IsNotNull (ex.Message, "#C4");
  630. Assert.IsNull (ex.ParamName, "#C5");
  631. }
  632. try {
  633. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  634. Assert.Fail ("#D1");
  635. } catch (ArgumentException ex) {
  636. // Native resource has already been defined
  637. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  638. Assert.IsNull (ex.InnerException, "#D3");
  639. Assert.IsNotNull (ex.Message, "#D4");
  640. Assert.IsNull (ex.ParamName, "#D5");
  641. }
  642. }
  643. [Test] // DefinedUnmanagedResource (String)
  644. [ExpectedException (typeof (FileNotFoundException))]
  645. public void TestDefineUnmanagedResource2_ResourceFile_DoesNotExist ()
  646. {
  647. ab.DefineUnmanagedResource ("not-exists.txt");
  648. }
  649. [Test] // DefinedUnmanagedResource (String)
  650. public void TestDefineUnmanagedResource2_ResourceFileName_Empty ()
  651. {
  652. try {
  653. ab.DefineUnmanagedResource (string.Empty);
  654. Assert.Fail ("#1");
  655. } catch (ArgumentException ex) {
  656. // The path is not of a legal form
  657. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  658. Assert.IsNull (ex.InnerException, "#3");
  659. Assert.IsNotNull (ex.Message, "#4");
  660. Assert.IsNull (ex.ParamName, "#5");
  661. }
  662. }
  663. [Test] // DefinedUnmanagedResource (String)
  664. public void TestDefineUnmanagedResource2_ResourceFileName_Null ()
  665. {
  666. try {
  667. ab.DefineUnmanagedResource ((string) null);
  668. Assert.Fail ("#1");
  669. } catch (ArgumentNullException ex) {
  670. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  671. Assert.IsNull (ex.InnerException, "#3");
  672. Assert.IsNotNull (ex.Message, "#4");
  673. Assert.IsNotNull (ex.ParamName, "#5");
  674. Assert.AreEqual ("resourceFileName", ex.ParamName, "#6");
  675. }
  676. }
  677. [Test] // DefineVersionInfoResource ()
  678. public void TestDefineVersionInfoResource1_Culture_NotSupported ()
  679. {
  680. AssemblyName aname = new AssemblyName ();
  681. aname.CultureInfo = new CultureInfo ("nl-BE");
  682. aname.Name = "lib";
  683. aname.Version = new Version (3, 5, 7);
  684. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  685. aname, AssemblyBuilderAccess.RunAndSave,
  686. tempDir);
  687. // AssemblyCulture
  688. Type attrType = typeof (AssemblyCultureAttribute);
  689. ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
  690. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  691. ci, new object [1] { "doesnotexist" });
  692. ab.SetCustomAttribute (cab);
  693. ab.DefineVersionInfoResource ();
  694. try {
  695. ab.Save ("lib.dll");
  696. Assert.Fail ("#A1");
  697. } catch (ArgumentException ex) {
  698. // Culture name doesnotexist is not supported
  699. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  700. Assert.IsNull (ex.InnerException, "#A3");
  701. Assert.IsNotNull (ex.Message, "#A4");
  702. Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#A5");
  703. Assert.AreEqual ("name", ex.ParamName, "#A6");
  704. }
  705. ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
  706. AssemblyBuilderAccess.RunAndSave, tempDir);
  707. // AssemblyCulture
  708. attrType = typeof (AssemblyCultureAttribute);
  709. ci = attrType.GetConstructor (new Type [] { typeof (String) });
  710. cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
  711. ab.SetCustomAttribute (cab);
  712. ab.DefineVersionInfoResource ();
  713. try {
  714. ab.Save ("lib.dll");
  715. Assert.Fail ("#B1");
  716. } catch (ArgumentException ex) {
  717. // Culture name neutral is not supported
  718. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  719. Assert.IsNull (ex.InnerException, "#B3");
  720. Assert.IsNotNull (ex.Message, "#B4");
  721. Assert.IsTrue (ex.Message.IndexOf ("neutral") != -1, "#B5");
  722. Assert.AreEqual ("name", ex.ParamName, "#B6");
  723. }
  724. }
  725. [Test] // DefineVersionInfoResource ()
  726. public void TestDefineVersionInfoResource1_ResourceAlreadyDefined ()
  727. {
  728. string version_res = Path.Combine (tempDir, "version.res");
  729. using (FileStream fs = File.OpenWrite (version_res)) {
  730. fs.WriteByte (0x0a);
  731. }
  732. ab.DefineVersionInfoResource ();
  733. try {
  734. ab.DefineUnmanagedResource (new byte [0]);
  735. Assert.Fail ("#A1");
  736. } catch (ArgumentException ex) {
  737. // Native resource has already been defined
  738. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  739. Assert.IsNull (ex.InnerException, "#A3");
  740. Assert.IsNotNull (ex.Message, "#A4");
  741. Assert.IsNull (ex.ParamName, "#A5");
  742. }
  743. try {
  744. ab.DefineUnmanagedResource (version_res);
  745. Assert.Fail ("#B1");
  746. } catch (ArgumentException ex) {
  747. // Native resource has already been defined
  748. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  749. Assert.IsNull (ex.InnerException, "#B3");
  750. Assert.IsNotNull (ex.Message, "#B4");
  751. Assert.IsNull (ex.ParamName, "#B5");
  752. }
  753. try {
  754. ab.DefineVersionInfoResource ();
  755. Assert.Fail ("#C1");
  756. } catch (ArgumentException ex) {
  757. // Native resource has already been defined
  758. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  759. Assert.IsNull (ex.InnerException, "#C3");
  760. Assert.IsNotNull (ex.Message, "#C4");
  761. Assert.IsNull (ex.ParamName, "#C5");
  762. }
  763. try {
  764. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  765. Assert.Fail ("#D1");
  766. } catch (ArgumentException ex) {
  767. // Native resource has already been defined
  768. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  769. Assert.IsNull (ex.InnerException, "#D3");
  770. Assert.IsNotNull (ex.Message, "#D4");
  771. Assert.IsNull (ex.ParamName, "#D5");
  772. }
  773. }
  774. [Test] // DefineVersionInfoResource (String, String, String, String, String)
  775. public void TestDefineVersionInfoResource2_Culture_NotSupported ()
  776. {
  777. AssemblyName aname = new AssemblyName ();
  778. aname.CultureInfo = new CultureInfo ("nl-BE");
  779. aname.Name = "lib";
  780. aname.Version = new Version (3, 5, 7);
  781. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  782. aname, AssemblyBuilderAccess.RunAndSave,
  783. tempDir);
  784. // AssemblyCulture
  785. Type attrType = typeof (AssemblyCultureAttribute);
  786. ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
  787. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  788. ci, new object [1] { "doesnotexist" });
  789. ab.SetCustomAttribute (cab);
  790. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  791. try {
  792. ab.Save ("lib.dll");
  793. Assert.Fail ("#A1");
  794. } catch (ArgumentException ex) {
  795. // Culture name doesnotexist is not supported
  796. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  797. Assert.IsNull (ex.InnerException, "#A3");
  798. Assert.IsNotNull (ex.Message, "#A4");
  799. Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#A5");
  800. Assert.AreEqual ("name", ex.ParamName, "#A6");
  801. }
  802. ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
  803. AssemblyBuilderAccess.RunAndSave, tempDir);
  804. // AssemblyCulture
  805. attrType = typeof (AssemblyCultureAttribute);
  806. ci = attrType.GetConstructor (new Type [] { typeof (String) });
  807. cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
  808. ab.SetCustomAttribute (cab);
  809. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  810. try {
  811. ab.Save ("lib.dll");
  812. Assert.Fail ("#B1");
  813. } catch (ArgumentException ex) {
  814. // Culture name neutral is not supported
  815. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  816. Assert.IsNull (ex.InnerException, "#B3");
  817. Assert.IsNotNull (ex.Message, "#B4");
  818. Assert.IsTrue (ex.Message.IndexOf ("neutral") != -1, "#B5");
  819. Assert.AreEqual ("name", ex.ParamName, "#B6");
  820. }
  821. }
  822. [Test] // DefineVersionInfoResource (String, String, String, String, String)
  823. public void TestDefineVersionInfoResource2_ResourceAlreadyDefined ()
  824. {
  825. string version_res = Path.Combine (tempDir, "version.res");
  826. using (FileStream fs = File.OpenWrite (version_res)) {
  827. fs.WriteByte (0x0a);
  828. }
  829. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  830. try {
  831. ab.DefineUnmanagedResource (new byte [0]);
  832. Assert.Fail ("#A1");
  833. } catch (ArgumentException ex) {
  834. // Native resource has already been defined
  835. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  836. Assert.IsNull (ex.InnerException, "#A3");
  837. Assert.IsNotNull (ex.Message, "#A4");
  838. Assert.IsNull (ex.ParamName, "#A5");
  839. }
  840. try {
  841. ab.DefineUnmanagedResource (version_res);
  842. Assert.Fail ("#B1");
  843. } catch (ArgumentException ex) {
  844. // Native resource has already been defined
  845. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  846. Assert.IsNull (ex.InnerException, "#B3");
  847. Assert.IsNotNull (ex.Message, "#B4");
  848. Assert.IsNull (ex.ParamName, "#B5");
  849. }
  850. try {
  851. ab.DefineVersionInfoResource ();
  852. Assert.Fail ("#C1");
  853. } catch (ArgumentException ex) {
  854. // Native resource has already been defined
  855. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  856. Assert.IsNull (ex.InnerException, "#C3");
  857. Assert.IsNotNull (ex.Message, "#C4");
  858. Assert.IsNull (ex.ParamName, "#C5");
  859. }
  860. try {
  861. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  862. Assert.Fail ("#D1");
  863. } catch (ArgumentException ex) {
  864. // Native resource has already been defined
  865. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  866. Assert.IsNull (ex.InnerException, "#D3");
  867. Assert.IsNotNull (ex.Message, "#D4");
  868. Assert.IsNull (ex.ParamName, "#D5");
  869. }
  870. }
  871. [Test]
  872. public void TestSetCustomAttribute1_CustomBuilder_Null ()
  873. {
  874. try {
  875. ab.SetCustomAttribute (null);
  876. Assert.Fail ("#1");
  877. } catch (ArgumentNullException ex) {
  878. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  879. Assert.IsNull (ex.InnerException, "#3");
  880. Assert.IsNotNull (ex.Message, "#4");
  881. Assert.IsNotNull (ex.ParamName, "#5");
  882. Assert.AreEqual ("customBuilder", ex.ParamName, "#6");
  883. }
  884. }
  885. [Test]
  886. public void TestSetCustomAttribute2_ConstructorInfo_Null ()
  887. {
  888. try {
  889. ab.SetCustomAttribute (null, new byte [0]);
  890. Assert.Fail ("#1");
  891. } catch (ArgumentNullException ex) {
  892. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  893. Assert.IsNull (ex.InnerException, "#3");
  894. Assert.IsNotNull (ex.Message, "#4");
  895. Assert.IsNotNull (ex.ParamName, "#5");
  896. Assert.AreEqual ("con", ex.ParamName, "#6");
  897. }
  898. }
  899. [Test]
  900. public void TestSetCustomAttribute2_BinaryAttribute_Null ()
  901. {
  902. try {
  903. ab.SetCustomAttribute (typeof (AssemblyCompanyAttribute).GetConstructor (
  904. new Type [] { typeof (String) }), null);
  905. Assert.Fail ("#1");
  906. } catch (ArgumentNullException ex) {
  907. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  908. Assert.IsNull (ex.InnerException, "#3");
  909. Assert.IsNotNull (ex.Message, "#4");
  910. Assert.IsNotNull (ex.ParamName, "#5");
  911. Assert.AreEqual ("binaryAttribute", ex.ParamName, "#6");
  912. }
  913. }
  914. [Test] // SetCustomAttribute (CustomAttributeBuilder)
  915. public void TestSetCustomAttribute1 ()
  916. {
  917. Assembly a;
  918. AssemblyName an;
  919. AssemblyName check;
  920. Attribute attr;
  921. string filename;
  922. an = new AssemblyName ();
  923. an.Name = "TestSetCustomAttributeA";
  924. ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
  925. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
  926. GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));
  927. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
  928. GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));
  929. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
  930. GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
  931. new object [] { AssemblyHashAlgorithm.MD5 }));
  932. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
  933. GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0xff }));
  934. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
  935. GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  936. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
  937. GetConstructor (Type.EmptyTypes), new object [0]));
  938. ab.Save ("TestSetCustomAttributeA.dll");
  939. filename = Path.Combine (tempDir, "TestSetCustomAttributeA.dll");
  940. check = AssemblyName.GetAssemblyName (filename);
  941. Assert.AreEqual (CultureInfo.InvariantCulture, check.CultureInfo, "#A1");
  942. #if NET_2_0
  943. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#A2");
  944. #else
  945. Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#A2");
  946. #endif
  947. Assert.AreEqual ("TestSetCustomAttributeA, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", check.FullName, "#A3");
  948. #if NET_2_0
  949. Assert.IsNull (check.GetPublicKey (), "#A4");
  950. #else
  951. Assert.AreEqual (new byte [0], check.GetPublicKey (), "#A4");
  952. #endif
  953. #if NET_2_0
  954. Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#A5");
  955. #else
  956. Assert.IsNull (check.GetPublicKeyToken (), "#A5");
  957. #endif
  958. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#A6");
  959. Assert.IsNull (check.KeyPair, "#A7");
  960. Assert.AreEqual ("TestSetCustomAttributeA", check.Name, "#A8");
  961. #if NET_2_0
  962. //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#A9");
  963. #endif
  964. Assert.AreEqual (new Version (0, 0, 0, 0), check.Version, "#A10");
  965. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#A11");
  966. using (FileStream fs = File.OpenRead (filename)) {
  967. byte [] buffer = new byte [fs.Length];
  968. fs.Read (buffer, 0, buffer.Length);
  969. a = Assembly.Load (buffer);
  970. }
  971. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
  972. Assert.IsNotNull (attr, "#A12a");
  973. Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#A12b");
  974. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
  975. Assert.IsNotNull (attr, "#A13a");
  976. Assert.AreEqual ("bar", ((AssemblyCultureAttribute) attr).Culture, "#A13b");
  977. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
  978. Assert.IsNotNull (attr, "#A14a");
  979. Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#A14b");
  980. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
  981. Assert.IsNotNull (attr, "#A15a");
  982. Assert.AreEqual ((uint) 0xff, ((AssemblyFlagsAttribute) attr).Flags, "#A15b");
  983. attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
  984. Assert.IsNotNull (attr, "#A16");
  985. an = new AssemblyName ();
  986. an.CultureInfo = new CultureInfo ("nl-BE");
  987. an.Flags = AssemblyNameFlags.Retargetable;
  988. an.Name = "TestSetCustomAttributeB";
  989. #if NET_2_0
  990. an.ProcessorArchitecture = ProcessorArchitecture.IA64;
  991. #endif
  992. an.Version = new Version (1, 3, 5, 7);
  993. an.VersionCompatibility = AssemblyVersionCompatibility.SameDomain;
  994. ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
  995. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
  996. GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4" }));
  997. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
  998. GetConstructor (new Type [] { typeof (string) }), new object [] { "en-US" }));
  999. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
  1000. GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
  1001. new object [] { AssemblyHashAlgorithm.MD5 }));
  1002. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
  1003. GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint) 0x0100 }));
  1004. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
  1005. GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  1006. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
  1007. GetConstructor (Type.EmptyTypes), new object [0]));
  1008. ab.Save ("TestSetCustomAttributeB.dll");
  1009. filename = Path.Combine (tempDir, "TestSetCustomAttributeB.dll");
  1010. check = AssemblyName.GetAssemblyName (filename);
  1011. Assert.AreEqual ("nl-BE", check.CultureInfo.Name, "#B1");
  1012. #if NET_2_0
  1013. Assert.AreEqual (AssemblyNameFlags.Retargetable, check.Flags, "#B2");
  1014. #else
  1015. Assert.AreEqual (AssemblyNameFlags.PublicKey | AssemblyNameFlags.Retargetable, check.Flags, "#B2");
  1016. #endif
  1017. Assert.AreEqual ("TestSetCustomAttributeB, Version=1.3.5.7, Culture=nl-BE, PublicKeyToken=null, Retargetable=Yes", check.FullName, "#B3");
  1018. #if NET_2_0
  1019. Assert.IsNull (check.GetPublicKey (), "#B4");
  1020. #else
  1021. Assert.AreEqual (new byte [0], check.GetPublicKey (), "#B4");
  1022. #endif
  1023. #if NET_2_0
  1024. Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#B5");
  1025. #else
  1026. Assert.IsNull (check.GetPublicKeyToken (), "#B5");
  1027. #endif
  1028. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#B6");
  1029. Assert.IsNull (check.KeyPair, "#B7");
  1030. Assert.AreEqual ("TestSetCustomAttributeB", check.Name, "#B8");
  1031. #if NET_2_0
  1032. //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#B9");
  1033. #endif
  1034. Assert.AreEqual (new Version (1, 3, 5, 7), check.Version, "#B10");
  1035. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#B11");
  1036. using (FileStream fs = File.OpenRead (filename)) {
  1037. byte [] buffer = new byte [fs.Length];
  1038. fs.Read (buffer, 0, buffer.Length);
  1039. a = Assembly.Load (buffer);
  1040. }
  1041. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
  1042. Assert.IsNotNull (attr, "#B12a");
  1043. Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#B12b");
  1044. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
  1045. Assert.IsNotNull (attr, "#B13a");
  1046. Assert.AreEqual ("en-US", ((AssemblyCultureAttribute) attr).Culture, "#B13b");
  1047. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
  1048. Assert.IsNotNull (attr, "#B14a");
  1049. Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#B14b");
  1050. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
  1051. Assert.IsNotNull (attr, "#B15a");
  1052. Assert.AreEqual ((uint) 0x0100, ((AssemblyFlagsAttribute) attr).Flags, "#B15b");
  1053. attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
  1054. Assert.IsNotNull (attr, "#B16");
  1055. }
  1056. // strongname generated using "sn -k unit.snk"
  1057. static byte[] strongName = {
  1058. 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
  1059. 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A,
  1060. 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D,
  1061. 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72,
  1062. 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16,
  1063. 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4,
  1064. 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4,
  1065. 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D,
  1066. 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25,
  1067. 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93,
  1068. 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0,
  1069. 0x4D, 0x27, 0xFC, 0xA7, 0x4F, 0x36, 0xCA, 0x13, 0x42, 0xD3, 0x83, 0xC4,
  1070. 0x06, 0x6E, 0x12, 0xE0, 0xA1, 0x3D, 0x9F, 0xA9, 0xEC, 0xD1, 0xC6, 0x08,
  1071. 0x1B, 0x3D, 0xF5, 0xDB, 0x4C, 0xD4, 0xF0, 0x2C, 0xAA, 0xFC, 0xBA, 0x18,
  1072. 0x6F, 0x48, 0x7E, 0xB9, 0x47, 0x68, 0x2E, 0xF6, 0x1E, 0x67, 0x1C, 0x7E,
  1073. 0x0A, 0xCE, 0x10, 0x07, 0xC0, 0x0C, 0xAD, 0x5E, 0xC1, 0x53, 0x70, 0xD5,
  1074. 0xE7, 0x25, 0xCA, 0x37, 0x5E, 0x49, 0x59, 0xD0, 0x67, 0x2A, 0xBE, 0x92,
  1075. 0x36, 0x86, 0x8A, 0xBF, 0x3E, 0x17, 0x04, 0xFB, 0x1F, 0x46, 0xC8, 0x10,
  1076. 0x5C, 0x93, 0x02, 0x43, 0x14, 0x96, 0x6A, 0xD9, 0x87, 0x17, 0x62, 0x7D,
  1077. 0x3A, 0x45, 0xBE, 0x35, 0xDE, 0x75, 0x0B, 0x2A, 0xCE, 0x7D, 0xF3, 0x19,
  1078. 0x85, 0x4B, 0x0D, 0x6F, 0x8D, 0x15, 0xA3, 0x60, 0x61, 0x28, 0x55, 0x46,
  1079. 0xCE, 0x78, 0x31, 0x04, 0x18, 0x3C, 0x56, 0x4A, 0x3F, 0xA4, 0xC9, 0xB1,
  1080. 0x41, 0xED, 0x22, 0x80, 0xA1, 0xB3, 0xE2, 0xC7, 0x1B, 0x62, 0x85, 0xE4,
  1081. 0x81, 0x39, 0xCB, 0x1F, 0x95, 0xCC, 0x61, 0x61, 0xDF, 0xDE, 0xF3, 0x05,
  1082. 0x68, 0xB9, 0x7D, 0x4F, 0xFF, 0xF3, 0xC0, 0x0A, 0x25, 0x62, 0xD9, 0x8A,
  1083. 0x8A, 0x9E, 0x99, 0x0B, 0xFB, 0x85, 0x27, 0x8D, 0xF6, 0xD4, 0xE1, 0xB9,
  1084. 0xDE, 0xB4, 0x16, 0xBD, 0xDF, 0x6A, 0x25, 0x9C, 0xAC, 0xCD, 0x91, 0xF7,
  1085. 0xCB, 0xC1, 0x81, 0x22, 0x0D, 0xF4, 0x7E, 0xEC, 0x0C, 0x84, 0x13, 0x5A,
  1086. 0x74, 0x59, 0x3F, 0x3E, 0x61, 0x00, 0xD6, 0xB5, 0x4A, 0xA1, 0x04, 0xB5,
  1087. 0xA7, 0x1C, 0x29, 0xD0, 0xE1, 0x11, 0x19, 0xD7, 0x80, 0x5C, 0xEE, 0x08,
  1088. 0x15, 0xEB, 0xC9, 0xA8, 0x98, 0xF5, 0xA0, 0xF0, 0x92, 0x2A, 0xB0, 0xD3,
  1089. 0xC7, 0x8C, 0x8D, 0xBB, 0x88, 0x96, 0x4F, 0x18, 0xF0, 0x8A, 0xF9, 0x31,
  1090. 0x9E, 0x44, 0x94, 0x75, 0x6F, 0x78, 0x04, 0x10, 0xEC, 0xF3, 0xB0, 0xCE,
  1091. 0xA0, 0xBE, 0x7B, 0x25, 0xE1, 0xF7, 0x8A, 0xA8, 0xD4, 0x63, 0xC2, 0x65,
  1092. 0x47, 0xCC, 0x5C, 0xED, 0x7D, 0x8B, 0x07, 0x4D, 0x76, 0x29, 0x53, 0xAC,
  1093. 0x27, 0x8F, 0x5D, 0x78, 0x56, 0xFA, 0x99, 0x45, 0xA2, 0xCC, 0x65, 0xC4,
  1094. 0x54, 0x13, 0x9F, 0x38, 0x41, 0x7A, 0x61, 0x0E, 0x0D, 0x34, 0xBC, 0x11,
  1095. 0xAF, 0xE2, 0xF1, 0x8B, 0xFA, 0x2B, 0x54, 0x6C, 0xA3, 0x6C, 0x09, 0x1F,
  1096. 0x0B, 0x43, 0x9B, 0x07, 0x95, 0x83, 0x3F, 0x97, 0x99, 0x89, 0xF5, 0x51,
  1097. 0x41, 0xF6, 0x8E, 0x5D, 0xEF, 0x6D, 0x24, 0x71, 0x41, 0x7A, 0xAF, 0xBE,
  1098. 0x81, 0x71, 0xAB, 0x76, 0x2F, 0x1A, 0x5A, 0xBA, 0xF3, 0xA6, 0x65, 0x7A,
  1099. 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27,
  1100. 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54,
  1101. 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57,
  1102. 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1,
  1103. 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96,
  1104. 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE,
  1105. 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9,
  1106. 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F,
  1107. 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
  1108. static byte [] token = { 0x0e, 0xea, 0x7c, 0xe6, 0x5f, 0x35, 0xf2, 0xd8 };
  1109. [Test]
  1110. public void StrongName_MissingKeyFile_NoDelay ()
  1111. {
  1112. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
  1113. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
  1114. ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
  1115. string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
  1116. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1117. // no exception is thrown (file not found)
  1118. // because it's not AssemblyBuilder.Save job to do the signing :-/
  1119. #if NET_2_0
  1120. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
  1121. Assert.IsNull (check.GetPublicKey (), "#2");
  1122. Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
  1123. Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
  1124. #else
  1125. Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#1");
  1126. Assert.IsNotNull (check.GetPublicKey (), "#2a");
  1127. Assert.AreEqual (0, check.GetPublicKey ().Length, "#2b");
  1128. Assert.IsNull (check.GetPublicKeyToken (), "#3");
  1129. #endif
  1130. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
  1131. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
  1132. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
  1133. }
  1134. [Test]
  1135. public void StrongName_KeyFile_Delay ()
  1136. {
  1137. string strongfile = Path.Combine (tempDir, "strongname.snk");
  1138. using (FileStream fs = File.OpenWrite (strongfile)) {
  1139. fs.Write (strongName, 0, strongName.Length);
  1140. fs.Close ();
  1141. }
  1142. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
  1143. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  1144. ab.Save ("StrongName_KeyFile_Delay.dll");
  1145. string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
  1146. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1147. // no public key is inserted into the assembly
  1148. // because it's not AssemblyBuilder.Save job to do the signing :-/
  1149. #if NET_2_0
  1150. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
  1151. Assert.IsNull (check.GetPublicKey (), "#2");
  1152. Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
  1153. Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
  1154. #else
  1155. Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#1");
  1156. Assert.IsNotNull (check.GetPublicKey (), "#2a");
  1157. Assert.AreEqual (0, check.GetPublicKey ().Length, "#2b");
  1158. Assert.IsNull (check.GetPublicKeyToken (), "#3");
  1159. #endif
  1160. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
  1161. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
  1162. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
  1163. }
  1164. [Test]
  1165. public void StrongName_WithoutAttributes ()
  1166. {
  1167. // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
  1168. // aren't required to sign an assembly.
  1169. AssemblyName an = genAssemblyName ();
  1170. an.KeyPair = new StrongNameKeyPair (strongName);
  1171. AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
  1172. ab.Save ("StrongName_WithoutAttributes.dll");
  1173. string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
  1174. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1175. Assert.IsNotNull (check.GetPublicKey (), "#1a");
  1176. Assert.IsTrue (check.GetPublicKey ().Length > 0, "#1b");
  1177. Assert.AreEqual ("0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()), "#2");
  1178. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#3");
  1179. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#4");
  1180. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#5");
  1181. Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#6");
  1182. }
  1183. [Test]
  1184. public void SaveUnfinishedTypes ()
  1185. {
  1186. mb.DefineType ("TestType", TypeAttributes.Class |
  1187. TypeAttributes.Public | TypeAttributes.Sealed |
  1188. TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
  1189. typeof(object));
  1190. try {
  1191. ab.Save ("def_module");
  1192. Assert.Fail ("#1");
  1193. } catch (NotSupportedException ex) {
  1194. // Type 'TestType' was not completed
  1195. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  1196. Assert.IsNull (ex.InnerException, "#3");
  1197. Assert.IsNotNull (ex.Message, "#4");
  1198. Assert.IsTrue (ex.Message.IndexOf ("TestType") != -1, "#5");
  1199. }
  1200. }
  1201. [Test]
  1202. public void GetModules ()
  1203. {
  1204. Module[] arr;
  1205. arr = ab.GetModules ();
  1206. Assert.IsNotNull (arr, "#A1");
  1207. // FIXME: This doesn't work on mono
  1208. //Assert.IsTrue (arr.Length >= 2, "#A2");
  1209. foreach (Module m in arr)
  1210. Assert.AreEqual (typeof (ModuleBuilder), m.GetType (), "#A3");
  1211. // Test with no modules
  1212. AssemblyBuilder ab2 = genAssembly ();
  1213. arr = ab2.GetModules ();
  1214. Assert.IsNotNull (arr, "#B1");
  1215. Assert.AreEqual (0, arr.Length, "#B2");
  1216. }
  1217. [Test]
  1218. [Category ("NotWorking")] // bug #351932
  1219. public void GetReferencedAssemblies ()
  1220. {
  1221. AssemblyBuilder ab1;
  1222. AssemblyBuilder ab2;
  1223. AssemblyBuilder ab3;
  1224. AssemblyName [] refs;
  1225. TypeBuilder tb1;
  1226. TypeBuilder tb2;
  1227. TypeBuilder tb3;
  1228. TypeBuilder tb4;
  1229. ModuleBuilder mb1;
  1230. ModuleBuilder mb2;
  1231. ModuleBuilder mb3;
  1232. AssemblyName an1 = genAssemblyName ();
  1233. an1.Version = new Version (3, 0);
  1234. AssemblyName an2 = genAssemblyName ();
  1235. an2.Version = new Version ("1.2.3.4");
  1236. an2.KeyPair = new StrongNameKeyPair (strongName);
  1237. AssemblyName an3 = genAssemblyName ();
  1238. ab1 = domain.DefineDynamicAssembly (an1,
  1239. AssemblyBuilderAccess.RunAndSave,
  1240. tempDir);
  1241. ab2 = domain.DefineDynamicAssembly (an2,
  1242. AssemblyBuilderAccess.RunAndSave,
  1243. tempDir);
  1244. ab3 = domain.DefineDynamicAssembly (an3,
  1245. AssemblyBuilderAccess.RunAndSave,
  1246. tempDir);
  1247. refs = ab1.GetReferencedAssemblies ();
  1248. Assert.AreEqual (0, refs.Length, "#A1");
  1249. refs = ab2.GetReferencedAssemblies ();
  1250. Assert.AreEqual (0, refs.Length, "#A2");
  1251. refs = ab3.GetReferencedAssemblies ();
  1252. Assert.AreEqual (0, refs.Length, "#A3");
  1253. mb1 = ab1.DefineDynamicModule (an1.Name + ".dll");
  1254. tb1 = mb1.DefineType ("TestType1", TypeAttributes.Class |
  1255. TypeAttributes.Public, typeof (Attribute));
  1256. tb1.CreateType ();
  1257. mb2 = ab2.DefineDynamicModule (an2.Name + ".dll");
  1258. tb2 = mb2.DefineType ("TestType2", TypeAttributes.Class |
  1259. TypeAttributes.Public, tb1);
  1260. tb2.CreateType ();
  1261. mb3 = ab3.DefineDynamicModule (an3.Name + ".dll");
  1262. tb3 = mb3.DefineType ("TestType3", TypeAttributes.Class |
  1263. TypeAttributes.Public, tb1);
  1264. tb3.CreateType ();
  1265. tb4 = mb3.DefineType ("TestType4", TypeAttributes.Class |
  1266. TypeAttributes.Public, tb2);
  1267. tb4.CreateType ();
  1268. refs = ab1.GetReferencedAssemblies ();
  1269. Assert.AreEqual (0, refs.Length, "#B1");
  1270. refs = ab2.GetReferencedAssemblies ();
  1271. Assert.AreEqual (0, refs.Length, "#B2");
  1272. refs = ab3.GetReferencedAssemblies ();
  1273. Assert.AreEqual (0, refs.Length, "#B3");
  1274. ab1.Save (an1.Name + ".dll");
  1275. ab2.Save (an2.Name + ".dll");
  1276. ab3.Save (an3.Name + ".dll");
  1277. refs = ab1.GetReferencedAssemblies ();
  1278. Assert.AreEqual (0, refs.Length, "#C1");
  1279. refs = ab2.GetReferencedAssemblies ();
  1280. Assert.AreEqual (0, refs.Length, "#C2");
  1281. refs = ab3.GetReferencedAssemblies ();
  1282. Assert.AreEqual (0, refs.Length, "#C3");
  1283. string assemblyFile = Path.Combine (tempDir, an1.Name + ".dll");
  1284. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1285. byte [] buffer = new byte [fs.Length];
  1286. fs.Read (buffer, 0, buffer.Length);
  1287. Assembly a = Assembly.Load (buffer);
  1288. refs = a.GetReferencedAssemblies ();
  1289. Assert.AreEqual (1, refs.Length, "#D1");
  1290. Assert.IsNull (refs [0].CodeBase, "#D2:CodeBase");
  1291. Assert.IsNotNull (refs [0].CultureInfo, "#D2:CultureInfo");
  1292. Assert.IsNull (refs [0].EscapedCodeBase, "#D2:EscapedCodeBase");
  1293. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#D2:Flags");
  1294. Assert.AreEqual (Consts.AssemblyCorlib, refs [0].FullName, "#D2:FullName");
  1295. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#D2:HashAlgorithm");
  1296. Assert.IsNull (refs [0].KeyPair, "#D2:KeyPair");
  1297. Assert.AreEqual ("mscorlib", refs [0].Name, "#D2:Name");
  1298. #if NET_2_0
  1299. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#D2:PA");
  1300. #endif
  1301. Assert.AreEqual (new Version (Consts.FxVersion), refs [0].Version, "#D2:Version");
  1302. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1303. refs [0].VersionCompatibility, "#D2:VersionCompatibility");
  1304. Assert.IsNull (refs [0].GetPublicKey (), "#D2:GetPublicKey");
  1305. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#D2:GetPublicKeyToken(a)");
  1306. Assert.AreEqual (8, refs [0].GetPublicKeyToken ().Length, "#D2:GetPublicKeyToken(b)");
  1307. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#D2:ToString");
  1308. }
  1309. assemblyFile = Path.Combine (tempDir, an2.Name + ".dll");
  1310. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1311. byte [] buffer = new byte [fs.Length];
  1312. fs.Read (buffer, 0, buffer.Length);
  1313. Assembly a = Assembly.Load (buffer);
  1314. refs = a.GetReferencedAssemblies ();
  1315. Assert.AreEqual (1, refs.Length, "#E1");
  1316. Assert.IsNull (refs [0].CodeBase, "#E2:CodeBase");
  1317. Assert.IsNotNull (refs [0].CultureInfo, "#E2:CultureInfo(a)");
  1318. Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#E2:CultureInfo(b)");
  1319. Assert.IsNull (refs [0].EscapedCodeBase, "#E2:EscapedCodeBase");
  1320. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#E2:Flags");
  1321. Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#E2:FullName");
  1322. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#E2:HashAlgorithm");
  1323. Assert.IsNull (refs [0].KeyPair, "#E2:KeyPair");
  1324. Assert.AreEqual (an1.Name, refs [0].Name, "#E2:Name");
  1325. #if NET_2_0
  1326. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#E2:PA");
  1327. #endif
  1328. Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#E2:Version");
  1329. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1330. refs [0].VersionCompatibility, "#E2:VersionCompatibility");
  1331. Assert.IsNull (refs [0].GetPublicKey (), "#E2:GetPublicKey");
  1332. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#E2:GetPublicKeyToken(a)");
  1333. Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#E2:GetPublicKeyToken(b)");
  1334. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#E2:ToString");
  1335. }
  1336. assemblyFile = Path.Combine (tempDir, an3.Name + ".dll");
  1337. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1338. byte [] buffer = new byte [fs.Length];
  1339. fs.Read (buffer, 0, buffer.Length);
  1340. Assembly a = Assembly.Load (buffer);
  1341. refs = a.GetReferencedAssemblies ();
  1342. Assert.AreEqual (2, refs.Length, "#F1");
  1343. Assert.IsNull (refs [0].CodeBase, "#F2:CodeBase");
  1344. Assert.IsNotNull (refs [0].CultureInfo, "#F2:CultureInfo(a)");
  1345. Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#F2:CultureInfo(b)");
  1346. Assert.IsNull (refs [0].EscapedCodeBase, "#F2:EscapedCodeBase");
  1347. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#F2:Flags");
  1348. Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#F2:FullName");
  1349. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#F2:HashAlgorithm");
  1350. Assert.IsNull (refs [0].KeyPair, "#F2:KeyPair");
  1351. Assert.AreEqual (an1.Name, refs [0].Name, "#F2:Name");
  1352. #if NET_2_0
  1353. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#F2:PA");
  1354. #endif
  1355. Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#F2:Version");
  1356. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1357. refs [0].VersionCompatibility, "#F2:VersionCompatibility");
  1358. Assert.IsNull (refs [0].GetPublicKey (), "#F2:GetPublicKey");
  1359. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#F2:GetPublicKeyToken(a)");
  1360. Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#F2:GetPublicKeyToken(b)");
  1361. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#F2:ToString");
  1362. Assert.IsNull (refs [1].CodeBase, "#F3:CodeBase");
  1363. Assert.IsNotNull (refs [1].CultureInfo, "#F3:CultureInfo(a)");
  1364. Assert.AreEqual (CultureInfo.InvariantCulture, refs [1].CultureInfo, "#F3:CultureInfo(b)");
  1365. Assert.IsNull (refs [1].EscapedCodeBase, "#F3:EscapedCodeBase");
  1366. Assert.AreEqual (AssemblyNameFlags.None, refs [1].Flags, "#F3:Flags");
  1367. Assert.AreEqual (an2.Name + ", Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8", refs [1].FullName, "#F3:FullName");
  1368. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [1].HashAlgorithm, "#F3:HashAlgorithm");
  1369. Assert.IsNull (refs [1].KeyPair, "#F3:KeyPair");
  1370. Assert.AreEqual (an2.Name, refs [1].Name, "#F3:Name");
  1371. #if NET_2_0
  1372. Assert.AreEqual (ProcessorArchitecture.None, refs [1].ProcessorArchitecture, "#F3:PA");
  1373. #endif
  1374. Assert.AreEqual (new Version (1, 2, 3, 4), refs [1].Version, "#F3:Version");
  1375. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1376. refs [1].VersionCompatibility, "#F3:VersionCompatibility");
  1377. Assert.IsNull (refs [1].GetPublicKey (), "#F3:GetPublicKey");
  1378. Assert.AreEqual (token, refs [1].GetPublicKeyToken (), "#F3:GetPublicKeyToken");
  1379. Assert.AreEqual (refs [1].FullName, refs [1].ToString (), "#F3:ToString");
  1380. }
  1381. }
  1382. [Test] // bug #78724
  1383. public void GetTypes ()
  1384. {
  1385. TypeBuilder tb = mb.DefineType ("sometype");
  1386. tb.CreateType ();
  1387. Type[] types = ab.GetTypes ();
  1388. Assert.AreEqual (1, types.Length, "#1");
  1389. Assert.AreEqual ("sometype", types[0].Name, "#2");
  1390. }
  1391. [Test]
  1392. public void AssemblyName_Culture ()
  1393. {
  1394. AssemblyName assemblyName = new AssemblyName ();
  1395. assemblyName.Name = "AssemblyNameTest";
  1396. assemblyName.Version = new Version ("1.0.0.0");
  1397. assemblyName.CultureInfo = new CultureInfo ("en-US");
  1398. const string fullName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null";
  1399. const string abName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US";
  1400. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1401. }
  1402. [Test]
  1403. public void AssemblyName_PublicKey ()
  1404. {
  1405. AssemblyName assemblyName = new AssemblyName ();
  1406. assemblyName.Name = "AssemblyNameTest_PublicKey";
  1407. assemblyName.Version = new Version ("1.2.3.4");
  1408. assemblyName.KeyPair = new StrongNameKeyPair (strongName);
  1409. Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4", assemblyName.FullName, "#A1");
  1410. const string fullName = "AssemblyNameTest_PublicKey, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
  1411. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1412. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1413. AssemblyName abName = ab.GetName ();
  1414. Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#B1");
  1415. Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B2");
  1416. Assert.IsNotNull (abName.GetPublicKey () != null, "#B3a");
  1417. Assert.IsTrue (abName.GetPublicKey ().Length > 0, "#B3b");
  1418. Assert.IsNotNull (abName.GetPublicKeyToken (), "#B4a");
  1419. Assert.IsTrue (abName.GetPublicKeyToken ().Length > 0, "#B4b");
  1420. #if NET_2_0
  1421. Assert.AreEqual (fullName, abName.FullName, "#B5");
  1422. #else
  1423. //Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4, PublicKeyToken=0eea7ce65f35f2d8", abName.FullName, "#B5");
  1424. Assert.IsTrue (abName.FullName.IndexOf ("AssemblyNameTest_PublicKey, Version=1.2.3.4") != -1, "#B5a");
  1425. Assert.IsTrue (abName.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#B5b");
  1426. #endif
  1427. ab.Save ("AssemblyNameTest_PublicKey.dll");
  1428. AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
  1429. tempDir, "AssemblyNameTest_PublicKey.dll"));
  1430. Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#C1");
  1431. Assert.AreEqual (AssemblyNameFlags.PublicKey, bakedName.Flags, "#C2");
  1432. Assert.IsNotNull (bakedName.GetPublicKeyToken (), "#C3");
  1433. Assert.IsNotNull (bakedName.GetPublicKey (), "#C4");
  1434. Assert.AreEqual (fullName, bakedName.FullName, "#C5");
  1435. }
  1436. [Test]
  1437. public void AssemblyName_MoreCultureInfo ()
  1438. {
  1439. AssemblyName assemblyName = new AssemblyName ();
  1440. assemblyName.Name = "AssemblyNameTest_MoreCultureInfo";
  1441. assemblyName.Version = new Version ("1.2.3.4");
  1442. assemblyName.KeyPair = new StrongNameKeyPair (strongName);
  1443. Assert.IsNull (assemblyName.CultureInfo, "#A1");
  1444. Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4", assemblyName.FullName, "#A2");
  1445. const string fullName = "AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
  1446. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1447. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1448. AssemblyName abName = ab.GetName ();
  1449. Assert.IsNotNull (abName.CultureInfo != null, "#B1");
  1450. #if NET_2_0
  1451. Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#B2a");
  1452. Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#B2a");
  1453. Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B3");
  1454. Assert.AreEqual (fullName, abName.FullName, "#B4");
  1455. #else
  1456. Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#B2");
  1457. Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B3");
  1458. //Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, PublicKeyToken=0eea7ce65f35f2d8", abName.FullName, "#B4");
  1459. Assert.IsTrue (abName.FullName.IndexOf ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4") != -1, "#B4a");
  1460. Assert.IsTrue (abName.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#B4b");
  1461. #endif
  1462. ab.Save ("AssemblyNameTest_MoreCultureInfo.dll");
  1463. AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
  1464. tempDir, "AssemblyNameTest_MoreCultureInfo.dll"));
  1465. Assert.IsNotNull (bakedName.CultureInfo, "#C1");
  1466. #if NET_2_0
  1467. Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#C2a");
  1468. Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#C2b");
  1469. #else
  1470. Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#C2");
  1471. #endif
  1472. Assert.AreEqual (fullName, bakedName.FullName, "#C3");
  1473. }
  1474. [Test]
  1475. public void AssemblyName_NoVersion ()
  1476. {
  1477. AssemblyName assemblyName = new AssemblyName ();
  1478. assemblyName.Name = "AssemblyNameTest";
  1479. const string fullName = "AssemblyNameTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
  1480. const string abName = "AssemblyNameTest, Version=0.0.0.0";
  1481. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1482. }
  1483. [Test]
  1484. public void AssemblyName_Version ()
  1485. {
  1486. AssemblyName assemblyName = new AssemblyName ();
  1487. assemblyName.Name = "AssemblyNameTest";
  1488. assemblyName.Version = new Version (1, 2, 3, 4);
  1489. string fullName = "AssemblyNameTest, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null";
  1490. string abName = "AssemblyNameTest, Version=1.2.3.4";
  1491. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1492. assemblyName = new AssemblyName ();
  1493. assemblyName.Name = "AssemblyNameTest";
  1494. assemblyName.Version = new Version (1, 2);
  1495. fullName = "AssemblyNameTest, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null";
  1496. abName = "AssemblyNameTest, Version=1.2.0.0";
  1497. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1498. }
  1499. [Test]
  1500. [Category ("NotDotNet")]
  1501. public void GetType_IgnoreCase ()
  1502. {
  1503. TypeBuilder tb = mb.DefineType ("Foo.Test2", TypeAttributes.Public, typeof (object));
  1504. // the previous line throws a TypeLoadException under MS 1.1 SP1
  1505. Type t;
  1506. t = ab.GetType ("foo.Test2", true, true);
  1507. Assert.AreEqual ("Test2", t.Name, "#1");
  1508. t = ab.GetType ("foo.test2", true, true);
  1509. Assert.AreEqual ("Test2", t.Name, "#2");
  1510. t = ab.GetType ("Foo.test2", true, true);
  1511. Assert.AreEqual ("Test2", t.Name, "#3");
  1512. }
  1513. [ExpectedException (typeof (TypeLoadException))]
  1514. public void GetCustomAttributes_NotCreated ()
  1515. {
  1516. AssemblyBuilder ab = genAssembly ();
  1517. ModuleBuilder mb = ab.DefineDynamicModule("tester", "tester.dll", false);
  1518. TypeBuilder tb = mb.DefineType ("T");
  1519. tb.SetParent (typeof (Attribute));
  1520. ConstructorBuilder ctor = tb.DefineDefaultConstructor (MethodAttributes.Public);
  1521. object [] o = new object [0];
  1522. CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, o);
  1523. ab.SetCustomAttribute (cab);
  1524. ab.GetCustomAttributes (true);
  1525. }
  1526. private static void AssertAssemblyName (string tempDir, AssemblyName assemblyName, string abName, string fullName)
  1527. {
  1528. AppDomain currentDomain = AppDomain.CurrentDomain;
  1529. AppDomain newDomain = null;
  1530. try {
  1531. AssemblyBuilder ab = currentDomain.DefineDynamicAssembly (
  1532. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1533. ab.Save (assemblyName.Name + ".dll");
  1534. #if NET_2_0
  1535. // on .NET 2.0, the full name of the AssemblyBuilder matches the
  1536. // fully qualified assembly name
  1537. Assert.AreEqual (fullName, ab.FullName, "#1");
  1538. #else
  1539. //Assert.AreEqual (abName, ab.FullName, "#1");
  1540. #endif
  1541. AssemblyName an = ab.GetName ();
  1542. Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#2");
  1543. Assert.IsNotNull (an.GetPublicKey (), "#3a");
  1544. Assert.AreEqual (0, an.GetPublicKey ().Length, "#3b");
  1545. #if NET_2_0
  1546. Assert.IsNotNull (an.GetPublicKeyToken (), "#4a");
  1547. Assert.AreEqual (0, an.GetPublicKeyToken ().Length, "#4b");
  1548. #else
  1549. Assert.IsNull (an.GetPublicKeyToken (), "#4");
  1550. #endif
  1551. // load assembly in separate domain, so we can clean-up after the
  1552. // test
  1553. newDomain = AppDomain.CreateDomain ("test2", currentDomain.Evidence,
  1554. currentDomain.SetupInformation);
  1555. Helper helper = new Helper (Path.Combine (tempDir, assemblyName.Name + ".dll"),
  1556. fullName);
  1557. newDomain.DoCallBack (new CrossAppDomainDelegate (helper.Test));
  1558. } finally {
  1559. if (newDomain != null) {
  1560. AppDomain.Unload (newDomain);
  1561. }
  1562. }
  1563. }
  1564. [Serializable ()]
  1565. private class Helper
  1566. {
  1567. private readonly string _assemblyPath;
  1568. private readonly string _assemblyName;
  1569. public Helper (string assemblyPath, string assemblyName)
  1570. {
  1571. _assemblyPath = assemblyPath;
  1572. _assemblyName = assemblyName;
  1573. }
  1574. public void Test ()
  1575. {
  1576. AssemblyName assemblyName = AssemblyName.GetAssemblyName (_assemblyPath);
  1577. Assert.AreEqual (_assemblyName, assemblyName.ToString ());
  1578. }
  1579. }
  1580. }
  1581. }