def test_bitstring_to_hex(self): """ Ensures that bitstrings are correctly converted to hex. """ self.assertEqual(ReportEncoder._ReportEncoder__to_hex("1010"), "A") self.assertEqual( ReportEncoder._ReportEncoder__to_hex("101010111100000100100011"), "ABC123")
def __create_components(self, config_utils, validate_contract_settings=True): # Creation of internal components self.__eth_provider = self.__create_eth_provider(config_utils) self.__web3_client, self.__account, self.__account_private_key = self.__create_web3_client( config_utils) # Contract settings validation if validate_contract_settings: config_utils.check_audit_contract_settings(self) # After having a web3 client object, use it to put addresses in a canonical format self.__audit_contract_address = mk_checksum_address( self.__audit_contract_address) self.__account = mk_checksum_address(self.__account) if self.has_audit_contract_abi: self.__audit_contract = self.__create_audit_contract(config_utils) if validate_contract_settings: config_utils.check_configuration_settings(self) self.__analyzers = self.__create_analyzers(config_utils) self.__event_pool_manager = EventPoolManager(self.evt_db_path) self.__report_encoder = ReportEncoder() self.__upload_provider = self.__create_upload_provider(config_utils)
def test_create_set_from_compressed_report(self): # Tests whether vulnerability sets for compressed reports match those from # their corresponding uncompressed ones. for report in os.listdir(fetch_file(resource_uri("reports/"))): uncompressed_report = load_json( fetch_file(resource_uri("reports/DAOBug.json"))) expected_set = VulnerabilitiesSet.from_uncompressed_report( uncompressed_report) request_id = uncompressed_report['request_id'] encoder = ReportEncoder() compressed_report = encoder.compress_report( uncompressed_report, request_id) decompressed_report = encoder.decode_report( compressed_report, request_id) found_set = VulnerabilitiesSet.from_uncompressed_report( decompressed_report) self.assertEquals(expected_set, found_set)
def test_audit_state_compression(self): """ Ensures that the audit_state is compressed properly. """ audit_states = [ (ReportEncoder._ReportEncoder__AUDIT_STATE_SUCCESS, "1"), (ReportEncoder._ReportEncoder__AUDIT_STATE_ERROR, "0"), ] for state, expected_state in audit_states: report = TestReportProcessing.mock_report(audit_state=state) bitstring = ReportEncoder._ReportEncoder__encode_header_bytes( report["version"], report["audit_state"], report["status"], ) audit_state_bit = bitstring[ self.__encoder._ReportEncoder__AUDIT_STATE_INDEX] self.assertEqual(audit_state_bit, expected_state)
def test_version_compression(self): """ Ensures that the version is compressed properly. """ tests = [ ("0.0.0", "00000000000000"), ("0.1.2", "00000001000010"), ("4.9.12", "01001001001100"), ("15.15.63", "11111111111111"), ] for version, expected_bitstring in tests: report = TestReportProcessing.mock_report(version=version) bitstring = ReportEncoder._ReportEncoder__encode_header_bytes( report["version"], report["audit_state"], report["status"], ) version_bitstring = bitstring[: 14] # the first 14 bits in the header encode version self.assertEqual(version_bitstring, expected_bitstring)
def __compressed_report(self, report_file_path): full_report = self.__load_report(report_file_path) request_id = full_report['request_id'] encoder = ReportEncoder() return encoder.compress_report(full_report, request_id)
def test_successful_police_audit(self): uncompressed_report = load_json( fetch_file(resource_uri("reports/DAOBug.json"))) request_id = uncompressed_report['request_id'] encoder = ReportEncoder() compressed_report = encoder.compress_report(uncompressed_report, request_id) # Creates a mocked method for retrieving the audit result from the blockchain. submit_report_instance = SubmitReportThread(self.__config) submit_report_instance._SubmitReportThread__get_report_in_blockchain = MagicMock( ) submit_report_instance._SubmitReportThread__get_report_in_blockchain.return_value = \ compressed_report replace_thread(self.__audit_node, SubmitReportThread, submit_report_instance) # Adds a police event to the database to trigger the flow of a police # check. Since no other thread should be writing to the DB at this # point, the write can be performed without a lock. poll_requests_instance = PollRequestsThread( self.__config, self.__block_mined_polling_thread) poll_requests_instance._PollRequestsThread__add_evt_to_db( request_id=request_id, requestor=self.__audit_node.config.audit_contract_address, price=100, uri=resource_uri("reports/DAOBug.json"), assigned_block_nbr=100, is_audit=False) replace_thread(self.__audit_node, PollRequestsThread, poll_requests_instance) # Disables the claim rewards threading from continuously running ahead; # negate the default mocking behaviour of always having rewards # available claim_rewards_instance = ClaimRewardsThread(self.__config) claim_rewards_instance._ClaimRewardsThread__has_available_rewards = MagicMock( ) claim_rewards_instance._ClaimRewardsThread__has_available_rewards.return_value = False replace_thread(self.__audit_node, ClaimRewardsThread, claim_rewards_instance) # Sets the node as a police officer. self.__audit_node.is_police_officer = MagicMock() self.__audit_node.is_police_officer.return_value = True # Sets the audit report value itself to be returned by the audit node. self.__audit_node.audit = MagicMock() self.__audit_node.audit.return_value = { 'audit_state': uncompressed_report['audit_state'], 'audit_uri': 'http://some-url.com', 'audit_hash': 'some-hash', 'full_report': json.dumps(uncompressed_report), 'compressed_report': compressed_report } self.__run_audit_node() sql3lite_worker = self.__audit_node.config.event_pool_manager.sql3lite_worker result_found = False # Waits till the record moves from assigned status to submitted. sql = "select * from audit_evt where request_id = {0} and fk_status == 'SB' and fk_type='PC'" while not result_found: rows = sql3lite_worker.execute(sql.format(request_id)) if len(rows) == 0: sleep(0.1) continue self.assertTrue(len(rows), 1) result_found = True
def __compress_report(self, report_path_uri): full_report = load_json(fetch_file(resource_uri(report_path_uri))) full_report['version'] = self.__config.node_version encoder = ReportEncoder() return encoder.compress_report(full_report, full_report['request_id'])
def __compress_report(report_path_uri): full_report = load_json(fetch_file(resource_uri(report_path_uri))) encoder = ReportEncoder() return encoder.compress_report(full_report, full_report['request_id'])