Exemplo n.º 1
0
        public void Infers_CoveredMethod_ReturnsTrue()
        {
            TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>();
            Method   sample         = IntrospectionUtility.MethodFactory <string> ("Concat", stringTypeNode, stringTypeNode);

            Assert.That(_customInferenceController.Infers(sample), Is.True);
        }
        public void ArrayConstructIsParsed()
        {
            MockRepository            mocks                    = new MockRepository();
            IBlacklistManager         blacklistManager         = mocks.Stub <IBlacklistManager>();
            Dictionary <string, bool> locallyInitializedArrays = new Dictionary <string, bool>();
            BlockParserContext        blockParserContext       = new BlockParserContext(
                new ProblemPipeStub(),
                Fragment.CreateNamed("returnFragmentType"),
                new List <ReturnCondition>(),
                blacklistManager,
                delegate { });
            ArrayConstructStatementHandler handler = new ArrayConstructStatementHandler(blockParserContext);

            Method    sampleMethod = IntrospectionUtility.MethodFactory <ArrayConstructStatementHandlerSample> ("LocallyInitializedArray");
            Block     sampleBlock  = (Block)sampleMethod.Body.Statements[0];
            Statement sample       = sampleBlock.Statements[1];

            ISymbolTable  symbolTable = mocks.Stub <ISymbolTable>();
            HandleContext context     = new HandleContext(
                sample,
                symbolTable,
                new List <IPreCondition>(),
                new List <string>(),
                new List <BlockAssignment>(),
                new List <int>(),
                locallyInitializedArrays,
                new Dictionary <string, bool>());

            handler.Handle(context);

            bool locallyInitializedArrayAdded = locallyInitializedArrays.ContainsKey("local$2") && locallyInitializedArrays["local$2"] == false;

            Assert.That(locallyInitializedArrayAdded, Is.True);
        }
        private Method GetMethodFromSampleClass <T> ()
        {
            var method = IntrospectionUtility.MethodFactory <T> (
                "Foo", IntrospectionUtility.TypeNodeFactory <int>(), IntrospectionUtility.TypeNodeFactory <string>());

            return(method);
        }
Exemplo n.º 4
0
        public void Handle_WrongStatementType_ThrowsException()
        {
            MockRepository     mocks              = new MockRepository();
            IBlacklistManager  blacklistManager   = mocks.Stub <IBlacklistManager>();
            BlockParserContext blockParserContext = new BlockParserContext(
                new ProblemPipeStub(),
                Fragment.CreateNamed("returnFragmentType"),
                new List <ReturnCondition>(),
                blacklistManager,
                delegate { });

            StatementHandlerBase <AssignmentStatement> handler = new AssignmentStatementHandlerController(blockParserContext);
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StatementHandlerBaseSample> ("ContainsReturnStatement");
            Block     sampleBlock  = (Block)sampleMethod.Body.Statements[1];
            Statement sample       = sampleBlock.Statements[0];

            ISymbolTable  symbolTable = mocks.Stub <ISymbolTable>();
            HandleContext context     = new HandleContext(
                sample,
                symbolTable,
                new List <IPreCondition>(),
                new List <string>(),
                new List <BlockAssignment>(),
                new List <int>(),
                new Dictionary <string, bool>(),
                new Dictionary <string, bool>());

            handler.Handle(context);
        }
Exemplo n.º 5
0
        public void MethodFactory_MethodWithParametersWrongParameterType_ReturnsNull()
        {
            Type     sampleType       = typeof(IntrospectionUtility_InterfaceSample);
            TypeNode stringTypeNode   = IntrospectionUtility.TypeNodeFactory <string>();
            string   sampleMethodname = "MethodWithParameter";
            Method   result           = IntrospectionUtility.MethodFactory(sampleType, sampleMethodname, stringTypeNode);

            Assert.That(result, Is.Null);
        }
Exemplo n.º 6
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()));
        }
Exemplo n.º 7
0
        public void InferFragmentType_SupportetCall_ReturnsFragment()
        {
            Method sampleMethod = IntrospectionUtility.MethodFactory <CustomInferenceControllerSample> ("SupportetCall");
            Block  sampleBlock  = (Block)sampleMethod.Body.Statements[0];
            AssignmentStatement sampleAssignment = (AssignmentStatement)sampleBlock.Statements[1];
            MethodCall          sampleMethodCall = (MethodCall)sampleAssignment.Source;
            Fragment            returnedFragment = _customInferenceController.InferFragmentType(sampleMethodCall, _symbolTable);

            Assert.That(returnedFragment, Is.EqualTo(Fragment.CreateNamed("SqlFragment")));
        }
