public void ReturnFragmentType_NonAnnotatedMethod_ReturnsEmptyFragment()
        {
            Method sample         = TestHelper.GetSample <FragmentUtilitySample> ("NoReturnFragment");
            var    returnFragment = FragmentUtility.ReturnFragmentType(sample);

            Assert.That(returnFragment, Is.EqualTo(Fragment.CreateEmpty()));
        }
Esempio n. 2
0
 private bool AnalysisRequired(IMethodGraph methodGraph)
 {
     return(methodGraph.Blocks.Any(
                block =>
                block.PreConditions.Any(
                    preCondition => preCondition.Fragment != Fragment.CreateEmpty() && preCondition.Fragment != Fragment.CreateLiteral())));
 }
        public static Fragment GetFragmentType(AttributeNodeCollection attributes)
        {
            var fragmentType = Fragment.CreateEmpty();

            if (attributes != null && ContainsFragment(attributes))
            {
                string fragmentFullName = typeof(FragmentAttribute).FullName;

                foreach (AttributeNode attributeNode in attributes)
                {
                    if (IsFragment(attributeNode))
                    {
                        if (attributeNode.Type.FullName == fragmentFullName)
                        {
                            foreach (Literal literal in attributeNode.Expressions)
                            {
                                string value = literal.Value as string;
                                if (value != null)
                                {
                                    fragmentType = Fragment.CreateNamed(value);
                                }
                            }
                        }
                        else
                        {
                            fragmentType = Fragment.CreateNamed(attributeNode.Type.Name.Name.Replace("Attribute", ""));
                        }
                    }
                }
            }
            return(fragmentType);
        }
        public void SetUp()
        {
            _problemMetadata = new ProblemMetadata(0, new SourceContext(), Fragment.CreateEmpty(), Fragment.CreateEmpty());
            MockRepository mocks = new MockRepository();

            _blacklistManager = mocks.Stub <IBlacklistManager>();
        }
        public void GetFragmentType_ContainsNonFragmentParameter_ThrowsException()
        {
            TypeNode stringTypeNode   = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample           = TestHelper.GetSample <FragmentUtilitySample>("ContainsNonFragmentParameter", stringTypeNode);
            var      returnedFragment = FragmentUtility.GetFragmentType(sample.Parameters[0].Attributes);

            Assert.That(returnedFragment, Is.EqualTo(Fragment.CreateEmpty()));
        }
        public void IsViolated_EmptyExpectedSymbolUnknown_ReturnsFalse()
        {
            var expectedFragment = Fragment.CreateEmpty();

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

            Assert.That(preCondition.IsViolated(context), Is.False);
        }
        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());
        }
Esempio n. 8
0
        public void InferFragmentType_AssignmentWithNonFragmentField_ReturnsEmptyFragmentType()
        {
            Method sample                        = TestHelper.GetSample <InferFragmentSample>("AssignmentWithNonFragmentField");
            Block  assignmentBlock               = (Block)sample.Body.Statements[0];
            AssignmentStatement assignment       = (AssignmentStatement)assignmentBlock.Statements[1];
            Expression          sampleExpression = assignment.Source;
            var fragmentType                     = _symbolTable.InferFragmentType(sampleExpression);

            Assert.That(fragmentType, Is.EqualTo(Fragment.CreateEmpty()));
        }
Esempio n. 9
0
        public void Parse_ValidReturnWithParameterAssignmentInsideIf_ReturnsCorrectPostCondition()
        {
            TypeNode   stringTypeNode            = IntrospectionUtility.TypeNodeFactory <string>();
            Method     sampleMethod              = TestHelper.GetSample <BlockParserSample> ("ValidReturnWithParameterAssignmentInsideIf", 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.CreateEmpty()));
        }
Esempio n. 10
0
        public void InferFragmentType_UnsupportetCall_ReturnsFragment()
        {
            Method sampleMethod = IntrospectionUtility.MethodFactory <CustomInferenceControllerSample> ("UnsupportetCall");
            Block  sampleBlock  = (Block)sampleMethod.Body.Statements[0];
            ExpressionStatement sampleExpression = (ExpressionStatement)sampleBlock.Statements[1];
            MethodCall          sampleMethodCall = (MethodCall)sampleExpression.Expression;
            Fragment            returnedFragment = _customInferenceController.InferFragmentType(sampleMethodCall, _symbolTable);

            Assert.That(returnedFragment, Is.EqualTo(Fragment.CreateEmpty()));
        }
        public Fragment InferFragmentType(MethodCall methodCall, ISymbolTable context)
        {
            Fragment returnFragment = Fragment.CreateEmpty();
            Method   calleeMethod   = IntrospectionUtility.ExtractMethod(methodCall);

            if (_coveredMethods.Contains(calleeMethod.FullName))
            {
                returnFragment = ParameterFragmentUtility.ParameterFragmentIntersection(methodCall, context);
            }
            return(returnFragment);
        }
