Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/src/main/java/io/ably/lib/types/BaseMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public void decode(ChannelOptions opts) throws AblyException {
if(!match.matches()) break;
String xform = match.group(1).intern();
if(xform == "base64") {
data = Base64Coder.decode((String)data);
try {
data = Base64Coder.decode((String) data);
}
catch (IllegalArgumentException e) {
Log.e(TAG, "Invalid base64 data received");
break;
}
continue;
}
if(xform == "utf-8") {
Expand All @@ -90,12 +96,20 @@ public void decode(ChannelOptions opts) throws AblyException {
String jsonText = ((String)data).trim();
data = Serialisation.gsonParser.parse(jsonText);
}
catch(JsonParseException e) { throw AblyException.fromThrowable(e); }
catch(JsonParseException e) {
Log.e(TAG, "Invalid JSON data received");
break;
}
continue;
}
if(xform == "cipher" && opts != null && opts.encrypted) {
data = opts.getCipher().decrypt((byte[])data);
continue;
if(xform == "cipher") {
if(opts != null && opts.encrypted) {
data = opts.getCipher().decrypt((byte[]) data);
continue;
}
else {
Log.i(TAG, "Encrypted message received but encryption is not set up");
}
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.ably.lib.http.Http;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.realtime.Channel;
Expand Down Expand Up @@ -842,6 +843,73 @@ public MessagesEncodingDataItem[] handleResponse(int statusCode, String contentT
}
}

/**
* Test behaviour when message is encoded as encrypted but encryption is not set up
*/
@Test
public void message_inconsistent_encoding () {
AblyRealtime realtimeSubscribeClient = null;
final ArrayList<String> log = new ArrayList<>();

try {
TestVars testVars = Setup.getTestVars();
ClientOptions apiOptions = testVars.createOptions(testVars.keys[0].keyStr);
apiOptions.logHandler = new Log.LogHandler() {
@Override
public void println(int severity, String tag, String msg, Throwable tr) {
synchronized (log) {
log.add(String.format(Locale.US, "%s: %s", tag, msg));
}
}
};
apiOptions.logLevel = Log.INFO;

AblyRest restPublishClient = new AblyRest(apiOptions);
realtimeSubscribeClient = new AblyRealtime(apiOptions);

final Channel realtimeSubscribeChannelJson = realtimeSubscribeClient.channels.get("test-encoding");

realtimeSubscribeChannelJson.attach();
(new ChannelWaiter(realtimeSubscribeChannelJson)).waitFor(ChannelState.attached);
assertEquals("Verify attached state reached", realtimeSubscribeChannelJson.state, ChannelState.attached);

MessageWaiter messageWaiter = new MessageWaiter(realtimeSubscribeChannelJson);

MessagesEncodingDataItem testData = new MessagesEncodingDataItem();
testData.data = "MDEyMzQ1Njc4OQ=="; /* Base64("0123456789") */
testData.encoding = "utf-8/cipher+aes-128-cbc/base64";
testData.expectedType = "binary";
testData.expectedHexValue = "30313233343536373839"; /* hex for "0123456789" */

restPublishClient.http.post("/channels/" + realtimeSubscribeChannelJson.name + "/messages", null, null, new Http.JSONRequestBody(testData), null);

messageWaiter.waitFor(1);
realtimeSubscribeChannelJson.unsubscribe(messageWaiter);

Message receivedMessage = messageWaiter.receivedMessages.get(0);

expectDataToMatch(testData, receivedMessage);
assertEquals("Verify resulting encoding", receivedMessage.encoding, "utf-8/cipher+aes-128-cbc");

synchronized (log) {
boolean foundErrorMessage = false;
for (String logMessage: log) {
if (logMessage.contains("encryption is not set up"))
foundErrorMessage = true;
}
assertTrue("Verify logged error messages", foundErrorMessage);
}
}
catch (AblyException e) {
e.printStackTrace();
fail("init0: Unexpected exception instantiating library");
}
finally {
if (realtimeSubscribeClient != null)
realtimeSubscribeClient.close();
}
}

private void expectDataToMatch(MessagesEncodingDataItem fixtureMessage, Message receivedMessage) {
if (fixtureMessage.expectedType.equals("string")) {
assertEquals("Verify decoded message data", fixtureMessage.expectedValue.getAsString(), receivedMessage.data);
Expand Down