示例#1
0
        public void InitializeThrowsBadImageExceptionOnInvalidInputImage()
        {
            CecilWeaverConfiguration configuration     = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("InvalidImage.exe"));
            ILanguageModelAccessor   langModelAccessor = new LanguageModelAccessorMock();

            ((IILWeaver) new CecilILWeaver(configuration, langModelAccessor)).DoWeave();
        }
示例#2
0
        public void WeaveThrowsInvalidOperationExceptionIfFileDoesntExists()
        {
            CecilWeaverConfiguration configuration     = CecilWeaverConfiguration.CreateDefaultConfiguration("c:\\this_file_doesnt_exist");
            ILanguageModelAccessor   langModelAccessor = new LanguageModelAccessorMock();

            ((IILWeaver) new CecilILWeaver(configuration, langModelAccessor)).DoWeave();
        }
示例#3
0
        public void WeavingInternalStringAddsPrivateStringFieldToTargetClass()
        {
            //set up model w/ 1 Internal typed 'string'
            LanguageModelAccessorMock model = new LanguageModelAccessorMock();

            model.AddInternalToType("TestTarget.Program", "System.String, mscorlib", "InternalString");

            //create configuration
            CecilWeaverConfiguration configuration = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe"), CreateFullPath("WeavingInternalStringAddsPrivateStringFieldToTargetClass.exe"));

            //do weaving
            IILWeaver weaver = DIHelper.CreateObject <CecilILWeaver>(CreateTestContainer(model, configuration));

            weaver.DoWeave();


            //test wether Internal is created
            Assembly  asm                 = Assembly.LoadFile(CreateFullPath("WeavingInternalStringAddsPrivateStringFieldToTargetClass.exe"));
            Type      programType         = asm.GetType("TestTarget.Program");
            FieldInfo internalStringField = programType.GetField("InternalString", BindingFlags.NonPublic | BindingFlags.Instance);

            //assert on requirements
            Assert.IsNotNull(internalStringField);
            Assert.AreEqual(typeof(string), internalStringField.FieldType);
        }
示例#4
0
        public void WeavingContextInstructionAddsFilterContextLogic()
        {
            // set up model
            LanguageModelAccessorMock model = new LanguageModelAccessorMock();

            // Create inputfilters
            Block block  = new Block();
            Block block2 = new Block();

            ContextInstruction ci  = new ContextInstruction(ContextInstruction.CHECK_INNER_CALL, 1000, block2);
            ContextInstruction ci2 = new ContextInstruction(ContextInstruction.RESET_INNER_CALL, 1000, null);
            ContextInstruction ci3 = new ContextInstruction(ContextInstruction.SET_INNER_CALL, 1000, null);

            block.addInstruction(ci);
            block.addInstruction(ci2);
            block.addInstruction(ci3);

            model.AddInputFilter("TestTarget.Program", "System.Void TestTarget.Program::TestMethod(System.Int32)", block);

            // create configuration
            CecilWeaverConfiguration configuration = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe"), CreateFullPath(OutputFileName));

            // do weaving
            IILWeaver weaver = DIHelper.CreateObject <CecilILWeaver>(CreateTestContainer(model, configuration));

            weaver.DoWeave();

            ILReader il = new ILReader();

            il.OpenAssembly(CreateFullPath(OutputFileName));
            List <ILInstruction> ils = il.GetILInstructions("TestTarget.Program", "TestMethod");

            List <ILInstruction> shouldContainCheck = new List <ILInstruction>();

            shouldContainCheck.Add(new ILInstruction(0, "ldarg", "this"));
            shouldContainCheck.Add(new ILInstruction(0, "ldc.i4", "1000"));
            shouldContainCheck.Add(new ILInstruction(0, "call", "System.Boolean Composestar.StarLight.ContextInfo.FilterContext::IsInnerCall(System.Object,System.Int32)"));

            Assert.IsTrue(il.ContainsILInstructions(ils, shouldContainCheck), "Generated IL code did not contain the check for IsInnerCall");

            List <ILInstruction> shouldContainReset = new List <ILInstruction>();

            shouldContainReset.Add(new ILInstruction(0, "call", "System.Void Composestar.StarLight.ContextInfo.FilterContext::ResetInnerCall()"));

            Assert.IsTrue(il.ContainsILInstructions(ils, shouldContainReset), "Generated IL code did not contain the call to the ResetInnerCall");

            List <ILInstruction> shouldContainSet = new List <ILInstruction>();

            shouldContainSet.Add(new ILInstruction(0, "ldarg", "this"));
            shouldContainSet.Add(new ILInstruction(0, "ldc.i4", "1000"));
            shouldContainSet.Add(new ILInstruction(0, "call", "System.Void Composestar.StarLight.ContextInfo.FilterContext::SetInnerCall(System.Object,System.Int32)"));

            Assert.IsTrue(il.ContainsILInstructions(ils, shouldContainSet), "Generated IL code did not contain the call to the SetInnerCall");
        }
