Ejemplo n.º 1
0
def test_zctx():

    # Create and destroy a context without using it
    ctx = zctx.new()
    assert ctx
    del ctx

    # Create a context with many busy sockets, destroy it
    ctx = zctx.new()
    assert ctx

    # call API functions
    zctx.set_iothreads(ctx, 1)
    zctx.set_linger(ctx, 5) # 5 msecs
    zctx.set_pipehwm(ctx, 500)
    zctx.set_sndhwm(ctx, 500)
    zctx.set_rcvhwm(ctx, 500)

    s1 = zsocket.new(ctx, zmq.PAIR)
    s2 = zsocket.new(ctx, zmq.PULL)  # zmq.XREQ ?
    s3 = zsocket.new(ctx, zmq.REQ)
    s4 = zsocket.new(ctx, zmq.REP)
    s5 = zsocket.new(ctx, zmq.PUB)
    s6 = zsocket.new(ctx, zmq.SUB)
    zsocket.connect(s1, "tcp://127.0.0.1:5555")
    zsocket.connect(s2, "tcp://127.0.0.1:5555")
    zsocket.connect(s3, "tcp://127.0.0.1:5555")
    zsocket.connect(s4, "tcp://127.0.0.1:5555")
    zsocket.connect(s5, "tcp://127.0.0.1:5555")
    zsocket.connect(s6, "tcp://127.0.0.1:5555")

    assert zctx.underlying(ctx);
    del ctx
Ejemplo n.º 2
0
def test_zpoller(verbose=False):

    ctx = zctx.new()

    # Create a few sockets
    vent = zsocket.new(ctx, zmq.PUSH);
    rc = zsocket.bind(vent, "tcp://*:9000")
    assert rc != -1, "vent bind error"
    sink = zsocket.new(ctx, zmq.PULL)
    rc = zsocket.connect(sink, "tcp://localhost:9000");
    assert rc != -1, "sink connect error"
    bowl = zsocket.new(ctx, zmq.PULL)
    dish = zsocket.new(ctx, zmq.PULL)

    # Set-up poller
    poller = zpoller.new(bowl, sink, dish)
    assert poller, "poller create error"

    zstr.send(vent, "Hello, World")

    # We expect a message only on the sink
    which = zpoller.wait(poller, 1000)

    assert which == sink, "expected message on sink only"
    assert not zpoller.expired(poller), "unexpected poll timer exipiry"
    assert not zpoller.terminated(poller), "unexpected poller termination (interrupted)"

    message = zstr.recv(which)
    assert message == "Hello, World", "unexpected message recevied"

    # Destroy poller and context
    del poller
    del ctx
Ejemplo n.º 3
0
def test_zbeacon():

    ctx = zctx.new()
    #  Create a service socket and bind to an ephemeral port
    service = zsocket.new(ctx, zmq.PUB)
    port_nbr = zsocket.bind(service, "tcp:#*:*")
    
    #  Create beacon to broadcast our service
    announcement = str(port_nbr)
    service_beacon = zbeacon.new(9999)
    zbeacon.set_interval(service_beacon, 100)
    zbeacon.publish(service_beacon, announcement)

    #  Create beacon to lookup service
    client_beacon = zbeacon.new (9999)
    zbeacon.subscribe(client_beacon, '')

    #  Wait for at most 1/2 second if there's no broadcast networking
    zsockopt.set_rcvtimeo(zbeacon.socket(client_beacon), 500)

    ipaddress = zstr.recv(zbeacon.socket(client_beacon))

    if ipaddress:
        content = zframe.recv(zbeacon.socket(client_beacon))
        received_port = int(zframe.data(content))
        assert received_port == port_nbr
        zframe.destroy(content)
Ejemplo n.º 4
0
def test_zbeacon():
    ctx = zctx.new()

    #  Create a service socket and bind to an ephemeral port
    service = zsocket.new(ctx, zmq.PUSH)
    port_nbr = zsocket.bind(service, "inproc://foo")

    #  Create beacon to broadcast our service
    announcement = str(port_nbr)
    service_beacon = zbeacon.new(ctx, 9999)
    zbeacon.set_interval(service_beacon, 100)
    zbeacon.publish(service_beacon, announcement)

    #  Create beacon to lookup service
    client_beacon = zbeacon.new(ctx, 9999)
    zbeacon.subscribe(client_beacon, '')

    #  Wait for at most 1/2 second if there's no broadcast networking
    beacon_socket = zbeacon.socket(client_beacon)
    zsocket.set_rcvtimeo(beacon_socket, 500)
    ipaddress = zstr.recv(beacon_socket)
    content = zframe.recv(beacon_socket)
    received_port = int(zframe.data(content))
    assert received_port == port_nbr
    zframe.destroy(content)

    del service_beacon
    del ctx
