Add missing copyright header

Add sanity tests for OlmAccount and OlmSession
Add a first version of MatchInboundSession
This commit is contained in:
pedroGitt 2016-10-25 18:21:50 +02:00
parent 232de794f2
commit b140e48183
5 changed files with 327 additions and 112 deletions

View file

@ -1,8 +1,22 @@
/*
* Copyright 2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.olm; package org.matrix.olm;
import android.accounts.Account;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
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;
@ -18,7 +32,6 @@ 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -28,7 +41,6 @@ import java.io.ObjectOutputStream;
import java.util.Iterator; import java.util.Iterator;
import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -71,6 +83,9 @@ public class OlmAccountTest {
// TBD // TBD
} }
/**
* Basic test: creation and release.
*/
@Test @Test
public void test01CreateReleaseAccount() { public void test01CreateReleaseAccount() {
try { try {
@ -102,12 +117,18 @@ public class OlmAccountTest {
assertTrue(0!=olmNativeInstance); assertTrue(0!=olmNativeInstance);
} }
/**
* Test if {@link OlmAccount#identityKeys()} returns a JSON object
* that contains the following keys: {@link OlmAccount#JSON_KEY_FINGER_PRINT_KEY}
* and {@link OlmAccount#JSON_KEY_IDENTITY_KEY}
*/
@Test @Test
public void test05IdentityKeys() { public void test05IdentityKeys() {
JSONObject identityKeysJson = mOlmAccount.identityKeys(); JSONObject identityKeysJson = mOlmAccount.identityKeys();
assertNotNull(identityKeysJson); assertNotNull(identityKeysJson);
Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson); Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson);
// is JSON_KEY_FINGER_PRINT_KEY present?
try { try {
String fingerPrintKey = identityKeysJson.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); String fingerPrintKey = identityKeysJson.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY);
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
@ -116,6 +137,7 @@ public class OlmAccountTest {
assertTrue("Exception MSg="+e.getMessage(), false); assertTrue("Exception MSg="+e.getMessage(), false);
} }
// is JSON_KEY_IDENTITY_KEY present?
try { try {
String identityKey = identityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY); String identityKey = identityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); assertTrue("identity key missing",!TextUtils.isEmpty(identityKey));
@ -138,12 +160,18 @@ public class OlmAccountTest {
assertTrue(maxOneTimeKeys>0); assertTrue(maxOneTimeKeys>0);
} }
/**
* Test one time keys generation.
*/
@Test @Test
public void test07GenerateOneTimeKeys() { public void test07GenerateOneTimeKeys() {
int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER);
assertTrue(0==retValue); assertTrue(0==retValue);
} }
/**
* Test the generated amount of one time keys = GENERATION_ONE_TIME_KEYS_NUMBER.
*/
@Test @Test
public void test08OneTimeKeysJsonFormat() { public void test08OneTimeKeysJsonFormat() {
int oneTimeKeysCount = 0; int oneTimeKeysCount = 0;
@ -227,11 +255,8 @@ public class OlmAccountTest {
// get keys references // get keys references
JSONObject identityKeysRef = accountRef.identityKeys(); JSONObject identityKeysRef = accountRef.identityKeys();
JSONObject oneTimeKeysRef = accountRef.oneTimeKeys(); JSONObject oneTimeKeysRef = accountRef.oneTimeKeys();
assertNotNull(identityKeysRef);
/*Context context = getInstrumentation().getContext(); assertNotNull(oneTimeKeysRef);
SharedPreferences sharedPref = context.getSharedPreferences("TestPref",Context.MODE_PRIVATE);
SharedPreferences.Editor editPref = sharedPref.edit();
editPref.putLong();*/
try { try {
Context context = getInstrumentation().getContext(); Context context = getInstrumentation().getContext();
@ -249,20 +274,19 @@ public class OlmAccountTest {
ObjectInputStream objectInput = new ObjectInputStream(fileInput); ObjectInputStream objectInput = new ObjectInputStream(fileInput);
accountDeserial = (OlmAccount) objectInput.readObject(); accountDeserial = (OlmAccount) objectInput.readObject();
objectInput.close(); objectInput.close();
assertNotNull(accountDeserial); assertNotNull(accountDeserial);
// get de-serialized keys // get de-serialized keys
JSONObject identityKeys2 = accountDeserial.identityKeys(); JSONObject identityKeysDeserial = accountDeserial.identityKeys();
assertNotNull(identityKeys2); JSONObject oneTimeKeysDeserial = accountDeserial.oneTimeKeys();
JSONObject oneTimeKeys2 = accountDeserial.oneTimeKeys(); assertNotNull(identityKeysDeserial);
assertNotNull(oneTimeKeys2); assertNotNull(oneTimeKeysDeserial);
// compare identity keys // compare identity keys
assertTrue(identityKeys2.toString().equals(identityKeysRef.toString())); assertTrue(identityKeysDeserial.toString().equals(identityKeysRef.toString()));
// compare onetime keys // compare onetime keys
assertTrue(oneTimeKeys2.toString().equals(oneTimeKeysRef.toString())); assertTrue(oneTimeKeysDeserial.toString().equals(oneTimeKeysRef.toString()));
accountRef.releaseAccount(); accountRef.releaseAccount();
accountDeserial.releaseAccount(); accountDeserial.releaseAccount();
@ -285,4 +309,50 @@ public class OlmAccountTest {
} }
// ****************************************************
// *************** SANITY CHECK TESTS *****************
// ****************************************************
@Test
public void test14GenerateOneTimeKeysError() {
// keys number = 0 => no error
int retValue = mOlmAccount.generateOneTimeKeys(0);
assertTrue(0==retValue);
// keys number = negative value
retValue = mOlmAccount.generateOneTimeKeys(-50);
assertTrue(-1==retValue);
}
@Test
public void test15RemoveOneTimeKeysForSessionError() {
OlmAccount olmAccount = null;
try {
olmAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(),false);
}
int sessionRetCode = olmAccount.removeOneTimeKeysForSession(null);
// test against no matching keys
assertTrue(-1 == sessionRetCode);
olmAccount.releaseAccount();
}
@Test
public void test16SignMessageError() {
OlmAccount olmAccount = null;
try {
olmAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(),false);
}
String clearMsg = null;
String signedMsg = olmAccount.signMessage(clearMsg);
assertNull(signedMsg);
olmAccount.releaseAccount();
}
} }

View file

@ -1,3 +1,19 @@
/*
* Copyright 2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.olm; package org.matrix.olm;
import android.content.Context; import android.content.Context;

View file

@ -1,10 +1,25 @@
/*
* Copyright 2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.olm; package org.matrix.olm;
import android.content.Context; import android.content.Context;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.util.Log; import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
@ -18,7 +33,6 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.Iterator;
import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -28,7 +42,9 @@ 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 INVALID_PRE_KEY = "invalid PRE KEY hu hu!";
private final String FILE_NAME_SERIAL_SESSION = "SerialSession"; private final String FILE_NAME_SERIAL_SESSION = "SerialSession";
private final int ONE_TIME_KEYS_NUMBER = 4;
private static OlmManager mOlmManager; private static OlmManager mOlmManager;
@ -73,30 +89,14 @@ public class OlmSessionTest {
// get bob identity key // get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys(); JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson); bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
try { assertTrue(null!=bobIdentityKey);
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 // get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys(); JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj); bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
try { assertNotNull(bobOneTimeKey);
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()) {
bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
}
assertNotNull(bobOneTimeKey);
} catch (JSONException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
// CREATE ALICE SESSION // CREATE ALICE SESSION
OlmSession aliceSession = null; OlmSession aliceSession = null;
@ -112,6 +112,7 @@ public class OlmSessionTest {
String clearMsg = "Heloo bob , this is alice!"; String clearMsg = "Heloo bob , this is alice!";
OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg); OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg);
assertNotNull(encryptedMsgToBob); assertNotNull(encryptedMsgToBob);
assertNotNull(encryptedMsgToBob.mCipherText);
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
@ -122,7 +123,7 @@ public class OlmSessionTest {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
assertNotNull(decryptedMsg); assertNotNull(decryptedMsg);
@ -131,6 +132,7 @@ public class OlmSessionTest {
// clean objects.. // clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
// release accounts // release accounts
bobAccount.releaseAccount(); bobAccount.releaseAccount();
aliceAccount.releaseAccount(); aliceAccount.releaseAccount();
@ -154,9 +156,8 @@ public class OlmSessionTest {
*/ */
@Test @Test
public void test02AliceToBobBackAndForth() { public void test02AliceToBobBackAndForth() {
final int ONE_TIME_KEYS_NUMBER = 1; String bobIdentityKey;
String bobIdentityKey = null; String bobOneTimeKey;
String bobOneTimeKey=null;
OlmAccount aliceAccount = null; OlmAccount aliceAccount = null;
OlmAccount bobAccount = null; OlmAccount bobAccount = null;
@ -174,31 +175,14 @@ public class OlmSessionTest {
// get bob identity key // get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys(); JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson); bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
try { assertTrue(null!=bobIdentityKey);
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 // get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys(); JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj); bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
try { assertNotNull(bobOneTimeKey);
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 // CREATE ALICE SESSION
OlmSession aliceSession = null; OlmSession aliceSession = null;
@ -215,6 +199,7 @@ public class OlmSessionTest {
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
assertNotNull(encryptedAliceToBobMsg1); assertNotNull(encryptedAliceToBobMsg1);
assertNotNull(encryptedAliceToBobMsg1.mCipherText);
// CREATE BOB INBOUND SESSION and decrypt message from alice // CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = null; OlmSession bobSession = null;
@ -224,7 +209,7 @@ public class OlmSessionTest {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// DECRYPT MESSAGE FROM ALICE // DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
@ -233,8 +218,6 @@ public class OlmSessionTest {
// MESSAGE COMPARISON: decrypted vs encrypted // MESSAGE COMPARISON: decrypted vs encrypted
assertTrue(helloClearMsg.equals(decryptedMsg01)); assertTrue(helloClearMsg.equals(decryptedMsg01));
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
// BACK/FORTH MESSAGE COMPARISON // BACK/FORTH MESSAGE COMPARISON
String clearMsg1 = "Hello I'm Bob!"; String clearMsg1 = "Hello I'm Bob!";
String clearMsg2 = "Isn't life grand?"; String clearMsg2 = "Isn't life grand?";
@ -256,15 +239,19 @@ public class OlmSessionTest {
String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
assertNotNull(decryptedMsg3); assertNotNull(decryptedMsg3);
// and one more form alice..
encryptedMsg1 = aliceSession.encryptMessage(clearMsg1);
// comparison tests // 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));
// clean objects.. // clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
bobAccount.releaseAccount(); bobAccount.releaseAccount();
aliceAccount.releaseAccount();
bobSession.releaseSession(); bobSession.releaseSession();
aliceAccount.releaseAccount();
aliceSession.releaseSession(); aliceSession.releaseSession();
} }
@ -312,24 +299,86 @@ 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));
aliceAccount.releaseAccount();
aliceSession.releaseSession();
bobAccount.releaseAccount();
bobSession.releaseSession();
}
@Test
public void test04MatchInboundSession() {
OlmAccount aliceAccount=null, bobAccount=null;
OlmSession aliceSession = null, bobSession = null;
// ACCOUNTS CREATION
try {
aliceAccount = new OlmAccount();
bobAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(), false);
}
// CREATE ALICE SESSION
try {
aliceSession = new OlmSession();
bobSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg=" + e.getMessage(), false);
}
// get bob/luke identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
JSONObject aliceIdentityKeysJson = aliceAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeysJson);
// get bob/luke one time keys
assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
assertTrue(0 == aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj, 1);
// create alice inbound session for bob
assertTrue(0==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey1));
String aliceClearMsg = "hello helooo to bob!";
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg);
assertTrue(false==bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));
// init bob session with alice PRE KEY
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// test matchesInboundSession() and matchesInboundSessionFrom()
assertTrue(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));
assertTrue(bobSession.matchesInboundSessionFrom(aliceIdentityKey, encryptedAliceToBobMsg1.mCipherText));
// following requires olm native lib new version with https://github.com/matrix-org/olm-backup/commit/7e9f3bebb8390f975a76c0188ce4cb460fe6692e
//assertTrue(false==bobSession.matchesInboundSessionFrom(bobIdentityKey, encryptedAliceToBobMsg1.mCipherText));
// release objects
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
aliceAccount.releaseAccount();
bobAccount.releaseAccount();
aliceSession.releaseSession();
bobSession.releaseSession();
} }
// ******************************************************** // ********************************************************
// ************* SERIALIZATION TEST *********************** // ************* SERIALIZATION TEST ***********************
// ******************************************************** // ********************************************************
/** /**
* Same as test02AliceToBobBackAndForth() but alice's session * Same as {@link #test02AliceToBobBackAndForth()}, but alice's session
* is serialized and de-serialized before performing the final * is serialized and de-serialized before performing the final
* comparison (encrypt vs ) * comparison (encrypt vs )
*/ */
@Test @Test
public void test03SessionSerialization() { public void test05SessionSerialization() {
final int ONE_TIME_KEYS_NUMBER = 1; final int ONE_TIME_KEYS_NUMBER = 1;
String bobIdentityKey = null; String bobIdentityKey;
String bobOneTimeKey=null; String bobOneTimeKey;
OlmAccount aliceAccount = null; OlmAccount aliceAccount = null;
OlmAccount bobAccount = null; OlmAccount bobAccount = null;
OlmSession aliceSessionDeserial; OlmSession aliceSessionDeserial = null;
// creates alice & bob accounts // creates alice & bob accounts
try { try {
@ -345,31 +394,14 @@ public class OlmSessionTest {
// get bob identity key // get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys(); JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
assertNotNull(bobIdentityKeysJson); bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
try { assertTrue(null!=bobIdentityKey);
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 // get bob one time keys
assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys(); JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj); bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
try { assertNotNull(bobOneTimeKey);
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 // CREATE ALICE SESSION
OlmSession aliceSession = null; OlmSession aliceSession = null;
@ -386,6 +418,7 @@ public class OlmSessionTest {
OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
assertNotNull(encryptedAliceToBobMsg1); assertNotNull(encryptedAliceToBobMsg1);
assertNotNull(encryptedAliceToBobMsg1.mCipherText);
// CREATE BOB INBOUND SESSION and decrypt message from alice // CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession bobSession = null; OlmSession bobSession = null;
@ -395,7 +428,7 @@ public class OlmSessionTest {
assertTrue("Exception Msg="+e.getMessage(), false); assertTrue("Exception Msg="+e.getMessage(), false);
} }
assertTrue(0!=bobSession.getOlmSessionId()); assertTrue(0!=bobSession.getOlmSessionId());
assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));
// DECRYPT MESSAGE FROM ALICE // DECRYPT MESSAGE FROM ALICE
String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
@ -404,8 +437,6 @@ public class OlmSessionTest {
// MESSAGE COMPARISON: decrypted vs encrypted // MESSAGE COMPARISON: decrypted vs encrypted
assertTrue(helloClearMsg.equals(decryptedMsg01)); assertTrue(helloClearMsg.equals(decryptedMsg01));
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
// BACK/FORTH MESSAGE COMPARISON // BACK/FORTH MESSAGE COMPARISON
String clearMsg1 = "Hello I'm Bob!"; String clearMsg1 = "Hello I'm Bob!";
String clearMsg2 = "Isn't life grand?"; String clearMsg2 = "Isn't life grand?";
@ -451,6 +482,7 @@ public class OlmSessionTest {
assertTrue(clearMsg3.equals(decryptedMsg3)); assertTrue(clearMsg3.equals(decryptedMsg3));
// clean objects.. // clean objects..
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
bobAccount.releaseAccount(); bobAccount.releaseAccount();
aliceAccount.releaseAccount(); aliceAccount.releaseAccount();
bobSession.releaseSession(); bobSession.releaseSession();
@ -473,4 +505,91 @@ public class OlmSessionTest {
Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage()); Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
} }
} }
// ****************************************************
// *************** SANITY CHECK TESTS *****************
// ****************************************************
@Test
public void test06SanityCheckErrors() {
final int ONE_TIME_KEYS_NUMBER = 5;
OlmAccount bobAccount = null;
OlmAccount aliceAccount = null;
// ALICE & BOB ACCOUNTS CREATION
try {
aliceAccount = new OlmAccount();
bobAccount = new OlmAccount();
} catch (OlmException e) {
assertTrue(e.getMessage(), false);
}
// get bob identity key
JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
assertTrue(null != bobIdentityKey);
// get bob one time keys
assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
assertNotNull(bobOneTimeKeysJsonObj);
String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1);
assertNotNull(bobOneTimeKey);
// CREATE ALICE SESSION
OlmSession aliceSession = null;
try {
aliceSession = new OlmSession();
} catch (OlmException e) {
assertTrue("Exception Msg=" + e.getMessage(), false);
}
// SANITY CHECK TESTS FOR: initOutboundSessionWithAccount()
assertTrue(-1==aliceSession.initOutboundSessionWithAccount(null, bobIdentityKey, bobOneTimeKey));
assertTrue(-1==aliceSession.initOutboundSessionWithAccount(aliceAccount, null, bobOneTimeKey));
assertTrue(-1==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, null));
assertTrue(-1==aliceSession.initOutboundSessionWithAccount(null, null, null));
// init properly
assertTrue(0==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
// SANITY CHECK TESTS FOR: encryptMessage()
assertTrue(null==aliceSession.encryptMessage(null));
// encrypt properly
OlmMessage encryptedMsgToBob = aliceSession.encryptMessage("A message for bob");
assertNotNull(encryptedMsgToBob);
// SANITY CHECK TESTS FOR: initInboundSessionWithAccount()
OlmSession bobSession = null;
try {
bobSession = new OlmSession();
assertTrue(-1==bobSession.initInboundSessionWithAccount(null, encryptedMsgToBob.mCipherText));
assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, null));
assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, INVALID_PRE_KEY));
// init properly
assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
} catch (OlmException e) {
assertTrue("Exception Msg="+e.getMessage(), false);
}
// SANITY CHECK TESTS FOR: decryptMessage()
String decryptedMsg = aliceSession.decryptMessage(null);
assertTrue(null==decryptedMsg);
// SANITY CHECK TESTS FOR: matchesInboundSession()
assertTrue(!aliceSession.matchesInboundSession(null));
// SANITY CHECK TESTS FOR: matchesInboundSessionFrom()
assertTrue(!aliceSession.matchesInboundSessionFrom(null,null));
// release objects
assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
aliceAccount.releaseAccount();
bobAccount.releaseAccount();
aliceSession.releaseSession();
bobSession.releaseSession();
}
} }

