From f2ca1ce304deee9e8e858ea4a71ea22ba38a342b Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Tue, 11 Oct 2016 15:53:49 +0200 Subject: - Add OlmSession unit test - Simplify JNI function signatures definition (Account & Session) --- .../java/org/matrix/olm/OlmAccountTest.java | 62 +++++++----- .../java/org/matrix/olm/OlmSessionTest.java | 110 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 27 deletions(-) create mode 100644 java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java (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 f592aa4..05480fc 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 @@ -30,9 +30,6 @@ public class OlmAccountTest { private static OlmManager mOlmManager; private boolean mIsAccountCreated; - public static final String TEST_STRING = "This is a string"; - public static final long TEST_LONG = 12345678L; - @BeforeClass public static void setUpClass(){ // load native lib @@ -61,26 +58,35 @@ public class OlmAccountTest { } @Test - public void test1CreateAccount() { + public void test01CreateReleaseAccount() { + mOlmAccount = new OlmAccount(); + assertNotNull(mOlmAccount); + + mOlmAccount.releaseAccount(); + assertTrue(0 == mOlmAccount.getOlmAccountId()); + } + + @Test + public void test02CreateAccount() { mOlmAccount = new OlmAccount(); assertNotNull(mOlmAccount); } @Test - public void test2InitNewAccount() { + public void test03InitNewAccount() { assertTrue(mOlmAccount.initNewAccount()); mIsAccountCreated = true; } @Test - public void test3GetOlmAccountId() { + public void test04GetOlmAccountId() { long olmNativeInstance = mOlmAccount.getOlmAccountId(); Log.d(LOG_TAG,"## testGetOlmAccountId olmNativeInstance="+olmNativeInstance); assertTrue(0!=olmNativeInstance); } @Test - public void test4IdentityKeys() { + public void test05IdentityKeys() { JSONObject identityKeysJson = mOlmAccount.identityKeys(); assertNotNull(identityKeysJson); Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson); @@ -108,7 +114,7 @@ public class OlmAccountTest { //** ************** One time keys TESTS ************** //**************************************************** @Test - public void test5MaxOneTimeKeys() { + public void test06MaxOneTimeKeys() { long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys(); Log.d(LOG_TAG,"## testMaxOneTimeKeys(): maxOneTimeKeys="+maxOneTimeKeys); @@ -116,13 +122,13 @@ public class OlmAccountTest { } @Test - public void test6GenerateOneTimeKeys() { + public void test07GenerateOneTimeKeys() { int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); assertTrue(0==retValue); } @Test - public void test7OneTimeKeysJsonFormat() { + public void test08OneTimeKeysJsonFormat() { int oneTimeKeysCount = 0; JSONObject generatedKeysJsonObj; JSONObject oneTimeKeysJson = mOlmAccount.oneTimeKeys(); @@ -145,27 +151,31 @@ public class OlmAccountTest { } } - // TODO testRemoveOneTimeKeysForSession when session is available - /*@Test - public void testRemoveOneTimeKeysForSession() { - Log.d(LOG_TAG,"## testRemoveOneTimeKeysForSession"); - OLMSession olmSession = new OLMSession(); - - JSONArray keysJsonArray = mOlmAccount.removeOneTimeKeysForSession(olmSession); - - assertNotNull(keysJsonArray); - // TODO add extra test to test the JSON content format.. - }*/ + @Test + public void test10RemoveOneTimeKeysForSession() { + OlmSession olmSession = new OlmSession(); + olmSession.initNewSession(); + long sessionId = olmSession.getOlmSessionId(); + assertTrue(0 != sessionId); + + int sessionRetCode = mOlmAccount.removeOneTimeKeysForSession(sessionId); + // no one time key has been use in the session, so removeOneTimeKeysForSession() returns an error + assertTrue(0 != sessionRetCode); + + olmSession.releaseSession(); + sessionId = olmSession.getOlmSessionId(); + assertTrue("sessionRetCode="+sessionRetCode,0 == sessionId); + } @Test - public void test8MarkOneTimeKeysAsPublished() { + public void test11MarkOneTimeKeysAsPublished() { int retCode = mOlmAccount.markOneTimeKeysAsPublished(); // if OK => retCode=0 assertTrue(0 == retCode); } @Test - public void test9SignMessage() { + public void test12SignMessage() { String clearMsg = "String to be signed by olm"; String signedMsg = mOlmAccount.signMessage(clearMsg); assertNotNull(signedMsg); @@ -193,10 +203,8 @@ public class OlmAccountTest { int generateRetCode = account.generateOneTimeKeys(50); Log.d(LOG_TAG, "## testJni(): generateRetCode="+generateRetCode); - JSONObject onteTimeKeysKeysJson = account.oneTimeKeys(); - Log.d(LOG_TAG, "## testJni(): onteTimeKeysKeysJson="+onteTimeKeysKeysJson.toString()); - - // TODO removeOneTimeKeysForSession(session); + JSONObject oneTimeKeysKeysJson = account.oneTimeKeys(); + Log.d(LOG_TAG, "## testJni(): oneTimeKeysKeysJson="+oneTimeKeysKeysJson.toString()); int asPublishedRetCode = account.markOneTimeKeysAsPublished(); Log.d(LOG_TAG, "## testJni(): asPublishedRetCode="+asPublishedRetCode); 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 new file mode 100644 index 0000000..fb2eebc --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java @@ -0,0 +1,110 @@ +package org.matrix.olm; + +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; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; + +import java.util.Iterator; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +@RunWith(AndroidJUnit4.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class OlmSessionTest { + private static final String LOG_TAG = "OlmSessionTest"; + + private static OlmManager mOlmManager; + + @BeforeClass + public static void setUpClass(){ + // load native lib + mOlmManager = new OlmManager(); + + String version = mOlmManager.getOlmLibVersion(); + assertNotNull(version); + Log.d(LOG_TAG, "## setUpClass(): lib version="+version); + } + + @Test + public void test01AliceToBob() { + String bobIdentityKey = null; + String bobOneTimeKey=null; + + // creates alice & bob accounts + OlmAccount aliceAccount = new OlmAccount(); + aliceAccount.initNewAccount(); + + OlmAccount bobAccount = new OlmAccount(); + bobAccount.initNewAccount(); + + // 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(5)); + 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); + + // test the count of the generated one time keys: + Iterator 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 + OlmSession aliceSession = new OlmSession(); + aliceSession.initNewSession(); + assertTrue(0!=aliceSession.getOlmSessionId()); + + // CREATE ALICE OUTBOUND SESSION and encrypt message to bob + assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey)); + String clearMsg = "Heloo bob , this is alice!"; + OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg); + assertNotNull(encryptedMsgToBob); + Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText); + + // CREATE BOB INBOUND SESSION and decrypt message from alice + OlmSession bobSession = new OlmSession(); + bobSession.initNewSession(); + assertTrue(0!=bobSession.getOlmSessionId()); + assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText)); + String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); + assertNotNull(decryptedMsg); + + // MESSAGE COMPARISON: decrypted vs encrypted + assertTrue(clearMsg.equals(decryptedMsg)); + + // clean objects.. + assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession.getOlmSessionId())); + bobAccount.releaseAccount(); + aliceAccount.releaseAccount(); + bobSession.releaseSession(); + aliceSession.releaseSession(); + } +} -- cgit v1.2.3