Пример #1
0
        public void Parse_UnsafeCallOnMethodInheritedFromSuperiorClass_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleMethod>();

            Microsoft.FxCop.Sdk.Method sample = IntrospectionUtility.MethodFactory(sampleTypeNode, "UnsafeCallOnMethodInheritedFromSuperiorClass");
            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
Пример #2
0
        public void Parse_SafeStaticBindingOnNewMethod_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleMethod>();

            Microsoft.FxCop.Sdk.Method sample = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeStaticBindingOnNewMethod");
            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void Parse_SafeCallWithInheritedProperty_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleProperty>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeCallWithInheritedProperty");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
Пример #4
0
        public void Parse_UnsafeDynamicBinding_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleMethod>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "UnsafeDynamicBinding");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        public void Parse_UnsafeAssignmentOnPropertyHidingParent_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleProperty>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "UnsafeAssignmentOnPropertyHidingParent");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void Parse_SafeCallOnClassImplementingInterfaceMethodWithFragmentParameter_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleInterface>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeCallOnClassImplementingInterfaceMethodWithFragmentParameter");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void Parse_InterfaceReturnFragmentsOfExplicitlyDeclaredMethodAreConsidered_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleInterface>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "InterfaceReturnFragmentsOfExplicitlyDeclaredMethodAreConsidered");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
Пример #8
0
        public void Parse_InvalidReturnWithIfFragmentTypeConsidered_NoProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample         = TestHelper.GetSample <AssignmentPropagationSample> ("InvalidReturnWithIfFragmentTypeConsidered", stringTypeNode);

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
Пример #9
0
        public void Parse_ValidReturnWithParameterReset_NoProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample         = TestHelper.GetSample <AssignmentPropagationSample> ("ValidReturnWithParameterReset", stringTypeNode);

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void Parse_InvalidReturnOnExplicitInterfaceMethod_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(InterfaceSampleExplicitDeclarationsInvalidReturn));
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "InjectionCop.IntegrationTests.Parser.TypeParsing.TypeParserTests.Inheritance.Interface.IInheritanceSample.MethodWithReturnFragment");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        public void Parse_InvalidReturnOnImplicitExtendedInterfaceMethod_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(ExtendedInterfaceSampleImplicitDeclarationsInvalidReturn));
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "MethodWithReturnFragment");

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        public void GetField_NonFieldAssignment_ReturnsNull()
        {
            Method sample          = TestHelper.GetSample <IntrospectionUtility_ClassSample> ("NonFieldAssignment");
            Block  assignmentBlock = (Block)sample.Body.Statements[0];
            AssignmentStatement assignmentStatement = (AssignmentStatement)assignmentBlock.Statements[1];
            Expression          sampleExpression    = assignmentStatement.Target;
            Field extractedField    = IntrospectionUtility.GetField(sampleExpression);
            bool  extractionCorrect = extractedField == null;

            Assert.That(extractionCorrect, Is.True);
        }
Пример #13
0
        public void Parse_UnsafePreCondition()
        {
            TypeNode   stringTypeNode      = IntrospectionUtility.TypeNodeFactory <string>();
            Method     sampleMethod        = TestHelper.GetSample <BlockParserSample> ("UnsafePreCondition", stringTypeNode);
            Block      sample              = sampleMethod.Body.Statements[0] as Block;
            BasicBlock basicBlock          = _blockParser.Parse(sample);
            bool       correctPreCondition = basicBlock.PreConditions[0].Symbol == "unSafe" &&
                                             basicBlock.PreConditions[0].Fragment == Fragment.CreateNamed("SqlFragment");

            Assert.That(correctPreCondition, Is.True);
        }
Пример #14
0
        public void Parse_RefReturnPreconditionCheckSafeLiteralAssignment_NoProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sampleMethod   = TestHelper.GetSample <BlockParserSample> ("RefReturnPreconditionCheckSafeLiteralAssignment", stringTypeNode.GetReferenceType());
            Block    sample         = sampleMethod.Body.Statements[0] as Block;

            _blockParser.Parse(sample);
            bool noProblemsArised = _problemPipeStub.Problems.Count == 0;

            Assert.That(noProblemsArised, Is.True);
        }
