From 88548f687e7f6f51a48ac9cf3a342354731ad694 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Wed, 4 Jan 2017 11:46:37 +0100 Subject: OlmAccount methods trigger an exception when they fail. --- .../java/org/matrix/olm/OlmAccountTest.java | 121 +++++++++++--- .../java/org/matrix/olm/OlmSessionTest.java | 185 ++++++++++++++++++--- .../java/org/matrix/olm/OlmUtilityTest.java | 18 +- 3 files changed, 277 insertions(+), 47 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/androidTest') 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 b275688..569b520 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 @@ -128,7 +128,12 @@ public class OlmAccountTest { */ @Test public void test05IdentityKeys() { - Map identityKeys = mOlmAccount.identityKeys(); + Map identityKeys = null; + try { + identityKeys = mOlmAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } assertNotNull(identityKeys); Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys); @@ -157,8 +162,15 @@ public class OlmAccountTest { */ @Test public void test07GenerateOneTimeKeys() { - int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - assertTrue(0==retValue); + String error = null; + + try { + mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + error = e.getMessage(); + } + + assertTrue(null == error); } /** @@ -167,7 +179,13 @@ public class OlmAccountTest { @Test public void test08OneTimeKeysJsonFormat() { int oneTimeKeysCount = 0; - Map> oneTimeKeysJson = mOlmAccount.oneTimeKeys(); + Map> oneTimeKeysJson = null; + + try { + oneTimeKeysJson = mOlmAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } assertNotNull(oneTimeKeysJson); try { @@ -195,26 +213,40 @@ public class OlmAccountTest { long sessionId = olmSession.getOlmSessionId(); assertTrue(0 != sessionId); - int sessionRetCode = mOlmAccount.removeOneTimeKeysForSession(olmSession); - // test against no matching keys - assertTrue(1 == sessionRetCode); + String errorMessage = null; + + try { + mOlmAccount.removeOneTimeKeys(olmSession); + } catch (Exception e) { + errorMessage = e.getMessage(); + } + assertTrue(null != errorMessage); olmSession.releaseSession(); sessionId = olmSession.getOlmSessionId(); - assertTrue("sessionRetCode="+sessionRetCode,0 == sessionId); + assertTrue(0 == sessionId); } @Test public void test11MarkOneTimeKeysAsPublished() { - int retCode = mOlmAccount.markOneTimeKeysAsPublished(); - // if OK => retCode=0 - assertTrue(0 == retCode); + try { + mOlmAccount.markOneTimeKeysAsPublished(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } } @Test public void test12SignMessage() { String clearMsg = "String to be signed by olm"; - String signedMsg = mOlmAccount.signMessage(clearMsg); + String signedMsg = null; + + try { + signedMsg = mOlmAccount.signMessage(clearMsg); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertNotNull(signedMsg); // additional tests are performed in test01VerifyEd25519Signing() } @@ -237,12 +269,29 @@ public class OlmAccountTest { assertTrue(e.getMessage(),false); } - int retValue = accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - assertTrue(0==retValue); + try { + accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(),false); + } // get keys references - Map identityKeysRef = accountRef.identityKeys(); - Map> oneTimeKeysRef = accountRef.oneTimeKeys(); + Map identityKeysRef = null; + + try { + identityKeysRef = accountRef.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + + Map> oneTimeKeysRef = null; + + try { + oneTimeKeysRef = accountRef.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertNotNull(identityKeysRef); assertNotNull(oneTimeKeysRef); @@ -304,12 +353,25 @@ public class OlmAccountTest { @Test public void test14GenerateOneTimeKeysError() { // keys number = 0 => no error - int retValue = mOlmAccount.generateOneTimeKeys(0); - assertTrue(0==retValue); + + String errorMessage = null; + try { + mOlmAccount.generateOneTimeKeys(0); + } catch (Exception e) { + errorMessage = e.getMessage(); + } + + assertTrue(null == errorMessage); // keys number = negative value - retValue = mOlmAccount.generateOneTimeKeys(-50); - assertTrue(-1==retValue); + errorMessage = null; + try { + mOlmAccount.generateOneTimeKeys(-50); + } catch (Exception e) { + errorMessage = e.getMessage(); + } + + assertTrue(null != errorMessage); } @Test @@ -321,9 +383,16 @@ public class OlmAccountTest { assertTrue(e.getMessage(),false); } - int sessionRetCode = olmAccount.removeOneTimeKeysForSession(null); + boolean sessionRetCode = true; + + try { + sessionRetCode = olmAccount.removeOneTimeKeys(null); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + // test against no matching keys - assertTrue(-1 == sessionRetCode); + assertFalse(sessionRetCode); olmAccount.releaseAccount(); } @@ -336,7 +405,13 @@ public class OlmAccountTest { } catch (OlmException e) { assertTrue(e.getMessage(),false); } - String signedMsg = olmAccount.signMessage(null); + String signedMsg = null; + + try { + signedMsg = olmAccount.signMessage(null); + } catch (Exception e) { + } + assertNull(signedMsg); olmAccount.releaseAccount(); 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 675d72c..e8b2288 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 @@ -92,13 +92,32 @@ public class OlmSessionTest { assertTrue(0!=aliceAccount.getOlmAccountId()); // get bob identity key - Map bobIdentityKeys = bobAccount.identityKeys(); + Map bobIdentityKeys = null; + + try { + bobIdentityKeys = bobAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); assertTrue(null!=bobIdentityKey); // get bob one time keys - assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - Map> bobOneTimeKeys = bobAccount.oneTimeKeys(); + try { + bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + Map> bobOneTimeKeys = null; + + try { + bobOneTimeKeys = bobAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); assertNotNull(bobOneTimeKey); @@ -141,7 +160,13 @@ public class OlmSessionTest { assertTrue(clearMsg.equals(decryptedMsg)); // clean objects.. - assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + boolean res = false; + try { + res = bobAccount.removeOneTimeKeys(bobSession); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertTrue(res); // release accounts bobAccount.releaseAccount(); @@ -191,13 +216,32 @@ public class OlmSessionTest { assertTrue(0!=aliceAccount.getOlmAccountId()); // get bob identity key - Map bobIdentityKeys = bobAccount.identityKeys(); + Map bobIdentityKeys = null; + + try { + bobIdentityKeys = bobAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); assertTrue(null!=bobIdentityKey); // get bob one time keys - assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - Map> bobOneTimeKeys = bobAccount.oneTimeKeys(); + try { + bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + Map> bobOneTimeKeys = null; + + try { + bobOneTimeKeys = bobAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); assertNotNull(bobOneTimeKey); @@ -279,7 +323,14 @@ public class OlmSessionTest { assertTrue(clearMsg1.equals(decryptedMsg1)); // clean objects.. - assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + boolean res = false; + try { + res = bobAccount.removeOneTimeKeys(bobSession); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertTrue(res); + bobAccount.releaseAccount(); aliceAccount.releaseAccount(); assertTrue(bobAccount.isReleased()); @@ -369,15 +420,46 @@ public class OlmSessionTest { } // get bob/luke identity key - Map bobIdentityKeys = bobAccount.identityKeys(); - Map aliceIdentityKeys = aliceAccount.identityKeys(); + Map bobIdentityKeys = null; + + try { + bobIdentityKeys = bobAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + + Map aliceIdentityKeys = null; + + try { + aliceIdentityKeys = aliceAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeys); // get bob/luke one time keys - assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - assertTrue(0 == aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - Map> bobOneTimeKeys = bobAccount.oneTimeKeys(); + try { + bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + try { + aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + Map> bobOneTimeKeys = null; + + try { + bobOneTimeKeys = bobAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeys, 1); // create alice inbound session for bob @@ -401,7 +483,14 @@ public class OlmSessionTest { //assertTrue(false==bobSession.matchesInboundSessionFrom(bobIdentityKey, encryptedAliceToBobMsg1.mCipherText)); // release objects - assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + boolean res = false; + try { + res = bobAccount.removeOneTimeKeys(bobSession); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertTrue(res); + aliceAccount.releaseAccount(); bobAccount.releaseAccount(); assertTrue(aliceAccount.isReleased()); @@ -443,13 +532,32 @@ public class OlmSessionTest { assertTrue(0!=aliceAccount.getOlmAccountId()); // get bob identity key - Map bobIdentityKeys = bobAccount.identityKeys(); + Map bobIdentityKeys = null; + + try { + bobIdentityKeys = bobAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); assertTrue(null!=bobIdentityKey); // get bob one time keys - assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - Map> bobOneTimeKeys = bobAccount.oneTimeKeys(); + try { + bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + Map> bobOneTimeKeys = null; + + try { + bobOneTimeKeys = bobAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); assertNotNull(bobOneTimeKey); @@ -538,7 +646,14 @@ public class OlmSessionTest { assertTrue(clearMsg3.equals(decryptedMsg3)); // clean objects.. - assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + boolean res = false; + try { + res = bobAccount.removeOneTimeKeys(bobSession); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertTrue(res); + bobAccount.releaseAccount(); aliceAccount.releaseAccount(); assertTrue(bobAccount.isReleased()); @@ -588,13 +703,32 @@ public class OlmSessionTest { } // get bob identity key - Map bobIdentityKeys = bobAccount.identityKeys(); + Map bobIdentityKeys = null; + + try { + bobIdentityKeys = bobAccount.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); assertTrue(null != bobIdentityKey); // get bob one time keys - assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER)); - Map> bobOneTimeKeys = bobAccount.oneTimeKeys(); + try { + bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + + Map> bobOneTimeKeys = null; + + try { + bobOneTimeKeys = bobAccount.oneTimeKeys(); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertNotNull(bobOneTimeKeys); String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); assertNotNull(bobOneTimeKey); @@ -678,7 +812,14 @@ public class OlmSessionTest { assertTrue(!aliceSession.matchesInboundSessionFrom(null,null)); // release objects - assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession)); + boolean res = false; + try { + res = bobAccount.removeOneTimeKeys(bobSession); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertTrue(res); + aliceAccount.releaseAccount(); bobAccount.releaseAccount(); assertTrue(aliceAccount.isReleased()); 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 e3ca544..4096001 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 @@ -71,11 +71,25 @@ public class OlmUtilityTest { assertNotNull(account); // sign message - String messageSignature = account.signMessage(message); + String messageSignature = null; + + try { + messageSignature = account.signMessage(message); + } catch (Exception e) { + assertTrue(e.getMessage(), false); + } + assertNotNull(messageSignature); // get identities key (finger print key) - Map identityKeys = account.identityKeys(); + Map identityKeys = null; + + try { + identityKeys = account.identityKeys(); + } catch (Exception e) { + assertTrue("identityKeys failed " + e.getMessage(), false); + } + assertNotNull(identityKeys); fingerPrintKey = TestHelper.getFingerprintKey(identityKeys); assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); -- cgit v1.2.3