Esempio n. 12
0
        public static Fragment GetMemberBindingFragmentType(MemberBinding memberBinding)
        {
            var fragmentType = Fragment.CreateEmpty();

            if (memberBinding.BoundMember is Field)
            {
                Field field = (Field)memberBinding.BoundMember;
                fragmentType = FragmentUtility.GetFragmentType(field.Attributes);
            }
            return(fragmentType);
        }
        public Fragment InferFragmentType(MethodCall methodCall, ISymbolTable context)
        {
            Fragment         fragmentType = Fragment.CreateEmpty();
            Method           calleeMethod = IntrospectionUtility.ExtractMethod(methodCall);
            ICustomInference matchingRule = MatchingInferRule(calleeMethod);

            if (matchingRule != null)
            {
                fragmentType = matchingRule.InferFragmentType(methodCall, context);
            }
            return(fragmentType);
        }
Esempio n. 14
0
        private Fragment FirstOperandsFragmentType(AssignmentStatement assignment, HandleContext context)
        {
            Fragment   fragmentType = Fragment.CreateEmpty();
            Expression firstOperand = FirstOperand(assignment.Source);

            if (firstOperand != null)
            {
                ISymbolTable symbolTable = context.SymbolTable;
                fragmentType = symbolTable.InferFragmentType(firstOperand);
            }
            return(fragmentType);
        }
        public void IsViolated_NamedExpectedEmptyGiven_ReturnsTrue()
        {
            var expectedFragment = Fragment.CreateNamed(c_expectedType);
            var givenFragment    = Fragment.CreateEmpty();

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

            context.MakeSafe(c_symbol, givenFragment);

            Assert.That(preCondition.IsViolated(context), Is.True);
        }
        private Fragment GetDelegateTypesReturnFragment(DelegateNode sourceDelegateType)
        {
            var returnFragment = Fragment.CreateEmpty();

            foreach (Member member in sourceDelegateType.Members)
            {
                if (member.Name.Name == "Invoke")
                {
                    Method invoke = (Method)member;
                    returnFragment = FragmentUtility.ReturnFragmentType(invoke);
                }
            }
            return(returnFragment);
        }
        public Fragment InferFragmentType(MethodCall methodCall, ISymbolTable context)
        {
            Fragment returnFragment = Fragment.CreateEmpty();
            Method   method         = IntrospectionUtility.ExtractMethod(methodCall);

            if (Infers(method) && methodCall.Callee is MemberBinding)
            {
                MemberBinding memberBinding = (MemberBinding)methodCall.Callee;
                string        variableName;
                if (IntrospectionUtility.IsVariable(memberBinding.TargetObject, out variableName))
                {
                    returnFragment = context.GetFragmentType(variableName);
                }
            }
            return(returnFragment);
        }
