Esempio n. 1
0
 def __init__(self, dft_config = None):
     """
     MongoDb operations
     """
     if dft_config == None:
         config_utils = ConfigUtils()
         self.config = config_utils.parse_json_config(config_utils.db_config)
     else: self.config = dft_config    # 如果有默认的配置使用之
Esempio n. 2
0
 def test_resolve_version(self, method_call_mock):
     config_utils = ConfigUtils('10.0.1')
     version = config_utils.resolve_version('a-{major-version}-b')
     self.assertEqual(version, "a-10-b")
     version = config_utils.resolve_version('a-b')
     self.assertEqual(version, "a-b")
     version = config_utils.resolve_version(None)
     self.assertEqual(version, None)
Esempio n. 3
0
 def test_configuration_checks(self):
     """
     Tests configuration sanity checks.
     Since this test requires loading the QuantstampAuditData contract, it is better here than
     test_config.py.
     """
     config = self.__audit_node.config
     config_utils = ConfigUtils(config.node_version)
     try:
         temp = config.submission_timeout_limit_blocks
         config._Config__submission_timeout_limit_blocks = 2
         config_utils.check_configuration_settings(config)
         self.fail("Configuration error should have been raised.")
     except ConfigurationException:
         config._Config__submission_timeout_limit_blocks = temp
     try:
         temp = config.submission_timeout_limit_blocks
         config._Config__submission_timeout_limit_blocks = 123
         config_utils.check_configuration_settings(config)
         self.fail("Configuration error should have been raised.")
     except ConfigurationException:
         config._Config__submission_timeout_limit_blocks = temp
     for i in range(0, len(self.__audit_node.config.analyzers)):
         try:
             temp = self.__audit_node.config.analyzers[
                 i].wrapper._Wrapper__timeout_sec
             self.__audit_node.config.analyzers[
                 i].wrapper._Wrapper__timeout_sec = 123456
             config_utils.check_configuration_settings(config)
             self.fail("Configuration error should have been raised.")
         except ConfigurationException:
             self.__audit_node.config.analyzers[
                 i].wrapper._Wrapper__timeout_sec = temp
Esempio n. 4
0
File: collect.py Progetto: ZisZ/owl
 def __init__(self, args, options):
   # Parse collector config.
   self.options = options
   config_path = os.path.join(config_utils.get_config_dir(), 'collector.cfg')
   self.args = args
   self.config = self.parse_config_file(config_path)
   self.services = {}
   for service_name in self.config.get("collector", "services").split():
     self.services[service_name] = CollectorConfig.Service(options,
       self.config, service_name)
   self.period = self.config.getint("collector", "period")
   self.owl_server_url = self.config.get("collector", "owl_server_url")
Esempio n. 5
0
def fetch_config(inject_contract=False):
    # create config from file, the contract is not provided and will be injected separately
    config_file_uri = resource_uri("test_config.yaml")
    config = ConfigFactory.create_from_file(config_file_uri,
                                            os.getenv("QSP_ENV",
                                                      default="dev"),
                                            validate_contract_settings=False)
    if inject_contract:
        contract_source_uri = "./tests/resources/QuantstampAuditMock.sol"
        contract_metadata_uri = "./tests/resources/QuantstampAudit-metadata.json"
        audit_contract_metadata = load_json(fetch_file(contract_metadata_uri))
        audit_contract_name = get(audit_contract_metadata, '/contractName')

        addr, contract = __load_audit_contract_from_src(
            config.web3_client, contract_source_uri, audit_contract_name,
            config.account)

        config._Config__audit_contract_address = addr
        config._Config__audit_contract = contract

        config_utils = ConfigUtils(config.node_version)
        config_utils.check_configuration_settings(config)

    return config
Esempio n. 6
0
File: collect.py Progetto: ZisZ/owl
 def __init__(self, options, config, name):
   # Parse service config.
   self.name = name
   self.jobs = config.get(name, "jobs").split()
   self.clusters = {}
   for cluster_name in config.get(name, "clusters").split():
     args = argparse.Namespace()
     args.service = self.name
     args.cluster = cluster_name
     # Parse cluster config.
     self.clusters[cluster_name] = config_utils.get_service_config(args)
   self.metric_url = config.get(name, "metric_url")
   self.need_analyze = True # analyze for default
   if config.has_option(name, "need_analyze"):
     self.need_analyze = config.getboolean(name, "need_analyze")
