public void ReturnFragmentType_ExplicitlyImplementedInterfaceMethod_ReturnsFragment() { Method sample = TestHelper.GetSample <ClassWithExplicitlyDeclaredMethodReturningFragment> ("InjectionCop.IntegrationTests.Utilities.InterfaceWithReturnFragment.MethodWithReturnFragment"); var returnFragment = FragmentUtility.ReturnFragmentType(sample); Assert.That(returnFragment, Is.EqualTo(Fragment.CreateNamed("ReturnFragmentType"))); }
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 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 ReturnFragmentType_ImplementedInterfaceMethod_ReturnsFragment() { Method sample = TestHelper.GetSample <ClassWithMethodReturningFragment> ("MethodWithReturnFragment"); var returnFragment = FragmentUtility.ReturnFragmentType(sample); Assert.That(returnFragment, Is.EqualTo(Fragment.CreateNamed("ReturnFragmentType"))); }
public void GetSafenessMap_ExistingEntry_ReturnsEntry() { _symbolTable.MakeSafe("key", Fragment.CreateNamed("FragmentType")); var fragmentType = _symbolTable.GetFragmentType("key"); Assert.That(fragmentType, Is.EqualTo(Fragment.CreateNamed("FragmentType"))); }
public void ReturnFragmentType_MethodWithAnnotatedReturn_ReturnsNull() { Method sample = TestHelper.GetSample <FragmentUtilitySample> ("ReturnFragment"); var returnFragment = FragmentUtility.ReturnFragmentType(sample); Assert.That(returnFragment, Is.EqualTo(Fragment.CreateNamed("ReturnFragmentType"))); }
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); }
public void Build_ReturnsAllStatementHandlersNeededByBlockParser() { MockRepository mocks = new MockRepository(); IBlacklistManager blacklistManager = mocks.Stub <IBlacklistManager>(); IProblemPipe problemPipe = mocks.Stub <IProblemPipe>(); BlockParserContext blockParserContext = new BlockParserContext( problemPipe, Fragment.CreateNamed("returnFragmentType"), new List <ReturnCondition>(), blacklistManager, delegate { }); StatementHandlerDictionaryBuilder builder = new StatementHandlerDictionaryBuilder(blockParserContext); Dictionary <Type, IStatementHandler> handlers = builder.Build(); bool assignmentStatementSupported = handlers.ContainsKey(typeof(AssignmentStatement)); bool branchSupported = handlers.ContainsKey(typeof(Branch)); bool expressionStatementSupported = handlers.ContainsKey(typeof(ExpressionStatement)); bool returnNodeSupported = handlers.ContainsKey(typeof(ReturnNode)); bool switchInstructionSupported = handlers.ContainsKey(typeof(SwitchInstruction)); bool necessaryHandlersSupported = assignmentStatementSupported && branchSupported && expressionStatementSupported && returnNodeSupported && switchInstructionSupported; bool correctHandlerCount = handlers.Keys.Count == 5; Assert.That(necessaryHandlersSupported && correctHandlerCount, Is.True); }
public void GetFragmentType_ContainsStronglyTypedSqlFragmentParameter_True() { TypeNode stringTypeNode = IntrospectionUtility.TypeNodeFactory <string>(); Method sample = TestHelper.GetSample <FragmentUtilitySample>("ContainsStronglyTypedSqlFragmentParameter", stringTypeNode); var fragmentType = FragmentUtility.GetFragmentType(sample.Parameters[0].Attributes); Assert.That(fragmentType, Is.EqualTo(Fragment.CreateNamed("SqlFragment"))); }
public void IsViolated_NamedExpectedSymbolUnknown_ReturnsTrue() { var expectedFragment = Fragment.CreateNamed(c_expectedType); IPreCondition preCondition = new CustomInferencePreCondition(c_symbol, expectedFragment, _problemMetadata); var context = new SymbolTable(_blacklistManager); Assert.That(preCondition.IsViolated(context), Is.True); }
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 Clone_ReturnsDeepCopy_True() { _symbolTable.MakeSafe("key", Fragment.CreateNamed("FragmentType")); ISymbolTable clone = _symbolTable.Copy(); clone.MakeUnsafe("key"); ISymbolTable result = _symbolTable.Copy(); Assert.That(result.IsAssignableTo("key", Fragment.CreateNamed("FragmentType")), Is.True); }
public void GetResult_ParameterizedMethodSymbolTableBuiltWithNonOverlappingEnvironment_FragmentFieldIsEmptyFragment() { Method sampleMethod = TestHelper.GetSample <SymbolTableBuilderSample>("ParameterizedMethod", _floatType, _objectType); EmbeddedInitialSymbolTableBuilder embeddedInitialSymbolTableBuilder = new EmbeddedInitialSymbolTableBuilder(sampleMethod, _blacklistManager, _environment); _environment.MakeSafe("_nonFragmentField", Fragment.CreateNamed("ThisShouldBeIgnored")); ISymbolTable resultSymbolTable = embeddedInitialSymbolTableBuilder.GetResult(); Assert.That(resultSymbolTable.GetFragmentType("_fragmentField"), Is.EqualTo(Fragment.CreateEmpty())); }
public void InferFragmentType_AssignmentWithFragmentProperty_ReturnsPropertyFragmentType() { Method sample = TestHelper.GetSample <InferFragmentSample>("AssignmentWithFragmentProperty"); 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("PropertyFragmentType"))); }
public void GetResult_ParameterizedMethodSymbolTableBuiltWithOverlappingNonFragmentField_EnvironmentSymbolIsPropagated() { Method sampleMethod = TestHelper.GetSample <SymbolTableBuilderSample>("ParameterizedMethod", _floatType, _objectType); EmbeddedInitialSymbolTableBuilder embeddedInitialSymbolTableBuilder = new EmbeddedInitialSymbolTableBuilder(sampleMethod, _blacklistManager, _environment); _environment.MakeSafe("_nonFragmentField", Fragment.CreateNamed("OverlappingType")); ISymbolTable resultSymbolTable = embeddedInitialSymbolTableBuilder.GetResult(); Assert.That(resultSymbolTable.GetFragmentType("_nonFragmentField"), Is.EqualTo(Fragment.CreateNamed("OverlappingType"))); }
public void InferFragmentType_AssignmentWithLocalFragmentSetToTrue_ReturnsTrue() { _symbolTable.MakeSafe("local$0", Fragment.CreateNamed("DummyType")); Method sample = TestHelper.GetSample <InferFragmentSample>("AssignmentWithLocal"); Block assignmentBlock = (Block)sample.Body.Statements[0]; AssignmentStatement assignment = (AssignmentStatement)assignmentBlock.Statements[4]; Expression sampleExpression = assignment.Source; var fragmentType = _symbolTable.InferFragmentType(sampleExpression); Assert.That(fragmentType, Is.Not.EqualTo(EmptyFragment)); }
public void InferFragmentType_AssignmentWithFragmentField_ReturnsEmptyFragmentType(/*[Values("AnyType1", "AnyType2")] string symbolTableType*/) { //_symbolTable.MakeSafe ("_field", symbolTableType); Method sample = TestHelper.GetSample <InferFragmentSample>("AssignmentWithFragmentField"); 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("SampleFragment"))); }
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 void SetUp() { _blacklist = new IDbCommandBlacklistManagerStub(); _problemPipeStub = new ProblemPipeStub(); _returnPreCondition = new ReturnCondition("returnPreCondition", Fragment.CreateNamed("ReturnPreConditionFragmentType")); List <ReturnCondition> returnPreConditions = new List <ReturnCondition> { _returnPreCondition }; _blockParser = new BlockParser(_blacklist, _problemPipeStub, c_returnFragmentType, returnPreConditions); }
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); }
public void Parse_BranchPreconditionsTwoBranchesViolated_ReturnsProblem() { List <AssignabilityPreCondition> preConditions = new List <AssignabilityPreCondition> { new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment")) }; SymbolTable postConditions = new SymbolTable(_blacklistManager); postConditions.MakeUnsafe("x"); List <int> successors = new List <int> { 1, 2 }; BasicBlock initialNode = new BasicBlock(0, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments); preConditions = new List <AssignabilityPreCondition> { new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment")) }; postConditions = new SymbolTable(_blacklistManager); successors = new List <int>(); BasicBlock firstBranch = new BasicBlock(1, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments); preConditions = new List <AssignabilityPreCondition> { new AssignabilityPreCondition("y", Fragment.CreateNamed("SqlFragment")) }; postConditions = new SymbolTable(_blacklistManager); successors = new List <int>(); BasicBlock secondBranch = new BasicBlock(2, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments); using (_mocks.Record()) { _methodGraph.IsEmpty(); LastCall.Return(false); SetupResult.For(_methodGraph.InitialBlock) .Return(initialNode); SetupResult.For(_methodGraph.Blocks) .Return(new BasicBlock[] { initialNode, firstBranch, secondBranch }); _methodGraph.GetBasicBlockById(1); LastCall.Return(firstBranch); _methodGraph.GetBasicBlockById(2); LastCall.Return(secondBranch); _methodGraphBuilder.GetResult(); LastCall.Return(_methodGraph); _parameterSymbolTableBuilder.GetResult(); LastCall.Return(_methodPreConditions); } ProblemCollection result = ParseGraph(); Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result) && result.Count == 2, Is.True); }
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"))); }
public void Parse_ReturnFragmentRequiredUnsafeReturn_ReturnsCorrectReturnFragmentType2() { var returnFragmentType = Fragment.CreateNamed("ReturnFragmentType2"); _blockParser = new BlockParser(_blacklist, _problemPipeStub, returnFragmentType, new List <ReturnCondition>()); Method sampleMethod = TestHelper.GetSample <BlockParserSample> ("UnsafeReturnWhenFragmentRequired"); Block sample = sampleMethod.Body.Statements[1] as Block; BasicBlock returnedBlock = _blockParser.Parse(sample); var preConditionFragment = returnedBlock.PreConditions[0].Fragment; Assert.That(preConditionFragment, Is.EqualTo(returnFragmentType)); }
public void IsViolated_NamedExpectedLiteralGiven_ReturnsFalse() { var expectedFragment = Fragment.CreateNamed(c_expectedType); 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); }
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>(); }
public void HandleViolation_ProblemMetadataGivenButNoViolation_NoProblemAdded() { var expectedFragment = Fragment.CreateNamed(c_expectedType); var givenFragment = Fragment.CreateNamed(c_expectedType); 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); problemPipe.AssertWasNotCalled(pipe => pipe.AddProblem(Arg <ProblemMetadata> .Is.Anything)); }
private void SetPreConditionForIndexerObject( AssignmentStatement assignmentStatement, string targetName, Fragment sourceFragmentType, HandleContext context) { if (targetName != null) { ProblemMetadata problemMetadata = new ProblemMetadata( assignmentStatement.UniqueKey, assignmentStatement.SourceContext, sourceFragmentType, Fragment.CreateNamed("??")); var preCondition = new EqualityPreCondition(targetName, sourceFragmentType, problemMetadata); context.PreConditions.Add(preCondition); } }
public void SetUp() { MockRepository mocks = new MockRepository(); IBlacklistManager blacklistManager = mocks.Stub <IBlacklistManager>(); BlockParserContext blockParserContext = new BlockParserContext( new ProblemPipeStub(), Fragment.CreateNamed("returnFragmentType"), new List <ReturnCondition>(), blacklistManager, delegate { }); _handler = new StringBuilderConstructStatementHandler(blockParserContext); _symbolTable = new SymbolTable(blacklistManager); _stringBuilderFragmentTypesDefined = new Dictionary <string, bool>(); }
public void IsViolated_ViolatingContext_ChangesProblemMetadatasGivenType() { var expectedFragment = Fragment.CreateNamed("expectedFragment"); var unexpectedFragment = Fragment.CreateNamed("unexpectedFragment"); ProblemMetadata problemMetaData = new ProblemMetadata(0, new SourceContext(), expectedFragment, Fragment.CreateNamed("dummy")); IBlacklistManager blackListManager = _mocks.Stub <IBlacklistManager>(); EqualityPreCondition preCondition = new EqualityPreCondition("testSymbol", expectedFragment, problemMetaData); SymbolTable context = new SymbolTable(blackListManager); context.MakeSafe("testSymbol", unexpectedFragment); preCondition.IsViolated(context); Assert.That(problemMetaData.GivenFragment, Is.EqualTo(unexpectedFragment)); }
public void Parse_LoopPreconditionsNotViolated_NoProblem() { List <AssignabilityPreCondition> preConditions = new List <AssignabilityPreCondition> { new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment")) }; SymbolTable postConditions = new SymbolTable(_blacklistManager); List <int> successors = new List <int> { 1 }; BasicBlock initialNode = new BasicBlock(0, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments); preConditions = new List <AssignabilityPreCondition> { new AssignabilityPreCondition("x", Fragment.CreateNamed("SqlFragment")) }; postConditions = new SymbolTable(_blacklistManager); successors = new List <int> { 0 }; BasicBlock firstNode = new BasicBlock(1, preConditions.ToArray(), postConditions, successors.ToArray(), c_EmptyAssignments); using (_mocks.Record()) { _methodGraph.IsEmpty(); LastCall.Return(false); SetupResult.For(_methodGraph.InitialBlock) .Return(initialNode); SetupResult.For(_methodGraph.Blocks) .Return(new BasicBlock[] { initialNode, firstNode }); SetupResult.For(_methodGraph.GetBasicBlockById(1)) .Return(firstNode); SetupResult.For(_methodGraph.GetBasicBlockById(0)) .Return(initialNode); _methodGraphBuilder.GetResult(); LastCall.Return(_methodGraph); _parameterSymbolTableBuilder.GetResult(); LastCall.Return(_methodPreConditions); } ProblemCollection result = ParseGraph(); Assert.That(TestHelper.ContainsProblemID(c_InjectionCopRuleId, result), Is.False); }