Ejemplo n.º 5
0
def test_zmsg():
    m = zmsg.new()
    foo = zframe.new('foo')
    zmsg.push(m, foo)
    assert zmsg.first(m) == foo
    bar = zframe.new('bar')
    zmsg.push(m, bar)
    assert zmsg.first(m) == bar
    assert zmsg.last(m) == foo
    zmsg.append(m, zframe.new('ding'))
    assert zframe.data(zmsg.last(m)) == 'ding'

    ctx = zctx.new()
    p = zsocket.new(ctx, zmq.PUB)
    u = zsocket.new(ctx, zmq.SUB)
    zsockopt.set_subscribe(u, '')
    zsocket.bind(p, 'inproc://qer')
    zsocket.connect(u, 'inproc://qer')
    zmsg.send(m, p)
    zsocket.poll(u, 1)
    n = zmsg.recv(u)
    assert zmsg.size(n) == 3
    assert zframe.data(zmsg.next(n)) == 'bar'
    assert zframe.data(zmsg.next(n)) == 'foo'
    assert zframe.data(zmsg.next(n)) == 'ding'
    assert zmsg.next(n) is None
Ejemplo n.º 6
0
def test_ctx():
    ctx = zctx.new()
    zctx.set_iothreads(ctx, 2)
    zctx.set_iothreads(ctx, 1)
    zctx.set_linger(ctx, 1)
    zctx.set_pipehwm(ctx, 500)
    zctx.set_rcvhwm(ctx, 500)
    underlying = zctx.underlying(ctx)
    del ctx
Ejemplo n.º 7
0
def test_zsocket():
    ctx = zctx.new()
    push = zsocket.new(ctx, zmq.PUSH)
    pull = zsocket.new(ctx, zmq.PULL)
    zsocket.bind(push, 'inproc://test')
    zsocket.connect(pull, 'inproc://test')
    zstr.send(push, 'foo')
    assert zstr.recv(pull) == 'foo'
    zstr.send(push, 'foo')
    zsocket.poll(pull, 1)
    assert zstr.recv_nowait(pull) == 'foo'
Ejemplo n.º 8
0
 def test_shadow_pyczmq(self):
     try:
         from pyczmq import zctx, zsocket, zstr
     except Exception:
         raise SkipTest("Requires pyczmq")
     
     ctx = zctx.new()
     a = zsocket.new(ctx, zmq.PUSH)
     zsocket.bind(a, "inproc://a")
     ctx2 = self.Context.shadow_pyczmq(ctx)
     b = ctx2.socket(zmq.PULL)
     b.connect("inproc://a")
     zstr.send(a, b'hi')
     rcvd = self.recv(b)
     self.assertEqual(rcvd, b'hi')
     b.close()
Ejemplo n.º 9
0
    def test_shadow_pyczmq(self):
        try:
            from pyczmq import zctx, zsocket, zstr
        except Exception:
            raise SkipTest("Requires pyczmq")

        ctx = zctx.new()
        a = zsocket.new(ctx, zmq.PUSH)
        zsocket.bind(a, "inproc://a")
        ctx2 = self.Context.shadow_pyczmq(ctx)
        b = ctx2.socket(zmq.PULL)
        b.connect("inproc://a")
        zstr.send(a, b'hi')
        rcvd = self.recv(b)
        self.assertEqual(rcvd, b'hi')
        b.close()
Ejemplo n.º 10
0
    def test_shadow_pyczmq(self):
        try:
            from pyczmq import zctx, zsocket
        except Exception:
            raise SkipTest("Requires pyczmq")

        ctx = zctx.new()
        ca = zsocket.new(ctx, zmq.PUSH)
        cb = zsocket.new(ctx, zmq.PULL)
        a = zmq.Socket.shadow(ca)
        b = zmq.Socket.shadow(cb)
        a.bind("inproc://a")
        b.connect("inproc://a")
        a.send(b'hi')
        rcvd = self.recv(b)
        self.assertEqual(rcvd, b'hi')
