def testRecvNoAnnotations(self): msg = Message(Pyro4.message.MSG_CONNECT, b"hello", 42, 0, 0) c = ConnectionMock() c.send(msg.to_bytes()) msg = Message.recv(c) self.assertEqual(0, len(c.received)) self.assertEqual(5, msg.data_size) self.assertEqual(b"hello", msg.data) self.assertEqual(0, msg.annotations_size) self.assertEqual(0, len(msg.annotations))
def testRecvAnnotations(self): annotations = {"TEST": b"abcde"} msg = Message(Pyro4.message.MSG_CONNECT, b"hello", self.ser.serializer_id, 0, 0, annotations, b"secret") c = ConnectionMock() c.send(msg.to_bytes()) msg = Message.recv(c, hmac_key=b"secret") self.assertEqual(0, len(c.received)) self.assertEqual(5, msg.data_size) self.assertEqual(b"hello", msg.data) self.assertEqual(b"abcde", msg.annotations["TEST"]) self.assertIn("HMAC", msg.annotations)
def testChecksum(self): msg = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1, hmac_key=b"secret") c = ConnectionMock() c.send(msg.to_bytes()) # corrupt the checksum bytes data = c.received data = data[:msg.header_size - 2] + b'\x00\x00' + data[msg.header_size:] c = ConnectionMock(data) try: Message.recv(c) self.fail("crash expected") except Pyro4.errors.ProtocolError as x: self.assertIn("checksum", str(x))
def testHmac(self): data = Message(Pyro4.message.MSG_RESULT, b"test", 42, 0, 1, hmac_key=b"test key").to_bytes() c = ConnectionMock(data) # test checking of different hmacs try: Message.recv(c, hmac_key=None) self.fail("crash expected") except Pyro4.errors.SecurityError as x: self.assertIn("hmac key config", str(x)) c = ConnectionMock(data) try: Message.recv(c, hmac_key=b"T3ST-K3Y") self.fail("crash expected") except Pyro4.errors.SecurityError as x: self.assertIn("hmac", str(x)) # test that it works again when providing the correct key c = ConnectionMock(data) msg = Message.recv(c, hmac_key=b"test key") self.assertEqual(b"test key", msg.hmac_key)