Exemplo n.º 1
0
        public static bool FragmentTypesAssignable(Fragment givenFragmentType, Fragment targetFragmentType)
        {
            ArgumentUtility.CheckNotNull("givenFragmentType", givenFragmentType);
            ArgumentUtility.CheckNotNull("targetFragmentType", targetFragmentType);

            return(targetFragmentType == givenFragmentType ||
                   targetFragmentType == Fragment.CreateEmpty() ||
                   givenFragmentType == Fragment.CreateLiteral());
        }
        public void IsViolated_LiteralExpectedSymbolUnknown_ReturnsTrue()
        {
            var expectedFragment = Fragment.CreateLiteral();

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

            Assert.That(preCondition.IsViolated(context), Is.True);
        }
Exemplo n.º 3
0
        public void Parse_ValidReturnWithParameterResetAndAssignmentInsideIf_ReturnsCorrectPostCondition()
        {
            TypeNode   stringTypeNode            = IntrospectionUtility.TypeNodeFactory <string>();
            Method     sampleMethod              = TestHelper.GetSample <BlockParserSample> ("ValidReturnWithParameterResetAndAssignmentInsideIf", stringTypeNode);
            Block      ifBlock                   = sampleMethod.Body.Statements[1] as Block;
            BasicBlock ifBasicBlock              = _blockParser.Parse(ifBlock);
            var        postConditionFragmentType = ifBasicBlock.PostConditionSymbolTable.GetFragmentType("local$0");

            Assert.That(postConditionFragmentType, Is.EqualTo(Fragment.CreateLiteral()));
        }
        public void IsViolated_LiteralExpectedLiteralGiven_ReturnsFalse()
        {
            var expectedFragment = Fragment.CreateLiteral();
            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);
        }
Exemplo n.º 5
0
 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>();
 }
Exemplo n.º 6
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");
            }
        }
        public void Analyze(MethodCall methodCall, ISymbolTable context, List <IPreCondition> preConditions)
        {
            Method method = IntrospectionUtility.ExtractMethod(methodCall);

            if (Analyzes(method) && methodCall.Callee is MemberBinding)
            {
                MemberBinding memberBinding = (MemberBinding)methodCall.Callee;

                if (IsFragmentParameterInferenceMethod(method))
                {
                    string variableName;
                    if (IntrospectionUtility.IsVariable(memberBinding.TargetObject, out variableName))
                    {
                        Fragment parameterFragment    = ParameterFragmentUtility.ParameterFragmentIntersection(methodCall, context);
                        Fragment targetObjectFragment = context.GetFragmentType(variableName);
                        if (targetObjectFragment == Fragment.CreateLiteral() || targetObjectFragment.Undefined)
                        {
                            context.MakeSafe(variableName, parameterFragment);
                        }
                        else
                        {
                            if (targetObjectFragment == Fragment.CreateEmpty() && parameterFragment != Fragment.CreateLiteral()) // && parameterFragment != Fragment.CreateEmpty()
                            {
                                ProblemMetadata problemMetadata = new ProblemMetadata(methodCall.UniqueKey, methodCall.SourceContext, parameterFragment, targetObjectFragment);
                                IPreCondition   precondition    = new CustomInferencePreCondition(variableName, parameterFragment, problemMetadata);
                                preConditions.Add(precondition);
                            }
                            else if (!FragmentUtility.FragmentTypesAssignable(parameterFragment, targetObjectFragment))
                            {
                                context.MakeUnsafe(variableName);
                            }
                        }
                    }
                }
                else if (!IsSafeMethod(method))
                {
                    string variableName;
                    if (IntrospectionUtility.IsVariable(memberBinding.TargetObject, out variableName))
                    {
                        context.MakeUnsafe(variableName);
                    }
                }
            }
        }
        private void InferArrayFragment(AssignmentStatement assignmentStatement, string targetName, HandleContext context)
        {
            ISymbolTable symbolTable        = context.SymbolTable;
            Fragment     targetFragmentType = symbolTable.InferFragmentType(assignmentStatement.Source);

            if (context.ArrayFragmentTypeDefined[targetName] == false)
            {
                symbolTable.MakeSafe(targetName, targetFragmentType);
                context.ArrayFragmentTypeDefined[targetName] = true;
            }
            else if (symbolTable.GetFragmentType(targetName) == Fragment.CreateLiteral())
            {
                symbolTable.MakeSafe(targetName, targetFragmentType);
            }
            else if (symbolTable.GetFragmentType(targetName) != targetFragmentType && targetFragmentType != Fragment.CreateLiteral())
            {
                symbolTable.MakeUnsafe(targetName);
            }
        }
