Example #1
0
        /// <summary>
        /// Pretty prints the path condition of the given execution node.
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private string PrettyPrintPathCondition(IExecutionNode node)
        {
            string output = "";

            try
            {
                if (node.GetPathCondition().Conjuncts.Count != 0)
                {
                    TermEmitter       termEmitter      = new TermEmitter(pathComponent.GetService <TermManager>());
                    SafeStringWriter  safeStringWriter = new SafeStringWriter();
                    IMethodBodyWriter methodBodyWriter = pathComponent.GetService <IPexTestManager>().Language.CreateBodyWriter(safeStringWriter, VisibilityContext.Private, 2000);
                    if (termEmitter.TryEvaluate(node.GetPathCondition().Conjuncts, 2000, methodBodyWriter))
                    {
                        for (int i = 0; i < node.GetPathCondition().Conjuncts.Count - 1; i++)
                        {
                            methodBodyWriter.ShortCircuitAnd();
                        }

                        methodBodyWriter.Statement();

                        output = safeStringWriter.ToString().Remove(safeStringWriter.ToString().Count() - 3);
                    }
                }
            }
            catch (Exception e)
            {
                // TODO : Exception handling?
            }
            return(output);
        }
        protected override void AfterRun(IPexPathComponent host, object data)
        {
            Term[]               pathConditions  = new Term[] {};
            IList <Term>         conditions      = null;
            IList <CodeLocation> locations       = null;
            SafeSet <Method>     trackingMethods = null;

            var database = host.GetService <IssueTrackDatabase>();
            var unInstrumentedMethods = database.UnInstrumentedMethods;

            log = database.SimpleLog;
            foreach (var unInstrumentedMethod in unInstrumentedMethods)
            {
                var controller = new ResultTracer(host, unInstrumentedMethod.Method, log);
                log.AppendLine("try tracking " + unInstrumentedMethod.Method.FullName);
                log.AppendLine("*****************************************************");
                using (IEngine trackingEngine = host.PathServices.TrackingEngineFactory.CreateTrackingEngine(controller))
                {
                    trackingEngine.GetService <IPexTrackingDriver>().Run();
                    pathConditions  = Enumerable.ToArray(controller.PathConditions);
                    conditions      = controller.Conditions;
                    locations       = controller.Locations;
                    trackingMethods = controller.TrackingMethods;
                }
                PexLog(host, "ResultTracing", prettyPrintPathCondition(host, pathConditions));
//                log.AppendLine("condition: " + prettyPrintPathCondition(host, pathConditions));
                PexLog(host, "tracking methods", trackingMethods.Select(x => x.FullName));
                for (int i = 0; i < conditions.Count; i++)
                {
                    var condition = conditions[i];
                    using (var extractor = new ResultTrackConditionExtractor(host.ExplorationServices.TermManager))
                    {
                        extractor.VisitTerm(default(TVoid), condition);
                        if (extractor.Method == null)
                        {
                            host.Log.Dump("method", "not in branch", "null");
                            continue;
                        }
                        PexLog(host, "method", extractor.Method.FullName);

                        PexLog(host, "offset", extractor.CallerOffset.ToString("x"));
                        PexLog(host, "location", extractor.Location.ToString());
                        var method = extractor.Signature as Method;
                        PexLog(host, "signature", method.FullName);
                        log.AppendLine("found method: " + method.FullName + " in branch " + locations[i]);
                        if (!host.GetService <IssueTrackDatabase>().UninstrumentedMethodInBranch.ContainsKey(locations[i]))
                        {
                            host.GetService <IssueTrackDatabase>().UninstrumentedMethodInBranch.Add(locations[i], method);
                        }
                    }
                }

                PexLog(host, "location", locations.Select(x => x.ToString()));
                log.AppendLine("*****************************************************");
            }
            log.AppendLine("=========================");
        }
