From 139402611aff5919844109f2c7f126bec5c31534 Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Tue, 18 Oct 2016 16:05:28 +0200 Subject: Add OlmUtility class - add unit tests for OlmUtility - rename OlmGroupTest to OlmGroupSessionTest - update OlmException --- .../java/org/matrix/olm/OlmAccountTest.java | 37 +---- .../java/org/matrix/olm/OlmGroupSessionTest.java | 144 +++++++++++++++++++ .../java/org/matrix/olm/OlmGroupTest.java | 152 --------------------- .../java/org/matrix/olm/OlmSessionTest.java | 1 - .../java/org/matrix/olm/OlmUtilityTest.java | 89 ++++++++++++ 5 files changed, 234 insertions(+), 189 deletions(-) create mode 100644 java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java delete mode 100644 java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupTest.java create mode 100644 java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.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 70db63e..45f6e63 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 @@ -173,41 +173,6 @@ public class OlmAccountTest { 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 + // additional tests are performed in test01VerifyEd25519Signing() } - - - private void testJni(){ - OlmManager mgr = new OlmManager(); - String versionLib = mgr.getOlmLibVersion(); - Log.d(LOG_TAG, "## testJni(): lib version="+versionLib); - - OlmAccount account = new OlmAccount(); - - 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/OlmGroupSessionTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java new file mode 100644 index 0000000..fc3c1f0 --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java @@ -0,0 +1,144 @@ +package org.matrix.olm; + +import android.support.test.runner.AndroidJUnit4; +import android.text.TextUtils; +import android.util.Log; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; + + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +@RunWith(AndroidJUnit4.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class OlmGroupSessionTest { + private static final String LOG_TAG = "OlmSessionTest"; + + private static OlmManager mOlmManager; + private static OlmOutboundGroupSession mAliceOutboundGroupSession; + private static String mAliceSessionIdentifier; + private static long mAliceMessageIndex; + public static final String CLEAR_MESSAGE1 = "Hello!"; + private static String mAliceToBobMessage; + private static OlmInboundGroupSession mBobInboundGroupSession; + private static String mAliceOutboundSessionKey; + private static String mBobSessionIdentifier; + private static String mBobDecryptedMessage; + + @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 outbound group session + * - bob creates an inbound group session with alice's outbound session key + * - alice encrypts a message with its session + * - bob decrypts the encrypted message with its session + * - decrypted message is identical to original alice message + */ + @Test + public void test01CreateOutboundSession() { + // alice creates OUTBOUND GROUP SESSION + try { + mAliceOutboundGroupSession = new OlmOutboundGroupSession(); + } catch (OlmException e) { + assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); + } + } + + @Test + public void test02GetOutboundGroupSessionIdentifier() { + // test session ID + mAliceSessionIdentifier = mAliceOutboundGroupSession.sessionIdentifier(); + assertNotNull(mAliceSessionIdentifier); + assertTrue(mAliceSessionIdentifier.length() > 0); + } + + @Test + public void test03GetOutboundGroupSessionKey() { + // test session Key + mAliceOutboundSessionKey = mAliceOutboundGroupSession.sessionKey(); + assertNotNull(mAliceOutboundSessionKey); + assertTrue(mAliceOutboundSessionKey.length() > 0); + } + + @Test + public void test04GetOutboundGroupMessageIndex() { + // test message index before any encryption + mAliceMessageIndex = mAliceOutboundGroupSession.messageIndex(); + assertTrue(0 == mAliceMessageIndex); + } + + @Test + public void test05OutboundGroupEncryptMessage() { + // alice encrypts a message to bob + mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1); + assertFalse(TextUtils.isEmpty(mAliceToBobMessage)); + + // test message index after encryption is incremented + mAliceMessageIndex = mAliceOutboundGroupSession.messageIndex(); + assertTrue(1 == mAliceMessageIndex); + } + + @Test + public void test06CreateInboundGroupSession() { + // bob creates INBOUND GROUP SESSION with alice outbound key + try { + mBobInboundGroupSession = new OlmInboundGroupSession(mAliceOutboundSessionKey); + } catch (OlmException e) { + assertTrue("Exception in bob OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false); + } + } + + @Test + public void test08GetInboundGroupSessionIdentifier() { + // check both session identifiers are equals + mBobSessionIdentifier = mBobInboundGroupSession.sessionIdentifier(); + assertFalse(TextUtils.isEmpty(mBobSessionIdentifier)); + } + + @Test + public void test09SessionIdentifiersAreIdentical() { + // check both session identifiers are equals: alice vs bob + assertTrue(mAliceSessionIdentifier.equals(mBobSessionIdentifier)); + } + + @Test + public void test10InboundDecryptMessage() { + // test decrypted message + mBobDecryptedMessage = mBobInboundGroupSession.decryptMessage(mAliceToBobMessage); + assertFalse(TextUtils.isEmpty(mBobDecryptedMessage)); + assertTrue(mBobDecryptedMessage.equals(CLEAR_MESSAGE1)); + } + + @Test + public void test11InboundDecryptedMessageIdentical() { + // test decrypted message + assertTrue(mBobDecryptedMessage.equals(CLEAR_MESSAGE1)); + } + + @Test + public void test12ReleaseOutboundSession() { + // release group sessions + mAliceOutboundGroupSession.releaseSession(); + } + + @Test + public void test13ReleaseInboundSession() { + // release group sessions + mBobInboundGroupSession.releaseSession(); + } +} diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupTest.java deleted file mode 100644 index 5a5ca57..0000000 --- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.matrix.olm; - -import android.support.test.runner.AndroidJUnit4; -import android.text.TextUtils; -import android.util.Log; - -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmGroupTest { - private static final String LOG_TAG = "OlmSessionTest"; - - private static OlmManager mOlmManager; - private static OlmOutboundGroupSession mAliceOutboundSession; - private static String mAliceSessionIdentifier; - private static long mAliceMessageIndex; - public static final String CLEAR_MESSAGE1 = "Hello!"; - private static String mAliceToBobMessage; - private static OlmInboundGroupSession mBobInboundSession; - private static String mAliceOutboundSessionKey; - private static String mBobSessionIdentifier; - private static String mBobDecryptedMessage; - - @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 outbound group session - * - bob creates an inbound group session with alice's outbound session key - * - alice encrypts a message with its session - * - bob decrypts the encrypted message with its session - * - decrypted message is identical to original alice message - */ - @Test - public void test01CreateOutboundSession() { - // alice creates OUTBOUND GROUP SESSION - try { - mAliceOutboundSession = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - @Test - public void test02GetOutboundGroupSessionIdentifier() { - // test session ID - mAliceSessionIdentifier = mAliceOutboundSession.sessionIdentifier(); - assertNotNull(mAliceSessionIdentifier); - assertTrue(mAliceSessionIdentifier.length() > 0); - } - - @Test - public void test03GetOutboundGroupSessionKey() { - // test session Key - mAliceOutboundSessionKey = mAliceOutboundSession.sessionKey(); - assertNotNull(mAliceOutboundSessionKey); - assertTrue(mAliceOutboundSessionKey.length() > 0); - } - - @Test - public void test04GetOutboundGroupMessageIndex() { - // test message index before any encryption - mAliceMessageIndex = mAliceOutboundSession.messageIndex(); - assertTrue(0 == mAliceMessageIndex); - } - - @Test - public void test05OutboundGroupEncryptMessage() { - // alice encrypts a message to bob - mAliceToBobMessage = mAliceOutboundSession.encryptMessage(CLEAR_MESSAGE1); - assertFalse(TextUtils.isEmpty(mAliceToBobMessage)); - - // test message index after encryption is incremented - mAliceMessageIndex = mAliceOutboundSession.messageIndex(); - assertTrue(1== mAliceMessageIndex); - } - - @Test - public void test06CreateInboundGroupSession() { - // bob creates INBOUND GROUP SESSION with alice outbound key - try { - mBobInboundSession = new OlmInboundGroupSession(mAliceOutboundSessionKey); - } catch (OlmException e) { - assertTrue("Exception in bob OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - @Test - public void test07OutboundGroupSessionIdentifiers() { - // check session identifiers are equals - mAliceSessionIdentifier = mAliceOutboundSession.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(mAliceSessionIdentifier)); - } - - @Test - public void test08InboundGroupSessionIdentifiers() { - // check session identifiers are equals - mBobSessionIdentifier = mBobInboundSession.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(mBobSessionIdentifier)); - assertTrue(mAliceSessionIdentifier.equals(mBobSessionIdentifier)); - } - - @Test - public void test09SessionIdentifiersIdentical() { - // check session identifiers are equals - assertTrue(mAliceSessionIdentifier.equals(mBobSessionIdentifier)); - } - - @Test - public void test10InboundDecryptMessage() { - // test decrypted message - mBobDecryptedMessage = mBobInboundSession.decryptMessage(mAliceToBobMessage); - assertFalse(TextUtils.isEmpty(mBobDecryptedMessage)); - assertTrue(mBobDecryptedMessage.equals(CLEAR_MESSAGE1)); - } - - @Test - public void test11InboundDecryptedMessageIdentical() { - // test decrypted message - assertTrue(mBobDecryptedMessage.equals(CLEAR_MESSAGE1)); - } - - @Test - public void test12ReleaseOutboundSession() { - // release group sessions - mAliceOutboundSession.releaseSession(); - } - - @Test - public void test13ReleaseInboundSession() { - // release group sessions - mBobInboundSession.releaseSession(); - } -} 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 6056466..311d6a8 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 @@ -250,5 +250,4 @@ public class OlmSessionTest { // must be the same for both ends of the conversation assertTrue(aliceSessionId.equals(bobSessionId)); } - } 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 new file mode 100644 index 0000000..5175424 --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java @@ -0,0 +1,89 @@ +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; + +@RunWith(AndroidJUnit4.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class OlmUtilityTest { + private static final String LOG_TAG = "OlmAccountTest"; + private static final int GENERATION_ONE_TIME_KEYS_NUMBER = 50; + + 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 the signing API + */ + @Test + public void test01VerifyEd25519Signing() { + String fingerPrintKey = null; + String errorMsg = new String(); + String message = "{\"key1\":\"value1\",\"key2\":\"value2\"};"; + + // create account + OlmAccount account = new OlmAccount(); + assertNotNull(account); + + // sign message + String messageSignature = account.signMessage(message); + assertNotNull(messageSignature); + + // get identity 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); + } + + // instance utility + OlmUtility utility = new OlmUtility(); + boolean isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg); + assertTrue(isVerified); + + utility.releaseUtility(); + } + + + @Test + public void test02sha256() { + OlmUtility utility = new OlmUtility(); + String msgToHash = "The quick brown fox jumps over the lazy dog"; + + String hashResult = utility.sha256(msgToHash); + assertFalse(TextUtils.isEmpty(hashResult)); + + utility.releaseUtility(); + } +} -- cgit v1.2.3