Exemplo n.º 9
0
        private static Fragment FragmentIntersection(Fragment fragmentA, Fragment fragmentB)
        {
            bool fragmentAIsSuperior = fragmentA != fragmentB && fragmentA == Fragment.CreateLiteral();
            bool fragmentBIsSuperior = fragmentA != fragmentB && fragmentB == Fragment.CreateLiteral();

            if (fragmentA == fragmentB)
            {
                return(fragmentA);
            }
            else if (fragmentAIsSuperior)
            {
                return(fragmentB);
            }
            else if (fragmentBIsSuperior)
            {
                return(fragmentA);
            }
            else
            {
                return(Fragment.CreateEmpty());
            }
        }
Exemplo n.º 10
0
 private bool AnalysisRequired(IMethodGraph methodGraph)
 {
     return(methodGraph.Blocks.Any(
                block =>
                block.PreConditions.Any(
                    preCondition => preCondition.Fragment != Fragment.CreateEmpty() && preCondition.Fragment != Fragment.CreateLiteral())));
 }
        public void HandleStatement_InitializationWithLiteralAndInt_VariableFragmentTypeIsLiteral()
        {
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StringBuilderConstructStatementHandlerSample> ("InitializationWithLiteralAndInt");
            Block     sampleBlock  = (Block)sampleMethod.Body.Statements[0];
            Statement sample       = sampleBlock.Statements[1];

            HandleContext context = new HandleContext(
                sample,
                _symbolTable,
                new List <IPreCondition>(),
                new List <string>(),
                new List <BlockAssignment>(),
                new List <int>(),
                new Dictionary <string, bool>(),
                _stringBuilderFragmentTypesDefined);

            _handler.Handle(context);

            bool symbolTableEntryCorrect = _symbolTable.GetFragmentType("local$0") == Fragment.CreateLiteral();

            Assert.That(symbolTableEntryCorrect, Is.True);
        }
Exemplo n.º 12
0
 public void DefinedFragmentIsNotUndefined()
 {
     Assert.That(Fragment.CreateLiteral().Undefined, Is.False);
 }
Exemplo n.º 13
0
 public void UndefinedFragmentIsNoLiteral()
 {
     Assert.That(Fragment.CreateUndefined() == Fragment.CreateLiteral(), Is.False);
 }
Exemplo n.º 14
0
        public void Parse_DeclarationWithReturn_ReturnsCorrectPostConditions()
        {
            Method     sampleMethod          = TestHelper.GetSample <BlockParserSample> ("DeclarationWithReturn");
            Block      initialBlock          = sampleMethod.Body.Statements[0] as Block;
            BasicBlock initialBasicBlock     = _blockParser.Parse(initialBlock);
            var        local0FragmentType    = initialBasicBlock.PostConditionSymbolTable.GetFragmentType("local$0");
            var        local1FragmentType    = initialBasicBlock.PostConditionSymbolTable.GetFragmentType("local$1");
            bool       correctPostConditions = local0FragmentType == local1FragmentType && local0FragmentType == Fragment.CreateLiteral();

            Assert.That(correctPostConditions, Is.True);
        }