示例#5
0
        public void WeavingErrorActionAddsExceptionFilter()
        {
            string outputFileName = "WeavingErrorActionAddsExceptionFilter.exe";

            // set up model
            LanguageModelAccessorMock model = new LanguageModelAccessorMock();

            // Create inputfilters
            Block block  = new Block();
            Block block2 = new Block();

            ContextInstruction ci  = new ContextInstruction(ContextInstruction.CHECK_INNER_CALL, 1000, block2);
            ContextInstruction ci2 = new ContextInstruction(ContextInstruction.RESET_INNER_CALL, 1000, null);
            ContextInstruction ci3 = new ContextInstruction(ContextInstruction.SET_INNER_CALL, 1000, null);

            FilterAction errorAction = new FilterAction("ErrorAction", "", "");

            block2.addInstruction(errorAction);

            block.addInstruction(ci);
            block.addInstruction(ci2);
            block.addInstruction(ci3);

            model.AddInputFilter("TestTarget.Program", "System.Void TestTarget.Program::TestMethod(System.Int32)", block);

            // create configuration
            CecilWeaverConfiguration configuration = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe"), CreateFullPath(outputFileName));

            // do weaving
            IILWeaver weaver = DIHelper.CreateObject <CecilILWeaver>(CreateTestContainer(model, configuration));

            weaver.DoWeave();

            ILReader il = new ILReader();

            il.OpenAssembly(CreateFullPath(outputFileName));
            List <ILInstruction> ils = il.GetILInstructions("TestTarget.Program", "TestMethod");

            // Expecting the following IL code
            List <ILInstruction> shouldContainThrow = new List <ILInstruction>();

            shouldContainThrow.Add(new ILInstruction(0, "newobj", "System.Void System.Exception::.ctor()"));
            shouldContainThrow.Add(new ILInstruction(0, "throw", ""));

            Assert.IsTrue(il.ContainsILInstructions(ils, shouldContainThrow), "Generated IL code did not contain the throw exception code");
        }
示例#6
0
        public void WeavingBeforeActionAddsPointCutContextLogic()
        {
            string outputFileName = "WeavingBeforeActionAddsPintCutContextLogic.exe";

            // set up model
            LanguageModelAccessorMock model = new LanguageModelAccessorMock();

            // Create inputfilters
            Block block  = new Block();
            Block block2 = new Block();

            ContextInstruction ci  = new ContextInstruction(ContextInstruction.CHECK_INNER_CALL, 1000, block2);
            ContextInstruction ci2 = new ContextInstruction(ContextInstruction.RESET_INNER_CALL, 1000, null);
            ContextInstruction ci3 = new ContextInstruction(ContextInstruction.SET_INNER_CALL, 1000, null);

            FilterAction errorAction = new FilterAction("BeforeAction", "", "");

            block2.addInstruction(errorAction);

            block.addInstruction(ci);
            block.addInstruction(ci2);
            block.addInstruction(ci3);

            model.AddInputFilter("TestTarget.Program", "System.Void TestTarget.Program::TestMethod(System.Int32)", block);

            // create configuration
            CecilWeaverConfiguration configuration = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe"), CreateFullPath(outputFileName));

            // do weaving
            IILWeaver weaver = DIHelper.CreateObject <CecilILWeaver>(CreateTestContainer(model, configuration));

            weaver.DoWeave();

            // Expecting the following IL code
            List <ILInstruction> shouldContainBefore = new List <ILInstruction>();

            shouldContainBefore.Add(new ILInstruction(0, "", ""));
            shouldContainBefore.Add(new ILInstruction(0, "", ""));

            // Assert.IsTrue(il.ContainsILInstructions(ils, shouldContainBefore), "Generated IL code did not contain the before action code");

            Assert.Inconclusive("Add the expected IL code to make this test work.");
        }
示例#7
0
        internal IServiceProvider CreateTestContainer(LanguageModelAccessorMock languageModel, CecilWeaverConfiguration configuration)
        {
            ServiceContainer serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(ILanguageModelAccessor), languageModel == null ? new LanguageModelAccessorMock() : languageModel);
            serviceContainer.AddService(typeof(CecilWeaverConfiguration), configuration == null ? CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe")) : configuration);
            serviceContainer.AddService(typeof(IBuilderConfigurator <BuilderStage>), new ILWeaverTestBuilderConfigurator());

            return(serviceContainer);
        }
示例#8
0
 public void TestInitialize()
 {
     svcContainer.AddService(typeof(IBuilderConfigurator <BuilderStage>), new ILWeaverTestBuilderConfigurator());
     svcContainer.AddService(typeof(CecilWeaverConfiguration), CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("TestTarget.exe")));
     svcContainer.AddService(typeof(ILanguageModelAccessor), new LanguageModelAccessorMock());
 }