private bool AnalysisRequired(IMethodGraph methodGraph) { return(methodGraph.Blocks.Any( block => block.PreConditions.Any( preCondition => preCondition.Fragment != Fragment.CreateEmpty() && preCondition.Fragment != Fragment.CreateLiteral()))); }
public void GetBlockById_InvalidId_ThrowsException() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); methodGraph.GetBasicBlockById(-1); }
private bool AnalysisRequired(IMethodGraph methodGraph) { return methodGraph.Blocks.Any ( block => block.PreConditions.Any ( preCondition => preCondition.Fragment != Fragment.CreateEmpty() && preCondition.Fragment != Fragment.CreateLiteral())); }
public void IsEmpty_MethodNonAnnotated_ReturnsTrue() { TypeNode objectTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(object)); TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(string)); Method sampleMethod = IntrospectionUtility.MethodFactory(typeof(InterfaceSample), "MethodNonAnnotated", objectTypeNode, stringTypeNode); IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); //Assert.That (methodGraph.IsEmpty(), Is.True); Assert.That(methodGraph, Is.Null); }
protected IMethodGraph BuildMethodGraph(Method method) { IProblemPipe problemPipe = new TypeParser(); IBlacklistManager blacklistManager = new IDbCommandBlacklistManagerStub(); IMethodGraphBuilder methodGraphBuilder = new MethodGraphBuilder(method, blacklistManager, problemPipe); methodGraphBuilder.Build(); IMethodGraph methodGraph = methodGraphBuilder.GetResult(); return(methodGraph); }
public void GetInitialBlockId_DeclarationWithReturn_ReturnsIdOfInitialBlock() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block initialBlock = sampleMethod.Body.Statements[0] as Block; if (initialBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); Assert.That(methodGraph.InitialBlock.Id, Is.EqualTo(initialBlock.UniqueKey)); } }
public void MethodGraph_TryCatchFinally_FinallyBlockIsParsed() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("TryCatchFinally"); TryNode outerTryNode = (TryNode)sampleMethod.Body.Statements[1]; Block finallyBlock = outerTryNode.Finally.Block; int finallyBlockUniqueKey = finallyBlock.UniqueKey; IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock finallyBasicBlock = methodGraph.GetBasicBlockById(finallyBlockUniqueKey); Assert.That(finallyBasicBlock, Is.Not.Null); }
private void Parse(IMethodGraph methodGraph, ISymbolTable context, BasicBlock currentBlock, Dictionary <int, int> visits) { UpdateVisits(currentBlock.Id, visits); bool loopIterationsExceeded = visits[currentBlock.Id] > 1; if (!loopIterationsExceeded) { CheckPreConditions(currentBlock.PreConditions, context); ISymbolTable adjustedContext = UpdateContext(context, currentBlock.PostConditionSymbolTable, currentBlock.BlockAssignments); ParseSuccessors(currentBlock.SuccessorKeys, visits, methodGraph, adjustedContext); } }
public void MethodGraph_TryCatchFinally_CatchBlockIsParsed() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("TryCatchFinally"); TryNode outerTryNode = (TryNode)sampleMethod.Body.Statements[1]; TryNode innerTryNode = (TryNode)outerTryNode.Block.Statements[0]; CatchNode catchNode = innerTryNode.Catchers[0]; int catchBlockUniqueKey = catchNode.Block.UniqueKey; IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock catchBasicBlock = methodGraph.GetBasicBlockById(catchBlockUniqueKey); Assert.That(catchBasicBlock, Is.Not.Null); }
public void SetUp() { _blacklistManager = new IDbCommandBlacklistManagerStub(); _methodPreConditions = new SymbolTable(_blacklistManager); _methodPreConditions.MakeSafe("x", Fragment.CreateNamed("SqlFragment")); _methodPreConditions.MakeSafe("l", Fragment.CreateLiteral()); _methodPreConditions.MakeUnsafe("y"); _problemPipeStub = new ProblemPipeStub(); _methodGraphAnalyzer = new MethodGraphAnalyzer(_problemPipeStub); _mocks = new MockRepository(); _methodGraph = _mocks.Stub <IMethodGraph>(); _methodGraphBuilder = _mocks.Stub <IMethodGraphBuilder>(); _parameterSymbolTableBuilder = _mocks.Stub <IInitialSymbolTableBuilder>(); }
public void MethodGraph_DeclarationWithReturn_ReturnBlockHasNoSuccessors() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block returnBlock = sampleMethod.Body.Statements[1] as Block; if (returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock returnBasicBlock = methodGraph.GetBasicBlockById(returnBlock.UniqueKey); Assert.That(returnBasicBlock.SuccessorKeys.Length, Is.EqualTo(0)); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_ForLoop_ReturnsCorrectReturnSuccessors() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("ForLoop"); Block returnBlock = sampleMethod.Body.Statements[4] as Block; if (returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock returnBasicBlock = methodGraph.GetBasicBlockById(returnBlock.UniqueKey); Assert.That(returnBasicBlock.SuccessorKeys.Length, Is.EqualTo(0)); } else { Assert.Ignore("Bad Sample"); } }
public void GetBlockById_FirstBlockIdOfDeclarationWithReturnSample_ReturnsInitialBasicBlock() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block initialBlock = sampleMethod.Body.Statements[0] as Block; if (initialBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock initialBasicBlock = methodGraph.GetBasicBlockById(initialBlock.UniqueKey); Assert.That(initialBasicBlock.Id, Is.EqualTo(initialBlock.UniqueKey)); } else { Assert.Ignore("Bad Sample"); } }
public void Build() { if (_result == null) { BlockParser parser = new BlockParser(_blacklistManager, _problemPipe, _returnFragment, _referenceAndOutConditions); Dictionary <int, BasicBlock> graph = new Dictionary <int, BasicBlock>(); int initialBlockId; List <Block> blockList = GetBlocks(_methodBody.Statements); using (var blocksEnumerator = blockList.GetEnumerator()) { if (!blocksEnumerator.MoveNext()) { return; } var currentBlock = blocksEnumerator.Current; // ReSharper disable PossibleNullReferenceException initialBlockId = currentBlock.UniqueKey; // ReSharper restore PossibleNullReferenceException BasicBlock currentBasicBlock; while (blocksEnumerator.MoveNext()) { var nextBlock = blocksEnumerator.Current; if (ContainsUnconditionalBranch(currentBlock)) { currentBasicBlock = parser.Parse(currentBlock); } else { // ReSharper disable PossibleNullReferenceException currentBasicBlock = parser.Parse(currentBlock, nextBlock.UniqueKey); // ReSharper restore PossibleNullReferenceException } graph.Add(currentBasicBlock.Id, currentBasicBlock); currentBlock = nextBlock; } currentBasicBlock = parser.Parse(currentBlock); graph.Add(currentBasicBlock.Id, currentBasicBlock); } _result = new MethodGraph(initialBlockId, graph); } }
public void MethodGraph_DeclarationWithReturn_ReturnBlockHasReturnFragmentPrecondition() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block returnBlock = sampleMethod.Body.Statements[1] as Block; if (returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock returnBasicBlock = methodGraph.GetBasicBlockById(returnBlock.UniqueKey); var preConditionFragmentType = returnBasicBlock.PreConditions[0].Fragment; Assert.That(preConditionFragmentType, Is.EqualTo(Fragment.CreateNamed("ReturnFragmentType"))); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_IfStatementTrueBlockOnly_ReturnsCorrectReturnSuccessors() { TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>(); Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("IfStatementTrueBlockOnly", stringTypeNode); Block returnBlock = sampleMethod.Body.Statements[3] as Block; if (returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock preReturnBasicBlock = methodGraph.GetBasicBlockById(returnBlock.UniqueKey); Assert.That(preReturnBasicBlock.SuccessorKeys.Length, Is.EqualTo(0)); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_DeclarationWithReturn_ReturnsCorrectPostConditions() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block initialBlock = sampleMethod.Body.Statements[0] as Block; if (initialBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock initialBasicBlock = methodGraph.GetBasicBlockById(initialBlock.UniqueKey); var postConditionFragmentType = initialBasicBlock.PostConditionSymbolTable.GetFragmentType("local$0"); Assert.That(postConditionFragmentType, Is.EqualTo(Fragment.CreateLiteral())); } else { Assert.Ignore("Bad Sample"); } }
public MethodGraphBuilder(Method method, IBlacklistManager blacklistManager, IProblemPipe problemPipe) { ArgumentUtility.CheckNotNull("method", method); _blacklistManager = ArgumentUtility.CheckNotNull("blacklistManager", blacklistManager); _problemPipe = ArgumentUtility.CheckNotNull("problemPipe", problemPipe); bool isInterfaceMethod = method.Body.Statements == null; if (!isInterfaceMethod) { _methodBody = method.Body; _referenceAndOutConditions = ReferenceAndOutConditions(method); _result = null; } else { _result = new MethodGraph(-1, new Dictionary <int, BasicBlock>()); } _returnFragment = FragmentUtility.ReturnFragmentType(method); }
public void MethodGraph_DeclarationWithReturn_ReturnsCorrectInitialBlockSuccessors() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn"); Block initialBlock = sampleMethod.Body.Statements[0] as Block; Block returnBlock = sampleMethod.Body.Statements[1] as Block; if (initialBlock != null && returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock initialBasicBlock = methodGraph.GetBasicBlockById(initialBlock.UniqueKey); bool initialBasicBlockConnectedWithReturn = initialBasicBlock.SuccessorKeys.Any(key => key == returnBlock.UniqueKey); Assert.That(initialBasicBlockConnectedWithReturn, Is.True); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_ValidReturnWithIf_ReturnsCorrectPostConditions() { TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>(); Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("ValidReturnWithIf", stringTypeNode); Block preReturnBlock = sampleMethod.Body.Statements[2] as Block; if (preReturnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock preReturnBasicBlock = methodGraph.GetBasicBlockById(preReturnBlock.UniqueKey); var postConditionFragmentType = preReturnBasicBlock.PostConditionSymbolTable.GetFragmentType("local$1"); Assert.That(postConditionFragmentType, Is.EqualTo(Fragment.CreateEmpty())); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_IfStatementTrueBlockOnly_ReturnsCorrectTrueBlockSuccessors() { TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>(); Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("IfStatementTrueBlockOnly", stringTypeNode); Block trueBlock = sampleMethod.Body.Statements[1] as Block; Block preReturnBlock = sampleMethod.Body.Statements[2] as Block; if (preReturnBlock != null && trueBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock trueBasicBlock = methodGraph.GetBasicBlockById(trueBlock.UniqueKey); bool trueBasicBlockSuccessorsOk = trueBasicBlock.SuccessorKeys.Any(key => key == preReturnBlock.UniqueKey); Assert.That(trueBasicBlockSuccessorsOk, Is.True); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_ForLoop_ReturnsCorrectPreReturnSuccessors() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("ForLoop"); Block preReturnBlock = sampleMethod.Body.Statements[3] as Block; Block returnBlock = sampleMethod.Body.Statements[4] as Block; if (preReturnBlock != null && returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock preReturnBasicBlock = methodGraph.GetBasicBlockById(preReturnBlock.UniqueKey); bool preReturnBasicBlockSuccessorsCorrect = preReturnBasicBlock.SuccessorKeys.Length == 1 && preReturnBasicBlock.SuccessorKeys[0] == returnBlock.UniqueKey; Assert.That(preReturnBasicBlockSuccessorsCorrect, Is.True); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_ForLoop_ReturnsCorrectInnerForSuccessors() { Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("ForLoop"); Block innerForBlock = sampleMethod.Body.Statements[1] as Block; Block conditionBlock = sampleMethod.Body.Statements[2] as Block; if (innerForBlock != null && conditionBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock innerForBasicBlock = methodGraph.GetBasicBlockById(innerForBlock.UniqueKey); bool innerForBasicBlockSuccessorsCorrect = innerForBasicBlock.SuccessorKeys.Length == 1 && innerForBasicBlock.SuccessorKeys[0] == conditionBlock.UniqueKey; Assert.That(innerForBasicBlockSuccessorsCorrect, Is.True); } else { Assert.Ignore("Bad Sample"); } }
public void MethodGraph_FragmentRefParameterSafeReturn_AddsPreconditionToReturnBlock() { TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>(); Method sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("FragmentRefParameterSafeReturn", stringTypeNode.GetReferenceType()); Block returnBlock = sampleMethod.Body.Statements[2] as Block; if (returnBlock != null) { IMethodGraph methodGraph = BuildMethodGraph(sampleMethod); BasicBlock returnBasicBlock = methodGraph.GetBasicBlockById(returnBlock.UniqueKey); string preConditionSymbolName = returnBasicBlock.PreConditions[0].Symbol; var preConditionFragmentType = returnBasicBlock.PreConditions[0].Fragment; bool correctPreCondition = preConditionSymbolName == "safe" && preConditionFragmentType == Fragment.CreateNamed("SqlFragment"); Assert.That(correctPreCondition, Is.True); } else { Assert.Ignore("Bad Sample"); } }
private void Parse(IMethodGraph methodGraph, ISymbolTable context, BasicBlock currentBlock, Dictionary<int, int> visits) { UpdateVisits (currentBlock.Id, visits); bool loopIterationsExceeded = visits[currentBlock.Id] > 1; if(!loopIterationsExceeded) { CheckPreConditions (currentBlock.PreConditions, context); ISymbolTable adjustedContext = UpdateContext (context, currentBlock.PostConditionSymbolTable, currentBlock.BlockAssignments); ParseSuccessors (currentBlock.SuccessorKeys, visits, methodGraph, adjustedContext); } }
public void SetUp() { _blacklistManager = new IDbCommandBlacklistManagerStub(); _methodPreConditions = new SymbolTable (_blacklistManager); _methodPreConditions.MakeSafe ("x", Fragment.CreateNamed("SqlFragment")); _methodPreConditions.MakeSafe ("l", Fragment.CreateLiteral()); _methodPreConditions.MakeUnsafe ("y"); _problemPipeStub = new ProblemPipeStub(); _methodGraphAnalyzer = new MethodGraphAnalyzer (_problemPipeStub); _mocks = new MockRepository(); _methodGraph = _mocks.Stub<IMethodGraph>(); _methodGraphBuilder = _mocks.Stub<IMethodGraphBuilder>(); _parameterSymbolTableBuilder = _mocks.Stub<IInitialSymbolTableBuilder>(); }
private void ParseSuccessors(int[] successorKeys, Dictionary <int, int> visits, IMethodGraph methodGraph, ISymbolTable adjustedContext) { foreach (int successorKey in successorKeys) { Dictionary <int, int> branchVisits = new Dictionary <int, int> (visits); BasicBlock successor = methodGraph.GetBasicBlockById(successorKey); Parse(methodGraph, adjustedContext, successor, branchVisits); } }
private void ParseSuccessors(int[] successorKeys, Dictionary<int, int> visits, IMethodGraph methodGraph, ISymbolTable adjustedContext) { foreach (int successorKey in successorKeys) { Dictionary<int, int> branchVisits = new Dictionary<int, int> (visits); BasicBlock successor = methodGraph.GetBasicBlockById (successorKey); Parse (methodGraph, adjustedContext, successor, branchVisits); } }