Exemple #1
0
        public void GetBlockById_InvalidId_ThrowsException()
        {
            Method       sampleMethod = TestHelper.GetSample <MethodGraph_ClassSample> ("DeclarationWithReturn");
            IMethodGraph methodGraph  = BuildMethodGraph(sampleMethod);

            methodGraph.GetBasicBlockById(-1);
        }
Exemple #2
0
 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);
     }
 }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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");
            }
        }
Exemple #6
0
        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");
            }
        }
Exemple #7
0
        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");
            }
        }
Exemple #8
0
        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");
            }
        }
Exemple #9
0
        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");
            }
        }
Exemple #10
0
        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");
            }
        }
Exemple #11
0
        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");
            }
        }
Exemple #12
0
        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");
            }
        }
Exemple #13
0
        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");
            }
        }
Exemple #14
0
        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");
            }
        }
Exemple #15
0
        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");
            }
        }
Exemple #16
0
        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");
            }
        }
Exemple #17
0
        public void Parse_SequencePreconditionsNotViolated_NoProblem()
        {
            List <AssignabilityPreCondition> preConditions = new List <AssignabilityPreCondition> {
                new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment"))
            };
            SymbolTable postConditions = new SymbolTable(_blacklistManager);
            List <int>  successors     = new List <int> {
                1
            };
            BasicBlock initialNode = new BasicBlock(0, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments);

            preConditions = new List <AssignabilityPreCondition> {
                new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment"))
            };
            postConditions = new SymbolTable(_blacklistManager);
            successors     = new List <int>();
            BasicBlock terminatingNode = new BasicBlock(1, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments);

            using (_mocks.Record())
            {
                _methodGraph.IsEmpty();
                LastCall.Return(false);

                SetupResult.For(_methodGraph.InitialBlock)
                .Return(initialNode);

                SetupResult.For(_methodGraph.Blocks)
                .Return(new BasicBlock[] { initialNode, terminatingNode });

                _methodGraph.GetBasicBlockById(1);
                LastCall.Return(terminatingNode);

                _methodGraphBuilder.GetResult();
                LastCall.Return(_methodGraph);
                _parameterSymbolTableBuilder.GetResult();
                LastCall.Return(_methodPreConditions);
            }
            ProblemCollection result = ParseGraph();

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
 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);
       }
 }