예제 #1
0
def basic_uv():
    """Create a basic libuv context."""
    chirp = ffi.new("ch_chirp_t*")
    config = ffi.new("ch_config_t*")
    loop = ffi.new("uv_loop_t*")
    lib.ch_chirp_config_init(config)
    cert, dh = common.get_crypto_files()
    cert_str = ffi.new(
        "char[]", cert.encode("UTF-8")
    )
    dh_str = ffi.new(
        "char[]", dh.encode("UTF-8")
    )
    config.CERT_CHAIN_PEM = cert_str
    config.DH_PARAMS_PEM = dh_str
    assert lib.ch_loop_init(loop) == lib.CH_SUCCESS
    error = lib.ch_chirp_init(
        chirp,
        config,
        loop,
        lib.python_log_cb
    )
    if error == lib.CH_SUCCESS:
        lib.ch_chirp_set_auto_stop(chirp)
    yield error
    if error == lib.CH_SUCCESS:
        assert lib.ch_chirp_close_ts(chirp) == lib.CH_SUCCESS
        assert lib.ch_run(loop) == lib.CH_SUCCESS
        assert lib.ch_loop_close(loop) == lib.CH_SUCCESS
예제 #2
0
def test_chirp_run():
    """Test if the do everything ch_chirp_run method works."""
    res = []
    chirp = ffi.new("ch_chirp_t**")
    config = ffi.new("ch_config_t*")
    lib.ch_chirp_config_init(config)
    cert, dh = common.get_crypto_files()
    cert_str = ffi.new(
        "char[]", cert.encode("UTF-8")
    )
    dh_str = ffi.new(
        "char[]", dh.encode("UTF-8")
    )
    config.CERT_CHAIN_PEM = cert_str
    config.DH_PARAMS_PEM  = dh_str

    def run():
        """Run chirp in a thread."""
        res.append(lib.ch_chirp_run(
            config, chirp
        ))

    thread = threading.Thread(target=run)
    thread.start()
    time.sleep(0.1)
    lib.ch_chirp_close_ts(chirp[0])
    thread.join()
    assert res[0] == lib.CH_SUCCESS
예제 #3
0
파일: chirp.py 프로젝트: sosterwalder/c4irp
 def _fill_c_config(self):
     """Fill in the c_config from the config."""
     c_conf = self._c_config
     conf   = self._config
     lib.ch_chirp_config_init(c_conf)
     (
         conf.CERT_CHAIN_PEM,
         conf.DH_PARAMS_PEM
     ) = common.get_crypto_files()
     for std_attr in [
             'REUSE_TIME',
             'TIMEOUT',
             'PORT',
             'BACKLOG',
             'RETRIES',
     ]:
         setattr(
             c_conf,
             std_attr,
             getattr(
                 conf,
                 std_attr,
             )
         )
     self._cert_str = ffi.new(
         "char[]", conf.CERT_CHAIN_PEM.encode("UTF-8")
     )
     self._dh_str = ffi.new(
         "char[]", conf.DH_PARAMS_PEM.encode("UTF-8")
     )
     c_conf.CERT_CHAIN_PEM = self._cert_str
     c_conf.DH_PARAMS_PEM = self._dh_str
예제 #4
0
파일: chirp.py 프로젝트: sosterwalder/c4irp
 def __init__(
         self,
         config = None,
 ):
     self._config    = common.complete_config(config, const.Config())
     self._c_config  = ffi.new("ch_config_t*")
     self._chirp     = ffi.new("ch_chirp_t*")
     self._loop      = ffi.new("uv_loop_t*")
     self._cert_str  = None
     self._dh_str    = None
     self._thread    = None
     self._pool      = None
     self._uv_ret    = 0
     self._chirp_ret = 0
예제 #5
0
def test_low_level_set_get_address4_error_back():
    """Test if cffi low level set and get of address errors are correct."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    ip_addr = "49.32.12.1".encode("UTF-8")
    assert message.port == 0
    assert lib.ch_msg_set_address(
        message,
        lib.CH_IPV4,
        ip_addr,
        3432,
    ) == lib.CH_SUCCESS
    assert message.port == 3432
    address = ffi.new("ch_text_address_t*")
    message.ip_protocol = 3
    assert lib.ch_msg_get_address(
        message,
        address
    ) == lib.CH_PROTOCOL_ERROR
예제 #6
0
def test_low_level_set_get_address4():
    """Test if cffi low level set and get of address works."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    ip_addr = "49.32.12.1".encode("UTF-8")
    assert message.port == 0
    assert lib.ch_msg_set_address(
        message,
        lib.CH_IPV4,
        ip_addr,
        3432,
    ) == lib.CH_SUCCESS
    assert message.port == 3432
    address = ffi.new("ch_text_address_t*")
    assert lib.ch_msg_get_address(
        message,
        address
    ) == lib.CH_SUCCESS
    assert ffi.string(address.data) == ip_addr
예제 #7
0
def test_low_level_set_get_address6():
    """Test if cffi low level set and get of address works."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    ip_addr = "2001:db8:85a3:0:0:8a2e:370:7334".encode("UTF-8")
    res = "2001:db8:85a3::8a2e:370:7334".encode("UTF-8")
    assert message.port == 0
    assert lib.ch_msg_set_address(
        message,
        lib.CH_IPV6,
        ip_addr,
        3432,
    ) == lib.CH_SUCCESS
    assert message.port == 3432
    address = ffi.new("ch_text_address_t*")
    assert lib.ch_msg_get_address(
        message,
        address
    ) == lib.CH_SUCCESS
    assert ffi.string(address.data) == res
예제 #8
0
def test_low_level_set_get_address4_bad_proto():
    """Test if cffi low level set and get of address errors are correct."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    ip_addr = "49.32.12.1".encode("UTF-8")
    assert message.port == 0
    assert lib.ch_msg_set_address(
        message,
        100,
        ip_addr,
        3432,
    ) == lib.CH_VALUE_ERROR
예제 #9
0
def test_low_level_set_get_address6_error():
    """Test if cffi low level set and get of address errors are correct."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    ip_addr = "200x:db8:85a3:0:0:8a2e:370:7334".encode("UTF-8")
    assert message.port == 0
    assert lib.ch_msg_set_address(
        message,
        lib.CH_IPV6,
        ip_addr,
        3432,
    ) == lib.CH_VALUE_ERROR
예제 #10
0
def init_bad_port(port):
    """Test if init with bad port throws CH_VALUE_ERROR."""
    chirp = ffi.new("ch_chirp_t*")
    loop = ffi.new("uv_loop_t*")
    config = ffi.new("ch_config_t*")
    lib.ch_chirp_config_init(config)
    (cert, dh) = common.get_crypto_files()
    cert_str = ffi.new(
        "char[]", cert.encode("UTF-8")
    )
    dh_str = ffi.new(
        "char[]", dh.encode("UTF-8")
    )
    config.CERT_CHAIN_PEM = cert_str
    config.DH_PARAMS_PEM = dh_str
    assert config.PORT == 2998
    config.PORT = port
    assert lib.ch_loop_init(loop) == lib.CH_SUCCESS
    assert lib.ch_chirp_init(
        chirp, config, loop, lib.python_log_cb
    ) == lib.CH_VALUE_ERROR
예제 #11
0
def test_low_level_new():
    """Test if cffi low level new works."""
    message = ffi.new("ch_message_t*")
    lib.ch_msg_init(message)
    assert message.port == 0