Ejemplo n.º 11
0
 def test_shadow_pyczmq(self):
     try:
         from pyczmq import zctx, zsocket
     except Exception:
         raise SkipTest("Requires pyczmq")
     
     ctx = zctx.new()
     ca = zsocket.new(ctx, zmq.PUSH)
     cb = zsocket.new(ctx, zmq.PULL)
     a = zmq.Socket.shadow(ca)
     b = zmq.Socket.shadow(cb)
     a.bind("inproc://a")
     b.connect("inproc://a")
     a.send(b'hi')
     rcvd = self.recv(b)
     self.assertEqual(rcvd, b'hi')
Ejemplo n.º 12
0
def test_zpoller():

    ctx = zctx.new()
    p = zsocket.new(ctx, zmq.PUB)
    zsocket.bind(p, 'inproc://sdf')

    s = zsocket.new(ctx, zmq.SUB)
    zsockopt.set_subscribe(s, '')
    zsocket.connect(s, 'inproc://sdf')

    s2 = zsocket.new(ctx, zmq.SUB)
    zsockopt.set_subscribe(s2, '')
    zsocket.connect(s2, 'inproc://sdf')

    zstr.send(p, 'foo')

    z = zpoller.new(s)
Ejemplo n.º 13
0
def test_zloop(verbose=False):
    ctx = zctx.new()
    output_s = zsocket.new(ctx, zmq.PAIR)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, 'inproc://lkj')
    zsocket.connect(input_s, 'inproc://lkj')

    @zloop.poll_callback
    def on_socket_event(loop, item, arg):
        assert zstr.recv(item.socket) == 'PING'
        assert arg == 3
        return -1

    @zloop.timer_callback
    def on_timer_event(loop, item, arg):
        zstr.send(arg, 'PING')
        return 0

    @zloop.timer_callback
    def on_cancel_timer_event(loop, item, arg):
        cancel_timer_id = arg
        rc = zloop.timer_end(loop, cancel_timer_id)
        assert (rc == 0)
        return 0

    @zloop.reader_callback
    def on_reader_event(loop, reader, arg):
        assert arg is None
        return -1

    l = zloop.new()
    zloop.set_verbose(l, verbose)

    # create a timer that will be cancelled
    cancel_timer_id = zloop.timer(l, 1000, 1, on_timer_event, None)
    zloop.timer(l, 5, 1, on_cancel_timer_event, cancel_timer_id)

    # After 10 msecs, send a ping message to output
    zloop.timer(l, 20, 1, on_timer_event, output_s)

    # When we get the ping message, end the reactor

    rc = zloop.reader(l, input_s, on_reader_event, None)
    zloop.reader_set_tolerant (l, input_s);
    zloop.start (l);
Ejemplo n.º 14
0
def test_zloop(verbose=False):
    ctx = zctx.new()
    output_s = zsocket.new(ctx, zmq.PAIR)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, 'inproc://lkj')
    zsocket.connect(input_s, 'inproc://lkj')

    @zloop.poll_callback
    def on_socket_event(loop, item, arg):
        assert zstr.recv(item.socket) == 'PING'
        assert arg == 3
        return -1

    @zloop.timer_callback
    def on_timer_event(loop, item, arg):
        zstr.send(arg, 'PING')
        return 0

    @zloop.timer_callback
    def on_cancel_timer_event(loop, item, arg):
        cancel_timer_id = arg
        rc = zloop.timer_end(loop, cancel_timer_id)
        assert (rc == 0)
        return 0

    l = zloop.new()
    zloop.set_verbose(l, verbose)

    # create a timer that will be cancelled
    cancel_timer_id = zloop.timer(l, 1000, 1, on_timer_event, None)
    zloop.timer(l, 5, 1, on_cancel_timer_event, cancel_timer_id)

    # After 10 msecs, send a ping message to output
    zloop.timer(l, 20, 1, on_timer_event, output_s)

    # When we get the ping message, end the reactor
    poll_input = zmq.pollitem(socket=input_s, events=zmq.POLLIN)
    zloop.poller(l, poll_input, on_socket_event, 3)
    zloop.set_tolerant(l, poll_input)
    zloop.start(l)

    del l
    del ctx
