public void HandleViolation_ProblemMetadataGivenButNoViolation_NoProblemAdded()
        {
            var expectedFragment = Fragment.CreateNamed (c_expectedType);
              var givenFragment = Fragment.CreateNamed (c_expectedType);

              IPreCondition preCondition = new CustomInferencePreCondition(c_symbol, expectedFragment, _problemMetadata);
              var context = new SymbolTable(_blacklistManager);
              context.MakeSafe (c_symbol, givenFragment);
              IProblemPipe problemPipe = MockRepository.GenerateMock<IProblemPipe>();

              preCondition.HandleViolation (context, problemPipe);

              problemPipe.AssertWasNotCalled (pipe => pipe.AddProblem (Arg<ProblemMetadata>.Is.Anything));
        }
        public void IsViolated_ViolatingContext_ChangesProblemMetadatasGivenType()
        {
            var expectedFragment = Fragment.CreateNamed( "expectedFragment");
              var unexpectedFragment = Fragment.CreateNamed( "unexpectedFragment");
              ProblemMetadata problemMetaData = new ProblemMetadata(0, new SourceContext(), expectedFragment, Fragment.CreateNamed( "dummy"));
              IBlacklistManager blackListManager = _mocks.Stub<IBlacklistManager>();
              EqualityPreCondition preCondition = new EqualityPreCondition("testSymbol", expectedFragment, problemMetaData);
              SymbolTable context = new SymbolTable(blackListManager);
              context.MakeSafe("testSymbol", unexpectedFragment);

              preCondition.IsViolated(context);

              Assert.That(problemMetaData.GivenFragment, Is.EqualTo(unexpectedFragment));
        }
        public void Parse_BranchPreconditionsNotViolated_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, 2 };
              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 firstBranch = new BasicBlock (1, 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 secondBranch = new BasicBlock (2, 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, firstBranch, secondBranch});

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

            _methodGraph.GetBasicBlockById (2);
            LastCall.Return (secondBranch);

            _methodGraphBuilder.GetResult();
            LastCall.Return (_methodGraph);
            _parameterSymbolTableBuilder.GetResult();
            LastCall.Return(_methodPreConditions);
              }
              ProblemCollection result = ParseGraph();
              Assert.That (TestHelper.ContainsProblemID (c_InjectionCopRuleId, result), Is.False);
        }
示例#4
0
 public ISymbolTable Copy()
 {
     SymbolTable clone = new SymbolTable (_blacklistManager);
       clone._safenessMap = new Dictionary<string, Fragment> (_safenessMap);
       return clone;
 }
 public void SetUp()
 {
     IBlacklistManager blacklistManager = new IDbCommandBlacklistManagerStub();
       _symbolTable = new SymbolTable (blacklistManager);
 }
 public void SetUp()
 {
     _symbolTable = new SymbolTable(new IDbCommandBlacklistManagerStub());
 }
 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 Parse_SingleNodePreconditionViolated_ReturnsProblem()
        {
            AssignabilityPreCondition[] preConditions = { new AssignabilityPreCondition("y", Fragment.CreateNamed("SqlFragment")) };
              SymbolTable postConditions = new SymbolTable (_blacklistManager);
              int[] successors = (new List<int>()).ToArray();

              BasicBlock node = new BasicBlock (0, preConditions, postConditions, successors, c_EmptyAssignments);

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

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

            SetupResult.For(_methodGraph.Blocks)
                    .Return(new BasicBlock[] { node });

            _methodGraphBuilder.GetResult();
            LastCall.Return (_methodGraph);
            _parameterSymbolTableBuilder.GetResult();
            LastCall.Return(_methodPreConditions);
              }
              ProblemCollection result = ParseGraph();
              Assert.That (TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        public void Parse_SingleNodeNoPrecondition_NoProblems()
        {
            AssignabilityPreCondition[] preConditions = (new List<AssignabilityPreCondition>()).ToArray();
              SymbolTable postConditions = new SymbolTable (_blacklistManager);
              int[] successors = (new List<int>()).ToArray();
              BasicBlock node = new BasicBlock (0, preConditions, postConditions, successors, c_EmptyAssignments);

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

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

            SetupResult.For(_methodGraph.Blocks)
                    .Return(new BasicBlock[] { node });

            _methodGraphBuilder.GetResult();
            LastCall.Return (_methodGraph);
            _parameterSymbolTableBuilder.GetResult();
            LastCall.Return(_methodPreConditions);
              }
              ProblemCollection result = ParseGraph();
              Assert.That (result.Count, Is.EqualTo (0));
        }
        public void Parse_SequenceWithLocalAssignment_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 };
              List<BlockAssignment> localAssignments = new List<BlockAssignment> { new BlockAssignment ("x", "y") };
              BasicBlock initialNode = new BasicBlock (0, preConditions.ToArray(), postConditions, successors.ToArray(), localAssignments.ToArray());

              preConditions = new List<AssignabilityPreCondition> { new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment")), new AssignabilityPreCondition("y", 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);
        }
        public void HandleViolation_ViolationNotProvoked_KeepsSymbolFragment()
        {
            var expectedFragment = Fragment.CreateNamed (c_expectedType);
              var givenFragment = expectedFragment;

              IPreCondition preCondition = new CustomInferencePreCondition(c_symbol, expectedFragment, _problemMetadata);
              var context = new SymbolTable(_blacklistManager);
              context.MakeSafe (c_symbol, givenFragment);
              IProblemPipe problemPipe = MockRepository.GenerateMock<IProblemPipe>();

              preCondition.HandleViolation (context, problemPipe);

              bool symbolFragmentKept = context.GetFragmentType (c_symbol) == expectedFragment;
              Assert.That (symbolFragmentKept, Is.True);
        }
        public void IsViolated_NamedExpectedSymbolUnknown_ReturnsTrue()
        {
            var expectedFragment = Fragment.CreateNamed(c_expectedType);

              IPreCondition preCondition = new CustomInferencePreCondition(c_symbol, expectedFragment, _problemMetadata);
              var context = new SymbolTable(_blacklistManager);

              Assert.That (preCondition.IsViolated (context), Is.True);
        }
        public void IsViolated_NamedExpectedLiteralGiven_ReturnsFalse()
        {
            var expectedFragment = Fragment.CreateNamed (c_expectedType);
              var givenFragment = Fragment.CreateLiteral();

              IPreCondition preCondition = new CustomInferencePreCondition(c_symbol, expectedFragment, _problemMetadata);
              var context = new SymbolTable(_blacklistManager);
              context.MakeSafe (c_symbol, givenFragment);

              Assert.That (preCondition.IsViolated (context), Is.False);
        }