Beispiel #1
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);
        }
Beispiel #2
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");
        }
Beispiel #3
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");
        }
Beispiel #4
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.");
        }
Beispiel #5
0
        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            base.Execute();
            Log.LogMessageFromResources("WeavingStartText");

            // Get the configuration container
            IEntitiesAccessor      entitiesAccessor = EntitiesAccessor.Instance;
            ConfigurationContainer configContainer  = entitiesAccessor.LoadConfiguration(RepositoryFileName);

            // Set the weave debug level
            CecilWeaverConfiguration.WeaveDebug weaveDebugLevel;
            if (string.IsNullOrEmpty(_weaveDebug))
            {
                weaveDebugLevel = CecilWeaverConfiguration.WeaveDebug.None;
            }
            else
            {
                try
                {
                    weaveDebugLevel = (CecilWeaverConfiguration.WeaveDebug)CecilWeaverConfiguration.WeaveDebug.Parse(typeof(CecilWeaverConfiguration.WeaveDebug), _weaveDebug, true);
                }
                catch (ArgumentException)
                {
                    Log.LogErrorFromResources("CouldNotParseWeaveDebugLevel", _weaveDebug);
                    return(false);
                }
            }

            // For each assembly in the config
            foreach (AssemblyConfig assembly in configContainer.Assemblies)
            {
                // Exclude StarLight ContextInfo assembly from the weaving process
                if (assembly.FileName.EndsWith(ContextInfoFileName))
                {
                    continue;
                }

                // Exclude references
                if (assembly.IsReference)
                {
                    continue;
                }

                // If there is no weaving spec file, then skip
                if (string.IsNullOrEmpty(assembly.WeaveSpecificationFile))
                {
                    Log.LogMessageFromResources("SkippedWeavingFile", assembly.FileName);
                    continue;
                }

                // Check for modification
                if (!ConcernsDirty && File.GetLastWriteTime(assembly.FileName).Ticks <= assembly.Timestamp)
                {
                    // we beter copy the backuped file
                    string backupWeavefile = string.Concat(assembly.FileName, ".weaved");
                    if (File.Exists(backupWeavefile))
                    {
                        File.Copy(backupWeavefile, assembly.FileName, true);
                        Log.LogMessageFromResources("UsingBackupWeaveFile", assembly.FileName);
                        continue;
                    }
                }

                Log.LogMessageFromResources("WeavingFile", assembly.FileName);

                // Preparing config
                CecilWeaverConfiguration configuration = new CecilWeaverConfiguration(assembly, configContainer, weaveDebugLevel);

                if (!String.IsNullOrEmpty(BinFolder))
                {
                    configuration.BinFolder = BinFolder;
                }

                try
                {
                    // Retrieve a weaver instance from the ObjectManager
                    using (IILWeaver weaver = DIHelper.CreateObject <CecilILWeaver>(CreateContainer(entitiesAccessor, configuration)))
                    {
                        // Perform weaving
                        IWeaveResults weaveResults = weaver.DoWeave();

                        // Output the logitems
                        foreach (LogItem item in weaveResults.Log.LogItems)
                        {
                            Log.LogMessageFromText(item.ToString(), MessageImportance.Normal);
                        }

                        // Show information about weaving
                        Log.LogMessageFromResources("WeavingCompleted", weaveResults.WeaveStatistics.TotalWeaveTime.TotalSeconds);
                        switch (configuration.WeaveDebugLevel)
                        {
                        case CecilWeaverConfiguration.WeaveDebug.None:
                            break;

                        case CecilWeaverConfiguration.WeaveDebug.Statistics:
                            ShowWeavingStats(assembly, weaveResults.WeaveStatistics);
                            break;

                        case CecilWeaverConfiguration.WeaveDebug.Detailed:
                            ShowWeavingStats(assembly, weaveResults.WeaveStatistics);

                            // Save instruction log
                            string logFilename    = assembly.FileName + ".weavelog.txt";
                            string timingFilename = assembly.FileName + ".weavetiming.txt";

                            weaveResults.WeaveStatistics.SaveInstructionsLog(logFilename);
                            weaveResults.WeaveStatistics.SaveTimingLog(timingFilename);

                            Log.LogMessageFromResources("WeavingInstructionsLogSaved", logFilename);
                            Log.LogMessageFromResources("WeavingTimingLogSaved", timingFilename);
                            break;

                        default:
                            break;
                        }
                    }
                }
                catch (ILWeaverException ex)
                {
                    //Log.LogErrorFromException(ex, true);
                    string errorMessage = ex.Message;
                    string stackTrace   = ex.StackTrace;

                    Exception innerException = ex.InnerException;
                    while (innerException != null)
                    {
                        errorMessage   = string.Concat(errorMessage, "; ", innerException.Message);
                        stackTrace     = string.Concat(innerException.StackTrace, stackTrace);
                        innerException = innerException.InnerException;
                    }
                    Log.LogErrorFromResources("WeaverException", errorMessage);

                    // Only show stacktrace when debugging is enabled
                    if (weaveDebugLevel != CecilWeaverConfiguration.WeaveDebug.None)
                    {
                        Log.LogErrorFromResources("WeaverExceptionStackTrace", stackTrace);
                    }
                }
                catch (ArgumentException ex)
                {
                    Log.LogErrorFromException(ex, true);
                }
                catch (BadImageFormatException ex)
                {
                    Log.LogErrorFromException(ex, false);
                }
            }

            return(!Log.HasLoggedErrors);
        }