Exemplo n.º 1
0
def test_LOG_LEVEL():
    setup_base_env()
    e = Env()
    assert e.log_level == 'INFO'
    os.environ['LOG_LEVEL'] = 'warning'
    e = Env()
    assert e.log_level == 'WARNING'
Exemplo n.º 2
0
def assert_default(env_var, attr, default):
    setup_base_env()
    e = Env()
    assert getattr(e, attr) == default
    os.environ[env_var] = 'foo'
    e = Env()
    assert getattr(e, attr) == 'foo'
Exemplo n.º 3
0
def test_duplicate_SERVICES():
    setup_base_env()
    os.environ['SERVICES'] = 'tcp://foo.bar:1234,ws://foo.bar:1235'
    Env()
    os.environ['SERVICES'] = 'tcp://foo.bar:1234,ws://foo.bar:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'multiple services' in str(err.value)
Exemplo n.º 4
0
def assert_boolean(env_var, attr, default):
    e = Env()
    assert getattr(e, attr) == default
    os.environ[env_var] = 'foo'
    e = Env()
    assert getattr(e, attr) == True
    os.environ[env_var] = ''
    e = Env()
    assert getattr(e, attr) == False
Exemplo n.º 5
0
def test_RPC_HOST():
    assert_default('RPC_HOST', 'rpc_host', 'localhost')
    os.environ['RPC_HOST'] = ''
    e = Env()
    # Blank reverts to localhost
    assert e.cs_host(for_rpc=True) == 'localhost'
    os.environ['RPC_HOST'] = '127.0.0.1, ::1'
    e = Env()
    assert e.cs_host(for_rpc=True) == ['127.0.0.1', '::1']
Exemplo n.º 6
0
def test_ban_versions():
    e = Env()
    assert e.drop_client is None
    ban_re = '1\.[0-2]\.\d+?[_\w]*'
    os.environ['DROP_CLIENT'] = ban_re
    e = Env()
    assert e.drop_client == re.compile(ban_re)
    assert e.drop_client.match("1.2.3_buggy_client")
    assert e.drop_client.match("1.3.0_good_client") is None
Exemplo n.º 7
0
def test_REPORT_SERVICES_private():
    setup_base_env()
    os.environ['REPORT_SERVICES'] = 'tcp://192.168.0.1:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'bad IP address' in str(err.value)
    # Accept it not PEER_ANNOUNCE
    os.environ['PEER_ANNOUNCE'] = ''
    Env()
Exemplo n.º 8
0
def test_bad_SERVICES():
    setup_base_env()
    os.environ['SERVICES'] = 'tcp:foo.bar:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'invalid service string' in str(err.value)
    os.environ['SERVICES'] = 'xxx://foo.com:50001'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'unknown protocol' in str(err.value)
Exemplo n.º 9
0
def assert_integer(env_var, attr, default=''):
    if default != '':
        e = Env()
        assert getattr(e, attr) == default
    value = random.randrange(5, 2000)
    os.environ[env_var] = str(value) + '.1'
    with pytest.raises(Env.Error):
        Env()
    os.environ[env_var] = str(value)
    e = Env()
    assert getattr(e, attr) == value
Exemplo n.º 10
0
def test_COIN_NET():
    '''Test COIN and NET defaults and redirection.'''
    setup_base_env()
    e = Env()
    assert e.coin == lib_coins.Bitcoin
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.BitcoinTestnet
    os.environ['NET'] = ' testnet '
    e = Env()
    assert e.coin == lib_coins.BitcoinTestnet
Exemplo n.º 11
0
def test_HOST():
    assert_default('HOST', 'host', 'localhost')
    os.environ['HOST'] = ''
    e = Env()
    assert e.cs_host(for_rpc=False) == ''
    os.environ['HOST'] = '192.168.0.1,23.45.67.89'
    e = Env()
    assert e.cs_host(for_rpc=False) == ['192.168.0.1', '23.45.67.89']
    os.environ['HOST'] = '192.168.0.1 , 23.45.67.89 '
    e = Env()
    assert e.cs_host(for_rpc=False) == ['192.168.0.1', '23.45.67.89']
Exemplo n.º 12
0
def test_REPORT_SERVICES():
    setup_base_env()
    e = Env()
    assert e.report_services == []
    # This has a blank entry between commas
    os.environ['REPORT_SERVICES'] = 'tcp://foo.bar:1234,,ws://1.2.3.4:567'
    e = Env()
    assert e.report_services == [
        Service('tcp', NetAddress('foo.bar', 1234)),
        Service('ws', NetAddress('1.2.3.4', 567)),
    ]
Exemplo n.º 13
0
def test_COIN_NET():
    '''Test COIN and NET defaults and redirection.'''
    setup_base_env()
    e = Env()
    assert e.coin == lib_coins.ElectricCash
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.ElectricCashTestnet
    os.environ['NET'] = ' regtest'
    e = Env()
    assert e.coin == lib_coins.ElectricCashRegTest
