Exemplo n.º 1
0
def test_encrypt_decrypt_payload():
    test_object = {"fake-key": "fake-value"}

    encrypted = DatagramStream.encrypt_payload(test_object)
    assert encrypted != test_object

    decrypted = DatagramStream.decrypt_payload(encrypted)
    assert decrypted == test_object
Exemplo n.º 2
0
async def test_broadcast_timeout(addr, bcast, family):
    """Create an async broadcast listener, test discovery responses."""

    # Run the listener portion now
    loop = asyncio.get_event_loop()
    recvq = asyncio.Queue()
    excq = asyncio.Queue()
    drained = asyncio.Event()

    bcast = (bcast, 7000)
    local_addr = (addr[0], 0)

    transport, _ = await loop.create_datagram_endpoint(
        lambda: BroadcastListenerProtocol(recvq, excq, drained),
        local_addr=local_addr,
    )
    stream = DatagramStream(transport,
                            recvq,
                            excq,
                            drained,
                            timeout=DEFAULT_TIMEOUT)

    # Send the scan command
    data = json.dumps(DISCOVERY_REQUEST).encode()
    await stream.send(data, bcast)

    # Wait on the scan response
    with pytest.raises(asyncio.TimeoutError):
        await stream.recv()
Exemplo n.º 3
0
async def test_broadcast_recv(addr, bcast, family):
    """Create a socket broadcast responder, an async broadcast listener, test discovery responses."""
    with socket.socket(family, socket.SOCK_DGRAM) as sock:
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        sock.bind(("", addr[1]))

        def responder(s):
            (d, addr) = s.recvfrom(2048)
            p = json.loads(d)
            assert p == DISCOVERY_REQUEST

            p = json.dumps(DISCOVERY_RESPONSE)
            s.sendto(p.encode(), addr)

        serv = Thread(target=responder, args=(sock, ))
        serv.start()

        # Run the listener portion now
        loop = asyncio.get_event_loop()
        recvq = asyncio.Queue()
        excq = asyncio.Queue()
        drained = asyncio.Event()

        bcast = (bcast, 7000)
        local_addr = (addr[0], 0)

        transport, _ = await loop.create_datagram_endpoint(
            lambda: BroadcastListenerProtocol(recvq, excq, drained),
            local_addr=local_addr,
        )
        stream = DatagramStream(transport, recvq, excq, drained)

        # Send the scan command
        data = json.dumps(DISCOVERY_REQUEST).encode()
        await stream.send(data, bcast)

        # Wait on the scan response
        task = asyncio.create_task(stream.recv())
        await asyncio.wait_for(task, timeout=DEFAULT_TIMEOUT)
        (response, _) = task.result()

        assert response
        assert len(response) > 0
        assert json.loads(response) == DISCOVERY_RESPONSE

        serv.join(timeout=DEFAULT_TIMEOUT)
Exemplo n.º 4
0
        def responder(s):
            (d, addr) = s.recvfrom(2048)
            p = json.loads(d)
            assert p == DISCOVERY_REQUEST

            r = dresp
            r["pack"] = DatagramStream.encrypt_payload(r["pack"])
            p = json.dumps(r)
            s.sendto(p.encode(), addr)
Exemplo n.º 5
0
async def test_datagram_connect(addr, family):
    """Create a socket responder, an async connection, test send and recv."""
    with socket.socket(family, socket.SOCK_DGRAM) as sock:
        sock.bind(("", addr[1]))

        def responder(s):
            (d, addr) = s.recvfrom(2048)
            p = json.loads(d)
            assert p == DISCOVERY_REQUEST

            p = json.dumps(DISCOVERY_RESPONSE)
            s.sendto(p.encode(), addr)

        serv = Thread(target=responder, args=(sock, ))
        serv.start()

        # Run the listener portion now
        loop = asyncio.get_event_loop()
        recvq = asyncio.Queue()
        excq = asyncio.Queue()
        drained = asyncio.Event()

        remote_addr = (addr[0], 7000)

        transport, _ = await loop.create_datagram_endpoint(
            lambda: DeviceProtocol(recvq, excq, drained),
            remote_addr=remote_addr)
        stream = DatagramStream(transport, recvq, excq, drained)

        # Send the scan command
        data = json.dumps(DISCOVERY_REQUEST).encode()
        await stream.send(data, None)

        # Wait on the scan response
        task = asyncio.create_task(stream.recv())
        await asyncio.wait_for(task, timeout=DEFAULT_TIMEOUT)
        (response, _) = task.result()

        assert response
        assert len(response) > 0
        assert json.loads(response) == DISCOVERY_RESPONSE

        serv.join(timeout=DEFAULT_TIMEOUT)
Exemplo n.º 6
0
        def responder(s):
            (d, addr) = s.recvfrom(2048)
            p = json.loads(d)

            r = DEFAULT_RESPONSE
            r["pack"] = DatagramStream.encrypt_payload({
                "cols": ["prop-a", "prop-b"],
                "dat": ["val-a", "val-b"]
            })
            p = json.dumps(r)
            s.sendto(p.encode(), addr)
Exemplo n.º 7
0
        def responder(s):
            (d, addr) = s.recvfrom(2048)
            p = json.loads(d)

            r = DEFAULT_RESPONSE
            r["pack"] = DatagramStream.encrypt_payload({
                "t": "bindok",
                "key": "acbd1234"
            })
            p = json.dumps(r)
            s.sendto(p.encode(), addr)