aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src/androidTest
diff options
context:
space:
mode:
authorpedroGitt <pedro.contreiras@amdocs.com>2016-10-21 18:09:20 +0200
committerpedroGitt <pedro.contreiras@amdocs.com>2016-10-21 18:09:20 +0200
commitfb87d8feee14b36a76d8022e2b742a6c1d65d0f0 (patch)
tree116b8df014a700354465b796f80db0599a6e38c4 /java/android/OlmLibSdk/olm-sdk/src/androidTest
parent1511962df194b9b414b3629b37b6fd3a42081295 (diff)
Serialization for OlmAccount and OlmSession OK
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src/androidTest')
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java17
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java72
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java221
3 files changed, 295 insertions, 15 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java
index 385fc0a..6c1cbf3 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java
@@ -42,7 +42,7 @@ public class OlmAccountTest {
private static OlmAccount mOlmAccount;
private static OlmManager mOlmManager;
private boolean mIsAccountCreated;
- final String FILE_NAME = "SerialTestFile";
+ private final String FILE_NAME = "SerialTestFile";
@BeforeClass
public static void setUpClass(){
@@ -170,7 +170,12 @@ public class OlmAccountTest {
@Test
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();
assertTrue(0 != sessionId);
@@ -230,16 +235,16 @@ public class OlmAccountTest {
try {
Context context = getInstrumentation().getContext();
- context.getFilesDir();
+ //context.getFilesDir();
fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
- // serialize
+ // serialize account
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(accountRef);
objectOutput.flush();
objectOutput.close();
- // deserialize
+ // deserialize account
FileInputStream fileInput = context.openFileInput(FILE_NAME);
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
accountDeserial = (OlmAccount) objectInput.readObject();
@@ -262,7 +267,6 @@ public class OlmAccountTest {
accountRef.releaseAccount();
accountDeserial.releaseAccount();
}
-
catch (FileNotFoundException e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage());
}
@@ -278,7 +282,6 @@ public class OlmAccountTest {
catch (Exception e) {
Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage());
}
-
}
diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
index fc3c1f0..e488ae0 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
@@ -1,5 +1,6 @@
package org.matrix.olm;
+import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;
import android.util.Log;
@@ -11,6 +12,14 @@ import org.junit.runner.RunWith;
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.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -19,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmGroupSessionTest {
private static final String LOG_TAG = "OlmSessionTest";
+ private final String FILE_NAME_SERIAL_SESSION = "SerialGroupSession";
private static OlmManager mOlmManager;
private static OlmOutboundGroupSession mAliceOutboundGroupSession;
@@ -141,4 +151,66 @@ public class OlmGroupSessionTest {
// release group sessions
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());
+ }
+
+ }
+
+
}
diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
index 4d7c5b0..8fd2744 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
@@ -1,5 +1,6 @@
package org.matrix.olm;
+import android.content.Context;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
@@ -11,8 +12,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
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 static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -20,6 +28,7 @@ import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmSessionTest {
private static final String LOG_TAG = "OlmSessionTest";
+ private final String FILE_NAME_SERIAL_SESSION = "SerialSession";
private static OlmManager mOlmManager;
@@ -86,11 +95,16 @@ public class OlmSessionTest {
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
- assertTrue("Exception MSg="+e.getMessage(), false);
+ assertTrue("Exception Msg="+e.getMessage(), false);
}
// 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());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
@@ -101,7 +115,12 @@ public class OlmSessionTest {
Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText);
// 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());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
@@ -173,6 +192,7 @@ public class OlmSessionTest {
Iterator<String> generatedKeysIt = generatedKeys.keys();
if(generatedKeysIt.hasNext()) {
+ // return first otk
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
@@ -181,7 +201,12 @@ public class OlmSessionTest {
}
// 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());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
@@ -192,7 +217,12 @@ public class OlmSessionTest {
assertNotNull(encryptedAliceToBobMsg1);
// 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());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
@@ -210,6 +240,7 @@ public class OlmSessionTest {
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);
@@ -217,6 +248,7 @@ public class OlmSessionTest {
OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
assertNotNull(encryptedMsg3);
+ // alice decrypts bob's messages
String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
assertNotNull(decryptedMsg1);
String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
@@ -224,6 +256,7 @@ public class OlmSessionTest {
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3);
+ // comparison tests
assertTrue(clearMsg1.equals(decryptedMsg1));
assertTrue(clearMsg2.equals(decryptedMsg2));
assertTrue(clearMsg3.equals(decryptedMsg3));
@@ -235,6 +268,7 @@ public class OlmSessionTest {
aliceSession.releaseSession();
}
+
@Test
public void test03AliceBobSessionId() {
// creates alice & bob accounts
@@ -252,11 +286,22 @@ public class OlmSessionTest {
assertTrue(0!=aliceAccount.getOlmAccountId());
// 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());
- // CREATE BOB INBOUND SESSION and decrypt message from alice
- OlmSession bobSession = new OlmSession();
+ // CREATE ALICE SESSION
+ OlmSession bobSession = null;
+ try {
+ bobSession = new OlmSession();
+ } catch (OlmException e) {
+ e.printStackTrace();
+ }
assertTrue(0!=bobSession.getOlmSessionId());
String aliceSessionId = aliceSession.sessionIdentifier();
@@ -268,4 +313,164 @@ public class OlmSessionTest {
// must be the same for both ends of the conversation
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());
+ }
+ 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());
+ }
+ }
}