Exemplo n.º 14
0
def test_drop_unknown_clients():
    e = Env()
    assert e.drop_client_unknown is False
    os.environ['DROP_CLIENT_UNKNOWN'] = ""
    e = Env()
    assert e.drop_client_unknown is False
    os.environ['DROP_CLIENT_UNKNOWN'] = "1"
    e = Env()
    assert e.drop_client_unknown is True
    os.environ['DROP_CLIENT_UNKNOWN'] = "whatever"
    e = Env()
    assert e.drop_client_unknown is True
Exemplo n.º 15
0
def test_BANNER_FILE():
    e = Env()
    assert e.banner_file is None
    assert e.tor_banner_file is None
    os.environ['BANNER_FILE'] = 'banner_file'
    e = Env()
    assert e.banner_file == 'banner_file'
    assert e.tor_banner_file == 'banner_file'
    os.environ['TOR_BANNER_FILE'] = 'tor_banner_file'
    e = Env()
    assert e.banner_file == 'banner_file'
    assert e.tor_banner_file == 'tor_banner_file'
Exemplo n.º 16
0
def test_PEER_DISCOVERY():
    e = Env()
    assert e.peer_discovery == Env.PD_ON
    os.environ['PEER_DISCOVERY'] = ' '
    e = Env()
    assert e.peer_discovery == Env.PD_OFF
    os.environ['PEER_DISCOVERY'] = 'ON'
    e = Env()
    assert e.peer_discovery == Env.PD_ON
    os.environ['PEER_DISCOVERY'] = 'self'
    e = Env()
    assert e.peer_discovery == Env.PD_SELF
Exemplo n.º 17
0
def test_SERVICES():
    setup_base_env()
    e = Env()
    assert e.services == []
    # This has a blank entry between commas
    os.environ['SERVICES'] = 'tcp://foo.bar:1234,,ws://1.2.3.4:567,rpc://[::1]:700'
    e = Env()
    assert e.services == [
        Service('tcp', NetAddress('foo.bar', 1234)),
        Service('ws', NetAddress('1.2.3.4', 567)),
        Service('rpc', NetAddress('::1', 700)),
    ]
Exemplo n.º 18
0
def assert_integer(env_var, attr, default='', *, min_value=5, max_value=2000):
    setup_base_env()
    if default != '':
        e = Env()
        assert getattr(e, attr) == default
    value = random.randrange(min_value, max_value)
    os.environ[env_var] = str(value) + '.1'
    with pytest.raises(Env.Error):
        Env()
    os.environ[env_var] = str(value)
    e = Env()
    assert getattr(e, attr) == value
Exemplo n.º 19
0
def test_EVENT_LOOP_POLICY():
    e = Env()
    assert e.loop_policy is None
    os.environ['EVENT_LOOP_POLICY'] = 'foo'
    with pytest.raises(Env.Error):
        Env()
    os.environ['EVENT_LOOP_POLICY'] = 'uvloop'
    try:
        Env()
    except ImportError:
        pass
    del os.environ['EVENT_LOOP_POLICY']
Exemplo n.º 20
0
def test_SERVICES_default_rpc():
    # This has a blank entry between commas
    os.environ['SERVICES'] = 'rpc://foo.bar'
    e = Env()
    assert e.services[0].host == 'foo.bar'
    assert e.services[0].port == 8000
    os.environ['SERVICES'] = 'rpc://:800'
    e = Env()
    assert e.services[0].host == 'localhost'
    assert e.services[0].port == 800
    os.environ['SERVICES'] = 'rpc://'
    e = Env()
    assert e.services[0].host == 'localhost'
    assert e.services[0].port == 8000
Exemplo n.º 21
0
def test_COIN_NET():
    '''Test COIN and NET defaults and redirection.'''
    setup_base_env()
    e = Env()
    assert e.coin == lib_coins.BitcoinCash
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.BitcoinCashTestnet
    os.environ['NET'] = ' testnet '
    e = Env()
    assert e.coin == lib_coins.BitcoinCashTestnet
    os.environ.pop('NET')
    os.environ['COIN'] = ' Litecoin '
    e = Env()
    assert e.coin == lib_coins.Litecoin
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.LitecoinTestnet
    os.environ.pop('NET')
    os.environ['COIN'] = ' BitcoinGold '
    e = Env()
    assert e.coin == lib_coins.BitcoinGold
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.BitcoinGoldTestnet
    os.environ['NET'] = 'regtest'
    e = Env()
    assert e.coin == lib_coins.BitcoinGoldRegtest