Ejemplo n.º 15
0
def test_zloop(verbose=False):
    ctx = zctx.new()
    output_s = zsocket.new(ctx, zmq.PAIR)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, "inproc://lkj")
    zsocket.connect(input_s, "inproc://lkj")

    @zloop.poll_callback
    def on_socket_event(loop, item, arg):
        assert zstr.recv(item.socket) == "PING"
        assert arg == 3
        return -1

    @zloop.timer_callback
    def on_timer_event(loop, item, arg):
        zstr.send(arg, "PING")
        return 0

    @zloop.timer_callback
    def on_cancel_timer_event(loop, item, arg):
        cancel_timer_id = arg
        rc = zloop.timer_end(loop, cancel_timer_id)
        assert rc == 0
        return 0

    l = zloop.new()
    zloop.set_verbose(l, verbose)

    # create a timer that will be cancelled
    cancel_timer_id = zloop.timer(l, 1000, 1, on_timer_event, None)
    zloop.timer(l, 5, 1, on_cancel_timer_event, cancel_timer_id)

    # After 10 msecs, send a ping message to output
    zloop.timer(l, 20, 1, on_timer_event, output_s)

    # When we get the ping message, end the reactor
    poll_input = zmq.pollitem(socket=input_s, events=zmq.POLLIN)
    zloop.poller(l, poll_input, on_socket_event, 3)
    zloop.set_tolerant(l, poll_input)
    zloop.start(l)

    del l
    del ctx
Ejemplo n.º 16
0
def test_zstr():
    ctx = zctx.new()
    test_endpoint = 'inproc://zstr.test'
    output_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, test_endpoint)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.connect(input_s, test_endpoint)

    # Send ten strings, five strings with MORE flag and then END
    for i in range(0, 10):
        rc = zstr.send(output_s, "this is string {}".format(i))
        assert(rc == 0)
    zstr.sendx(output_s, "This", "is", "almost", "the", "very", "END")

    # Read and count until we receive END
    string_nbr = 0
    while True:
        string = zstr.recv(input_s)
        if string == "END":
            break
        string_nbr += 1
    assert(string_nbr == 15)
Ejemplo n.º 17
0
def test_zstr():
    ctx = zctx.new()
    test_endpoint = 'inproc://zstr.test'
    output_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, test_endpoint)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.connect(input_s, test_endpoint)

    # Send ten strings, five strings with MORE flag and then END
    for i in range(0, 10):
        rc = zstr.send(output_s, "this is string {0}".format(i))
        assert (rc == 0)
    zstr.sendx(output_s, "This", "is", "almost", "the", "very", "END")

    # Read and count until we receive END
    string_nbr = 0
    while True:
        string = zstr.recv(input_s)
        if string == "END":
            break
        string_nbr += 1
    assert (string_nbr == 15)
Ejemplo n.º 18
0
def test_zcert():
    ctx = zctx.new()

    if os.path.exists(TESTDIR):
        # delete data from a previous test run
        shutil.rmtree(TESTDIR)
    os.mkdir(TESTDIR)

    # Create a simple certificate with metadata
    cert = zcert.new()
    zcert.set_meta(cert, "email", "*****@*****.**")
    zcert.set_meta(cert, "name", "Pieter Hintjens")
    zcert.set_meta(cert, "organization", "iMatix Corporation")
    zcert.set_meta(cert, "version", "1")
    assert zcert.meta(cert, "email") == "*****@*****.**"

    # Check the dup and eq methods
    shadow = zcert.dup(cert)
    assert zcert.eq(cert, shadow)
    del shadow

    # Check we can save and load certificate
    cert_file = os.path.join(TESTDIR, "mycert.txt")
    zcert.save(cert, cert_file)
    assert os.path.exists(cert_file)
    cert_secret_file = os.path.join(TESTDIR, "mycert.txt_secret")
    assert os.path.exists(cert_secret_file)

    # Load certificate, will in fact load secret one
    shadow = zcert.load(cert_file)
    assert shadow
    assert zcert.eq(cert, shadow)
    del shadow

    del ctx

    # Delete all test files
    shutil.rmtree(TESTDIR)
