AssemblyBuilderTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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.Threading;
  11. using System.Reflection;
  12. using System.Reflection.Emit;
  13. using System.IO;
  14. using System.Configuration.Assemblies;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Reflection.Emit
  17. {
  18. [TestFixture]
  19. public class AssemblyBuilderTest : Assertion
  20. {
  21. [AttributeUsage (AttributeTargets.Assembly)]
  22. public sealed class FooAttribute : Attribute
  23. {
  24. public FooAttribute (string arg)
  25. {
  26. }
  27. public FooAttribute ()
  28. {
  29. }
  30. }
  31. static int nameIndex = 0;
  32. static AppDomain domain;
  33. static AssemblyBuilder ab;
  34. string tempDir = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.Emit.AssemblyBuilderTest");
  35. [SetUp]
  36. protected void SetUp () {
  37. if (Directory.Exists (tempDir))
  38. Directory.Delete (tempDir, true);
  39. Directory.CreateDirectory (tempDir);
  40. for (int i = 1; i < 3; ++i) {
  41. string resFile = Path.Combine (tempDir, "res" + i + ".txt");
  42. using (StreamWriter sw = new StreamWriter (resFile)) {
  43. sw.WriteLine ("FOO");
  44. }
  45. }
  46. domain = Thread.GetDomain ();
  47. ab = genAssembly ();
  48. ab.DefineDynamicModule ("def_module");
  49. }
  50. [TearDown]
  51. protected void TearDown () {
  52. if (Directory.Exists (tempDir))
  53. Directory.Delete (tempDir, true);
  54. }
  55. private AssemblyName genAssemblyName () {
  56. AssemblyName assemblyName = new AssemblyName();
  57. assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);
  58. return assemblyName;
  59. }
  60. private AssemblyBuilder genAssembly () {
  61. return domain.DefineDynamicAssembly (genAssemblyName (),
  62. AssemblyBuilderAccess.RunAndSave,
  63. tempDir);
  64. }
  65. private MethodInfo genEntryFunction (AssemblyBuilder assembly) {
  66. ModuleBuilder module = assembly.DefineDynamicModule("module1");
  67. TypeBuilder tb = module.DefineType ("A");
  68. MethodBuilder mb = tb.DefineMethod ("A",
  69. MethodAttributes.Static, typeof (void), new Type [0]);
  70. mb.GetILGenerator ().Emit (OpCodes.Ret);
  71. return mb;
  72. }
  73. [ExpectedException (typeof (NotSupportedException))]
  74. public void TestCodeBase () {
  75. string codebase = ab.CodeBase;
  76. }
  77. [ExpectedException (typeof (NotSupportedException))]
  78. public void TestLocation () {
  79. string location = ab.Location;
  80. }
  81. public void TestEntryPoint () {
  82. AssertEquals ("EntryPoint defaults to null",
  83. null, ab.EntryPoint);
  84. MethodInfo mi = genEntryFunction (ab);
  85. ab.SetEntryPoint (mi);
  86. AssertEquals ("EntryPoint works", mi, ab.EntryPoint);
  87. }
  88. public void TestSetEntryPoint () {
  89. // Check invalid arguments
  90. try {
  91. ab.SetEntryPoint (null);
  92. Fail ();
  93. }
  94. catch (ArgumentNullException) {
  95. }
  96. // Check method from other assembly
  97. try {
  98. ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
  99. Fail ();
  100. }
  101. catch (InvalidOperationException) {
  102. }
  103. }
  104. public void TestIsDefined () {
  105. CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
  106. ab.SetCustomAttribute (cab);
  107. AssertEquals ("IsDefined works",
  108. true, ab.IsDefined (typeof (FooAttribute), false));
  109. AssertEquals ("IsDefined works",
  110. false, ab.IsDefined (typeof (AssemblyVersionAttribute), false));
  111. }
  112. [ExpectedException (typeof (NotSupportedException))]
  113. public void TestGetManifestResourceNames () {
  114. ab.GetManifestResourceNames ();
  115. }
  116. [ExpectedException (typeof (NotSupportedException))]
  117. public void TestGetManifestResourceInfo () {
  118. ab.GetManifestResourceInfo ("foo");
  119. }
  120. [ExpectedException (typeof (NotSupportedException))]
  121. public void TestGetManifestResourceStream1 () {
  122. ab.GetManifestResourceStream ("foo");
  123. }
  124. [ExpectedException (typeof (NotSupportedException))]
  125. public void TestGetManifestResourceStream2 () {
  126. ab.GetManifestResourceStream (typeof (int), "foo");
  127. }
  128. [ExpectedException (typeof (NotSupportedException))]
  129. public void TestGetFiles1 () {
  130. ab.GetFiles ();
  131. }
  132. [ExpectedException (typeof (NotSupportedException))]
  133. public void TestGetFiles2 () {
  134. ab.GetFiles (true);
  135. }
  136. [ExpectedException (typeof (NotSupportedException))]
  137. public void TestGetFile () {
  138. ab.GetFile ("foo");
  139. }
  140. [ExpectedException (typeof (NotSupportedException))]
  141. public void TestGetExportedTypes () {
  142. ab.GetExportedTypes ();
  143. }
  144. [ExpectedException (typeof (ArgumentNullException))]
  145. public void TestGetDynamicModule1 () {
  146. ab.GetDynamicModule (null);
  147. }
  148. [ExpectedException (typeof (ArgumentException))]
  149. public void TestGetDynamicModule2 () {
  150. ab.GetDynamicModule ("");
  151. }
  152. public void TestGetDynamicModule3 () {
  153. AssertNull (ab.GetDynamicModule ("FOO2"));
  154. ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
  155. AssertEquals (mb, ab.GetDynamicModule ("FOO"));
  156. AssertNull (ab.GetDynamicModule ("FOO4"));
  157. }
  158. #if NET_1_1
  159. public void TestImageRuntimeVersion () {
  160. string version = ab.ImageRuntimeVersion;
  161. Assert (version.Length > 0);
  162. }
  163. #endif
  164. [ExpectedException (typeof (ArgumentNullException))]
  165. public void TestAddResourceFileNullName () {
  166. ab.AddResourceFile (null, "foo.txt");
  167. }
  168. [ExpectedException (typeof (ArgumentNullException))]
  169. public void TestAddResourceFileNullFilename () {
  170. ab.AddResourceFile ("foo", null);
  171. }
  172. [ExpectedException (typeof (ArgumentException))]
  173. public void TestAddResourceFileEmptyName () {
  174. ab.AddResourceFile ("", "foo.txt");
  175. }
  176. [ExpectedException (typeof (ArgumentException))]
  177. public void TestAddResourceFileEmptyFilename () {
  178. ab.AddResourceFile ("foo", "");
  179. }
  180. [ExpectedException (typeof (FileNotFoundException))]
  181. public void TestAddResourceFileNonexistentFile () {
  182. ab.AddResourceFile ("foo", "not-existent.txt");
  183. }
  184. [ExpectedException (typeof (ArgumentException))]
  185. public void TestAddResourceFileDuplicateFileName () {
  186. ab.AddResourceFile ("foo", "res1.txt");
  187. ab.AddResourceFile ("foo2", "res1.txt");
  188. }
  189. [ExpectedException (typeof (ArgumentException))]
  190. public void TestAddResourceFileDuplicateName () {
  191. ab.AddResourceFile ("foo", "res1.txt");
  192. ab.AddResourceFile ("foo", "res2.txt");
  193. }
  194. [ExpectedException (typeof (ArgumentException))]
  195. public void TestAddResourceFileFilenameIncludesPath () {
  196. ab.AddResourceFile ("foo", "/tmp/res1.txt");
  197. }
  198. public void TestAddResourceFile () {
  199. ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);
  200. ab.Save ("TestAddResourceFile.dll");
  201. // TODO: Test reading back
  202. }
  203. public void TestDefineResource () {
  204. ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);
  205. ab.DefineResource ("foo2", "FOO", "foo2.txt");
  206. ab.Save ("TestDefineResource.dll");
  207. }
  208. [ExpectedException (typeof (ArgumentNullException))]
  209. public void TestDefineDynamicModuleNullName () {
  210. ab.DefineDynamicModule (null, "foo.txt");
  211. }
  212. [ExpectedException (typeof (ArgumentNullException))]
  213. public void TestDefineDynamicModuleNullFilename () {
  214. ab.DefineDynamicModule ("foo", null);
  215. }
  216. [ExpectedException (typeof (ArgumentException))]
  217. public void TestDefineDynamicModuleEmptyName () {
  218. ab.DefineDynamicModule ("", "foo.txt");
  219. }
  220. [ExpectedException (typeof (ArgumentException))]
  221. public void TestDefineDynamicModuleEmptyFilename () {
  222. ab.DefineDynamicModule ("foo", "");
  223. }
  224. [ExpectedException (typeof (ArgumentException))]
  225. public void TestDefineDynamicModuleDuplicateFileName () {
  226. ab.DefineDynamicModule ("foo", "res1.txt");
  227. ab.DefineDynamicModule ("foo2", "res1.txt");
  228. }
  229. [ExpectedException (typeof (ArgumentException))]
  230. public void TestDefineDynamicModuleDuplicateName () {
  231. ab.DefineDynamicModule ("foo", "res1.txt");
  232. ab.DefineDynamicModule ("foo", "res2.txt");
  233. }
  234. [ExpectedException (typeof (ArgumentException))]
  235. public void TestDefineDynamicModuleFilenameIncludesPath () {
  236. ab.DefineDynamicModule ("foo", "/tmp/res1.txt");
  237. }
  238. [ExpectedException (typeof (ArgumentException))]
  239. public void TestDefineDynamicModule5 () {
  240. // Filename without extension
  241. ab.DefineDynamicModule ("foo", "foo");
  242. }
  243. /*
  244. [ExpectedException (typeof (ArgumentException))]
  245. public void TestDefineDynamicModule6 () {
  246. // Name too long
  247. string name = "";
  248. for (int i = 0; i < 259; ++i)
  249. name = name + "A";
  250. try {
  251. ab.DefineDynamicModule (name);
  252. }
  253. catch (Exception) {
  254. Fail ();
  255. }
  256. name = name + "A";
  257. // LAMESPEC: According to MSDN, this should throw an ArgumentException
  258. ab.DefineDynamicModule (name);
  259. }
  260. */
  261. [ExpectedException (typeof (InvalidOperationException))]
  262. public void TestDefineDynamicModule7 () {
  263. // Called when assembly was already saved
  264. ab.Save ("TestDefineDynamicModule7.dll");
  265. ab.DefineDynamicModule ("foo", "foo.dll");
  266. }
  267. [ExpectedException (typeof (NotSupportedException))]
  268. public void TestDefineDynamicModule8 () {
  269. // Called on an assembly defined with the Run attribute
  270. AssemblyBuilder ab =
  271. domain.DefineDynamicAssembly (genAssemblyName (),
  272. AssemblyBuilderAccess.Run,
  273. tempDir);
  274. ab.DefineDynamicModule ("foo", "foo.dll");
  275. }
  276. public void TestDefineDynamicModule () {
  277. ab.DefineDynamicModule ("foo", "foo.dll");
  278. ab.DefineDynamicModule ("foo2", true);
  279. ab.DefineDynamicModule ("foo3", "foo3.dll");
  280. ab.DefineDynamicModule ("foo4", "foo4.dll", true);
  281. }
  282. [ExpectedException (typeof (ArgumentNullException))]
  283. public void TestDefineUnmanagedResource1 () {
  284. // Null argument
  285. ab.DefineUnmanagedResource ((byte[])null);
  286. }
  287. [ExpectedException (typeof (ArgumentNullException))]
  288. public void TestDefineUnmanagedResource2 () {
  289. // Null argument
  290. ab.DefineUnmanagedResource ((string)null);
  291. }
  292. [ExpectedException (typeof (ArgumentException))]
  293. public void TestDefineUnmanagedResource3 () {
  294. // Empty filename
  295. ab.DefineUnmanagedResource ("");
  296. }
  297. [ExpectedException (typeof (FileNotFoundException))]
  298. public void TestDefineUnmanagedResource4 () {
  299. // Nonexistent file
  300. ab.DefineUnmanagedResource ("not-exists.txt");
  301. }
  302. [ExpectedException (typeof (ArgumentNullException))]
  303. public void TestSetCustomAttribute1 () {
  304. // Null argument
  305. ab.SetCustomAttribute (null);
  306. }
  307. [ExpectedException (typeof (ArgumentNullException))]
  308. public void TestSetCustomAttribute2 () {
  309. // Null constructor
  310. ab.SetCustomAttribute (null, new byte [0]);
  311. }
  312. [ExpectedException (typeof (ArgumentNullException))]
  313. public void TestSetCustomAttribute3 () {
  314. // Null blob
  315. ab.SetCustomAttribute (typeof(AssemblyCompanyAttribute).GetConstructor (new Type [] { typeof (String) }), null);
  316. }
  317. public void TestSetCustomAttribute () {
  318. // Test common custom attributes
  319. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));
  320. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));
  321. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }), new object [] { AssemblyHashAlgorithm.MD5 }));
  322. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0x0100 }));
  323. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  324. ab.SetCustomAttribute (typeof (FooAttribute).GetConstructor (new Type [] {}), new byte [0]);
  325. ab.Save ("TestSetCustomAttribute.dll");
  326. /* We should read back the assembly and check the attributes ... */
  327. }
  328. // strongname generated using "sn -k unit.snk"
  329. static byte[] strongName = {
  330. 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
  331. 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A,
  332. 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D,
  333. 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72,
  334. 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16,
  335. 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4,
  336. 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4,
  337. 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D,
  338. 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25,
  339. 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93,
  340. 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0,
  341. 0x4D, 0x27, 0xFC, 0xA7, 0x4F, 0x36, 0xCA, 0x13, 0x42, 0xD3, 0x83, 0xC4,
  342. 0x06, 0x6E, 0x12, 0xE0, 0xA1, 0x3D, 0x9F, 0xA9, 0xEC, 0xD1, 0xC6, 0x08,
  343. 0x1B, 0x3D, 0xF5, 0xDB, 0x4C, 0xD4, 0xF0, 0x2C, 0xAA, 0xFC, 0xBA, 0x18,
  344. 0x6F, 0x48, 0x7E, 0xB9, 0x47, 0x68, 0x2E, 0xF6, 0x1E, 0x67, 0x1C, 0x7E,
  345. 0x0A, 0xCE, 0x10, 0x07, 0xC0, 0x0C, 0xAD, 0x5E, 0xC1, 0x53, 0x70, 0xD5,
  346. 0xE7, 0x25, 0xCA, 0x37, 0x5E, 0x49, 0x59, 0xD0, 0x67, 0x2A, 0xBE, 0x92,
  347. 0x36, 0x86, 0x8A, 0xBF, 0x3E, 0x17, 0x04, 0xFB, 0x1F, 0x46, 0xC8, 0x10,
  348. 0x5C, 0x93, 0x02, 0x43, 0x14, 0x96, 0x6A, 0xD9, 0x87, 0x17, 0x62, 0x7D,
  349. 0x3A, 0x45, 0xBE, 0x35, 0xDE, 0x75, 0x0B, 0x2A, 0xCE, 0x7D, 0xF3, 0x19,
  350. 0x85, 0x4B, 0x0D, 0x6F, 0x8D, 0x15, 0xA3, 0x60, 0x61, 0x28, 0x55, 0x46,
  351. 0xCE, 0x78, 0x31, 0x04, 0x18, 0x3C, 0x56, 0x4A, 0x3F, 0xA4, 0xC9, 0xB1,
  352. 0x41, 0xED, 0x22, 0x80, 0xA1, 0xB3, 0xE2, 0xC7, 0x1B, 0x62, 0x85, 0xE4,
  353. 0x81, 0x39, 0xCB, 0x1F, 0x95, 0xCC, 0x61, 0x61, 0xDF, 0xDE, 0xF3, 0x05,
  354. 0x68, 0xB9, 0x7D, 0x4F, 0xFF, 0xF3, 0xC0, 0x0A, 0x25, 0x62, 0xD9, 0x8A,
  355. 0x8A, 0x9E, 0x99, 0x0B, 0xFB, 0x85, 0x27, 0x8D, 0xF6, 0xD4, 0xE1, 0xB9,
  356. 0xDE, 0xB4, 0x16, 0xBD, 0xDF, 0x6A, 0x25, 0x9C, 0xAC, 0xCD, 0x91, 0xF7,
  357. 0xCB, 0xC1, 0x81, 0x22, 0x0D, 0xF4, 0x7E, 0xEC, 0x0C, 0x84, 0x13, 0x5A,
  358. 0x74, 0x59, 0x3F, 0x3E, 0x61, 0x00, 0xD6, 0xB5, 0x4A, 0xA1, 0x04, 0xB5,
  359. 0xA7, 0x1C, 0x29, 0xD0, 0xE1, 0x11, 0x19, 0xD7, 0x80, 0x5C, 0xEE, 0x08,
  360. 0x15, 0xEB, 0xC9, 0xA8, 0x98, 0xF5, 0xA0, 0xF0, 0x92, 0x2A, 0xB0, 0xD3,
  361. 0xC7, 0x8C, 0x8D, 0xBB, 0x88, 0x96, 0x4F, 0x18, 0xF0, 0x8A, 0xF9, 0x31,
  362. 0x9E, 0x44, 0x94, 0x75, 0x6F, 0x78, 0x04, 0x10, 0xEC, 0xF3, 0xB0, 0xCE,
  363. 0xA0, 0xBE, 0x7B, 0x25, 0xE1, 0xF7, 0x8A, 0xA8, 0xD4, 0x63, 0xC2, 0x65,
  364. 0x47, 0xCC, 0x5C, 0xED, 0x7D, 0x8B, 0x07, 0x4D, 0x76, 0x29, 0x53, 0xAC,
  365. 0x27, 0x8F, 0x5D, 0x78, 0x56, 0xFA, 0x99, 0x45, 0xA2, 0xCC, 0x65, 0xC4,
  366. 0x54, 0x13, 0x9F, 0x38, 0x41, 0x7A, 0x61, 0x0E, 0x0D, 0x34, 0xBC, 0x11,
  367. 0xAF, 0xE2, 0xF1, 0x8B, 0xFA, 0x2B, 0x54, 0x6C, 0xA3, 0x6C, 0x09, 0x1F,
  368. 0x0B, 0x43, 0x9B, 0x07, 0x95, 0x83, 0x3F, 0x97, 0x99, 0x89, 0xF5, 0x51,
  369. 0x41, 0xF6, 0x8E, 0x5D, 0xEF, 0x6D, 0x24, 0x71, 0x41, 0x7A, 0xAF, 0xBE,
  370. 0x81, 0x71, 0xAB, 0x76, 0x2F, 0x1A, 0x5A, 0xBA, 0xF3, 0xA6, 0x65, 0x7A,
  371. 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27,
  372. 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54,
  373. 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57,
  374. 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1,
  375. 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96,
  376. 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE,
  377. 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9,
  378. 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F,
  379. 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
  380. [Test]
  381. public void StrongName_MissingKeyFile_NoDelay ()
  382. {
  383. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
  384. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
  385. ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
  386. string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
  387. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  388. // no exception is thrown (file not found)
  389. // because it's not AssemblyBuilder.Save job to do the signing :-/
  390. AssertNull ("Token", check.GetPublicKeyToken ());
  391. }
  392. [Test]
  393. public void StrongName_KeyFile_Delay ()
  394. {
  395. string strongfile = Path.Combine (tempDir, "strongname.snk");
  396. using (FileStream fs = File.OpenWrite (strongfile)) {
  397. fs.Write (strongName, 0, strongName.Length);
  398. fs.Close ();
  399. }
  400. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
  401. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  402. ab.Save ("StrongName_KeyFile_Delay.dll");
  403. string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
  404. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  405. // no public key is inserted into the assembly
  406. // because it's not AssemblyBuilder.Save job to do the signing :-/
  407. AssertNull ("Token", check.GetPublicKeyToken ());
  408. }
  409. [Test]
  410. public void StrongName_WithoutAttributes ()
  411. {
  412. // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
  413. // aren't required to sign an assembly.
  414. AssemblyName an = genAssemblyName ();
  415. an.KeyPair = new StrongNameKeyPair (strongName);
  416. AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
  417. ab.Save ("StrongName_WithoutAttributes.dll");
  418. string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
  419. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  420. AssertEquals ("Token", "0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()));
  421. }
  422. }
  423. }