Commit fb87d8fe authored by pedroGitt's avatar pedroGitt
Browse files

Serialization for OlmAccount and OlmSession OK

parent 1511962d
...@@ -42,7 +42,7 @@ public class OlmAccountTest { ...@@ -42,7 +42,7 @@ public class OlmAccountTest {
private static OlmAccount mOlmAccount; private static OlmAccount mOlmAccount;
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
private boolean mIsAccountCreated; private boolean mIsAccountCreated;
final String FILE_NAME = "SerialTestFile"; private final String FILE_NAME = "SerialTestFile";
@BeforeClass @BeforeClass
public static void setUpClass(){ public static void setUpClass(){
...@@ -170,7 +170,12 @@ public class OlmAccountTest { ...@@ -170,7 +170,12 @@ public class OlmAccountTest {
@Test @Test
public void test10RemoveOneTimeKeysForSession() { public void test10RemoveOneTimeKeysForSession() {
OlmSession olmSession = new OlmSession(); OlmSession olmSession = null;
try {
olmSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
long sessionId = olmSession.getOlmSessionId(); long sessionId = olmSession.getOlmSessionId();
assertTrue(0 != sessionId); assertTrue(0 != sessionId);
...@@ -230,16 +235,16 @@ public class OlmAccountTest { ...@@ -230,16 +235,16 @@ public class OlmAccountTest {
try { try {
Context context = getInstrumentation().getContext(); Context context = getInstrumentation().getContext();
context.getFilesDir(); //context.getFilesDir();
fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
// serialize // serialize account
objectOutput = new ObjectOutputStream(fileOutput); objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(accountRef); objectOutput.writeObject(accountRef);
objectOutput.flush(); objectOutput.flush();
objectOutput.close(); objectOutput.close();
// deserialize // deserialize account
FileInputStream fileInput = context.openFileInput(FILE_NAME); FileInputStream fileInput = context.openFileInput(FILE_NAME);
ObjectInputStream objectInput = new ObjectInputStream(fileInput); ObjectInputStream objectInput = new ObjectInputStream(fileInput);
accountDeserial = (OlmAccount) objectInput.readObject(); accountDeserial = (OlmAccount) objectInput.readObject();
...@@ -262,7 +267,6 @@ public class OlmAccountTest { ...@@ -262,7 +267,6 @@ public class OlmAccountTest {
accountRef.releaseAccount(); accountRef.releaseAccount();
accountDeserial.releaseAccount(); accountDeserial.releaseAccount();
} }
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage());
} }
...@@ -278,7 +282,6 @@ public class OlmAccountTest { ...@@ -278,7 +282,6 @@ public class OlmAccountTest {
catch (Exception e) { catch (Exception e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage());
} }
} }
......
package org.matrix.olm; package org.matrix.olm;
import android.content.Context;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
...@@ -11,6 +12,14 @@ import org.junit.runner.RunWith; ...@@ -11,6 +12,14 @@ import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -19,6 +28,7 @@ import static org.junit.Assert.assertTrue; ...@@ -19,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmGroupSessionTest { public class OlmGroupSessionTest {
private static final String LOG_TAG = "OlmSessionTest"; private static final String LOG_TAG = "OlmSessionTest";
private final String FILE_NAME_SERIAL_SESSION = "SerialGroupSession";
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
private static OlmOutboundGroupSession mAliceOutboundGroupSession; private static OlmOutboundGroupSession mAliceOutboundGroupSession;
...@@ -141,4 +151,66 @@ public class OlmGroupSessionTest { ...@@ -141,4 +151,66 @@ public class OlmGroupSessionTest {
// release group sessions // release group sessions
mBobInboundGroupSession.releaseSession(); mBobInboundGroupSession.releaseSession();
} }
@Test
public void test14SerializeOutboundSession() {
OlmOutboundGroupSession outboundGroupSessionRef=null;
OlmOutboundGroupSession outboundGroupSessionSerial=null;
// alice creates OUTBOUND GROUP SESSION
try {
outboundGroupSessionRef = new OlmOutboundGroupSession();
} catch (OlmException e) {
assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false);
}
assertNotNull(outboundGroupSessionRef);
// serialize alice session
Context context = getInstrumentation().getContext();
try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(outboundGroupSessionRef);
objectOutput.flush();
objectOutput.close();
// deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject();
objectInput.close();
// get sessions IDs
String sessionKeyRef = outboundGroupSessionRef.sessionKey();
String sessionKeySerial = outboundGroupSessionSerial.sessionKey();
// session ID sanity check
assertFalse(TextUtils.isEmpty(sessionKeyRef));
assertFalse(TextUtils.isEmpty(sessionKeySerial));
// session IDs comparison
assertTrue(sessionKeyRef.equals(sessionKeySerial));
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
}
catch (IOException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage());
}
/*catch (OlmException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage());
}*/
catch (Exception e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
}
}
} }
package org.matrix.olm; package org.matrix.olm;
import android.content.Context;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.util.Log; import android.util.Log;
...@@ -11,8 +12,15 @@ import org.junit.Test; ...@@ -11,8 +12,15 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator; import java.util.Iterator;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -20,6 +28,7 @@ import static org.junit.Assert.assertTrue; ...@@ -20,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmSessionTest { public class OlmSessionTest {
private static final String LOG_TAG = "OlmSessionTest"; private static final String LOG_TAG = "OlmSessionTest";
private final String FILE_NAME_SERIAL_SESSION = "SerialSession";
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
...@@ -86,11 +95,16 @@ public class OlmSessionTest { ...@@ -86,11 +95,16 @@ public class OlmSessionTest {
} }
assertNotNull(bobOneTimeKey); assertNotNull(bobOneTimeKey);
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
// CREATE ALICE SESSION // CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession(); OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId()); assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob // CREATE ALICE OUTBOUND SESSION and encrypt message to bob
...@@ -101,7 +115,12 @@ public class OlmSessionTest { ...@@ -101,7 +115,12 @@ public class OlmSessionTest {
Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText); Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText);
// CREATE BOB INBOUND SESSION and decrypt message from alice // CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession(); OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
...@@ -173,6 +192,7 @@ public class OlmSessionTest { ...@@ -173,6 +192,7 @@ public class OlmSessionTest {
Iterator<String> generatedKeysIt = generatedKeys.keys(); Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) { if(generatedKeysIt.hasNext()) {
// return first otk
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
} }
assertNotNull(bobOneTimeKey); assertNotNull(bobOneTimeKey);
...@@ -181,7 +201,12 @@ public class OlmSessionTest { ...@@ -181,7 +201,12 @@ public class OlmSessionTest {
} }
// CREATE ALICE SESSION // CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession(); OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId()); assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob // CREATE ALICE OUTBOUND SESSION and encrypt message to bob
...@@ -192,7 +217,12 @@ public class OlmSessionTest { ...@@ -192,7 +217,12 @@ public class OlmSessionTest {
assertNotNull(encryptedAliceToBobMsg1); assertNotNull(encryptedAliceToBobMsg1);
// CREATE BOB INBOUND SESSION and decrypt message from alice // CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = new OlmSession(); OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
...@@ -210,6 +240,7 @@ public class OlmSessionTest { ...@@ -210,6 +240,7 @@ public class OlmSessionTest {
String clearMsg2 = "Isn't life grand?"; String clearMsg2 = "Isn't life grand?";
String clearMsg3 = "Let's go to the opera."; String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1); OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
assertNotNull(encryptedMsg1); assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2); OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
...@@ -217,6 +248,7 @@ public class OlmSessionTest { ...@@ -217,6 +248,7 @@ public class OlmSessionTest {
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3); OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3); assertNotNull(encryptedMsg3);
// alice decrypts bob's messages
String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1); String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
assertNotNull(decryptedMsg1); assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2); String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
...@@ -224,6 +256,7 @@ public class OlmSessionTest { ...@@ -224,6 +256,7 @@ public class OlmSessionTest {
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3); assertNotNull(decryptedMsg3);
// comparison tests
assertTrue(clearMsg1.equals(decryptedMsg1)); assertTrue(clearMsg1.equals(decryptedMsg1));
assertTrue(clearMsg2.equals(decryptedMsg2)); assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3)); assertTrue(clearMsg3.equals(decryptedMsg3));
...@@ -235,6 +268,7 @@ public class OlmSessionTest { ...@@ -235,6 +268,7 @@ public class OlmSessionTest {
aliceSession.releaseSession(); aliceSession.releaseSession();
} }
@Test @Test
public void test03AliceBobSessionId() { public void test03AliceBobSessionId() {
// creates alice & bob accounts // creates alice & bob accounts
...@@ -252,11 +286,22 @@ public class OlmSessionTest { ...@@ -252,11 +286,22 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId()); assertTrue(0!=aliceAccount.getOlmAccountId());
// CREATE ALICE SESSION // CREATE ALICE SESSION
OlmSession aliceSession = new OlmSession();
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId()); assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE BOB INBOUND SESSION and decrypt message from alice // CREATE ALICE SESSION
OlmSession bobSession = new OlmSession(); OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
e.printStackTrace();
}
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
String aliceSessionId = aliceSession.sessionIdentifier(); String aliceSessionId = aliceSession.sessionIdentifier();
...@@ -268,4 +313,164 @@ public class OlmSessionTest { ...@@ -268,4 +313,164 @@ public class OlmSessionTest {
// must be the same for both ends of the conversation // must be the same for both ends of the conversation
assertTrue(aliceSessionId.equals(bobSessionId)); assertTrue(aliceSessionId.equals(bobSessionId));
} }
// ********************************************************
// ************* SERIALIZATION TEST ***********************
// ********************************************************
/**
* Same as test02AliceToBobBackAndForth() but alice's session
* is serialized and de-serialized before performing the final
* comparison (encrypt vs )
*/
@Test
public void test03SessionSerialization() {
final int ONE_TIME_KEYS_NUMBER = 1;
String bobIdentityKey = null;
String bobOneTimeKey=null;
OlmAccount aliceAccount = null;
OlmAccount bobAccount = null;
OlmSession aliceSessionDeserial;
// creates alice & bob accounts
try {
aliceAccount = new OlmAccount();
bobAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(),false);
}
// test accounts creation
assertTrue(0!=bobAccount.getOlmAccountId());
assertTrue(0!=aliceAccount.getOlmAccountId());
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson);
try {
bobIdentityKey = bobIdentityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
assertTrue(null!=bobIdentityKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj);
try {
JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
// return first otk
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
assertTrue("Exception MSg="+e.getMessage(), false);
}
// CREATE ALICE SESSION
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=aliceSession.getOlmSessionId());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
String helloClearMsg = "Hello I'm Alice!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
assertNotNull(encryptedAliceToBobMsg1);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
assertNotNull(decryptedMsg01);
// MESSAGE COMPARISON: decrypted vs encrypted
assertTrue(helloClearMsg.equals(decryptedMsg01));
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
// BACK/FORTH MESSAGE COMPARISON
String clearMsg1 = "Hello I'm Bob!";
String clearMsg2 = "Isn't life grand?";
String clearMsg3 = "Let's go to the opera.";
// bob encrypts messages
OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
assertNotNull(encryptedMsg1);
OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
assertNotNull(encryptedMsg2);
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3);
// serialize alice session
Context context = getInstrumentation().getContext();
try {
FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(aliceSession);
objectOutput.flush();
objectOutput.close();
// deserialize session
FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
aliceSessionDeserial = (OlmSession) objectInput.readObject();
objectInput.close();
// test deserialize return value
assertNotNull(aliceSessionDeserial);
// de-serialized alice session decrypts bob's messages
String decryptedMsg1 = aliceSessionDeserial.decryptMessage(encryptedMsg1);
assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSessionDeserial.decryptMessage(encryptedMsg2);
assertNotNull(decryptedMsg2);
String decryptedMsg3 = aliceSessionDeserial.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3);
// comparison tests
assertTrue(clearMsg1.equals(decryptedMsg1));
assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3));
// clean objects..
bobAccount.releaseAccount();
aliceAccount.releaseAccount();
bobSession.releaseSession();
aliceSession.releaseSession();
aliceSessionDeserial.releaseSession();
}
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
catch (ClassNotFoundException e) {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());