def test_crypto_publish_key_mismatch(self): channel_name = self.protocol_channel_name( 'persisted:crypto_publish_key_mismatch') channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) rx_channel = self.ably2.channels.get(channel_name, channel_options) try: with self.assertRaises(AblyException) as cm: messages = rx_channel.history() except Exception as e: log.debug( 'test_crypto_publish_key_mismatch_fail: rx_channel.history not creating exception' ) log.debug(messages.items[0].data) raise (e) the_exception = cm.exception self.assertTrue( 'invalid-padding' == the_exception.message or the_exception.message.startswith("UnicodeDecodeError: 'utf8'"))
def test_crypto_publish_key_mismatch(self): channel_name = self.protocol_channel_name("persisted:crypto_publish_key_mismatch") channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) rx_channel = self.ably2.channels.get(channel_name, channel_options) try: with self.assertRaises(AblyException) as cm: messages = rx_channel.history() except Exception as e: log.debug("test_crypto_publish_key_mismatch_fail: rx_channel.history not creating exception") log.debug(messages.items[0].data) raise (e) the_exception = cm.exception self.assertTrue( "invalid-padding" == the_exception.message or the_exception.message.startswith("UnicodeDecodeError: 'utf8'") )
def test_crypto_encrypted_unhandled(self): channel_name = self.protocol_channel_name("persisted:crypto_send_encrypted_unhandled") key = "0123456789abcdef" data = six.u("foobar") channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params(key)) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params(key)) self.assertEqual(cipher.decrypt(message.data).decode(), data) self.assertEqual(message.encoding, "utf-8/cipher+aes-128-cbc")
def test_crypto_publish(self): channel_name = self.protocol_channel_name( 'persisted:crypto_publish_text') channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) history = publish0.history() messages = history.items self.assertIsNotNone(messages, msg="Expected non-None messages") self.assertEqual(4, len(messages), msg="Expected 4 messages") message_contents = dict((m.name, m.data) for m in messages) log.debug("message_contents: %s" % str(message_contents)) self.assertEqual(six.u("This is a string message payload"), message_contents["publish3"], msg="Expect publish3 to be expected String)") self.assertEqual( b"This is a byte[] message payload", message_contents["publish4"], msg="Expect publish4 to be expected byte[]. Actual: %s" % str(message_contents['publish4'])) self.assertEqual({"test": "This is a JSONObject message payload"}, message_contents["publish5"], msg="Expect publish5 to be expected JSONObject") self.assertEqual(["This is a JSONArray message payload"], message_contents["publish6"], msg="Expect publish6 to be expected JSONObject")
def test_crypto_encrypted_unhandled(self): channel_name = self.protocol_channel_name( 'persisted:crypto_send_encrypted_unhandled') key = '0123456789abcdef' data = six.u('foobar') channel_options = ChannelOptions(encrypted=True, cipher_params=get_default_params(key)) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params(key)) self.assertEqual(cipher.decrypt(message.data).decode(), data) self.assertEqual(message.encoding, 'utf-8/cipher+aes-128-cbc')
def test_presence_history_encrypted(self): params = get_default_params('0123456789abcdef') self.ably.channels.release('persisted:presence_fixtures') self.channel = self.ably.channels.get('persisted:presence_fixtures', options=ChannelOptions( encrypted=True, cipher_params=params)) presence_history = self.channel.presence.history() self.assertEqual(presence_history.items[0].data, {'foo': 'bar'})
def test_channels_get_updates_existing_with_options(self): options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) options_new = ChannelOptions(encrypted=False) channel = self.ably.channels.get('new_channel', options=options) self.assertIs(channel.options, options) channel_same = self.ably.channels.get('new_channel', options=options_new) self.assertIs(channel, channel_same) self.assertIs(channel.options, options_new)
def test_channels_get_doesnt_updates_existing_with_none_options(self): options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) channel = self.ably.channels.get('new_channel', options=options) self.assertIs(channel.options, options) channel_same = self.ably.channels.get('new_channel') self.assertIs(channel, channel_same) self.assertIsNot(channel.options, None) self.assertIs(channel.options, options)
def test_presence_get_encrypted(self): params = get_default_params('0123456789abcdef') self.ably.channels.release('persisted:presence_fixtures') self.channel = self.ably.channels.get('persisted:presence_fixtures', options=ChannelOptions( encrypted=True, cipher_params=params)) presence_messages = self.channel.presence.get() message = list( filter(lambda message: message.client_id == 'client_encoded', presence_messages.items))[0] self.assertEqual(message.data, {'foo': 'bar'})
def test_presence_get_encrypted(self): params = get_default_params('0123456789abcdef') self.ably.channels.release('persisted:presence_fixtures') self.channel = self.ably.channels.get('persisted:presence_fixtures', options=ChannelOptions( encrypted=True, cipher_params=params)) presence_messages = self.channel.presence.get() message = list(filter( lambda message: message.client_id == 'client_encoded', presence_messages.items))[0] self.assertEqual(message.data, {'foo': 'bar'})
def test_crypto_encrypted_unhandled(self): channel_name = self.get_channel_name('persisted:crypto_send_encrypted_unhandled') key = six.b('0123456789abcdef') data = six.u('foobar') publish0 = self.ably.channels.get(channel_name, cipher={'key': key}) publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params({'key': key})) assert cipher.decrypt(message.data).decode() == data assert message.encoding == 'utf-8/cipher+aes-128-cbc'
async def test_crypto_encrypted_unhandled(self): channel_name = self.get_channel_name('persisted:crypto_send_encrypted_unhandled') key = b'0123456789abcdef' data = 'foobar' publish0 = self.ably.channels.get(channel_name, cipher={'key': key}) await publish0.publish("publish0", data) rx_channel = self.ably2.channels[channel_name] history = await rx_channel.history() message = history.items[0] cipher = get_cipher(get_default_params({'key': key})) assert cipher.decrypt(message.data).decode() == data assert message.encoding == 'utf-8/cipher+aes-128-cbc'
def test_crypto_publish_text_256(self): rndfile = Random.new() key = rndfile.read(32) cipher_params = get_default_params(key=key) channel_options = ChannelOptions(encrypted=True, cipher_params=cipher_params) publish0 = TestRestCrypto.ably.channels.get("persisted:crypto_publish_text_256", channel_options) publish0.publish("publish0", True) publish0.publish("publish1", 24) publish0.publish("publish2", 24.234) publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) time.sleep(16) history = publish0.history() messages = history.current self.assertIsNotNone(messages, msg="Expected non-None messages") self.assertEqual(7, len(messages), msg="Expected 7 messages") message_contents = dict((m.name, m.data) for m in messages) log.debug("message_contents: %s" % str(message_contents)) self.assertEqual(True, message_contents["publish0"], msg="Expect publish0 to be Boolean(true)") self.assertEqual(24, int(message_contents["publish1"]), msg="Expect publish1 to be Int(24)") self.assertEqual(24.234, float(message_contents["publish2"]), msg="Expect publish2 to be Double(24.234)") self.assertEqual(six.u("This is a string message payload"), message_contents["publish3"], msg="Expect publish3 to be expected String)") self.assertEqual(b"This is a byte[] message payload", message_contents["publish4"], msg="Expect publish4 to be expected byte[]. Actual: %s" % str(message_contents['publish4'])) self.assertEqual({"test": "This is a JSONObject message payload"}, message_contents["publish5"], msg="Expect publish5 to be expected JSONObject") self.assertEqual(["This is a JSONArray message payload"], message_contents["publish6"], msg="Expect publish6 to be expected JSONObject")
def test_crypto_publish_256(self): rndfile = Random.new() key = rndfile.read(32) channel_name = "persisted:crypto_publish_text_256" channel_name += "_bin" if self.use_binary_protocol else "_text" cipher_params = get_default_params(key=key) channel_options = ChannelOptions(encrypted=True, cipher_params=cipher_params) publish0 = self.ably.channels.get(channel_name, channel_options) publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) history = publish0.history() messages = history.items self.assertIsNotNone(messages, msg="Expected non-None messages") self.assertEqual(4, len(messages), msg="Expected 4 messages") message_contents = dict((m.name, m.data) for m in messages) log.debug("message_contents: %s" % str(message_contents)) self.assertEqual( six.u("This is a string message payload"), message_contents["publish3"], msg="Expect publish3 to be expected String)", ) self.assertEqual( b"This is a byte[] message payload", message_contents["publish4"], msg="Expect publish4 to be expected byte[]. Actual: %s" % str(message_contents["publish4"]), ) self.assertEqual( {"test": "This is a JSONObject message payload"}, message_contents["publish5"], msg="Expect publish5 to be expected JSONObject", ) self.assertEqual( ["This is a JSONArray message payload"], message_contents["publish6"], msg="Expect publish6 to be expected JSONObject", )
def test_crypto_send_unencrypted(self): channel_name = self.protocol_channel_name("persisted:crypto_send_unencrypted") publish0 = self.ably.channels[channel_name] publish0.publish("publish3", six.u("This is a string message payload")) publish0.publish("publish4", six.b("This is a byte[] message payload")) publish0.publish("publish5", {"test": "This is a JSONObject message payload"}) publish0.publish("publish6", ["This is a JSONArray message payload"]) rx_options = ChannelOptions(encrypted=True, cipher_params=get_default_params()) rx_channel = self.ably2.channels.get(channel_name, rx_options) history = rx_channel.history() messages = history.items self.assertIsNotNone(messages, msg="Expected non-None messages") self.assertEqual(4, len(messages), msg="Expected 4 messages") message_contents = dict((m.name, m.data) for m in messages) log.debug("message_contents: %s" % str(message_contents)) self.assertEqual( six.u("This is a string message payload"), message_contents["publish3"], msg="Expect publish3 to be expected String)", ) self.assertEqual( b"This is a byte[] message payload", message_contents["publish4"], msg="Expect publish4 to be expected byte[]. Actual: %s" % str(message_contents["publish4"]), ) self.assertEqual( {"test": "This is a JSONObject message payload"}, message_contents["publish5"], msg="Expect publish5 to be expected JSONObject", ) self.assertEqual( ["This is a JSONArray message payload"], message_contents["publish6"], msg="Expect publish6 to be expected JSONObject", )
def decrypt(self, payload, options={}): cipher = get_cipher(get_default_params('keyfordecrypt_16')) return cipher.decrypt(payload)
def decrypt(self, payload, options={}): ciphertext = base64.b64decode(payload.encode('ascii')) cipher = get_cipher(get_default_params('keyfordecrypt_16')) return cipher.decrypt(ciphertext)