Exemple #1
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)),
    ]
Exemple #2
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']
Exemple #3
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'
Exemple #4
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
Exemple #5
0
def assert_integer(env_var, attr, default=''):
    setup_base_env()
    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
Exemple #6
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')
Exemple #7
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()
Exemple #8
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
Exemple #9
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)
Exemple #10
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
Exemple #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']
Exemple #12
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
Exemple #13
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)
Exemple #14
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()
Exemple #15
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()
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)
Exemple #17
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']
Exemple #18
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)
Exemple #19
0
def test_tor_identity():
    tor_host = 'something.onion'
    os.environ.pop('REPORT_HOST', None)
    os.environ.pop('REPORT_HOST_TOR', None)
    e = Env()
    assert len(e.identities) == 0
    os.environ['REPORT_HOST_TOR'] = 'foo'
    os.environ['REPORT_SSL_PORT_TOR'] = '123'
    os.environ['TCP_PORT'] = '456'
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST_TOR'] = tor_host
    e = Env()
    assert len(e.identities) == 1
    ident = e.identities[0]
    assert ident.host == tor_host
    assert ident.tcp_port == 456
    assert ident.ssl_port == 123
    assert ident.nick_suffix == '_tor'
    os.environ['REPORT_TCP_PORT_TOR'] = os.environ['REPORT_SSL_PORT_TOR']
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = 'foo.com'
    os.environ['TCP_PORT'] = '456'
    os.environ['SSL_PORT'] = '789'
    os.environ['REPORT_TCP_PORT'] = '654'
    os.environ['REPORT_SSL_PORT'] = '987'
    os.environ['SSL_CERTFILE'] = 'certfile'
    os.environ['SSL_KEYFILE'] = 'keyfile'
    os.environ.pop('REPORT_TCP_PORT_TOR', None)
    os.environ.pop('REPORT_SSL_PORT_TOR', None)
    e = Env()
    assert len(e.identities) == 2
    ident = e.identities[1]
    assert ident.host == tor_host
    assert ident.tcp_port == 654
    assert ident.ssl_port == 987
    os.environ['REPORT_TCP_PORT_TOR'] = '234'
    os.environ['REPORT_SSL_PORT_TOR'] = '432'
    e = Env()
    assert len(e.identities) == 2
    ident = e.identities[1]
    assert ident.host == tor_host
    assert ident.tcp_port == 234
    assert ident.ssl_port == 432
Exemple #20
0
def test_REPORT_SERVICES_rpc():
    setup_base_env()
    os.environ['REPORT_SERVICES'] = 'rpc://foo.bar:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'bad protocol' in str(err.value)
Exemple #21
0
def test_DAEMON_URL():
    assert_required('DAEMON_URL')
    setup_base_env()
    e = Env()
    assert e.daemon_url == BASE_DAEMON_URL
Exemple #22
0
def test_DB_DIRECTORY():
    assert_required('DB_DIRECTORY')
    setup_base_env()
    e = Env()
    assert e.db_dir == BASE_DB_DIR
Exemple #23
0
def test_minimal():
    setup_base_env()
    Env()
Exemple #24
0
def test_coin_class_provided():
    e = Env(lib_coins.BitcoinCash)
    assert e.coin == lib_coins.BitcoinCash
Exemple #25
0
def test_clearnet_identity():
    os.environ['REPORT_TCP_PORT'] = '456'
    e = Env()
    assert len(e.identities) == 0
    os.environ['REPORT_HOST'] = '8.8.8.8'
    e = Env()
    assert len(e.identities) == 1
    assert e.identities[0].host == '8.8.8.8'
    os.environ['REPORT_HOST'] = 'localhost'
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = ''
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = '127.0.0.1'
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = '0.0.0.0'
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = '224.0.0.2'
    with pytest.raises(Env.Error):
        Env()
    os.environ['REPORT_HOST'] = '$HOST'
    with pytest.raises(Env.Error):
        Env()
    # Accept private IP, unless PEER_ANNOUNCE
    os.environ['PEER_ANNOUNCE'] = ''
    os.environ['REPORT_HOST'] = '192.168.0.1'
    os.environ['SSL_CERTFILE'] = 'certfile'
    os.environ['SSL_KEYFILE'] = 'keyfile'
    Env()
    os.environ['PEER_ANNOUNCE'] = 'OK'
    with pytest.raises(Env.Error) as err:
        Env()
    os.environ.pop('PEER_ANNOUNCE', None)
    assert 'not a valid REPORT_HOST' in str(err)

    os.environ['REPORT_HOST'] = '1.2.3.4'
    os.environ['REPORT_SSL_PORT'] = os.environ['REPORT_TCP_PORT']
    with pytest.raises(Env.Error) as err:
        Env()
    assert 'both resolve' in str(err)

    os.environ['REPORT_SSL_PORT'] = '457'
    os.environ['REPORT_HOST'] = 'foo.com'
    e = Env()
    assert len(e.identities) == 1
    ident = e.identities[0]
    assert ident.host == 'foo.com'
    assert ident.tcp_port == 456
    assert ident.ssl_port == 457
    assert ident.nick_suffix == ''
Exemple #26
0
def assert_required(env_var):
    setup_base_env()
    os.environ.pop(env_var, None)
    with pytest.raises(Env.Error):
        Env()
Exemple #27
0
def test_MAX_SESSIONS():
    too_big = 10000000
    os.environ['MAX_SESSIONS'] = str(too_big)
    e = Env()
    assert e.max_sessions < too_big
Exemple #28
0
def test_COIN_NET():
    '''Test COIN and NET defaults and redirection.'''
    setup_base_env()
    e = Env()
    assert e.coin == lib_coins.BitcoinSV
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.BitcoinSVTestnet
    os.environ['NET'] = ' testnet '
    e = Env()
    assert e.coin == lib_coins.BitcoinSVTestnet
    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
    os.environ.pop('NET')
    os.environ['COIN'] = ' Decred '
    e = Env()
    assert e.coin == lib_coins.Decred
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.DecredTestnet
    os.environ.pop('NET')
    os.environ['COIN'] = ' BitcoinGreen '
    e = Env()
    assert e.coin == lib_coins.Bitg
    os.environ['NET'] = 'mainnet'
    e = Env()
    os.environ.pop('NET')
    os.environ['COIN'] = ' Pivx '
    os.environ['NET'] = 'mainnet'
    e = Env()
    assert e.coin == lib_coins.Pivx
    os.environ['NET'] = 'testnet'
    e = Env()
    assert e.coin == lib_coins.PivxTestnet
    os.environ.pop('NET')
    os.environ['NET'] = 'mainnet'
    os.environ['COIN'] = ' TokenPay '
    e = Env()
    assert e.coin == lib_coins.TokenPay
Exemple #29
0
def test_REPORT_SERVICES_localhost():
    setup_base_env()
    os.environ['REPORT_SERVICES'] = 'tcp://localhost:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'bad host' in str(err.value)
Exemple #30
0
def test_onion_SERVICES():
    setup_base_env()
    os.environ['SERVICES'] = 'tcp://foo.bar.onion:1234'
    with pytest.raises(ServiceError) as err:
        Env()
    assert 'bad host' in str(err.value)