Ejemplo n.º 19
0
def test_zcert():
    ctx = zctx.new()

    if os.path.exists(TESTDIR):
        # delete data from a previous test run
        shutil.rmtree(TESTDIR)
    os.mkdir(TESTDIR)

    # Create a simple certificate with metadata
    cert = zcert.new()
    zcert.set_meta(cert, "email", "*****@*****.**")
    zcert.set_meta(cert, "name", "Pieter Hintjens")
    zcert.set_meta(cert, "organization", "iMatix Corporation")
    zcert.set_meta(cert, "version", "1")
    assert zcert.meta(cert, "email") == "*****@*****.**"

    # Check the dup and eq methods
    shadow = zcert.dup(cert)
    assert zcert.eq(cert, shadow)
    del shadow

    # Check we can save and load certificate
    cert_file = os.path.join(TESTDIR, "mycert.txt")
    zcert.save(cert, cert_file)
    assert os.path.exists(cert_file)
    cert_secret_file = os.path.join(TESTDIR, "mycert.txt_secret")
    assert os.path.exists(cert_secret_file)

    # Load certificate, will in fact load secret one
    shadow = zcert.load(cert_file)
    assert shadow
    assert zcert.eq(cert, shadow)
    del shadow

    del ctx

    # Delete all test files
    shutil.rmtree(TESTDIR)
Ejemplo n.º 20
0
def test_zloop():
    ctx = zctx.new()
    output = zsocket.new(ctx, zmq.PAIR)
    input = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output, 'inproc://lkj')
    zsocket.connect(input, 'inproc://lkj')

    @ffi.callback('zloop_fn')
    def item_handler(loop, item, arg):
        assert zstr.recv(item.socket) == 'PING'
        assert ffi.from_handle(arg) == 3
        return -1

    @ffi.callback('zloop_fn')
    def timer_handler(loop, item, arg):
        zstr.send(arg, 'PING')
        return 0

    l = zloop.new()
    zloop.timer(l, 10, 1, timer_handler, output)
    zloop.poller(
        l, zmq.pollitem(socket=input, events=zmq.POLLIN), item_handler, 3)
    zloop.start(l)
Ejemplo n.º 21
0
def test_zmsg():
    m = zmsg.new()
    foo = zframe.new('foo')
    zmsg.push(m, foo)
    assert zmsg.first(m) == foo
    bar = zframe.new('bar')
    zmsg.push(m, bar)
    assert zmsg.first(m) == bar
    assert zmsg.last(m) == foo
    zmsg.append(m, zframe.new('ding'))

    # dup and mutate the buffer view
    d = zframe.dup(zmsg.last(m))
    assert zframe.data(d)[:] == 'ding'
    zframe.data(d)[1] = 'o'
    assert zframe.data(d)[:] == 'dong'
    assert zframe.size(d) == 4
    zmsg.append(m, d)

    ctx = zctx.new()
    p = zsocket.new(ctx, zmq.PUB)
    u = zsocket.new(ctx, zmq.SUB)
    zsocket.set_subscribe(u, '')
    zsocket.bind(p, 'inproc://qer')
    zsocket.connect(u, 'inproc://qer')
    zmsg.send(m, p)
    zsocket.poll(u, 1)
    n = zmsg.recv(u)
    assert zmsg.size(n) == 4
    assert zframe.data(zmsg.next(n))[:] == 'bar'
    assert zframe.data(zmsg.next(n))[:] == 'foo'
    assert zframe.data(zmsg.next(n))[:] == 'ding'
    assert zframe.data(zmsg.next(n))[:] == 'dong'
    assert zmsg.next(n) is None

    zmsg.destroy(n)