Esempio n. 7
0
 def setUp(self):
     dummy_node_version = '2.0.1'
     self.config_utils = ConfigUtils(dummy_node_version)
Esempio n. 8
0
class TestConfigUtil(QSPTest):
    def setUp(self):
        dummy_node_version = '2.0.1'
        self.config_utils = ConfigUtils(dummy_node_version)

    def test_create_upload_provider_ok(self, method_call_mock):
        """
        Tests that the S3Provider can be created and is properly returend.
        """
        upload_provider_name = "S3Provider"
        upload_provider_args = {
            "bucket_name": "test-bucket",
            "contract_bucket_name": "contract_test-bucket"
        }
        account = "account"
        result = self.config_utils.create_upload_provider(
            account, upload_provider_name, upload_provider_args, True)
        self.assertTrue(isinstance(result, S3Provider),
                        "The created provider is not an S3Provider")

    def test_create_upload_provider_not_ok(self, method_call_mock):
        """
        Tests that wrong upload provider specification causes an exception being thrown.
        """
        upload_provider_name = "nonsense"
        upload_provider_args = {}
        account = "account"
        try:
            self.config_utils.create_upload_provider(account,
                                                     upload_provider_name,
                                                     upload_provider_args,
                                                     True)
            self.fail(
                "Succeeded to create upload provider without proper provider name."
            )
        except ConfigurationException:
            # expected
            pass
        try:
            self.config_utils.create_upload_provider(None,
                                                     upload_provider_name,
                                                     upload_provider_args,
                                                     True)
            self.fail("Succeeded to create upload provider without account.")
        except ConfigurationException:
            # expected
            pass

    def test_raise_error(self, method_call_mock):
        """
        Tests that raising an error throws an exception
        """
        self.config_utils.raise_err(False, "Message")
        try:
            self.config_utils.raise_err(True, "Message")
            self.fail("An exception was not raised")
        except ConfigurationException:
            # Expected
            pass

    def test_check_audit_contract_settings(self, method_call_mock):
        """
        Tests that verification of ABI and source code in a config happens properly and admits
        exactly one of the two but not both.
        """
        # Test ABI
        abi = ConfigStub(True, True, True)
        self.config_utils.check_audit_contract_settings(abi)

        abi_faulty = ConfigStub(True, False, True)
        try:
            self.config_utils.check_audit_contract_settings(abi_faulty)
            self.fail(
                "ABI is missing configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass

        abi_faulty = ConfigStub(True, True, False)
        try:
            self.config_utils.check_audit_contract_settings(abi_faulty)
            self.fail(
                "ABI is missing configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass

        none = ConfigStub(False, False, False)
        try:
            self.config_utils.check_audit_contract_settings(none)
            self.fail("Neither ABI is not present but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass

    def test_check_configuration_settings(self, method_call_mock):
        """
        Tests various configuration settings
        """
        # Test ABI
        abi = ConfigStubForCheckSettings()
        self.config_utils.check_configuration_settings(abi)
        # if max_gas_price <= 0, it should be ignored
        abi = ConfigStubForCheckSettings(max_gas_price=0)
        self.config_utils.check_configuration_settings(abi)
        abi = ConfigStubForCheckSettings(gas_price_strategy="static")
        self.config_utils.check_configuration_settings(abi)
        abi_faulty = ConfigStubForCheckSettings(start_n_blocks=10)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(submission_timeout=26)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(max_requests=100)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(default_gas_price=500)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(
            gas_price_strategy="invalid_strategy")
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(n_blocks_confirmation=10000)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass
        abi_faulty = ConfigStubForCheckSettings(n_blocks_confirmation=-1)
        try:
            self.config_utils.check_configuration_settings(abi_faulty)
            self.fail(
                "ABI has faulty configuration but no exception was thrown")
        except ConfigurationException:
            # Expected
            pass

    def test_create_eth_provider(self, method_call_mock):
        """
        Tests that all providers can be successfully created and if a wrong name is specified, an
        an exception is raised.
        """
        result = self.config_utils.create_eth_provider(
            "EthereumTesterProvider", {})
        self.assertTrue(isinstance(result, EthereumTesterProvider))
        result = self.config_utils.create_eth_provider("IPCProvider", {})
        self.assertTrue(isinstance(result, IPCProvider))
        result = self.config_utils.create_eth_provider("HTTPProvider", {})
        self.assertTrue(isinstance(result, HTTPProvider))
        try:
            self.config_utils.create_eth_provider("NonesenseProvider", {})
            self.fail(
                "This provider does not exist, a configuration error should be raised."
            )
        except ConfigurationException:
            # Expected
            pass

    def test_create_web3_client(self, method_call_mock):
        """
        Test that web3 client and accounts can be created using an ethereum provider
        """
        eth_provider = self.config_utils.create_eth_provider(
            "EthereumTesterProvider", {})
        client, new_account, new_private_key = self.config_utils.create_web3_client(
            eth_provider, None, None, 2)
        self.assertTrue(isinstance(client, Web3))
        self.assertIsNotNone(new_account,
                             "The account was none and was not created")
        # None ETH provider will make this fail
        try:
            client, new_account, new_private_key = self.config_utils.create_web3_client(
                None, None, None, 2)
            self.fail(
                "No exception was thrown even though the eth provider does not exist and web3 cannot connect"
            )
        except ConfigurationException:
            # Expected
            pass

    def test_create_web3_client_private_key(self, method_call_mock):
        """
        Test that private key is instantiated correctly when creating web3 client
        """
        eth_provider = self.config_utils.create_eth_provider(
            "EthereumTesterProvider", {})
        private_key = "0xc2fd94c5216e754d3eb8f4f34017120fef318c50780ce408b54db575b120229f"
        passphrase = "abc123ropsten"
        client, new_account, new_private_key = self.config_utils.create_web3_client(
            eth_provider, passphrase,
            io_utils.fetch_file(resource_uri("mykey.json")), 2)
        self.assertEqual(private_key, Web3.toHex(new_private_key),
                         "Private key was not decrypted correctly")
        # None ETH provider will make this fail
        try:
            client, new_account, new_private_key = self.config_utils.create_web3_client(
                eth_provider, "incorrect-passphrase",
                io_utils.fetch_file(resource_uri("mykey.json")), 2)
            self.fail(
                "No exception was thrown even though the private key isn't correct"
            )
        except ConfigurationException as e:
            self.assertTrue("MAC mismatch" in str(e),
                            "Expected the MAC mismatch exception")
            # Expected
            pass

    def test_load_config(self, method_call_mock):
        """
        Tests that utils are able to load a configuration dictionary from yaml file.
        """
        uri = resource_uri("test_config.yaml")
        config_dict = self.config_utils.load_config(uri, "dev")
        self.assertIsNotNone(config_dict,
                             "Configuration dictionary was not loaded")
        self.assertTrue("evt_db_path" in config_dict.keys(),
                        "Key evt_db_path is missing from loaded data")

    def test_create_contract(self, method_call_mock):
        eth_provider = self.config_utils.create_eth_provider(
            "EthereumTesterProvider", {})
        client, new_account, new_private_key = self.config_utils.create_web3_client(
            eth_provider, None, None, 2)
        abi_uri = "file://tests/resources/QuantstampAudit.abi.json"
        address = "0xc1220b0bA0760817A9E8166C114D3eb2741F5949"
        self.config_utils.create_contract(client, abi_uri, address)

    def test_resolve_version(self, method_call_mock):
        config_utils = ConfigUtils('10.0.1')
        version = config_utils.resolve_version('a-{major-version}-b')
        self.assertEqual(version, "a-10-b")
        version = config_utils.resolve_version('a-b')
        self.assertEqual(version, "a-b")
        version = config_utils.resolve_version(None)
        self.assertEqual(version, None)