Esempio n. 18
0
        public static Fragment InferReturnFragmentType(Method method)
        {
            Fragment fragmentType;
            AttributeNodeCollection definingAttributes = GetDefiningAttributes(method);

            if (definingAttributes != null)
            {
                fragmentType = FragmentUtility.GetFragmentType(definingAttributes);
            }
            else
            {
                fragmentType = Fragment.CreateEmpty();
            }

            return(fragmentType);
        }
        public void HandleViolation_ViolationProvoked_SetsSymbolUnsafe()
        {
            var expectedFragment = Fragment.CreateNamed(c_expectedType);
            var givenFragment    = Fragment.CreateNamed("unexpected");

            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 symbolSetUnafe = context.GetFragmentType(c_symbol) == Fragment.CreateEmpty();

            Assert.That(symbolSetUnafe, Is.True);
        }
        protected void MatchFragments(Method baseMethod, Method overridingMethod)
        {
            foreach (var parameter in baseMethod.Parameters)
            {
                var fragmentType = FragmentUtility.GetFragmentType(parameter.Attributes);

                if (fragmentType != Fragment.CreateEmpty())
                {
                    var overriddenParameter    = overridingMethod.Parameters[parameter.ParameterListIndex];
                    var overriddenFragmentType = FragmentUtility.GetFragmentType(overriddenParameter.Attributes);

                    if (overriddenFragmentType != fragmentType && overriddenFragmentType != Fragment.CreateEmpty())
                    {
                        AddProblem(new ProblemMetadata(overridingMethod.UniqueKey, overridingMethod.SourceContext, fragmentType, overriddenFragmentType));
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
Esempio n. 22
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");
            }
        }
Esempio n. 23
0
        private void AddAssignmentPreCondition(AssignmentStatement assignmentStatement, HandleContext context)
        {
            Field    targetField        = IntrospectionUtility.GetField(assignmentStatement.Target);
            Fragment targetFragmentType = FragmentUtility.GetFragmentType(targetField.Attributes);

            if (targetFragmentType != Fragment.CreateEmpty())
            {
                ProblemMetadata problemMetadata = new ProblemMetadata(
                    assignmentStatement.UniqueKey,
                    assignmentStatement.SourceContext,
                    targetFragmentType,
                    Fragment.CreateNamed("??"));
                string sourceSymbol = IntrospectionUtility.GetVariableName(assignmentStatement.Source);
                if (sourceSymbol != null)
                {
                    AssignabilityPreCondition preCondition = new AssignabilityPreCondition(sourceSymbol, targetFragmentType, problemMetadata);
                    context.PreConditions.Add(preCondition);
                }
            }
        }
Esempio n. 24
0
        public static Fragment ReturnFragmentType(Method method)
        {
            ArgumentUtility.CheckNotNull("method", method);
            var    returnFragment = Fragment.CreateEmpty();
            Method determiningMethod;

            Method[] interfaceDeclarations = IntrospectionUtility.InterfaceDeclarations(method);
            if (interfaceDeclarations.Any())
            {
                determiningMethod = interfaceDeclarations.First();
            }
            else
            {
                determiningMethod = method;
            }

            if (determiningMethod.ReturnAttributes != null && ContainsFragment(determiningMethod.ReturnAttributes))
            {
                returnFragment = GetFragmentType(determiningMethod.ReturnAttributes);
            }
            return(returnFragment);
        }
Esempio n. 25
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());
            }
        }
        public void GetResult_ParameterizedMethodSymbolTableBuiltWithOverlappingEnvironment_FragmentFieldIsEmptyFragment()
        {
            Method sampleMethod = TestHelper.GetSample <SymbolTableBuilderSample>("ParameterizedMethod", _floatType, _objectType);
            EmbeddedInitialSymbolTableBuilder embeddedInitialSymbolTableBuilder = new EmbeddedInitialSymbolTableBuilder(sampleMethod, _blacklistManager, _environment);

            _environment.MakeSafe("_fragmentField", Fragment.CreateNamed("ThisShouldBeIgnored"));
            ISymbolTable resultSymbolTable = embeddedInitialSymbolTableBuilder.GetResult();

            Assert.That(resultSymbolTable.GetFragmentType("_fragmentField"), Is.EqualTo(Fragment.CreateEmpty()));
        }
        public void GetResult_ParameterizedMethodSymbolTableBuiltWithEmptyEnvironment_NonFragmentFieldIsEmptyFragment()
        {
            Method sampleMethod = TestHelper.GetSample <SymbolTableBuilderSample>("ParameterizedMethod", _floatType, _objectType);
            EmbeddedInitialSymbolTableBuilder embeddedInitialSymbolTableBuilder = new EmbeddedInitialSymbolTableBuilder(sampleMethod, _blacklistManager, _environment);
            ISymbolTable resultSymbolTable = embeddedInitialSymbolTableBuilder.GetResult();

            Assert.That(resultSymbolTable.GetFragmentType("_nonFragmentField"), Is.EqualTo(Fragment.CreateEmpty()));
        }
Esempio n. 28
0
 public void UndefinedFragmentBehavesLikeEmptyFragment()
 {
     Assert.That(Fragment.CreateUndefined() == Fragment.CreateEmpty(), Is.True);
 }
Esempio n. 29
0
        public void GetResult_ParameterizedMethodSymbolTable_NonFragmentParameterIsEmptyFragment()
        {
            Method sampleMethod = TestHelper.GetSample <SymbolTableBuilderSample>("ParameterizedMethod", _floatType, _objectType);
            InitialSymbolTableBuilder initialSymbolTableBuilder = new InitialSymbolTableBuilder(sampleMethod, _blacklistManager);
            ISymbolTable resultSymbolTable = initialSymbolTableBuilder.GetResult();

            Assert.That(resultSymbolTable.GetFragmentType("nonFragmentParameter"), Is.EqualTo(Fragment.CreateEmpty()));
        }
        public void HandleStatement_InitializationWithEmptyFragment_VariableFragmentTypeIsEmptyFragment()
        {
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StringBuilderConstructStatementHandlerSample> ("InitializationWithEmptyFragment");
            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.CreateEmpty();

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