Exemplo n.º 1
0
        public void WeaveThrowsInvalidOperationExceptionIfFileDoesntExists()
        {
            CecilWeaverConfiguration configuration     = CecilWeaverConfiguration.CreateDefaultConfiguration("c:\\this_file_doesnt_exist");
            ILanguageModelAccessor   langModelAccessor = new LanguageModelAccessorMock();

            ((IILWeaver) new CecilILWeaver(configuration, langModelAccessor)).DoWeave();
        }
Exemplo n.º 2
0
        public void InitializeThrowsBadImageExceptionOnInvalidInputImage()
        {
            CecilWeaverConfiguration configuration     = CecilWeaverConfiguration.CreateDefaultConfiguration(CreateFullPath("InvalidImage.exe"));
            ILanguageModelAccessor   langModelAccessor = new LanguageModelAccessorMock();

            ((IILWeaver) new CecilILWeaver(configuration, langModelAccessor)).DoWeave();
        }
Exemplo n.º 3
0
        public void InitializeThrowsBadImageExceptionOnInvalidInputImage()
        {
            CecilAnalyzerConfiguration configuration     = CecilAnalyzerConfiguration.CreateDefaultConfiguration(string.Empty);
            ILanguageModelAccessor     langModelAccessor = new LanguageModelAccessorMock();

            AssemblyElement ae = ((IILAnalyzer) new CecilILAnalyzer(configuration, langModelAccessor)).ExtractAllTypes(CreateFullPath("InvalidImage.exe"));
        }
Exemplo n.º 4
0
        public void AnalyzerThrowsExceptionIfFileDoesntExists()
        {
            CecilAnalyzerConfiguration configuration     = CecilAnalyzerConfiguration.CreateDefaultConfiguration(string.Empty);
            ILanguageModelAccessor     langModelAccessor = new LanguageModelAccessorMock();

            AssemblyElement ae = ((IILAnalyzer) new CecilILAnalyzer(configuration, langModelAccessor)).ExtractAllTypes("c:\\this_file_doesnt_exist");
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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");
        }
Exemplo n.º 8
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");
        }
Exemplo n.º 9
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.");
        }
Exemplo n.º 10
0
        public void TestTargetMustReturnCorrectAssembly()
        {
            // set up model
            LanguageModelAccessorMock model = new LanguageModelAccessorMock();

            // create configuration
            CecilAnalyzerConfiguration configuration = CecilAnalyzerConfiguration.CreateDefaultConfiguration(string.Empty);

            // do weaving
            IILAnalyzer     analyzer = DIHelper.CreateObject <CecilILAnalyzer>(CreateTestContainer(model, configuration));
            AssemblyElement ae       = analyzer.ExtractAllTypes(CreateFullPath("TestTarget.exe"));

            Assert.IsNotNull(ae, "Could not create an AssemblyElement.");
            Assert.IsFalse(string.IsNullOrEmpty(ae.FileName), "Filename is not set.");
            Assert.IsFalse(string.IsNullOrEmpty(ae.Name), "Assembly name is not set.");
            Assert.IsFalse(ae.Timestamp == 0, "Timestamp is not set.");

            Assert.IsTrue(ae.TypeElements.Length > 0, "TypeElements were not retrieved.");
            Assert.IsTrue(ae.TypeElements.Length == 1, "Found {0} TypeElements, expecting 1.", ae.TypeElements.Length);

            TypeElement te = ae.TypeElements[0];

            Assert.IsNotNull(te, "Could not set the TypeElement.");

            Assert.IsFalse(string.IsNullOrEmpty(te.BaseType), "BaseType has not been stored.");
            Assert.IsFalse(string.IsNullOrEmpty(te.FullName), "FullName has not been stored.");
            Assert.IsFalse(string.IsNullOrEmpty(te.Name), "Name has not been stored.");
            Assert.IsFalse(string.IsNullOrEmpty(te.Namespace), "Namespace has not been stored.");

            Assert.IsTrue(te.MethodElements.Length == 2, "Methods not stored in the TypeElement. {0} methods found", te.MethodElements.Length);

            MethodElement me = te.MethodElements[0];

            Assert.IsNotNull(me, "Could not set the methodElement");

            Assert.IsFalse(string.IsNullOrEmpty(me.Name), "Name has not been stored.");
            Assert.IsFalse(string.IsNullOrEmpty(me.ReturnType), "Returntype has not been stored.");
            Assert.IsFalse(string.IsNullOrEmpty(me.Signature), "Signature has not been stored.");
        } // TestTargetMustReturnAssembly()