def test_encrypt_decrypt_payload(): test_object = {"fake-key": "fake-value"} encrypted = DeviceProtocol2.encrypt_payload(test_object) assert encrypted != test_object decrypted = DeviceProtocol2.decrypt_payload(encrypted) assert decrypted == test_object
def responder(s): (d, addr) = s.recvfrom(2048) p = json.loads(d) r = DEFAULT_RESPONSE r["pack"] = DeviceProtocol2.encrypt_payload( {"cols": ["prop-a", "prop-b"], "dat": ["val-a", "val-b"]} ) p = json.dumps(r) s.sendto(p.encode(), addr)
def responder(s): (d, addr) = s.recvfrom(2048) p = json.loads(d) r = DEFAULT_RESPONSE r["pack"] = DeviceProtocol2.encrypt_payload( {"t": "bindok", "key": "acbd1234"} ) p = json.dumps(r) s.sendto(p.encode(), addr)
async def test_connection_error(addr, bcast): """Test the encryption key property.""" dp2 = DeviceProtocol2() loop = asyncio.get_event_loop() transport, _ = await loop.create_datagram_endpoint( lambda: dp2, local_addr=addr, ) # Send the scan command data = DISCOVERY_REQUEST await dp2.send(data, bcast) dp2.connection_lost(RuntimeError()) assert transport.is_closing()
async def test_pause_resume(addr, bcast): """Test the encryption key property.""" event = asyncio.Event() dp2 = DeviceProtocol2(drained=event) loop = asyncio.get_event_loop() transport, _ = await loop.create_datagram_endpoint( lambda: dp2, local_addr=addr, ) dp2.pause_writing() assert not event.is_set() dp2.resume_writing() assert event.is_set() dp2.close() assert transport.is_closing()
def encrypt_payload(data): """Encrypt the payload of responses quickly.""" d = data.copy() d["pack"] = DeviceProtocol2.encrypt_payload(d["pack"]) return d
async def test_set_get_key(): """Test the encryption key property.""" key = "faketestkey" dp2 = DeviceProtocol2() dp2.device_key = key assert dp2.device_key == key