Example #3
0
        /// <summary>
        /// The method, which is called before each Pex run.
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        protected override object BeforeRun(IPexPathComponent host)
        {
            pathComponent = host;

            var runStorage = host.GetService <PexRunAndTestStorageComponent>();

            return(new Tuple <PexExecutionNodeStorageComponent, PexRunAndTestStorageComponent>(host.GetService <PexExecutionNodeStorageComponent>(), runStorage));
        }
Example #4
0
        /// <summary>
        /// Maps the execution node to source code location string.
        /// </summary>
        /// <param name="host">Host of the Pex Path Component</param>
        /// <param name="node">The execution node to map</param>
        /// <returns>The source code location string in the form of [documentlocation]:[linenumber]</returns>
        private string MapToSourceCodeLocationString(IPexPathComponent host, IExecutionNode node)
        {
            MessageBox.Show("MapToSourceCodeLocationString()");

            try
            {
                var symbolManager = host.GetService <ISymbolManager>();
                var sourceManager = host.GetService <ISourceManager>();
                MethodDefinitionBodyInstrumentationInfo nfo;
                if (node.CodeLocation.Method == null)
                {
                    if (node.InCodeBranch.Method == null)
                    {
                        return(null);
                    }
                    else
                    {
                        node.InCodeBranch.Method.TryGetBodyInstrumentationInfo(out nfo);
                    }
                }
                else
                {
                    node.CodeLocation.Method.TryGetBodyInstrumentationInfo(out nfo);
                }
                SequencePoint point;
                int           targetOffset;
                nfo.TryGetTargetOffset(node.InCodeBranch.BranchLabel, out targetOffset);
                if (symbolManager.TryGetSequencePoint(node.CodeLocation.Method == null ? node.InCodeBranch.Method : node.CodeLocation.Method, node.CodeLocation.Method == null ? targetOffset : node.CodeLocation.Offset, out point))
                {
                    return(point.Document + ":" + point.Line);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                // TODO Exception handling
                return(null);
            }
        }
Example #5
0
        /// <summary>
        /// Gets invoked after each run, which happens for each path in the exploration
        /// </summary>
        /// <param name="host"></param>
        /// <param name="data"></param>
        void IPexPathPackage.AfterRun(IPexPathComponent host, object data)
        {
            if (pathObserver != null)
            {
                pathObserver = host.GetService <IFieldAccessPathObserver>();
            }
            pathObserver.Analyze();


            //aggregate the coverage
            this.pmd.AccumulateMaxCoverage(host.ExplorationServices.Driver.TotalCoverageBuilder);

            //Check whether any uncovered locations are covered and then store the associated sequence
            this.pmd.CheckForNewlyCoveredLocations();
        }
            public override void FrameHasExplicitFailures(int frameId, Method method,
                                                          IEnumerable <object> exceptionObjects)
            {
                Dump("in FrameHasExplicitFailures=====================");
                Dump("frameId: " + frameId + " method: " + method);
                StringBuilder methodsInExceptionBuilder = new StringBuilder("methods: \n");
                List <string> methodsInException        = new List <string>();
                List <string> lines            = new List <string>();
                List <string> exceptionStrings = extractMethods(exceptionObjects, methodsInExceptionBuilder, methodsInException, lines);

                for (int i = 0; i < methodsInException.Count; i++)
                {
                    var exceptionMethod = methodsInException[i];
//                    string fullNameWithParameters = _trackMethod.FullNameWithParameters;
//                    Dump("fullNameWithParameters: " + fullNameWithParameters);
//                    Dump("ShortNameWithParameterNames: " + _trackMethod.ShortNameWithParameterNames);
//                    Dump("ShortNameWithParameters: " + _trackMethod.ShortNameWithParameters);
//                    Dump("ShortTypedNameWithParameters: " + _trackMethod.ShortTypedNameWithParameters);
//                    Dump("ShortTypedPrettyNameWithParameters: " + _trackMethod.ShortTypedPrettyNameWithParameters);
//                    fullNameWithParameters = fullNameWithParameters.Substring(fullNameWithParameters.IndexOf(" ") + 1);
//                    Dump("changed signature: " + fullNameWithParameters);

                    if (exceptionMethod.Equals(_trackMethod.FullName))
                    {
                        Dump("************found external method throw exception: " + _trackMethod);
                        Dump("at " + methodsInException[i + 1] + " line: " + lines[i + 1]);

                        host.GetService <IssueTrackDatabase>().ExceptionExternalMethods.Add(new ExceptionExternalMethod(_trackMethod.FullName, methodsInException[i + 1], lines[i + 1], exceptionStrings[i]));
                    }
                    Dump("method: " + exceptionMethod);
                    Dump("line: " + lines[i]);
                }

                exceptionObjects.ToList().ForEach(x => Dump("type: " + x.GetType() + " " + x.ToString()));
                Dump("end FrameHasExplicitFailures=====================");
                Dump("");
            }
        protected override void AfterRun(IPexPathComponent host, object data)
        {
            Term[] pathConditions = new Term[] {};
            IList<Term> conditions = null;
            IList<CodeLocation> locations = null;
            SafeSet<Method> trackingMethods = null;

            var database = host.GetService<IssueTrackDatabase>();
            var unInstrumentedMethods = database.UnInstrumentedMethods;
            log = database.SimpleLog;
            foreach (var unInstrumentedMethod in unInstrumentedMethods)
            {
                var controller = new ResultTracer(host, unInstrumentedMethod.Method,log);
                log.AppendLine("try tracking " + unInstrumentedMethod.Method.FullName);
                log.AppendLine("*****************************************************");
                using (IEngine trackingEngine = host.PathServices.TrackingEngineFactory.CreateTrackingEngine(controller))
                {
                    trackingEngine.GetService<IPexTrackingDriver>().Run();
                    pathConditions = Enumerable.ToArray(controller.PathConditions);
                    conditions = controller.Conditions;
                    locations = controller.Locations;
                    trackingMethods = controller.TrackingMethods;
                }
                PexLog(host, "ResultTracing", prettyPrintPathCondition(host, pathConditions));
            //                log.AppendLine("condition: " + prettyPrintPathCondition(host, pathConditions));
                PexLog(host, "tracking methods", trackingMethods.Select(x => x.FullName));
                for (int i = 0; i < conditions.Count; i++)
                {
                    var condition = conditions[i];
                    using (var extractor = new ResultTrackConditionExtractor(host.ExplorationServices.TermManager))
                    {
                        extractor.VisitTerm(default(TVoid), condition);
                        if (extractor.Method == null)
                        {
                            host.Log.Dump("method", "not in branch", "null");
                            continue;
                        }
                        PexLog(host, "method", extractor.Method.FullName);

                        PexLog(host, "offset", extractor.CallerOffset.ToString("x"));
                        PexLog(host, "location", extractor.Location.ToString());
                        var method = extractor.Signature as Method;
                        PexLog(host, "signature", method.FullName);
                        log.AppendLine("found method: " + method.FullName + " in branch " + locations[i]);
                        if (!host.GetService<IssueTrackDatabase>().UninstrumentedMethodInBranch.ContainsKey(locations[i]))
                        {
                            host.GetService<IssueTrackDatabase>().UninstrumentedMethodInBranch.Add(locations[i], method);
                        }
                    }
                }

                PexLog(host, "location", locations.Select(x => x.ToString()));
                log.AppendLine("*****************************************************");
            }
            log.AppendLine("=========================");
        }
Example #8
0
        public void AfterRun(IPexPathComponent host, object data)
        {
            MessageBox.Show("AfterRun");

            // Getting the executions nodes in the current path
            var nodesInPath = host.PathServices.CurrentExecutionNodeProvider.ReversedNodes.Reverse().ToArray();

            // Getting the sequence id of the current run
            var runId = host.ExplorationServices.Driver.Runs;

            // Iterating over the nodes in the path
            foreach (var node in nodesInPath)
            {
                var vertex = new CFGNode(node.UniqueIndex, false);

                // Adding the method name this early in order to color edges
                string methodName = null;
                int    offset     = 0;
                if (node.CodeLocation.Method == null)
                {
                    if (node.InCodeBranch.Method != null)
                    {
                        methodName = node.InCodeBranch.Method.FullName;
                    }
                }
                else
                {
                    methodName = node.CodeLocation.Method.FullName;
                    offset     = node.CodeLocation.Offset;
                }
                // Setting the method name
                vertex.MethodName = methodName;

                // Setting the offset
                vertex.ILOffset = (uint)offset;

                var nodeIndex = nodesInPath.ToList().IndexOf(node);
                if (nodeIndex > 0)
                {
                    var prevNode = nodesInPath [nodeIndex - 1];
                    // If there is no edge between the previous and the current node
                    if (!(Edges.ContainsKey(prevNode.UniqueIndex) && Edges [prevNode.UniqueIndex].ContainsKey(node.UniqueIndex)))
                    {
                        var prevVertex = Vertices [prevNode.UniqueIndex];

                        var edge = new CFGEdge(new Random().Next(), prevVertex, vertex);

                        Dictionary <int, CFGEdge> outEdges = null;
                        if (Edges.TryGetValue(prevNode.UniqueIndex, out outEdges))
                        {
                            outEdges.Add(node.UniqueIndex, edge);
                        }
                        else
                        {
                            Edges.Add(prevNode.UniqueIndex, new Dictionary <int, CFGEdge>());
                            Edges [prevNode.UniqueIndex].Add(node.UniqueIndex, edge);
                        }

                        // Edge coloring based on unit border detection
                        if (UnitNamespace != null)
                        {
                            // Checking if pointing into the unit from outside
                            if (!(prevVertex.MethodName.StartsWith(UnitNamespace + ".") || prevVertex.MethodName.Equals(UnitNamespace)) && (vertex.MethodName.StartsWith(UnitNamespace + ".") || vertex.MethodName.Equals(UnitNamespace)))
                            {
                                edge.Color = CFGEdge.EdgeColor.Green;
                            }

                            // Checking if pointing outside the unit from inside
                            if ((prevVertex.MethodName.StartsWith(UnitNamespace + ".") || prevVertex.MethodName.Equals(UnitNamespace)) && !(vertex.MethodName.StartsWith(UnitNamespace + ".") || vertex.MethodName.Equals(UnitNamespace)))
                            {
                                edge.Color = CFGEdge.EdgeColor.Red;
                            }
                        }
                    }
                }

                // If the node is new then it is added to the list and the metadata is filled
                if (!Vertices.ContainsKey(node.UniqueIndex))
                {
                    Vertices.Add(node.UniqueIndex, vertex);

                    // Adding source code mapping
                    vertex.SourceCodeMappingString = MapToSourceCodeLocationString(host, node);

                    // Setting the border based on mapping existence
                    vertex.Border = vertex.SourceCodeMappingString == null ? CFGNode.NodeBorder.Single : CFGNode.NodeBorder.Double;

                    // Setting the color
                    if (nodesInPath.LastOrDefault() == node)
                    {
                        if (!EmittedTestResult.ContainsKey(runId))
                        {
                            vertex.Color = CFGNode.NodeColor.Orange;
                        }
                        else
                        {
                            if (EmittedTestResult [runId].Item1)
                            {
                                vertex.Color = CFGNode.NodeColor.Red;
                            }
                            else
                            {
                                vertex.Color = CFGNode.NodeColor.Green;
                            }
                            vertex.GenerateTestCode = EmittedTestResult [runId].Item2;
                        }
                    }
                    else
                    {
                        vertex.Color = CFGNode.NodeColor.White;
                    }

                    // Setting the default shape
                    vertex.Shape = CFGNode.NodeShape.Rectangle;

                    // Adding path condition tasks and getting the required services
                    TermEmitter       termEmitter      = new TermEmitter(host.GetService <TermManager>());
                    SafeStringWriter  safeStringWriter = new SafeStringWriter();
                    IMethodBodyWriter methodBodyWriter = host.GetService <IPexTestManager>().Language.CreateBodyWriter(safeStringWriter, VisibilityContext.Private, 2000);
                    PrettyPathConditionTasks.Add(vertex.Id, PrettyPrintPathCondition(termEmitter, methodBodyWriter, safeStringWriter, node));

                    // Setting the status
                    vertex.Status = node.ExhaustedReason.ToString();

                    // Collecting the parent nodes for the later incremental path condition calculation
                    if (nodeIndex > 0)
                    {
                        ParentNodes.Add(vertex.Id, nodesInPath [nodeIndex - 1].UniqueIndex);
                    }
                }

                // Adding the Id of the run
                Vertices [node.UniqueIndex].Runs += (runId + ";");
            }
        }
            public override void FrameHasExplicitFailures(int frameId, Method method,
                                                          IEnumerable <object> exceptionObjects)
            {
                Dump("in FrameHasExplicitFailures=====================");
                Dump("frameId: " + frameId + " method: " + method);
                StringBuilder methodsInExceptionBuilder = new StringBuilder("methods: \n");
                List <string> methodsInException        = new List <string>();
                List <string> lines            = new List <string>();
                List <string> exceptionStrings = new List <string>();

                try
                {
                    exceptionStrings = extractMethods(exceptionObjects, methodsInExceptionBuilder,
                                                      methodsInException, lines);
                }
                catch (Exception ex)
                {
                    Dump("ex: in extractMethods of FrameHasExplicitFailures: " + ex);
                }

                if (methodsInException.Count > 0)
                {
                    methodsInException.ForEach(x => Dump("extracted methods: " + x));
                }

                for (int i = 0; i < methodsInException.Count; i++)
                {
                    var exceptionMethod = methodsInException[i];
//                    string fullNameWithParameters = _trackMethod.FullNameWithParameters;
//                    Dump("fullNameWithParameters: " + fullNameWithParameters);
//                    Dump("ShortNameWithParameterNames: " + _trackMethod.ShortNameWithParameterNames);
//                    Dump("ShortNameWithParameters: " + _trackMethod.ShortNameWithParameters);
//                    Dump("ShortTypedNameWithParameters: " + _trackMethod.ShortTypedNameWithParameters);
//                    Dump("ShortTypedPrettyNameWithParameters: " + _trackMethod.ShortTypedPrettyNameWithParameters);
//                    fullNameWithParameters = fullNameWithParameters.Substring(fullNameWithParameters.IndexOf(" ") + 1);
//                    Dump("changed signature: " + fullNameWithParameters);
                    Dump("try test exceptionMethod: " + exceptionMethod + " with tracking method: " +
                         _trackMethod.FullName);
                    if (exceptionMethod.Equals(_trackMethod.FullName))
                    {
                        Dump("************found external method throw exception: " + _trackMethod);
                        try
                        {
                            host.GetService <ProblemTrackDatabase>().ExceptionExternalMethods.Add(
                                new ExceptionExternalMethod(_trackMethod.FullName, methodsInException[i + 1],
                                                            lines[i + 1],
                                                            exceptionStrings[i]));

                            Dump("at " + methodsInException[i + 1] + " line: " + lines[i + 1]);
                        }
                        catch (Exception ex)
                        {
                            Dump("ex" + ex + " showing line number of exception or extrenalmethod " + _trackMethod);
                            Dump("i: " + i + " methodsInException.Count: " + methodsInException.Count + " lines.Count: " + lines.Count);
                        }
                    }
                    else
                    {
                        Dump(exceptionMethod + " is not tracking method: " + _trackMethod.FullName);
                    }
                    Dump("method: " + exceptionMethod);
                    Dump("line: " + lines[i]);
                }

                exceptionObjects.ToList().ForEach(x => Dump("type: " + x.GetType() + " " + x.ToString()));
                Dump("end FrameHasExplicitFailures=====================");
                Dump("");
            }