View file

@ -1,22 +1,32 @@
/*
* Copyright 2016 OpenMarket Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.olm; package org.matrix.olm;
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;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; 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.util.Iterator;
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;
@ -64,13 +74,8 @@ public class OlmUtilityTest {
// get identities key (finger print key) // get identities key (finger print key)
JSONObject identityKeysJson = account.identityKeys(); JSONObject identityKeysJson = account.identityKeys();
assertNotNull(identityKeysJson); assertNotNull(identityKeysJson);
try { fingerPrintKey = TestHelper.getFingerprintKey(identityKeysJson);
fingerPrintKey = identityKeysJson.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey));
} catch (JSONException e) {
e.printStackTrace();
assertTrue("Exception MSg="+e.getMessage(), false);
}
// instantiate utility object // instantiate utility object
OlmUtility utility = new OlmUtility(); OlmUtility utility = new OlmUtility();

View file

@ -64,18 +64,23 @@ public class TestHelper {
/** /**
* Return the first one time key from the JSON object. * Return the first one time key from the JSON object.
* @param aIdentityKeysObj JSON result of {@link OlmAccount#oneTimeKeys()} * @param aIdentityKeysObj JSON result of {@link OlmAccount#oneTimeKeys()}
* @param aKeyPosition the poistion of the key to be retrieved
* @return one time key string if operation succeed, null otherwise * @return one time key string if operation succeed, null otherwise
*/ */
static public String getFirstOneTimeKey(JSONObject aIdentityKeysObj) { static public String getOneTimeKey(JSONObject aIdentityKeysObj, int aKeyPosition) {
String firstOneTimeKey = null; String firstOneTimeKey = null;
int i=0;
try { try {
JSONObject generatedKeys = aIdentityKeysObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY); JSONObject generatedKeys = aIdentityKeysObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", generatedKeys); assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", generatedKeys);
Iterator<String> generatedKeysIt = generatedKeys.keys(); Iterator<String> generatedKeysIt = generatedKeys.keys();
if (generatedKeysIt.hasNext()) { while(i<aKeyPosition) {
firstOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); if (generatedKeysIt.hasNext()) {
firstOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
i++;
}
} }
} catch (JSONException e) { } catch (JSONException e) {
assertTrue("Exception Msg=" + e.getMessage(), false); assertTrue("Exception Msg=" + e.getMessage(), false);