/src/UnitTests/Reflection/ReflectionEmitTests.cs

http://github.com/philiplaureano/LinFu · C# · 131 lines · 97 code · 28 blank · 6 comment · 4 complexity · ea845dfe595199c123443c4288e82277 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using LinFu.AOP.Cecil;
  6. using LinFu.IoC;
  7. using LinFu.IoC.Configuration;
  8. using LinFu.IoC.Configuration.Interfaces;
  9. using LinFu.Reflection.Emit;
  10. using Mono.Cecil;
  11. using Xunit;
  12. using SampleStronglyNamedLibrary;
  13. namespace LinFu.UnitTests.Reflection
  14. {
  15. public class ReflectionEmitTests : BasePEVerifyTestCase
  16. {
  17. private Loader loader;
  18. private ServiceContainer container;
  19. private string filename;
  20. protected override void OnInit()
  21. {
  22. if (File.Exists(filename))
  23. File.Delete(filename);
  24. // Initialize the loader
  25. // with all of the default LinFu plugins
  26. loader = new Loader();
  27. container = new ServiceContainer();
  28. filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.dll", Guid.NewGuid()));
  29. AutoDelete(filename);
  30. loader.LoadDirectory(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
  31. loader.LoadInto(container);
  32. }
  33. protected override void OnTerm()
  34. {
  35. if (!File.Exists(filename))
  36. return;
  37. PEVerify(filename);
  38. }
  39. [Fact]
  40. public void AssemblyDefinitionMustBeConvertibleToActualAssembly()
  41. {
  42. var name = new AssemblyNameDefinition("testAssembly", new Version(1, 0));
  43. var definition = AssemblyDefinition.CreateAssembly(name, "testModule", ModuleKind.Dll);
  44. var assembly = definition.ToAssembly();
  45. Assert.True(assembly != null);
  46. }
  47. [Fact]
  48. public void CecilShouldExtractSampleClassFromSignedAssembly()
  49. {
  50. var location = typeof(SampleHelloClass).Assembly.Location;
  51. var sourceAssembly = AssemblyDefinition.ReadAssembly(location);
  52. Assert.NotNull(sourceAssembly);
  53. var name = new AssemblyNameDefinition("testAssembly", new Version(1, 0));
  54. var definition = AssemblyDefinition.CreateAssembly(name, "testModule", ModuleKind.Dll);
  55. var targetModule = definition.MainModule;
  56. foreach (TypeDefinition typeDef in sourceAssembly.MainModule.Types)
  57. {
  58. // Copy the source type to the target assembly
  59. targetModule.Types.Add(typeDef);
  60. }
  61. // Convert the new assemblyDef into an actual assembly
  62. var assembly = definition.ToAssembly();
  63. Assert.NotNull(assembly);
  64. var types = assembly.GetTypes();
  65. Assert.True(types.Length > 0);
  66. // The imported type must match the original type
  67. var firstType = types.FirstOrDefault();
  68. Assert.NotNull(firstType);
  69. Assert.Equal(firstType.Name, typeof(SampleHelloClass).Name);
  70. var instance = Activator.CreateInstance(firstType);
  71. Assert.NotNull(instance);
  72. var speakMethod = firstType.GetMethod("Speak");
  73. Assert.NotNull(speakMethod);
  74. speakMethod.Invoke(instance, new object[] { });
  75. }
  76. [Fact]
  77. public void CecilShouldRemoveStrongNameFromAssembly()
  78. {
  79. var location = typeof(SampleHelloClass).Assembly.Location;
  80. var sourceAssembly = AssemblyDefinition.ReadAssembly(location);
  81. Assert.NotNull(sourceAssembly);
  82. sourceAssembly.RemoveStrongName();
  83. var assembly = sourceAssembly.ToAssembly();
  84. Assert.NotNull(assembly);
  85. var assemblyName = assembly.GetName();
  86. // The public key should be empty
  87. var bytes = assemblyName.GetPublicKey();
  88. Assert.True(bytes.Length == 0);
  89. }
  90. [Fact]
  91. public void MethodInvokerShouldProperlyHandleReturnValues()
  92. {
  93. var targetMethod = typeof(object).GetMethod("GetHashCode");
  94. var instance = new object();
  95. var hash = instance.GetHashCode();
  96. container.AddDefaultServices();
  97. var invoker = container.GetService<IMethodInvoke<MethodInfo>>();
  98. Assert.NotNull(invoker);
  99. var result = invoker.Invoke(instance, targetMethod);
  100. Assert.Equal(result, hash);
  101. }
  102. }
  103. }