Exemplo n.º 8
0
        public void Parse_UnsafeAssignmentOnFieldResetByHidingParent_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleField>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "UnsafeAssignmentOnFieldResetByHidingParent");

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

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
        public void Parse_SafeMethodCallUsingBaseProperty_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleProperty>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeMethodCallUsingBaseProperty");

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

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

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

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
        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);
        }
        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);
        }
Exemplo n.º 16
0
        public void Parse_SafeAssignmentOnInheritedFieldWithLiteral_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleField>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeAssignmentOnInheritedFieldWithLiteral");

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

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
Exemplo n.º 17
0
        public void MethodFactory_MethodWithoutParametersCorrectCall_ReturnsMethod()
        {
            Type   sampleType       = typeof(IntrospectionUtility_InterfaceSample);
            string sampleMethodname = "MethodWithoutParameters";
            Method result           = IntrospectionUtility.MethodFactory(sampleType, sampleMethodname);
            bool   correctType      = result.DeclaringType.FullName == sampleType.FullName;
            bool   correctMethod    = result.Name.Name == sampleMethodname;

            Assert.That(correctMethod && correctType, Is.True);
        }
Exemplo n.º 18
0
        public void Parse_UnsafeCallWithInheritedField_ReturnsProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleField>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "UnsafeCallWithInheritedField");

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

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.True);
        }
Exemplo n.º 19
0
        public void Parse_SafeCallWithFieldSetByHidingParentField_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleField>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, "SafeCallWithFieldSetByHidingParentField");

            _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_ConstructorChainingWithBaseClassCorrectFragment_NoProblem()
        {
            TypeNode sampleTypeNode = IntrospectionUtility.TypeNodeFactory <InheritanceSampleConstructor>();
            Method   sample         = IntrospectionUtility.MethodFactory(sampleTypeNode, ".ctor");

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

            Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False);
        }
        public void IsEmpty_MethodNonAnnotated_ReturnsTrue()
        {
            TypeNode     objectTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(object));
            TypeNode     stringTypeNode = IntrospectionUtility.TypeNodeFactory(typeof(string));
            Method       sampleMethod   = IntrospectionUtility.MethodFactory(typeof(InterfaceSample), "MethodNonAnnotated", objectTypeNode, stringTypeNode);
            IMethodGraph methodGraph    = BuildMethodGraph(sampleMethod);

            //Assert.That (methodGraph.IsEmpty(), Is.True);
            Assert.That(methodGraph, Is.Null);
        }
        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);
        }
        public void HandleStatement_InitializationWithEmptyConstructor_VariableFragmentTypeIsNull()
        {
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StringBuilderConstructStatementHandlerSample> ("InitializationWithEmptyConstructor");
            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 variableUndefined = _symbolTable.GetFragmentType("local$0").Undefined;

            Assert.That(variableUndefined, Is.True);
        }
        public void HandleStatement_NonStringBuilderAssignment_VariableNotMapped()
        {
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StringBuilderConstructStatementHandlerSample> ("NonStringBuilderAssignment");
            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 variableAdded = _stringBuilderFragmentTypesDefined.ContainsKey("local$0");

            Assert.That(variableAdded, Is.False);
        }
        public void HandleStatement_InitializationWithFragmentAndInts_VariableFragmentTypeIsFragment()
        {
            Method    sampleMethod = IntrospectionUtility.MethodFactory <StringBuilderConstructStatementHandlerSample> ("InitializationWithFragmentAndInts");
            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.CreateNamed("StringBuilderFragment");

            Assert.That(symbolTableEntryCorrect, Is.True);
        }
Exemplo n.º 27
0
        public static Method GetSample(Type targetType, string methodName, params TypeNode[] methodParameters)
        {
            Method sample = IntrospectionUtility.MethodFactory(targetType, methodName, methodParameters);

            return(sample);
        }
Exemplo n.º 28
0
        public static Method GetSample <SampleClass>(string methodName, params TypeNode[] methodParameters)
        {
            Method sample = IntrospectionUtility.MethodFactory <SampleClass> (methodName, methodParameters);

            return(sample);
        }
Exemplo n.º 29
0
 public void MethodFactory_MethodWithoutParametersParametersNull_ReturnsException()
 {
     IntrospectionUtility.MethodFactory(typeof(IntrospectionUtility_InterfaceSample), "MethodWithoutParameters", null);
 }
Exemplo n.º 30
0
        public void Infers_UncoveredMethod_ReturnsFalse()
        {
            Method sample = IntrospectionUtility.MethodFactory <string> ("ToString");

            Assert.That(_customInferenceController.Infers(sample), Is.False);
        }