def test_serialize_from_objects(self): """ Tests that serialize/deserialize are inverse operations with from_objects factory function """ sk, vk = W.new() message = self._default_msg() env = Envelope.create_from_message(message=message, signing_key=sk, verifying_key=vk) self.assertEqual(env, Envelope.from_bytes(env.serialize()))
def _package_msg(self, msg: MessageBase) -> Envelope: """ Convenience method to package a message into an envelope :param msg: The MessageBase instance to package :return: An Envelope instance """ assert type(msg) is not Envelope, "Attempted to package a 'message' that is already an envelope" assert issubclass(type(msg), MessageBase), "Attempted to package a message that is not a MessageBase subclass" return Envelope.create_from_message(message=msg, signing_key=self.signing_key, verifying_key=self.verifying_key)
def test_create_from_message(self): """ Tests create_from_message with valid args (no vk passed) creates an envelope valid signature and expected fields """ sk, vk = W.new() msg = self._default_msg() env = Envelope.create_from_message(message=msg, signing_key=sk) self.assertTrue(env.verify_seal()) self.assertEqual(env.message, msg) self.assertEqual(env.seal.verifying_key, vk)
def test_create_with_envelope(self): """ Tests creating a message with an envelope produces an object with the expected properties """ sk, vk = ED25519Wallet.new() tx = StandardTransactionBuilder.random_tx() sender = 'me' env = Envelope.create_from_message(message=tx, signing_key=sk) cmd = ReactorCommand.create_cmd('some_cls', 'some_func', envelope=env) self.assertTrue(ReactorCommand.envelope, env)
def test_verify_seal(self): """ Tests verify seal with a valid signature """ meta = self._default_meta() message = self._default_msg() sk, vk = W.new() signature = EnvelopeAuth.seal(signing_key=sk, meta=meta, message=message) seal = Seal.create(signature=signature, verifying_key=vk) env = Envelope.create_from_message(message=message, signing_key=sk, verifying_key=vk) self.assertTrue(env.verify_seal())
def _package_reply(self, reply: MessageBase, req_env: Envelope) -> Envelope: """ Convenience method to create a reply envelope. The difference between this func and _package_msg, is that in the reply envelope the UUID must be the hash of the original request's uuid (not some randomly generated int) :param reply: The reply message (an instance of MessageBase) :param req_env: The original request envelope (an instance of Envelope) :return: An Envelope instance """ self.log.debug("Creating REPLY envelope with msg type {} for request envelope {}".format(type(reply), req_env)) request_uuid = req_env.meta.uuid reply_uuid = EnvelopeAuth.reply_uuid(request_uuid) return Envelope.create_from_message(message=reply, signing_key=self.signing_key, verifying_key=self.verifying_key, uuid=reply_uuid)
def test_create_from_message_bad_keypair(self): """ test create_from_message passing in sk does and incorrect vk """ sk, vk = W.new() sk1, vk1 = W.new() msg = self._default_msg() sender = 'dat boi' env = Envelope.create_from_message(message=msg, signing_key=sk, verifying_key=vk) # no error self.assertEqual(env.seal.verifying_key, vk) self.assertRaises(Exception, Envelope.create_from_message, msg, sk, sender, vk1) self.assertNotEqual(env.seal.verifying_key, vk1)
def test_lazy_serialize(self): """ Tests that creating an envelope from_bytes does not try to repack the underlying struct when serialize is called. This is b/c the serialize() func should be 'cached' with the binary data passed into from_bytes """ sk, vk = W.new() message = self._default_msg() env = Envelope.create_from_message(message=message, signing_key=sk, verifying_key=vk) env_binary = env.serialize() clone = Envelope.from_bytes(env_binary) clone._data = MagicMock() clone_binary = clone.serialize() # this should not pack the capnp struct (_data) again # The Envelope Kitchen Sink lol -- make sure no serialization related API is called clone._data.as_builder.assert_not_called() clone._data.to_bytes_packed.assert_not_called() clone._data.to_bytes.assert_not_called()