Ejemplo n.º 22
0
def test_zauth(verbose=False):

    if os.path.exists(TESTDIR):
        # delete data from a previous test run
        shutil.rmtree(TESTDIR)
    os.mkdir(TESTDIR)

    # Install the authenticator
    ctx = zctx.new()

    auth = zauth.new(ctx)
    zauth.set_verbose(auth, verbose)

    # A default NULL connection should always success, and not go
    # through our authentication infrastructure at all.
    server = zsocket.new(ctx, zmq.PUSH)
    client = zsocket.new(ctx, zmq.PULL)
    zsocket.set_reconnect_ivl(client, 1000) # slow down reconnect attempts
    success = s_can_connect(server, client)
    assert success

    # When we set a domain on the server, we switch on authentication
    # for NULL sockets, but with no policies, the client connection will
    # be allowed.
    server = zsocket.new(ctx, zmq.PUSH)
    zsocket.set_zap_domain(server, 'global')
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: no authenticator test"

    # Blacklist 127.0.0.1, connection should fail
    zauth.deny(auth, "127.0.0.1")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: blacklist test"

    # Whitelist our address, which overrides the blacklist
    zauth.allow (auth, "127.0.0.1")
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: whitelist test"

    # Try PLAIN authentication
    password_file = os.path.join(TESTDIR, "password-file")
    fd = open(password_file, "w")
    fd.write("admin=Password\n")
    fd.close()

    zsocket.set_plain_server(server, 1)
    zsocket.set_plain_username(client, "admin")
    zsocket.set_plain_password(client, "Password")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test no password-file set"

    zauth.configure_plain(auth, "*", password_file)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: Test password-file set and valid client username and password"

    zsocket.set_plain_password (client, "Bogus")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test invalid password"

    server_cert = zcert.new()
    zcert.apply(server_cert, server)
    zsocket.set_curve_server(server, 1)

    client_cert = zcert.new()
    zcert.apply(client_cert, client)
    server_key = zcert.public_txt(server_cert)
    zsocket.set_curve_serverkey(client, server_key)

    # We've not set-up any authentication, connection will fail
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test no curve authentication set"

    # Test CURVE_ALLOW_ANY
    zauth.configure_curve(auth, "*", zauth.CURVE_ALLOW_ANY)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: CURVE_ALLOW_ANY test"

    # Test full client authentication using certificates
    certificate_file = os.path.join(TESTDIR, "mycert.txt")
    zcert.save_public(client_cert, certificate_file)
    zauth.configure_curve(auth, "*", TESTDIR)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: client authentication test"

    del server_cert
    del client_cert

    # Remove the authenticator and check a normal connection works
    del auth

    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: no authenticator test"

    del ctx

    # Delete all test files
    shutil.rmtree(TESTDIR)
Ejemplo n.º 23
0
from pyczmq import zctx

ctx = zctx.new()
Ejemplo n.º 24
0
 def __init__(self, iothreads=1):
     self.ctx = zctx.new()
     zctx.set_iothreads(self.ctx, iothreads)
Ejemplo n.º 25
0
def test_zsocket():

    ctx = zctx.new()

    # Create a detached thread, let it run
    interf = "*"
    domain = "localhost"
    service = 5560

    writer = zsocket.new(ctx, zmq.PUSH)
    assert (writer)
    reader = zsocket.new(ctx, zmq.PULL)
    assert (reader)
    assert zsocket.type_str(writer) == "PUSH"
    assert zsocket.type_str(reader) == "PULL"
    rc = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service

    # Check unbind
    rc = zsocket.unbind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == 0

    # In some cases and especially when running under Valgrind, doing
    # a bind immediately after an unbind causes an EADDRINUSE error.
    # Even a short sleep allows the OS to release the port for reuse.
    time.sleep(0.1)

    # Bind again
    rc = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service


    rc = zsocket.connect(reader, "tcp://{0}:{1}".format(domain, service))
    assert rc == 0
    zstr.send(writer, "HELLO")
    message = zstr.recv(reader)
    assert message
    assert message == "HELLO"

    # Test binding to ports
    port = zsocket.bind(writer, "tcp://{0}:*".format(interf))
    assert (port >= zsocket.DYNFROM and port <= zsocket.DYNTO)

    assert zsocket.poll(writer, 100) == False

    rc = zsocket.connect(reader, "txp://{0}:{1}".format(domain, service))
    assert rc == -1

    # Test sending frames to socket
    frame = zframe.new("ABC")
    rc = zframe.send(frame, writer, zframe.MORE)
    assert rc == 0
    frame = zframe.new("DEFG")
    rc = zframe.send(frame, writer, 0)
    assert rc == 0

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "ABC")
    assert zframe.more(frame)
    zframe.destroy(frame)

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "DEFG")
    assert not zframe.more(frame)
    zframe.destroy(frame)

    del writer
    del ctx
Ejemplo n.º 26
0
 def __init__(self, iothreads=1):
     self.ctx = zctx.new()
     zctx.set_iothreads(self.ctx, iothreads)