Пример #15
0
        public void Parse_OutReturnPreconditionCheckUnSafe_ReturnsProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sampleMethod   = TestHelper.GetSample <BlockParserSample> ("OutReturnPreconditionCheckUnSafe", stringTypeNode.GetReferenceType());
            Block    sample         = sampleMethod.Body.Statements[0] as Block;

            _blockParser.Parse(sample);
            bool problemFound = _problemPipeStub.Problems.Count != 0;

            Assert.That(problemFound, Is.True);
        }
Пример #16
0
        public void Parse_ValidReturnWithIf_ReturnsCorrectReturnFragmentType()
        {
            _blockParser = new BlockParser(_blacklist, _problemPipeStub, Fragment.CreateNamed("DummyFragment"), new List <ReturnCondition>());
            TypeNode   stringTypeNode           = IntrospectionUtility.TypeNodeFactory <string>();
            Method     sampleMethod             = TestHelper.GetSample <BlockParserSample> ("ValidReturnWithLiteralAssignmentInsideIf", stringTypeNode);
            Block      sample                   = sampleMethod.Body.Statements[3] as Block;
            BasicBlock returnedBlock            = _blockParser.Parse(sample);
            var        preConditionFragmentType = returnedBlock.PreConditions[0].Fragment;

            Assert.That(preConditionFragmentType, Is.EqualTo(Fragment.CreateNamed("DummyFragment")));
        }
        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);
        }
        public void Check_MatchesParametersCorrectly()
        {
            var rule    = new OverridingMethodFragmentUsageRule();
            var method1 = IntrospectionUtility.MethodFactory <DerivedClassWithInvalidFragmentUsage> (
                "MethodWithFragmentOnSecondParameter", IntrospectionUtility.TypeNodeFactory <int>(), IntrospectionUtility.TypeNodeFactory <int>());
            var method = method1;

            var result = rule.Check(method);

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
Пример #19
0
        public void MethodFactory_MethodWithParametersCorrectCall_ReturnsMethod()
        {
            Type     sampleType       = typeof(IntrospectionUtility_InterfaceSample);
            TypeNode intTypeNode      = IntrospectionUtility.TypeNodeFactory <int>();
            string   sampleMethodname = "MethodWithParameter";
            Method   result           = IntrospectionUtility.MethodFactory(sampleType, sampleMethodname, intTypeNode);
            bool     correctType      = result.DeclaringType.FullName == sampleType.FullName;
            bool     correctMethod    = result.Name.Name == sampleMethodname;

            Assert.That(correctMethod && correctType, Is.True);
        }
Пример #20
0
        private void AddBlockAssignment(AssignmentStatement assignmentStatement, HandleContext context)
        {
            string targetSymbol = IntrospectionUtility.GetVariableName(assignmentStatement.Target);
            string sourceSymbol = IntrospectionUtility.GetVariableName(assignmentStatement.Source);

            if (targetSymbol != null && sourceSymbol != null)
            {
                BlockAssignment blockAssignment = new BlockAssignment(sourceSymbol, targetSymbol);
                context.BlockAssignments.Add(blockAssignment);
            }
        }
        public void IsVariable_Method_IsFalse()
        {
            Method sample          = TestHelper.GetSample <IntrospectionUtility_ClassSample>("UsingField");
            Block  expressionBlock = (Block)sample.Body.Statements[0];
            ExpressionStatement expressionStatement   = (ExpressionStatement)expressionBlock.Statements[1];
            UnaryExpression     sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
            MethodCall          sampleMethodCall      = (MethodCall)sampleUnaryExpression.Operand;
            bool methodIsVariable = IntrospectionUtility.IsVariable(sampleMethodCall);

            Assert.That(methodIsVariable, Is.False);
        }
Пример #22
0
        public void Parse_UnsafeAssignmentInsideIfNested_ReturnsProblem()
        {
            TypeNode intTypeNode = IntrospectionUtility.TypeNodeFactory <int>();
            Method   sample      = TestHelper.GetSample <IfSample>("UnsafeAssignmentInsideIfNested",
                                                                   intTypeNode, intTypeNode, intTypeNode);

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        public void PassProblem(
            MethodCall methodCall, List <IPreCondition> preConditions, ProblemMetadata problemMetadata, ISymbolTable symbolTable, IProblemPipe problemPipe)
        {
            Method           calleeMethod = IntrospectionUtility.ExtractMethod(methodCall);
            ICustomInference matchingRule = MatchingAnalyzeRule(calleeMethod);

            if (matchingRule != null)
            {
                matchingRule.PassProblem(methodCall, preConditions, problemMetadata, symbolTable, problemPipe);
            }
        }
Пример #24
0
        public void InferFragmentType_AssignmentWithParameterNonFragment_ReturnsNoFragmentType()
        {
            TypeNode            intTypeNode      = IntrospectionUtility.TypeNodeFactory <int>();
            Method              sample           = TestHelper.GetSample <InferFragmentSample>("AssignmentWithParameter", intTypeNode);
            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(EmptyFragment));
        }
Пример #25
0
        public void Parse_ValidExampleInsideIf_NoProblem()
        {
            TypeNode intTypeNode = IntrospectionUtility.TypeNodeFactory <int>();
            Method   sample      = TestHelper.GetSample <IfSample>("ValidExampleInsideIf",
                                                                   intTypeNode, intTypeNode);

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void Parse_ConstructorChainingWithBaseClassConstructorWithoutFragment_NoProblem()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleConstructor>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, ".ctor", stringTypeNode, stringTypeNode);

            _typeParser.Parse(sample);
            ProblemCollection result = _typeParser.Problems;

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void GetVariableName_Method_IsNull()
        {
            Method sample          = TestHelper.GetSample <IntrospectionUtility_ClassSample>("UsingField");
            Block  expressionBlock = (Block)sample.Body.Statements[0];
            ExpressionStatement expressionStatement   = (ExpressionStatement)expressionBlock.Statements[1];
            UnaryExpression     sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
            MethodCall          sampleMethodCall      = (MethodCall)sampleUnaryExpression.Operand;
            string variableName = IntrospectionUtility.GetVariableName(sampleMethodCall);

            Assert.That(variableName, Is.Null);
        }
Пример #28
0
        public void InferFragmentType_AssignmentWithParameterFragmentSetToTrue_InfersFragmentType()
        {
            _symbolTable.MakeSafe("parameter", Fragment.CreateNamed("DummyType"));
            TypeNode            intTypeNode      = IntrospectionUtility.TypeNodeFactory <int>();
            Method              sample           = TestHelper.GetSample <InferFragmentSample>("AssignmentWithParameter", intTypeNode);
            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.CreateNamed("DummyType")));
        }
Пример #29
0
        private void AnalyzeFields()
        {
            IEnumerable <Field> fields = IntrospectionUtility.FilterFields(_method.DeclaringType);

            foreach (Field field in fields)
            {
                if (field.Attributes != null && FragmentUtility.ContainsFragment(field.Attributes))
                {
                    _result.MakeUnsafe(field.Name.Name);
                }
            }
        }
        public void GetVariableName_FieldInMemberbinding_ReturnsName()
        {
            Method sample          = TestHelper.GetSample <IntrospectionUtility_ClassSample>("UsingField");
            Block  expressionBlock = (Block)sample.Body.Statements[0];
            ExpressionStatement expressionStatement   = (ExpressionStatement)expressionBlock.Statements[1];
            UnaryExpression     sampleUnaryExpression = (UnaryExpression)expressionStatement.Expression;
            MethodCall          sampleMethodCall      = (MethodCall)sampleUnaryExpression.Operand;
            Expression          sampleExpression      = sampleMethodCall.Operands[0];
            string variableName = IntrospectionUtility.GetVariableName(sampleExpression);

            Assert.That(variableName, Is.EqualTo("_field"));
        }