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(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_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_with_json_list_data_decode(self): channel = self.ably.channels.get("persisted:enc_list-bin", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) data = [six.u('foó'), six.u('bár')] channel.publish('event', data) message = channel.history().items[0] self.assertEqual(message.data, data) self.assertFalse(message.encoding)
def test_text_utf8_decode(self): channel = self.ably.channels.get("persisted:enc_stringdecode-bin", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) channel.publish('event', six.u('foó')) message = channel.history().items[0] self.assertEqual(message.data, six.u('foó')) self.assertIsInstance(message.data, six.text_type) self.assertFalse(message.encoding)
def test_with_binary_type_decode(self): channel = self.ably.channels.get("persisted:enc_binarydecode-bin", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) channel.publish('event', bytearray(b'foob')) message = channel.history().items[0] self.assertEqual(message.data, bytearray(b'foob')) self.assertIsInstance(message.data, bytearray) self.assertFalse(message.encoding)
def test_text_utf8(self): channel = self.ably.channels.get("persisted:publish_enc", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) with mock.patch('ably.rest.rest.Http.post') as post_mock: channel.publish('event', six.u('fóo')) _, kwargs = post_mock.call_args self.assertEquals(json.loads(kwargs['body'])['encoding'].strip('/'), 'utf-8/cipher+aes-128-cbc/base64') data = self.decrypt(json.loads(kwargs['body'])['data']).decode('utf-8') self.assertEquals(data, six.u('fóo'))
def test_with_json_list_data(self): channel = self.ably.channels.get("persisted:publish_enc", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) data = [six.u('foó'), six.u('bár')] with mock.patch('ably.rest.rest.Http.post') as post_mock: channel.publish('event', data) _, kwargs = post_mock.call_args self.assertEquals(json.loads(kwargs['body'])['encoding'].strip('/'), 'json/utf-8/cipher+aes-128-cbc/base64') raw_data = self.decrypt(json.loads(kwargs['body'])['data']).decode('ascii') self.assertEqual(json.loads(raw_data), data)
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_with_binary_type(self): channel = self.ably.channels.get("persisted:publish_enc", options=ChannelOptions( encrypted=True, cipher_params=self.cipher_params)) with mock.patch('ably.rest.rest.Http.post') as post_mock: channel.publish('event', bytearray(b'foo')) _, kwargs = post_mock.call_args self.assertEquals(json.loads(kwargs['body'])['encoding'].strip('/'), 'cipher+aes-128-cbc/base64') data = self.decrypt(json.loads(kwargs['body'])['data']) self.assertEqual(data, bytearray(b'foo')) self.assertIsInstance(data, bytearray)
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')