Ejemplo n.º 27
0
def test_zauth(verbose=False):

    if os.path.exists(TESTDIR):
        # delete data from a previous test run
        shutil.rmtree(TESTDIR)
    os.mkdir(TESTDIR)

    # Install the authenticator
    ctx = zctx.new()

    auth = zauth.new(ctx)
    zauth.set_verbose(auth, verbose)

    # A default NULL connection should always success, and not go
    # through our authentication infrastructure at all.
    server = zsocket.new(ctx, zmq.PUSH)
    client = zsocket.new(ctx, zmq.PULL)
    zsocket.set_reconnect_ivl(client, 1000)  # slow down reconnect attempts
    success = s_can_connect(server, client)
    assert success

    # When we set a domain on the server, we switch on authentication
    # for NULL sockets, but with no policies, the client connection will
    # be allowed.
    server = zsocket.new(ctx, zmq.PUSH)
    zsocket.set_zap_domain(server, 'global')
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: no authenticator test"

    # Blacklist 127.0.0.1, connection should fail
    zauth.deny(auth, "127.0.0.1")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: blacklist test"

    # Whitelist our address, which overrides the blacklist
    zauth.allow(auth, "127.0.0.1")
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: whitelist test"

    # Try PLAIN authentication
    password_file = os.path.join(TESTDIR, "password-file")
    fd = open(password_file, "w")
    fd.write("admin=Password\n")
    fd.close()

    zsocket.set_plain_server(server, 1)
    zsocket.set_plain_username(client, "admin")
    zsocket.set_plain_password(client, "Password")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test no password-file set"

    zauth.configure_plain(auth, "*", password_file)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: Test password-file set and valid client username and password"

    zsocket.set_plain_password(client, "Bogus")
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test invalid password"

    server_cert = zcert.new()
    zcert.apply(server_cert, server)
    zsocket.set_curve_server(server, 1)

    client_cert = zcert.new()
    zcert.apply(client_cert, client)
    server_key = zcert.public_txt(server_cert)
    zsocket.set_curve_serverkey(client, server_key)

    # We've not set-up any authentication, connection will fail
    success = s_can_connect(server, client)
    assert not success, "Unexpected connection success: Test no curve authentication set"

    # Test CURVE_ALLOW_ANY
    zauth.configure_curve(auth, "*", zauth.CURVE_ALLOW_ANY)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: CURVE_ALLOW_ANY test"

    # Test full client authentication using certificates
    certificate_file = os.path.join(TESTDIR, "mycert.txt")
    zcert.save_public(client_cert, certificate_file)
    zauth.configure_curve(auth, "*", TESTDIR)
    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: client authentication test"

    del server_cert
    del client_cert

    # Remove the authenticator and check a normal connection works
    del auth

    success = s_can_connect(server, client)
    assert success, "Unexpected connection failure: no authenticator test"

    del ctx

    # Delete all test files
    shutil.rmtree(TESTDIR)
Ejemplo n.º 28
0
def test_zsocket():

    ctx = zctx.new()

    # Create a detached thread, let it run
    interf = "*"
    domain = "localhost"
    service = 5560

    writer = zsocket.new(ctx, zmq.PUSH)
    assert (writer)
    reader = zsocket.new(ctx, zmq.PULL)
    assert (reader)
    assert zsocket.type_str(writer) == "PUSH"
    assert zsocket.type_str(reader) == "PULL"
    rc = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service

    # Check unbind
    rc = zsocket.unbind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == 0

    # In some cases and especially when running under Valgrind, doing
    # a bind immediately after an unbind causes an EADDRINUSE error.
    # Even a short sleep allows the OS to release the port for reuse.
    time.sleep(0.1)

    # Bind again
    rc = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service

    rc = zsocket.connect(reader, "tcp://{0}:{1}".format(domain, service))
    assert rc == 0
    zstr.send(writer, "HELLO")
    message = zstr.recv(reader)
    assert message
    assert message == "HELLO"

    # Test binding to ports
    port = zsocket.bind(writer, "tcp://{0}:*".format(interf))
    assert (port >= zsocket.DYNFROM and port <= zsocket.DYNTO)

    assert zsocket.poll(writer, 100) == False

    rc = zsocket.connect(reader, "txp://{0}:{1}".format(domain, service))
    assert rc == -1

    # Test sending frames to socket
    frame = zframe.new("ABC")
    rc = zframe.send(frame, writer, zframe.MORE)
    assert rc == 0
    frame = zframe.new("DEFG")
    rc = zframe.send(frame, writer, 0)
    assert rc == 0

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "ABC")
    assert zframe.more(frame)
    zframe.destroy(frame)

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "DEFG")
    assert not zframe.more(frame)
    zframe.destroy(frame)

    del writer
    del ctx