Exemplo n.º 22
0
def test_SSL_PORT():
    # Requires both SSL_CERTFILE and SSL_KEYFILE to be set
    os.environ['SSL_PORT'] = '50002'
    os.environ['SSL_CERTFILE'] = 'certfile'
    with pytest.raises(Env.Error):
        Env()
    os.environ.pop('SSL_CERTFILE')
    os.environ['SSL_KEYFILE'] = 'keyfile'
    with pytest.raises(Env.Error):
        Env()
    os.environ['SSL_CERTFILE'] = 'certfile'
    Env()
    os.environ.pop('SSL_PORT')
    assert_integer('SSL_PORT', 'ssl_port', None)
Exemplo n.º 23
0
def setup_session(data_dir, rpc_port):
    conf = {'DB_DIRECTORY': data_dir,
            'DAEMON_URL': 'http://*****:*****@localhost:{}/'.format(rpc_port)}
    os.environ.update(conf)
    controller = Controller(Env(LBCRegTest))
    session = LBC.SESSIONCLS(controller, 'TCP')
    return session
Exemplo n.º 24
0
def test_ssl_SERVICES(service):
    setup_base_env()
    os.environ['SERVICES'] = service
    with pytest.raises(Env.Error) as err:
        Env()
    assert 'SSL_CERTFILE' in str(err.value)
    os.environ['SSL_CERTFILE'] = 'certfile'
    with pytest.raises(Env.Error) as err:
        Env()
    assert 'SSL_KEYFILE' in str(err.value)
    os.environ['SSL_KEYFILE'] = 'keyfile'
    Env()
    setup_base_env()
    os.environ['SERVICES'] = service
    os.environ['SSL_KEYFILE'] = 'keyfile'
    with pytest.raises(Env.Error) as err:
        Env()
    assert 'SSL_CERTFILE' in str(err.value)
Exemplo n.º 25
0
def block_processor(tmpdir_factory):
    environ.clear()
    environ['DB_DIRECTORY'] = tmpdir_factory.mktemp('db',
                                                    numbered=True).strpath
    environ['DAEMON_URL'] = ''
    env = Env(LBC)
    bp = LBC.BLOCK_PROCESSOR(env, None, None)
    yield bp
    for attr in dir(bp):  # hack to close dbs on tear down
        obj = getattr(bp, attr)
        if isinstance(obj, Storage):
            obj.close()
Exemplo n.º 26
0
 async def start(self):
     self.data_path = tempfile.mkdtemp()
     conf = {
         'DB_DIRECTORY': self.data_path,
         'DAEMON_URL': 'http://*****:*****@localhost:50001/',
         'REORG_LIMIT': '100',
         'TCP_PORT': '1984'
     }
     os.environ.update(conf)
     self.controller = Controller(Env(self.coin_class))
     self.controller.start_time = time.time()
     await self.controller.start_servers()
     await self.controller.tcp_server_started.wait()
Exemplo n.º 27
0
 def target():
     loop = self.loop
     asyncio.set_event_loop(loop)
     env = Env()
     env.loop_policy = asyncio.get_event_loop_policy()
     self.controller = Controller(env)
     logging.basicConfig(level=logging.DEBUG)
     loop.run_until_complete(
         asyncio.wait(
             [self.controller.serve(self.evt),
              self.evt.wait()],
             return_when=asyncio.FIRST_COMPLETED))
     loop.close()
Exemplo n.º 28
0
def main():
    '''Set up logging and run the server.'''
    log_fmt = Env.default('LOG_FORMAT', '%(levelname)s:%(name)s:%(message)s')
    logging.basicConfig(level=logging.INFO, format=log_fmt)
    logging.info('LbryumX server starting')
    try:
        controller = Controller(Env(LBC))
        controller.run()
    except Exception:
        traceback.print_exc()
        logging.critical('LbryumX server terminated abnormally')
    else:
        logging.info('LbryumX server terminated normally')
Exemplo n.º 29
0
def run_test(db_dir):
    environ.clear()
    environ['DB_DIRECTORY'] = db_dir
    environ['DAEMON_URL'] = ''
    environ['COIN'] = 'BitcoinCash'
    env = Env()
    history = DB(env).history
    # Test abstract compaction
    check_hashX_compaction(history)
    # Now test in with random data
    histories = create_histories(history)
    check_written(history, histories)
    compact_history(history)
    check_written(history, histories)
Exemplo n.º 30
0
async def run_test(db_dir):
    environ.clear()
    environ['DB_DIRECTORY'] = db_dir
    environ['DAEMON_URL'] = ''
    environ['COIN'] = 'Bitcoin'
    db = DB(Env())
    await db.open_for_serving()
    history = db.history

    # Test abstract compaction
    check_hashX_compaction(history)
    # Now test in with random data
    histories = create_histories(history)
    check_written(history, histories)
    compact_history(history)
    check_written(history, histories)