def test_externalContractInteractions(self): parseAST = ParseAST() astFD = open("./tests/externalContractInteractions.ast", "r") parseResults = parseAST.parse(astFD) expressions = AnalyseExpression.getAllExpressions() print("Number of expressions: " + str(len(expressions))) transfers = AnalyseExpression.getAllTransfers() self.assertEqual(len(transfers), 1) for transfer in transfers: print("Transfer node:" + str(transfer)) print("Transfer nodeType:" + transfer.nodeType) print("Number of children:" + str(len(transfer.children))) for child in transfer.children: print("Child of Transfer" + str(child)) print("Child nodeType: " + child.nodeType) if (child.nodeType == "Identifier"): print("transfer call to " + child.name) sends = AnalyseExpression.getAllSends() self.assertEqual(len(sends), 1) calls = AnalyseExpression.getAllCalls() self.assertEqual(len(calls), 1) delegateCalls = AnalyseExpression.getAllDelegateCalls() self.assertEqual(len(delegateCalls), 1) # staticCalls = AnalyseExpression.getAllStaticCalls() # self.assertEqual(len(staticCalls), 1) astFD.close()
def test_functionConstructor(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) constructors = AnalyseFunctionDefinition.getAllConstructors() self.assertEqual(len(constructors), 1) astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseExternalContractInteractions = AnalyseExternalContractInteractions( ) analyseExternalContractInteractions.analyser() self.assertEqual( len(analyseExternalContractInteractions.statsTransfers), 1) self.assertEqual( analyseExternalContractInteractions.statsTransfers[0]["line"], "6") self.assertEqual(len(analyseExternalContractInteractions.statsSends), 1) self.assertEqual( analyseExternalContractInteractions.statsSends[0]["line"], "7") self.assertEqual(len(analyseExternalContractInteractions.statsCalls), 1) self.assertEqual( analyseExternalContractInteractions.statsCalls[0]["line"], "8") self.assertEqual( len(analyseExternalContractInteractions.statsDelegateCalls), 1) self.assertEqual( analyseExternalContractInteractions.statsDelegateCalls[0]["line"], "9") astFD.close()
def test_expressions(self): parseAST = ParseAST() astFD = open("./tests/expressions.ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['ExpressionCount'], 30) self.assertEqual(parseResults['AST'].children[0].name, "TestExpressions") astFD.close()
def test_ifThenElse(self): parseAST = ParseAST() astFD = open("./tests/ifThenElse.ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['ifCondition'], 1) self.assertEqual(parseResults['Counts']['ifTrueBody'], 1) self.assertEqual(parseResults['Counts']['ifFalseBody'], 1) astFD.close()
def test_functionPayableFallback(self): parseAST = ParseAST() astFD = open("./tests/functionPayableFallback.ast", "r") parseResults = parseAST.parse(astFD) payableFunctionFallbackCount = AnalyseFunctionDefinition.hasPayableFallbackFunction( ) self.assertEqual(payableFunctionFallbackCount, 1) astFD.close()
def test_identifiers(self): parseAST = ParseAST() astFD = open("./tests/identifiers.ast", "r") parseResults = parseAST.parse(astFD) identifiers = AnalyseIdentifier.getAllIdentifiers() for identifier in identifiers: print("Identifier In Expression: " + identifier.name) astFD.close()
def test_contractName(self): parseAST = ParseAST() astFD = open("./tests/functionCall.ast", "r") parseResults = parseAST.parse(astFD) functionCalls = AnalyseFunctionCall.getAllFunctionCalls() for item in functionCalls: print("Function Call Name: " + item) self.assertEqual(len(functionCalls), 1) astFD.close()
def test_functionCall(self): parseAST = ParseAST() astFD = open("./tests/functionCall.ast", "r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['FunctionCallCount'], 1) functionCalls = AnalyseFunctionCall.getAllFunctionCalls() self.assertEqual(len(functionCalls), 1) self.assertEqual(functionCalls[0], "foo") astFD.close()
def test_contractName(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) contracts = AnalyseContract.getAllContractNames() for item in contracts: print("Contract Name: " + item) self.assertEqual(len(contracts), 2) astFD.close()
def test_selfDestruct(self): parseAST = ParseAST() astFD = open(self.testPath+".ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['FunctionCallCount'], 2) functionCalls = AnalyseFunctionCall.getAllFunctionCallNames() self.assertEqual(len(functionCalls), 2) self.assertEqual(functionCalls[0],"selfdestruct") self.assertEqual(functionCalls[1],"selfdestruct") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath+".ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['FunctionCallCount'], 3) functionCallNames = AnalyseFunctionCall.getAllFunctionCallNames() self.assertEqual(len(functionCallNames), 3) self.assertIn("assert", functionCallNames) self.assertIn("require", functionCallNames) self.assertIn("revert", functionCallNames) astFD.close()
def test_selfDestruct(self): parseAST = ParseAST() astFD = open(self.testPath+".ast","r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir+"Contracts/"+self.testFile+".sol") analyseDoSPatterns = AnalyseDoSPatterns() analyseDoSPatterns.analyser() self.assertEqual(len(analyseDoSPatterns.statsDoSWithUnexpectedRevert), 1) self.assertEqual(analyseDoSPatterns.statsDoSWithUnexpectedRevert[0]["line"], "9") self.assertEqual(analyseDoSPatterns.statsDoSWithUnexpectedRevert[0]["info"], "send") astFD.close()
def test_functionDefinition(self): parseAST = ParseAST() astFD = open("./tests/functionDefinition.ast","r") parseResults = parseAST.parse(astFD) functionDefinitions = AnalyseFunctionDefinition.getAllFunctionDefinitions() for item in functionDefinitions: print("Function Definition Name: " + item) self.assertEqual(len(functionDefinitions), 2) functionDefinitionsPublicVisibility = AnalyseFunctionDefinition.getAllFunctionDefinitionsWithPublicVisibility() for item in functionDefinitionsPublicVisibility: print("Public Function Definition Name: " + item) self.assertEqual(len(functionDefinitionsPublicVisibility), 1) astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseReentrancy = AnalyseReentrancy() analyseReentrancy.analyser() self.assertEqual(len(analyseReentrancy.statsPotentialReentrancy), 1) self.assertEqual(analyseReentrancy.statsPotentialReentrancy[0]["line"], "8") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseControlFlowGraph = AnalyseControlFlowGraph() analyseControlFlowGraph.analyser() self.assertEqual(len(analyseControlFlowGraph.controlFlowGraph), 1) for item in analyseControlFlowGraph.controlFlowGraph: print(item) astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath+".ast","r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir+"Contracts/"+self.testFile+".sol") analyseDeprecatedConstructs = AnalyseDeprecatedConstructs() analyseDeprecatedConstructs.analyser() self.assertEqual(len(analyseDeprecatedConstructs.statsTxOrigin), 1) self.assertEqual(analyseDeprecatedConstructs.statsTxOrigin[0]["line"], "21") self.assertEqual(len(analyseDeprecatedConstructs.statsBlockMembers), 1) self.assertEqual(analyseDeprecatedConstructs.statsBlockMembers[0]["line"], "7") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseExceptions = AnalyseExceptions() analyseExceptions.analyser() self.assertEqual(len(analyseExceptions.statsRequires), 1) self.assertEqual(analyseExceptions.statsRequires[0]["line"], "9") self.assertEqual(len(analyseExceptions.statsAsserts), 1) self.assertEqual(analyseExceptions.statsAsserts[0]["line"], "11") self.assertEqual(len(analyseExceptions.statsReverts), 1) self.assertEqual(analyseExceptions.statsReverts[0]["line"], "13") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseUninitialisedStoragePointers = AnalyseUninitialisedStoragePointers( ) analyseUninitialisedStoragePointers.analyser() self.assertEqual( len(analyseUninitialisedStoragePointers. statsUninitialisedStoragePointers), 1) self.assertEqual( analyseUninitialisedStoragePointers. statsUninitialisedStoragePointers[0]["line"], "17") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseDefaultVisibility = AnalyseDefaultVisibility() analyseDefaultVisibility.analyser() self.assertEqual( len(analyseDefaultVisibility.statsDefaultVisibilityForFunction), 1) self.assertEqual( analyseDefaultVisibility.statsDefaultVisibilityForFunction[0] ["line"], "8") self.assertEqual( len(analyseDefaultVisibility.statsDefaultVisibilityForVariable), 1) self.assertEqual( analyseDefaultVisibility.statsDefaultVisibilityForVariable[0] ["line"], "5") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseUncheckedSelfdestructs = AnalyseUncheckedSelfdestructs() analyseUncheckedSelfdestructs.analyser() self.assertEqual( len(analyseUncheckedSelfdestructs. statsConditionalCheckedSelfdestructs), 1) self.assertEqual( analyseUncheckedSelfdestructs. statsConditionalCheckedSelfdestructs[0]["line"], "13") self.assertEqual( len(analyseUncheckedSelfdestructs.statsUncheckedSelfdestructs), 1) self.assertEqual( analyseUncheckedSelfdestructs.statsUncheckedSelfdestructs[0] ["line"], "17") astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseTaintedVariables = AnalyseTaintedVariables() analyseTaintedVariables.analyser() self.assertEqual(len(analyseTaintedVariables.statsTaintedVariables), 6) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[0]["info"]), 1) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[1]["info"]), 2) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[2]["info"]), 3) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[3]["info"]), 3) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[4]["info"]), 4) self.assertEqual( len(analyseTaintedVariables.statsTaintedVariables[5]["info"]), 5) astFD.close()
def test_for(self): parseAST = ParseAST() astFD = open("./Tests/Parsing/for.ast", "r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['ForCount'], 1) astFD.close()
def test_while(self): parseAST = ParseAST() astFD = open("./tests/while.ast", "r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['WhileCount'], 1) astFD.close()
def test_modifier(self): parseAST = ParseAST() astFD = open("./tests/modifier.ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['ModifierCount'], 1) astFD.close()
def test_event(self): parseAST = ParseAST() astFD = open("./tests/event.ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['EventCount'], 1) astFD.close()
def test_exceptions(self): parseAST = ParseAST() astFD = open(self.testPath + ".ast", "r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.testDir + "Contracts/" + self.testFile + ".sol") analyseDefsUses = AnalyseDefsUses() analyseDefsUses.analyser() self.assertEqual(len(analyseDefsUses.statsDefs), 16) self.assertEqual(analyseDefsUses.statsDefs[0]["line"], "14") self.assertEqual(analyseDefsUses.statsDefs[0]["info"], "k") self.assertEqual(analyseDefsUses.statsDefs[1]["line"], "18") self.assertEqual(analyseDefsUses.statsDefs[1]["info"], "i") self.assertEqual(analyseDefsUses.statsDefs[2]["line"], "21") self.assertEqual(analyseDefsUses.statsDefs[2]["info"], "a") self.assertEqual(analyseDefsUses.statsDefs[3]["line"], "22") self.assertEqual(analyseDefsUses.statsDefs[3]["info"], "a") self.assertEqual(analyseDefsUses.statsDefs[4]["line"], "23") self.assertEqual(analyseDefsUses.statsDefs[4]["info"], "j") self.assertEqual(analyseDefsUses.statsDefs[5]["line"], "24") self.assertEqual(analyseDefsUses.statsDefs[5]["info"], "a") self.assertEqual(analyseDefsUses.statsDefs[6]["line"], "25") self.assertEqual(analyseDefsUses.statsDefs[6]["info"], "i") self.assertEqual(analyseDefsUses.statsDefs[7]["line"], "28") self.assertEqual(analyseDefsUses.statsDefs[7]["info"], "i") self.assertEqual(analyseDefsUses.statsDefs[8]["line"], "31") self.assertEqual(analyseDefsUses.statsDefs[8]["info"], "j") self.assertEqual(analyseDefsUses.statsDefs[9]["line"], "34") self.assertEqual(analyseDefsUses.statsDefs[9]["info"], "a") self.assertEqual(analyseDefsUses.statsDefs[10]["line"], "35") self.assertEqual(analyseDefsUses.statsDefs[10]["info"], "j") self.assertEqual(analyseDefsUses.statsDefs[11]["line"], "38") self.assertEqual(analyseDefsUses.statsDefs[11]["info"], "data") self.assertEqual(analyseDefsUses.statsDefs[12]["line"], "40") self.assertEqual(analyseDefsUses.statsDefs[12]["info"], "balances") self.assertEqual(analyseDefsUses.statsDefs[13]["line"], "40") self.assertEqual(analyseDefsUses.statsDefs[13]["info"], "msg") self.assertEqual(analyseDefsUses.statsDefs[14]["line"], "41") self.assertEqual(analyseDefsUses.statsDefs[14]["info"], "balances") self.assertEqual(analyseDefsUses.statsDefs[15]["line"], "41") self.assertEqual(analyseDefsUses.statsDefs[15]["info"], "i") self.assertEqual(len(analyseDefsUses.statsUses), 22) self.assertEqual(analyseDefsUses.statsUses[0]["line"], "15") self.assertEqual(analyseDefsUses.statsUses[0]["info"], "k") self.assertEqual(analyseDefsUses.statsUses[1]["line"], "21") self.assertEqual(analyseDefsUses.statsUses[1]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[2]["line"], "22") self.assertEqual(analyseDefsUses.statsUses[2]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[3]["line"], "23") self.assertEqual(analyseDefsUses.statsUses[3]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[4]["line"], "24") self.assertEqual(analyseDefsUses.statsUses[4]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[5]["line"], "24") self.assertEqual(analyseDefsUses.statsUses[5]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[6]["line"], "24") self.assertEqual(analyseDefsUses.statsUses[6]["info"], "j") self.assertEqual(analyseDefsUses.statsUses[7]["line"], "25") self.assertEqual(analyseDefsUses.statsUses[7]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[8]["line"], "25") self.assertEqual(analyseDefsUses.statsUses[8]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[9]["line"], "25") self.assertEqual(analyseDefsUses.statsUses[9]["info"], "j") self.assertEqual(analyseDefsUses.statsUses[10]["line"], "25") self.assertEqual(analyseDefsUses.statsUses[10]["info"], "msg") self.assertEqual(analyseDefsUses.statsUses[11]["line"], "27") self.assertEqual(analyseDefsUses.statsUses[11]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[12]["line"], "28") self.assertEqual(analyseDefsUses.statsUses[12]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[13]["line"], "31") self.assertEqual(analyseDefsUses.statsUses[13]["info"], "bar") self.assertEqual(analyseDefsUses.statsUses[14]["line"], "31") self.assertEqual(analyseDefsUses.statsUses[14]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[15]["line"], "31") self.assertEqual(analyseDefsUses.statsUses[15]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[16]["line"], "34") self.assertEqual(analyseDefsUses.statsUses[16]["info"], "a") self.assertEqual(analyseDefsUses.statsUses[17]["line"], "35") self.assertEqual(analyseDefsUses.statsUses[17]["info"], "j") self.assertEqual(analyseDefsUses.statsUses[18]["line"], "38") self.assertEqual(analyseDefsUses.statsUses[18]["info"], "j") self.assertEqual(analyseDefsUses.statsUses[19]["line"], "40") self.assertEqual(analyseDefsUses.statsUses[19]["info"], "j") self.assertEqual(analyseDefsUses.statsUses[20]["line"], "41") self.assertEqual(analyseDefsUses.statsUses[20]["info"], "i") self.assertEqual(analyseDefsUses.statsUses[21]["line"], "41") self.assertEqual(analyseDefsUses.statsUses[21]["info"], "a") astFD.close()
def test_stateVariable(self): parseAST = ParseAST() astFD = open("./tests/stateVariable.ast","r") parseResults = parseAST.parse(astFD) self.assertEqual(parseResults['Counts']['VariableDeclarationCount'], 1) astFD.close()
def analyse(self): parseAST = ParseAST() astFD = open(self.astFile,"r") parseResults = parseAST.parse(astFD) mapASTSourceToLineNumbers = MapASTSourceToLineNumbers() mapASTSourceToLineNumbers.analyser(self.inputFile) if (self.runAllAnalysers or self.runAnalyser1): analyseContractFeatures = AnalyseContractFeatures() analyseContractFeatures.analyser() if (self.runAllAnalysers or self.runAnalyser2): analyseControlFlowGraph = AnalyseControlFlowGraph() analyseControlFlowGraph.analyser() if (self.runAllAnalysers or self.runAnalyser3): analyseDefaultVisibility = AnalyseDefaultVisibility() analyseDefaultVisibility.analyser() if (self.runAllAnalysers or self.runAnalyser4): analyseDeprecatedConstructs = AnalyseDeprecatedConstructs() analyseDeprecatedConstructs.analyser() if (self.runAllAnalysers or (self.runAnalyser5 or self.runAnalyser6 or self.runAnalyser7)): analyseDoSPatterns = AnalyseDoSPatterns() analyseDoSPatterns.analyser() if (self.runAllAnalysers or self.runAnalyser8): analyseExternalContractInteractions = AnalyseExternalContractInteractions() analyseExternalContractInteractions.analyser() if (self.runAllAnalysers or self.runAnalyser9): analyseExceptions = AnalyseExceptions() analyseExceptions.analyser() if (self.runAllAnalysers or self.runAnalyser10): analyseReentrancy = AnalyseReentrancy() analyseReentrancy.analyser() if (self.runAllAnalysers or self.runAnalyser11): analyseUncheckedCalls = AnalyseUncheckedCalls() analyseUncheckedCalls.analyser() if (self.runAllAnalysers or self.runAnalyser12): analyseUncheckedSelfdestructs = AnalyseUncheckedSelfdestructs() analyseUncheckedSelfdestructs.analyser() if (self.runAllAnalysers or self.runAnalyser13): analyseUninitialisedStoragePointers = AnalyseUninitialisedStoragePointers() analyseUninitialisedStoragePointers.analyser() if (self.runAllAnalysers or self.runAnalyser14): analyseDefsUses = AnalyseDefsUses() analyseDefsUses.analyser() if (self.runAllAnalysers or self.runAnalyser15): analyseTaintVariables = AnalyseTaintedVariables() analyseTaintVariables.analyser() astFD.close()