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); }
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); }
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); }
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 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"))); }
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); }
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_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); }
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); }
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); }
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); }
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); }
public static Method GetSample(Type targetType, string methodName, params TypeNode[] methodParameters) { Method sample = IntrospectionUtility.MethodFactory(targetType, methodName, methodParameters); return(sample); }
public static Method GetSample <SampleClass>(string methodName, params TypeNode[] methodParameters) { Method sample = IntrospectionUtility.MethodFactory <SampleClass> (methodName, methodParameters); return(sample); }
public void MethodFactory_MethodWithoutParametersParametersNull_ReturnsException() { IntrospectionUtility.MethodFactory(typeof(IntrospectionUtility_InterfaceSample), "MethodWithoutParameters", null); }
public void Infers_UncoveredMethod_ReturnsFalse() { Method sample = IntrospectionUtility.MethodFactory <string> ("ToString"); Assert.That(_customInferenceController.Infers(sample), Is.False); }