ModuleBuilderTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // ModuleBuilderTest - NUnit Test Cases for the ModuleBuilder 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.Collections;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Reflection.Emit
  17. {
  18. [TestFixture]
  19. public class ModuleBuilderTest : Assertion
  20. {
  21. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.Emit.ModuleBuilderTest");
  22. [SetUp]
  23. public void SetUp () {
  24. while (Directory.Exists (TempFolder))
  25. TempFolder = Path.Combine (TempFolder, "2");
  26. Directory.CreateDirectory (TempFolder);
  27. }
  28. [TearDown]
  29. public void TearDown () {
  30. try {
  31. // This throws an exception under MS.NET, since the directory contains loaded
  32. // assemblies.
  33. Directory.Delete (TempFolder, true);
  34. }
  35. catch (Exception) {
  36. }
  37. }
  38. // Some of these tests overlap with the tests for Module
  39. [Test]
  40. public void TestGlobalData () {
  41. AssemblyName assemblyName = new AssemblyName();
  42. assemblyName.Name = "foo";
  43. AssemblyBuilder ab
  44. = Thread.GetDomain().DefineDynamicAssembly(
  45. assemblyName, AssemblyBuilderAccess.RunAndSave, TempFolder);
  46. string resfile = Path.Combine (TempFolder, "res");
  47. using (StreamWriter sw = new StreamWriter (resfile)) {
  48. sw.WriteLine ("FOO");
  49. }
  50. ModuleBuilder mb = ab.DefineDynamicModule("foo.dll", "foo.dll");
  51. mb.DefineInitializedData ("DATA", new byte [100], FieldAttributes.Public);
  52. mb.DefineInitializedData ("DATA2", new byte [100], FieldAttributes.Public);
  53. mb.DefineInitializedData ("DATA3", new byte [99], FieldAttributes.Public);
  54. mb.DefineUninitializedData ("DATA4", 101, FieldAttributes.Public);
  55. mb.DefineInitializedData ("DATA_PRIVATE", new byte [100], 0);
  56. mb.CreateGlobalFunctions ();
  57. ab.Save ("foo.dll");
  58. Assembly assembly = Assembly.LoadFrom (Path.Combine (TempFolder, "foo.dll"));
  59. Module module = assembly.GetLoadedModules ()[0];
  60. string[] expectedFieldNames = new string [] {
  61. "DATA", "DATA2", "DATA3", "DATA4"
  62. };
  63. ArrayList fieldNames = new ArrayList ();
  64. foreach (FieldInfo fi in module.GetFields ()) {
  65. fieldNames.Add (fi.Name);
  66. }
  67. AssertArrayEqualsSorted (expectedFieldNames, fieldNames.ToArray (typeof (string)));
  68. AssertEquals (module.GetField ("DATA") != null, true);
  69. AssertEquals (module.GetField ("DATA2") != null, true);
  70. AssertEquals (module.GetField ("DATA3") != null, true);
  71. AssertEquals (module.GetField ("DATA4") != null, true);
  72. AssertEquals (module.GetField ("DATA_PRIVATE"), null);
  73. AssertEquals (module.GetField ("DATA_PRIVATE", BindingFlags.NonPublic | BindingFlags.Static) != null, true);
  74. }
  75. private static void AssertArrayEqualsSorted (Array o1, Array o2) {
  76. Array s1 = (Array)o1.Clone ();
  77. Array s2 = (Array)o2.Clone ();
  78. Array.Sort (s1);
  79. Array.Sort (s2);
  80. AssertEquals (s1.Length, s2.Length);
  81. for (int i = 0; i < s1.Length; ++i)
  82. AssertEquals (s1.GetValue (i), s2.GetValue (i));
  83. }
  84. }
  85. }