diff options
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src')
5 files changed, 327 insertions, 112 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 6c1cbf3..838fba2 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 @@ -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; -import android.accounts.Account; import android.content.Context; -import android.content.SharedPreferences; import android.support.test.runner.AndroidJUnit4; import android.text.TextUtils; import android.util.Log; @@ -18,7 +32,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; -import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -28,7 +41,6 @@ import java.io.ObjectOutputStream; import java.util.Iterator; import static android.support.test.InstrumentationRegistry.getInstrumentation; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -71,6 +83,9 @@ public class OlmAccountTest { // TBD } + /** + * Basic test: creation and release. + */ @Test public void test01CreateReleaseAccount() { try { @@ -102,12 +117,18 @@ public class OlmAccountTest { 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 public void test05IdentityKeys() { JSONObject identityKeysJson = mOlmAccount.identityKeys(); assertNotNull(identityKeysJson); Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson); + // is JSON_KEY_FINGER_PRINT_KEY present? try { String fingerPrintKey = identityKeysJson.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); @@ -116,6 +137,7 @@ public class OlmAccountTest { assertTrue("Exception MSg="+e.getMessage(), false); } + // is JSON_KEY_IDENTITY_KEY present? try { String identityKey = identityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY); assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); @@ -138,12 +160,18 @@ public class OlmAccountTest { assertTrue(maxOneTimeKeys>0); } + /** + * Test one time keys generation. + */ @Test public void test07GenerateOneTimeKeys() { int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); assertTrue(0==retValue); } + /** + * Test the generated amount of one time keys = GENERATION_ONE_TIME_KEYS_NUMBER. + */ @Test public void test08OneTimeKeysJsonFormat() { int oneTimeKeysCount = 0; @@ -227,11 +255,8 @@ public class OlmAccountTest { // get keys references JSONObject identityKeysRef = accountRef.identityKeys(); JSONObject oneTimeKeysRef = accountRef.oneTimeKeys(); - - /*Context context = getInstrumentation().getContext(); - SharedPreferences sharedPref = context.getSharedPreferences("TestPref",Context.MODE_PRIVATE); - SharedPreferences.Editor editPref = sharedPref.edit(); - editPref.putLong();*/ + assertNotNull(identityKeysRef); + assertNotNull(oneTimeKeysRef); try { Context context = getInstrumentation().getContext(); @@ -249,20 +274,19 @@ public class OlmAccountTest { ObjectInputStream objectInput = new ObjectInputStream(fileInput); accountDeserial = (OlmAccount) objectInput.readObject(); objectInput.close(); - assertNotNull(accountDeserial); // get de-serialized keys - JSONObject identityKeys2 = accountDeserial.identityKeys(); - assertNotNull(identityKeys2); - JSONObject oneTimeKeys2 = accountDeserial.oneTimeKeys(); - assertNotNull(oneTimeKeys2); + JSONObject identityKeysDeserial = accountDeserial.identityKeys(); + JSONObject oneTimeKeysDeserial = accountDeserial.oneTimeKeys(); + assertNotNull(identityKeysDeserial); + assertNotNull(oneTimeKeysDeserial); // compare identity keys - assertTrue(identityKeys2.toString().equals(identityKeysRef.toString())); + assertTrue(identityKeysDeserial.toString().equals(identityKeysRef.toString())); // compare onetime keys - assertTrue(oneTimeKeys2.toString().equals(oneTimeKeysRef.toString())); + assertTrue(oneTimeKeysDeserial.toString().equals(oneTimeKeysRef.toString())); accountRef.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(); + } + } 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 c2a266b..64eb7ce 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,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; import android.content.Context; 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 caa1e46..f8519d1 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,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; import android.content.Context; import android.support.test.runner.AndroidJUnit4; import android.util.Log; -import org.json.JSONException; import org.json.JSONObject; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -18,7 +33,6 @@ 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; @@ -28,7 +42,9 @@ import static org.junit.Assert.assertTrue; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class 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 int ONE_TIME_KEYS_NUMBER = 4; private static OlmManager mOlmManager; @@ -73,30 +89,14 @@ public class OlmSessionTest { // 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); - } + 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); - 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()) { - bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); - } - assertNotNull(bobOneTimeKey); - } catch (JSONException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1); + assertNotNull(bobOneTimeKey); // CREATE ALICE SESSION OlmSession aliceSession = null; @@ -112,6 +112,7 @@ public class OlmSessionTest { String clearMsg = "Heloo bob , this is alice!"; OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg); assertNotNull(encryptedMsgToBob); + assertNotNull(encryptedMsgToBob.mCipherText); Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText); // CREATE BOB INBOUND SESSION and decrypt message from alice @@ -122,7 +123,7 @@ public class OlmSessionTest { assertTrue("Exception Msg="+e.getMessage(), false); } assertTrue(0!=bobSession.getOlmSessionId()); - assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); + assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); assertNotNull(decryptedMsg); @@ -131,6 +132,7 @@ public class OlmSessionTest { // clean objects.. assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + // release accounts bobAccount.releaseAccount(); aliceAccount.releaseAccount(); @@ -154,9 +156,8 @@ public class OlmSessionTest { */ @Test public void test02AliceToBobBackAndForth() { - final int ONE_TIME_KEYS_NUMBER = 1; - String bobIdentityKey = null; - String bobOneTimeKey=null; + String bobIdentityKey; + String bobOneTimeKey; OlmAccount aliceAccount = null; OlmAccount bobAccount = null; @@ -174,31 +175,14 @@ public class OlmSessionTest { // 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); - } + 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); - 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); - } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1); + assertNotNull(bobOneTimeKey); // CREATE ALICE SESSION OlmSession aliceSession = null; @@ -215,6 +199,7 @@ public class OlmSessionTest { OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); assertNotNull(encryptedAliceToBobMsg1); + assertNotNull(encryptedAliceToBobMsg1.mCipherText); // CREATE BOB INBOUND SESSION and decrypt message from alice OlmSession bobSession = null; @@ -224,7 +209,7 @@ public class OlmSessionTest { assertTrue("Exception Msg="+e.getMessage(), false); } assertTrue(0!=bobSession.getOlmSessionId()); - assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); + assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); // DECRYPT MESSAGE FROM ALICE String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); @@ -233,8 +218,6 @@ public class OlmSessionTest { // 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?"; @@ -256,15 +239,19 @@ public class OlmSessionTest { String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); assertNotNull(decryptedMsg3); + // and one more form alice.. + encryptedMsg1 = aliceSession.encryptMessage(clearMsg1); + // comparison tests assertTrue(clearMsg1.equals(decryptedMsg1)); assertTrue(clearMsg2.equals(decryptedMsg2)); assertTrue(clearMsg3.equals(decryptedMsg3)); // clean objects.. + assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); bobSession.releaseSession(); + aliceAccount.releaseAccount(); aliceSession.releaseSession(); } @@ -312,24 +299,86 @@ public class OlmSessionTest { // must be the same for both ends of the conversation 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 *********************** // ******************************************************** /** - * Same as test02AliceToBobBackAndForth() but alice's session + * Same as {@link #test02AliceToBobBackAndForth()}, but alice's session * is serialized and de-serialized before performing the final * comparison (encrypt vs ) */ @Test - public void test03SessionSerialization() { + public void test05SessionSerialization() { final int ONE_TIME_KEYS_NUMBER = 1; - String bobIdentityKey = null; - String bobOneTimeKey=null; + String bobIdentityKey; + String bobOneTimeKey; OlmAccount aliceAccount = null; OlmAccount bobAccount = null; - OlmSession aliceSessionDeserial; + OlmSession aliceSessionDeserial = null; // creates alice & bob accounts try { @@ -345,31 +394,14 @@ public class OlmSessionTest { // 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); - } + 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); - 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); - } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,1); + assertNotNull(bobOneTimeKey); // CREATE ALICE SESSION OlmSession aliceSession = null; @@ -386,6 +418,7 @@ public class OlmSessionTest { OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); assertNotNull(encryptedAliceToBobMsg1); + assertNotNull(encryptedAliceToBobMsg1.mCipherText); // CREATE BOB INBOUND SESSION and decrypt message from alice OlmSession bobSession = null; @@ -395,7 +428,7 @@ public class OlmSessionTest { assertTrue("Exception Msg="+e.getMessage(), false); } assertTrue(0!=bobSession.getOlmSessionId()); - assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); + assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText)); // DECRYPT MESSAGE FROM ALICE String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); @@ -404,8 +437,6 @@ public class OlmSessionTest { // 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?"; @@ -451,6 +482,7 @@ public class OlmSessionTest { assertTrue(clearMsg3.equals(decryptedMsg3)); // clean objects.. + assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); bobAccount.releaseAccount(); aliceAccount.releaseAccount(); bobSession.releaseSession(); @@ -473,4 +505,91 @@ public class OlmSessionTest { 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(); + } + } diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java index 12aff1f..e483d71 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java @@ -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; import android.support.test.runner.AndroidJUnit4; import android.text.TextUtils; import android.util.Log; -import org.json.JSONException; import org.json.JSONObject; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; -import java.util.Iterator; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -64,13 +74,8 @@ public class OlmUtilityTest { // get identities key (finger print key) JSONObject identityKeysJson = account.identityKeys(); assertNotNull(identityKeysJson); - try { - fingerPrintKey = identityKeysJson.getString(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); - assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); - } catch (JSONException e) { - e.printStackTrace(); - assertTrue("Exception MSg="+e.getMessage(), false); - } + fingerPrintKey = TestHelper.getFingerprintKey(identityKeysJson); + assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); // instantiate utility object OlmUtility utility = new OlmUtility(); diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java index fbb9e24..88b54e6 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java @@ -64,18 +64,23 @@ public class TestHelper { /** * Return the first one time key from the JSON object. * @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 */ - static public String getFirstOneTimeKey(JSONObject aIdentityKeysObj) { + static public String getOneTimeKey(JSONObject aIdentityKeysObj, int aKeyPosition) { String firstOneTimeKey = null; + int i=0; try { JSONObject generatedKeys = aIdentityKeysObj.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()) { - firstOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); + while(i<aKeyPosition) { + if (generatedKeysIt.hasNext()) { + firstOneTimeKey = generatedKeys.getString(generatedKeysIt.next()); + i++; + } } } catch (JSONException e) { assertTrue("Exception Msg=" + e.getMessage(), false); |