diff options
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.java | 220 | ||||
-rw-r--r-- | java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java | 269 |
2 files changed, 489 insertions, 0 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 new file mode 100644 index 0000000..05480fc --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java @@ -0,0 +1,220 @@ +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.assertNotNull; +import static org.junit.Assert.assertTrue; + +@RunWith(AndroidJUnit4.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class OlmAccountTest { + private static final String LOG_TAG = "OlmAccountTest"; + private static final int GENERATION_ONE_TIME_KEYS_NUMBER = 50; + + private static OlmAccount mOlmAccount; + private static OlmManager mOlmManager; + private boolean mIsAccountCreated; + + @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); + } + + @AfterClass + public static void tearDownClass() { + // TBD + } + + @Before + public void setUp() { + if(mIsAccountCreated) { + assertNotNull(mOlmAccount); + } + } + + @After + public void tearDown() { + // TBD + } + + @Test + 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 test03InitNewAccount() { + assertTrue(mOlmAccount.initNewAccount()); + mIsAccountCreated = true; + } + + @Test + public void test04GetOlmAccountId() { + long olmNativeInstance = mOlmAccount.getOlmAccountId(); + Log.d(LOG_TAG,"## testGetOlmAccountId olmNativeInstance="+olmNativeInstance); + assertTrue(0!=olmNativeInstance); + } + + @Test + public void test05IdentityKeys() { + JSONObject identityKeysJson = mOlmAccount.identityKeys(); + assertNotNull(identityKeysJson); + Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeysJson); + + try { + String 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); + } + + try { + String identityKey = identityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY); + assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); + } catch (JSONException e) { + e.printStackTrace(); + assertTrue("Exception MSg="+e.getMessage(), false); + } + + + } + + //**************************************************** + //** ************** One time keys TESTS ************** + //**************************************************** + @Test + public void test06MaxOneTimeKeys() { + long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys(); + Log.d(LOG_TAG,"## testMaxOneTimeKeys(): maxOneTimeKeys="+maxOneTimeKeys); + + assertTrue(maxOneTimeKeys>0); + } + + @Test + public void test07GenerateOneTimeKeys() { + int retValue = mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); + assertTrue(0==retValue); + } + + @Test + public void test08OneTimeKeysJsonFormat() { + int oneTimeKeysCount = 0; + JSONObject generatedKeysJsonObj; + JSONObject oneTimeKeysJson = mOlmAccount.oneTimeKeys(); + assertNotNull(oneTimeKeysJson); + + try { + generatedKeysJsonObj = oneTimeKeysJson.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY); + assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=generatedKeysJsonObj); + + // test the count of the generated one time keys: + Iterator<String> generatedKeysIt = generatedKeysJsonObj.keys(); + while(generatedKeysIt.hasNext()){ + generatedKeysIt.next(); + oneTimeKeysCount++; + } + assertTrue("Expected count="+GENERATION_ONE_TIME_KEYS_NUMBER+" found="+oneTimeKeysCount,GENERATION_ONE_TIME_KEYS_NUMBER==oneTimeKeysCount); + + } catch (JSONException e) { + assertTrue("Exception MSg="+e.getMessage(), false); + } + } + + @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 test11MarkOneTimeKeysAsPublished() { + int retCode = mOlmAccount.markOneTimeKeysAsPublished(); + // if OK => retCode=0 + assertTrue(0 == retCode); + } + + @Test + public void test12SignMessage() { + String clearMsg = "String to be signed by olm"; + String signedMsg = mOlmAccount.signMessage(clearMsg); + assertNotNull(signedMsg); + // TODO add test to unsign the signedMsg and compare it ot clearMsg + } + + + private void testJni(){ + OlmManager mgr = new OlmManager(); + String versionLib = mgr.getOlmLibVersion(); + Log.d(LOG_TAG, "## testJni(): lib version="+versionLib); + + OlmAccount account = new OlmAccount(); + boolean initStatus = account.initNewAccount(); + + long accountNativeId = account.getOlmAccountId(); + Log.d(LOG_TAG, "## testJni(): lib accountNativeId="+accountNativeId); + + JSONObject identityKeys = account.identityKeys(); + Log.d(LOG_TAG, "## testJni(): identityKeysJson="+identityKeys.toString()); + + long maxOneTimeKeys = account.maxOneTimeKeys(); + Log.d(LOG_TAG, "## testJni(): lib maxOneTimeKeys="+maxOneTimeKeys); + + int generateRetCode = account.generateOneTimeKeys(50); + Log.d(LOG_TAG, "## testJni(): generateRetCode="+generateRetCode); + + JSONObject oneTimeKeysKeysJson = account.oneTimeKeys(); + Log.d(LOG_TAG, "## testJni(): oneTimeKeysKeysJson="+oneTimeKeysKeysJson.toString()); + + int asPublishedRetCode = account.markOneTimeKeysAsPublished(); + Log.d(LOG_TAG, "## testJni(): asPublishedRetCode="+asPublishedRetCode); + + String clearMsg ="My clear message"; + String signedMsg = account.signMessage(clearMsg); + Log.d(LOG_TAG, "## testJni(): signedMsg="+signedMsg); + + account.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 new file mode 100644 index 0000000..56048cc --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java @@ -0,0 +1,269 @@ +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); + } + + /** + * Basic test: + * - alice creates an account + * - bob creates an account + * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey) + * - alice encrypts a message with its session + * - bob creates an inbound session based on alice's encrypted message + * - bob decrypts the encrypted message with its session + */ + @Test + public void test01AliceToBob() { + final int ONE_TIME_KEYS_NUMBER = 5; + 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(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); + } + + // 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())); + // release accounts + bobAccount.releaseAccount(); + aliceAccount.releaseAccount(); + // release sessions + bobSession.releaseSession(); + aliceSession.releaseSession(); + } + + + /** + * Same as test01AliceToBob but with bob who's encrypting messages + * to alice and alice decrypt them.<br> + * - alice creates an account + * - bob creates an account + * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey) + * - alice encrypts a message with its own session + * - bob creates an inbound session based on alice's encrypted message + * - bob decrypts the encrypted message with its own session + * - bob encrypts messages with its own session + * - alice decrypts bob's messages with its own message + */ + @Test + public void test02AliceToBobBackAndForth() { + final int ONE_TIME_KEYS_NUMBER = 1; + 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(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); + } + + // 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 helloClearMsg = "Hello I'm Alice!"; + + OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); + assertNotNull(encryptedAliceToBobMsg1); + + // CREATE BOB INBOUND SESSION and decrypt message from alice + OlmSession bobSession = new OlmSession(); + bobSession.initNewSession(); + 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.getOlmSessionId())); + + // BACK/FORTH MESSAGE COMPARISON + String clearMsg1 = "Hello I'm Bob!"; + String clearMsg2 = "Isn't life grand?"; + String clearMsg3 = "Let's go to the opera."; + + OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1); + assertNotNull(encryptedMsg1); + OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2); + assertNotNull(encryptedMsg2); + OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3); + assertNotNull(encryptedMsg3); + + String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1); + assertNotNull(decryptedMsg1); + String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2); + assertNotNull(decryptedMsg2); + String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); + assertNotNull(decryptedMsg3); + + assertTrue(clearMsg1.equals(decryptedMsg1)); + assertTrue(clearMsg2.equals(decryptedMsg2)); + assertTrue(clearMsg3.equals(decryptedMsg3)); + + // clean objects.. + bobAccount.releaseAccount(); + aliceAccount.releaseAccount(); + bobSession.releaseSession(); + aliceSession.releaseSession(); + } + + @Test + public void test03AliceBobSessionId() { + // 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()); + + // CREATE ALICE SESSION + OlmSession aliceSession = new OlmSession(); + aliceSession.initNewSession(); + assertTrue(0!=aliceSession.getOlmSessionId()); + + // CREATE BOB INBOUND SESSION and decrypt message from alice + OlmSession bobSession = new OlmSession(); + bobSession.initNewSession(); + assertTrue(0!=bobSession.getOlmSessionId()); + + String aliceSessionId = aliceSession.sessionIdentifier(); + assertNotNull(aliceSessionId); + + String bobSessionId = bobSession.sessionIdentifier(); + assertNotNull(bobSessionId); + + // must be the same for both ends of the conversation + assertTrue(aliceSessionId.equals(bobSessionId)); + } + +} |