diff options
Diffstat (limited to 'android/olm-sdk/src/androidTest/java/org')
7 files changed, 0 insertions, 2684 deletions
diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java deleted file mode 100644 index 44405e3..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java +++ /dev/null @@ -1,493 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations 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.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.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Map; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -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; - private final String FILE_NAME = "SerialTestFile"; - - @BeforeClass - public static void setUpClass(){ - // load native lib - mOlmManager = new OlmManager(); - - String olmLibVersion = mOlmManager.getOlmLibVersion(); - assertNotNull(olmLibVersion); - String olmSdkVersion = mOlmManager.getDetailedVersion(getInstrumentation().getContext()); - assertNotNull(olmLibVersion); - Log.d(LOG_TAG, "## setUpClass(): Versions - Android Olm SDK = "+olmSdkVersion+" Olm lib ="+olmLibVersion); - } - - @AfterClass - public static void tearDownClass() { - // TBD - } - - @Before - public void setUp() { - if(mIsAccountCreated) { - assertNotNull(mOlmAccount); - } - } - - @After - public void tearDown() { - // TBD - } - - /** - * Basic test: creation and release. - */ - @Test - public void test01CreateReleaseAccount() { - try { - mOlmAccount = new OlmAccount(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmAccount failed " + e.getMessage(), false); - } - assertNotNull(mOlmAccount); - - mOlmAccount.releaseAccount(); - assertTrue(0 == mOlmAccount.getOlmAccountId()); - } - - @Test - public void test02CreateAccount() { - try { - mOlmAccount = new OlmAccount(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmAccount failed " + e.getMessage(), false); - } - assertNotNull(mOlmAccount); - mIsAccountCreated = true; - } - - @Test - public void test04GetOlmAccountId() { - long olmNativeInstance = mOlmAccount.getOlmAccountId(); - Log.d(LOG_TAG,"## testGetOlmAccountId olmNativeInstance="+olmNativeInstance); - 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() { - Map<String, String> identityKeys = null; - try { - identityKeys = mOlmAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - assertNotNull(identityKeys); - Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys); - - // is JSON_KEY_FINGER_PRINT_KEY present? - String fingerPrintKey = TestHelper.getFingerprintKey(identityKeys); - assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); - - // is JSON_KEY_IDENTITY_KEY present? - String identityKey = TestHelper.getIdentityKey(identityKeys); - assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); - } - - //**************************************************** - //***************** ONE TIME KEYS TESTS ************** - //**************************************************** - @Test - public void test06MaxOneTimeKeys() { - long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys(); - Log.d(LOG_TAG,"## testMaxOneTimeKeys(): maxOneTimeKeys="+maxOneTimeKeys); - - assertTrue(maxOneTimeKeys>0); - } - - /** - * Test one time keys generation. - */ - @Test - public void test07GenerateOneTimeKeys() { - String error = null; - - try { - mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - error = e.getMessage(); - } - - assertTrue(null == error); - } - - /** - * Test the generated amount of one time keys = GENERATION_ONE_TIME_KEYS_NUMBER. - */ - @Test - public void test08OneTimeKeysJsonFormat() { - int oneTimeKeysCount = 0; - Map<String, Map<String, String>> oneTimeKeysJson = null; - - try { - oneTimeKeysJson = mOlmAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(oneTimeKeysJson); - - try { - Map<String, String> map = oneTimeKeysJson.get(OlmAccount.JSON_KEY_ONE_TIME_KEY); - assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=map); - - // test the count of the generated one time keys: - oneTimeKeysCount = map.size(); - - assertTrue("Expected count="+GENERATION_ONE_TIME_KEYS_NUMBER+" found="+oneTimeKeysCount,GENERATION_ONE_TIME_KEYS_NUMBER==oneTimeKeysCount); - - } catch (Exception e) { - assertTrue("Exception MSg="+e.getMessage(), false); - } - } - - @Test - public void test10RemoveOneTimeKeysForSession() { - OlmSession olmSession = null; - try { - olmSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - long sessionId = olmSession.getOlmSessionId(); - assertTrue(0 != sessionId); - - String errorMessage = null; - - try { - mOlmAccount.removeOneTimeKeys(olmSession); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - olmSession.releaseSession(); - sessionId = olmSession.getOlmSessionId(); - assertTrue(0 == sessionId); - } - - @Test - public void test11MarkOneTimeKeysAsPublished() { - try { - mOlmAccount.markOneTimeKeysAsPublished(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - } - - @Test - public void test12SignMessage() { - String clearMsg = "String to be signed by olm"; - String signedMsg = null; - - try { - signedMsg = mOlmAccount.signMessage(clearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(signedMsg); - // additional tests are performed in test01VerifyEd25519Signing() - } - - - // ******************************************************** - // ************* SERIALIZATION TEST *********************** - // ******************************************************** - - @Test - public void test13Serialization() { - FileOutputStream fileOutput; - ObjectOutputStream objectOutput; - OlmAccount accountRef = null; - OlmAccount accountDeserial = null; - - try { - accountRef = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - try { - accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(),false); - } - - // get keys references - Map<String, String> identityKeysRef = null; - - try { - identityKeysRef = accountRef.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - Map<String, Map<String, String>> oneTimeKeysRef = null; - - try { - oneTimeKeysRef = accountRef.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(identityKeysRef); - assertNotNull(oneTimeKeysRef); - - try { - Context context = getInstrumentation().getContext(); - //context.getFilesDir(); - fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); - - // serialize account - objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(accountRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize account - FileInputStream fileInput = context.openFileInput(FILE_NAME); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - accountDeserial = (OlmAccount) objectInput.readObject(); - objectInput.close(); - assertNotNull(accountDeserial); - - // get de-serialized keys - Map<String, String> identityKeysDeserial = accountDeserial.identityKeys(); - Map<String, Map<String, String>> oneTimeKeysDeserial = accountDeserial.oneTimeKeys(); - assertNotNull(identityKeysDeserial); - assertNotNull(oneTimeKeysDeserial); - - // compare identity keys - assertTrue(identityKeysDeserial.toString().equals(identityKeysRef.toString())); - - // compare onetime keys - assertTrue(oneTimeKeysDeserial.toString().equals(oneTimeKeysRef.toString())); - - accountRef.releaseAccount(); - accountDeserial.releaseAccount(); - } - catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - catch (IOException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception IOException Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - /*catch (OlmException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception OlmException Msg==" + e.getMessage()); - }*/ - catch (Exception e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - } - - - // **************************************************** - // *************** SANITY CHECK TESTS ***************** - // **************************************************** - - @Test - public void test14GenerateOneTimeKeysError() { - // keys number = 0 => no error - - String errorMessage = null; - try { - mOlmAccount.generateOneTimeKeys(0); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(null == errorMessage); - - // keys number = negative value - errorMessage = null; - try { - mOlmAccount.generateOneTimeKeys(-50); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(null != errorMessage); - } - - @Test - public void test15RemoveOneTimeKeysForSessionError() { - OlmAccount olmAccount = null; - try { - olmAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - try { - olmAccount.removeOneTimeKeys(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - olmAccount.releaseAccount(); - } - - @Test - public void test16SignMessageError() { - OlmAccount olmAccount = null; - try { - olmAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - String signedMsg = null; - - try { - signedMsg = olmAccount.signMessage(null); - } catch (Exception e) { - } - - assertNull(signedMsg); - - olmAccount.releaseAccount(); - } - - /** - * Create multiple accounts and check that identity keys are still different. - * This test validates random series are provide enough random values. - */ - @Test - public void test17MultipleAccountCreation() { - try { - OlmAccount account1 = new OlmAccount(); - OlmAccount account2 = new OlmAccount(); - OlmAccount account3 = new OlmAccount(); - OlmAccount account4 = new OlmAccount(); - OlmAccount account5 = new OlmAccount(); - OlmAccount account6 = new OlmAccount(); - OlmAccount account7 = new OlmAccount(); - OlmAccount account8 = new OlmAccount(); - OlmAccount account9 = new OlmAccount(); - OlmAccount account10 = new OlmAccount(); - - Map<String, String> identityKeys1 = account1.identityKeys(); - Map<String, String> identityKeys2 = account2.identityKeys(); - Map<String, String> identityKeys3 = account3.identityKeys(); - Map<String, String> identityKeys4 = account4.identityKeys(); - Map<String, String> identityKeys5 = account5.identityKeys(); - Map<String, String> identityKeys6 = account6.identityKeys(); - Map<String, String> identityKeys7 = account7.identityKeys(); - Map<String, String> identityKeys8 = account8.identityKeys(); - Map<String, String> identityKeys9 = account9.identityKeys(); - Map<String, String> identityKeys10 = account10.identityKeys(); - - String identityKey1 = TestHelper.getIdentityKey(identityKeys1); - String identityKey2 = TestHelper.getIdentityKey(identityKeys2); - assertFalse(identityKey1.equals(identityKey2)); - - String identityKey3 = TestHelper.getIdentityKey(identityKeys3); - assertFalse(identityKey2.equals(identityKey3)); - - String identityKey4 = TestHelper.getIdentityKey(identityKeys4); - assertFalse(identityKey3.equals(identityKey4)); - - String identityKey5 = TestHelper.getIdentityKey(identityKeys5); - assertFalse(identityKey4.equals(identityKey5)); - - String identityKey6 = TestHelper.getIdentityKey(identityKeys6); - assertFalse(identityKey5.equals(identityKey6)); - - String identityKey7 = TestHelper.getIdentityKey(identityKeys7); - assertFalse(identityKey6.equals(identityKey7)); - - String identityKey8 = TestHelper.getIdentityKey(identityKeys8); - assertFalse(identityKey7.equals(identityKey8)); - - String identityKey9 = TestHelper.getIdentityKey(identityKeys9); - assertFalse(identityKey8.equals(identityKey9)); - - String identityKey10 = TestHelper.getIdentityKey(identityKeys10); - assertFalse(identityKey9.equals(identityKey10)); - - account1.releaseAccount(); - account2.releaseAccount(); - account3.releaseAccount(); - account4.releaseAccount(); - account5.releaseAccount(); - account6.releaseAccount(); - account7.releaseAccount(); - account8.releaseAccount(); - account9.releaseAccount(); - account10.releaseAccount(); - - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java deleted file mode 100644 index 69eb0e8..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java +++ /dev/null @@ -1,627 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations 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.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 java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -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 final String FILE_NAME_SERIAL_OUT_SESSION = "SerialOutGroupSession"; - private final String FILE_NAME_SERIAL_IN_SESSION = "SerialInGroupSession"; - - private static OlmManager mOlmManager; - private static OlmOutboundGroupSession mAliceOutboundGroupSession; - private static String mAliceSessionIdentifier; - private static long mAliceMessageIndex; - private 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 = null; - - try { - mAliceSessionIdentifier = mAliceOutboundGroupSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(mAliceSessionIdentifier); - assertTrue(mAliceSessionIdentifier.length() > 0); - } - - @Test - public void test03GetOutboundGroupSessionKey() { - // test session Key - mAliceOutboundSessionKey = null; - - try { - mAliceOutboundSessionKey = mAliceOutboundGroupSession.sessionKey(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - 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 - try { - mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1); - } catch (Exception e) { - assertTrue("Exception in bob encryptMessage, Exception code=" + e.getMessage(), false); - } - 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 = null; - - try { - mBobSessionIdentifier = mBobInboundGroupSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - 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() { - mBobDecryptedMessage = null; - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = mBobInboundGroupSession.decryptMessage(mAliceToBobMessage); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - // test decrypted message - mBobDecryptedMessage = result.mDecryptedMessage; - assertFalse(TextUtils.isEmpty(mBobDecryptedMessage)); - assertTrue(0 == result.mIndex); - } - - @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(); - } - - @Test - public void test14CheckUnreleaseedCount() { - assertTrue(mAliceOutboundGroupSession.isReleased()); - assertTrue(mBobInboundGroupSession.isReleased()); - } - - @Test - public void test15SerializeOutboundSession() { - OlmOutboundGroupSession outboundGroupSessionRef=null; - OlmOutboundGroupSession outboundGroupSessionSerial; - - // create one OUTBOUND GROUP SESSION - try { - outboundGroupSessionRef = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(outboundGroupSessionRef); - - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_OUT_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(outboundGroupSessionRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_OUT_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject(); - assertNotNull(outboundGroupSessionSerial); - objectInput.close(); - - // get sessions keys - String sessionKeyRef = outboundGroupSessionRef.sessionKey(); - String sessionKeySerial = outboundGroupSessionSerial.sessionKey(); - assertFalse(TextUtils.isEmpty(sessionKeyRef)); - assertFalse(TextUtils.isEmpty(sessionKeySerial)); - - // session keys comparison - assertTrue(sessionKeyRef.equals(sessionKeySerial)); - - // get sessions IDs - String sessionIdRef = outboundGroupSessionRef.sessionIdentifier(); - String sessionIdSerial = outboundGroupSessionSerial.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(sessionIdRef)); - assertFalse(TextUtils.isEmpty(sessionIdSerial)); - - // session IDs comparison - assertTrue(sessionIdRef.equals(sessionIdSerial)); - - outboundGroupSessionRef.releaseSession(); - outboundGroupSessionSerial.releaseSession(); - - assertTrue(outboundGroupSessionRef.isReleased()); - assertTrue(outboundGroupSessionSerial.isReleased()); - } catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (OlmException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception OlmException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (IOException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (Exception e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - @Test - public void test16SerializeInboundSession() { - OlmOutboundGroupSession aliceOutboundGroupSession=null; - OlmInboundGroupSession bobInboundGroupSessionRef=null; - OlmInboundGroupSession bobInboundGroupSessionSerial; - - // alice creates OUTBOUND GROUP SESSION - try { - aliceOutboundGroupSession = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(aliceOutboundGroupSession); - - // get the session key from the outbound group session - String sessionKeyRef = null; - - try { - sessionKeyRef = aliceOutboundGroupSession.sessionKey(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(sessionKeyRef); - - // bob creates INBOUND GROUP SESSION - try { - bobInboundGroupSessionRef = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(bobInboundGroupSessionRef); - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_IN_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(bobInboundGroupSessionRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_IN_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - bobInboundGroupSessionSerial = (OlmInboundGroupSession)objectInput.readObject(); - assertNotNull(bobInboundGroupSessionSerial); - objectInput.close(); - - // get sessions IDs - String aliceSessionId = aliceOutboundGroupSession.sessionIdentifier(); - String sessionIdRef = bobInboundGroupSessionRef.sessionIdentifier(); - String sessionIdSerial = bobInboundGroupSessionSerial.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(aliceSessionId)); - assertFalse(TextUtils.isEmpty(sessionIdRef)); - assertFalse(TextUtils.isEmpty(sessionIdSerial)); - - // session IDs comparison - assertTrue(aliceSessionId.equals(sessionIdSerial)); - assertTrue(sessionIdRef.equals(sessionIdSerial)); - - aliceOutboundGroupSession.releaseSession(); - bobInboundGroupSessionRef.releaseSession(); - bobInboundGroupSessionSerial.releaseSession(); - - assertTrue(aliceOutboundGroupSession.isReleased()); - assertTrue(bobInboundGroupSessionRef.isReleased()); - assertTrue(bobInboundGroupSessionSerial.isReleased()); - } catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (OlmException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception OlmException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (IOException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (Exception e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - /** - * Create multiple outbound group sessions and check that session Keys are different. - * This test validates random series are provide enough random values. - */ - @Test - public void test17MultipleOutboundSession() { - OlmOutboundGroupSession outboundGroupSession1; - OlmOutboundGroupSession outboundGroupSession2; - OlmOutboundGroupSession outboundGroupSession3; - OlmOutboundGroupSession outboundGroupSession4; - OlmOutboundGroupSession outboundGroupSession5; - OlmOutboundGroupSession outboundGroupSession6; - OlmOutboundGroupSession outboundGroupSession7; - OlmOutboundGroupSession outboundGroupSession8; - - try { - outboundGroupSession1 = new OlmOutboundGroupSession(); - outboundGroupSession2 = new OlmOutboundGroupSession(); - outboundGroupSession3 = new OlmOutboundGroupSession(); - outboundGroupSession4 = new OlmOutboundGroupSession(); - outboundGroupSession5 = new OlmOutboundGroupSession(); - outboundGroupSession6 = new OlmOutboundGroupSession(); - outboundGroupSession7 = new OlmOutboundGroupSession(); - outboundGroupSession8 = new OlmOutboundGroupSession(); - - // get the session key from the outbound group sessions - String sessionKey1 = outboundGroupSession1.sessionKey(); - String sessionKey2 = outboundGroupSession2.sessionKey(); - assertFalse(sessionKey1.equals(sessionKey2)); - - String sessionKey3 = outboundGroupSession3.sessionKey(); - assertFalse(sessionKey2.equals(sessionKey3)); - - String sessionKey4 = outboundGroupSession4.sessionKey(); - assertFalse(sessionKey3.equals(sessionKey4)); - - String sessionKey5 = outboundGroupSession5.sessionKey(); - assertFalse(sessionKey4.equals(sessionKey5)); - - String sessionKey6 = outboundGroupSession6.sessionKey(); - assertFalse(sessionKey5.equals(sessionKey6)); - - String sessionKey7 = outboundGroupSession7.sessionKey(); - assertFalse(sessionKey6.equals(sessionKey7)); - - String sessionKey8 = outboundGroupSession8.sessionKey(); - assertFalse(sessionKey7.equals(sessionKey8)); - - // get the session IDs from the outbound group sessions - String sessionId1 = outboundGroupSession1.sessionIdentifier(); - String sessionId2 = outboundGroupSession2.sessionIdentifier(); - assertFalse(sessionId1.equals(sessionId2)); - - String sessionId3 = outboundGroupSession3.sessionKey(); - assertFalse(sessionId2.equals(sessionId3)); - - String sessionId4 = outboundGroupSession4.sessionKey(); - assertFalse(sessionId3.equals(sessionId4)); - - String sessionId5 = outboundGroupSession5.sessionKey(); - assertFalse(sessionId4.equals(sessionId5)); - - String sessionId6 = outboundGroupSession6.sessionKey(); - assertFalse(sessionId5.equals(sessionId6)); - - String sessionId7 = outboundGroupSession7.sessionKey(); - assertFalse(sessionId6.equals(sessionId7)); - - String sessionId8 = outboundGroupSession8.sessionKey(); - assertFalse(sessionId7.equals(sessionId8)); - - outboundGroupSession1.releaseSession(); - outboundGroupSession2.releaseSession(); - outboundGroupSession3.releaseSession(); - outboundGroupSession4.releaseSession(); - outboundGroupSession5.releaseSession(); - outboundGroupSession6.releaseSession(); - outboundGroupSession7.releaseSession(); - outboundGroupSession8.releaseSession(); - - assertTrue(outboundGroupSession1.isReleased()); - assertTrue(outboundGroupSession2.isReleased()); - assertTrue(outboundGroupSession3.isReleased()); - assertTrue(outboundGroupSession4.isReleased()); - assertTrue(outboundGroupSession5.isReleased()); - assertTrue(outboundGroupSession6.isReleased()); - assertTrue(outboundGroupSession7.isReleased()); - assertTrue(outboundGroupSession8.isReleased()); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - /** - * Specific test for the following run time error: - * "JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0 in call to NewStringUTF".<br> - * When the msg to decrypt contain emojis, depending on the android platform, the NewStringUTF() behaves differently and - * can even crash. - * This issue is described in details here: https://github.com/eclipsesource/J2V8/issues/142 - */ - @Test - public void test18TestBadCharacterCrashInDecrypt() { - OlmInboundGroupSession bobInboundGroupSession=null; - - // values taken from a "real life" crash case - String sessionKeyRef = "AgAAAAycZE6AekIctJWYxd2AWLOY15YmxZODm/WkgbpWkyycp6ytSp/R+wo84jRrzBNWmv6ySLTZ9R0EDOk9VI2eZyQ6Efdwyo1mAvrWvTkZl9yALPdkOIVHywyG65f1SNiLrnsln3hgsT1vUrISGyKtsljoUgQpr3JDPEhD0ilAi63QBjhnGCW252b+7nF+43rb6O6lwm93LaVwe2341Gdp6EkhTUvetALezEqDOtKN00wVqAbq0RQAnUJIowxHbMswg+FyoR1K1oCjnVEoF23O9xlAn5g1XtuBZP3moJlR2lwsBA"; - String msgToDecryptWithEmoji = "AwgNEpABpjs+tYF+0y8bWtzAgYAC3N55p5cPJEEiGPU1kxIHSY7f2aG5Fj4wmcsXUkhDv0UePj922kgf+Q4dFsPHKq2aVA93n8DJAQ/FRfcM98B9E6sKCZ/PsCF78uBvF12Aaq9D3pUHBopdd7llUfVq29d5y6ZwX5VDoqV2utsATkKjXYV9CbfZuvvBMQ30ZLjEtyUUBJDY9K4FxEFcULytA/IkVnATTG9ERuLF/yB6ukSFR+iUWRYAmtuOuU0k9BvaqezbGqNoK5Grlkes+dYX6/0yUObumcw9/iAI"; - - // bob creates INBOUND GROUP SESSION - try { - bobInboundGroupSession = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getExceptionCode(), false); - } - - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = bobInboundGroupSession.decryptMessage(msgToDecryptWithEmoji); - } catch (Exception e) { - assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getMessage(), false); - } - - assertNotNull(result.mDecryptedMessage); - assertTrue(13 == result.mIndex); - } - - /** - * Specific test to check an error message is returned by decryptMessage() API.<br> - * A corrupted encrypted message is passed, and a INVALID_BASE64 is - * espexted. - **/ - @Test - public void test19TestErrorMessageReturnedInDecrypt() { - OlmInboundGroupSession bobInboundGroupSession=null; - final String EXPECTED_ERROR_MESSAGE= "INVALID_BASE64"; - - String sessionKeyRef = "AgAAAAycZE6AekIctJWYxd2AWLOY15YmxZODm/WkgbpWkyycp6ytSp/R+wo84jRrzBNWmv6ySLTZ9R0EDOk9VI2eZyQ6Efdwyo1mAvrWvTkZl9yALPdkOIVHywyG65f1SNiLrnsln3hgsT1vUrISGyKtsljoUgQpr3JDPEhD0ilAi63QBjhnGCW252b+7nF+43rb6O6lwm93LaVwe2341Gdp6EkhTUvetALezEqDOtKN00wVqAbq0RQAnUJIowxHbMswg+FyoR1K1oCjnVEoF23O9xlAn5g1XtuBZP3moJlR2lwsBA"; - String corruptedEncryptedMsg = "AwgANYTHINGf87ge45ge7gr*/rg5ganything4gr41rrgr4re55tanythingmcsXUkhDv0UePj922kgf+"; - - // valid INBOUND GROUP SESSION - try { - bobInboundGroupSession = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in test19TestErrorMessageReturnedInDecrypt, Exception code=" + e.getExceptionCode(), false); - } - - String exceptionMessage = null; - try { - bobInboundGroupSession.decryptMessage(corruptedEncryptedMsg); - } catch (OlmException e) { - exceptionMessage = e.getMessage(); - } - - assertTrue(0!=EXPECTED_ERROR_MESSAGE.length()); - assertTrue(EXPECTED_ERROR_MESSAGE.equals(exceptionMessage)); - } - - - /** - * Test the import/export functions.<br> - **/ - @Test - public void test20TestInboundGroupSessionImportExport() { - - String sessionKey = "AgAAAAAwMTIzNDU2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCREVGM" + - "DEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkRFRjAxMjM0NTY3ODlBQkNERUYwMTIzND" + - "U2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMw0bdg1BDq4Px/slBow06q8n/B9WBfw" + - "WYyNOB8DlUmXGGwrFmaSb9bR/eY8xgERrxmP07hFmD9uqA2p8PMHdnV5ysmgufE6oLZ5+" + - "8/mWQOW3VVTnDIlnwd8oHUYRuk8TCQ"; - - String message = "AwgAEhAcbh6UpbByoyZxufQ+h2B+8XHMjhR69G8F4+qjMaFlnIXusJZX3r8LnRORG9T3D" + - "XFdbVuvIWrLyRfm4i8QRbe8VPwGRFG57B1CtmxanuP8bHtnnYqlwPsD"; - - - OlmInboundGroupSession inboundGroupSession = null; - - try { - inboundGroupSession = new OlmInboundGroupSession(sessionKey); - } catch (Exception e) { - assertTrue("OlmInboundGroupSession failed " + e.getMessage(), false); - } - - boolean isVerified = false; - - try { - isVerified = inboundGroupSession.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertTrue(isVerified); - - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = inboundGroupSession.decryptMessage(message); - } catch (Exception e) { - assertTrue("decryptMessage failed " + e.getMessage(), false); - } - - assertTrue(TextUtils.equals(result.mDecryptedMessage, "Message")); - assertTrue(0 == result.mIndex); - - String export = null; - - try { - export = inboundGroupSession.export(0); - } catch (Exception e) { - assertTrue("export failed " + e.getMessage(), false); - } - assertTrue(!TextUtils.isEmpty(export)); - - long index = -1; - try { - index = inboundGroupSession.getFirstKnownIndex(); - } catch (Exception e) { - assertTrue("getFirstKnownIndex failed " + e.getMessage(), false); - } - assertTrue(index >=0); - - inboundGroupSession.releaseSession(); - inboundGroupSession = null; - - OlmInboundGroupSession inboundGroupSession2 = null; - - try { - inboundGroupSession2 = inboundGroupSession.importSession(export); - } catch (Exception e) { - assertTrue("OlmInboundGroupSession failed " + e.getMessage(), false); - } - - try { - isVerified = inboundGroupSession2.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertFalse(isVerified); - - result = null; - try { - result = inboundGroupSession2.decryptMessage(message); - } catch (Exception e) { - assertTrue("decryptMessage failed " + e.getMessage(), false); - } - - assertTrue(TextUtils.equals(result.mDecryptedMessage, "Message")); - assertTrue(0 == result.mIndex); - - try { - isVerified = inboundGroupSession2.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertTrue(isVerified); - inboundGroupSession2.releaseSession(); - } -} - diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java deleted file mode 100644 index d1e4a2e..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright 2018,2019 New Vector 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.util.Log; - -import java.util.Arrays; - -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.assertNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmPkTest { - private static final String LOG_TAG = "OlmPkEncryptionTest"; - - private static OlmPkEncryption mOlmPkEncryption; - private static OlmPkDecryption mOlmPkDecryption; - private static OlmPkSigning mOlmPkSigning; - - @Test - public void test01EncryptAndDecrypt() { - try { - mOlmPkEncryption = new OlmPkEncryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - try { - mOlmPkDecryption = new OlmPkDecryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkEncryption); - assertNotNull(mOlmPkDecryption); - - String key = null; - try { - key = mOlmPkDecryption.generateKey(); - } catch (OlmException e) { - assertTrue("Exception in generateKey, Exception code=" + e.getExceptionCode(), false); - } - Log.d(LOG_TAG, "Ephemeral Key: " + key); - try { - mOlmPkEncryption.setRecipientKey(key); - } catch (OlmException e) { - assertTrue("Exception in setRecipientKey, Exception code=" + e.getExceptionCode(), false); - } - - String clearMessage = "Public key test"; - OlmPkMessage message = null; - try { - message = mOlmPkEncryption.encrypt(clearMessage); - } catch (OlmException e) { - assertTrue("Exception in encrypt, Exception code=" + e.getExceptionCode(), false); - } - Log.d(LOG_TAG, "message: " + message.mCipherText + " " + message.mMac + " " + message.mEphemeralKey); - - String decryptedMessage = null; - try { - decryptedMessage = mOlmPkDecryption.decrypt(message); - } catch (OlmException e) { - assertTrue("Exception in decrypt, Exception code=" + e.getExceptionCode(), false); - } - assertTrue(clearMessage.equals(decryptedMessage)); - - mOlmPkEncryption.releaseEncryption(); - mOlmPkDecryption.releaseDecryption(); - assertTrue(mOlmPkEncryption.isReleased()); - assertTrue(mOlmPkDecryption.isReleased()); - } - - @Test - public void test02PrivateKey() { - try { - mOlmPkDecryption = new OlmPkDecryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkDecryption); - - byte[] privateKey = { - (byte)0x77, (byte)0x07, (byte)0x6D, (byte)0x0A, - (byte)0x73, (byte)0x18, (byte)0xA5, (byte)0x7D, - (byte)0x3C, (byte)0x16, (byte)0xC1, (byte)0x72, - (byte)0x51, (byte)0xB2, (byte)0x66, (byte)0x45, - (byte)0xDF, (byte)0x4C, (byte)0x2F, (byte)0x87, - (byte)0xEB, (byte)0xC0, (byte)0x99, (byte)0x2A, - (byte)0xB1, (byte)0x77, (byte)0xFB, (byte)0xA5, - (byte)0x1D, (byte)0xB9, (byte)0x2C, (byte)0x2A - }; - - assertTrue(privateKey.length == OlmPkDecryption.privateKeyLength()); - - try { - mOlmPkDecryption.setPrivateKey(privateKey); - } catch (OlmException e) { - assertTrue("Exception in setPrivateKey, Exception code=" + e.getExceptionCode(), false); - } - - byte[] privateKeyCopy = null; - - try { - privateKeyCopy = mOlmPkDecryption.privateKey(); - } catch (OlmException e) { - assertTrue("Exception in privateKey, Exception code=" + e.getExceptionCode(), false); - } - - assertTrue(Arrays.equals(privateKey, privateKeyCopy)); - - mOlmPkDecryption.releaseDecryption(); - assertTrue(mOlmPkDecryption.isReleased()); - } - - @Test - public void test03Signing() { - try { - mOlmPkSigning = new OlmPkSigning(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkSigning failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkSigning); - - byte[] seed = null; - try { - seed = OlmPkSigning.generateSeed(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("generateSeed failed " + e.getMessage(), false); - } - - assertTrue(seed.length == OlmPkSigning.seedLength()); - - String pubkey = null; - try { - pubkey = mOlmPkSigning.initWithSeed(seed); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("initWithSeed failed " + e.getMessage(), false); - } - - String message = "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness."; - - String signature = null; - try { - signature = mOlmPkSigning.sign(message); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("sign failed " + e.getMessage(), false); - } - - OlmUtility olmUtility = null; - try { - olmUtility = new OlmUtility(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("olmUtility failed " + e.getMessage(), false); - } - - try { - olmUtility.verifyEd25519Signature(signature, pubkey, message); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("Signature verification failed " + e.getMessage(), false); - } - - mOlmPkSigning.releaseSigning(); - assertTrue(mOlmPkSigning.isReleased()); - - olmUtility.releaseUtility(); - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java deleted file mode 100644 index bbb50ae..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2019 New Vector 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.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.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmSasTest { - - private static OlmManager mOlmManager; - - //Enable the native lib - @BeforeClass - public static void setUpClass() { - // load native librandomBytesOfLength - mOlmManager = new OlmManager(); - } - - @Test - public void testSASCode() { - OlmSAS aliceSas = null; - OlmSAS bobSas = null; - - try { - aliceSas = new OlmSAS(); - bobSas = new OlmSAS(); - - String alicePKey = aliceSas.getPublicKey(); - String bobPKey = bobSas.getPublicKey(); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice pub Key is " + alicePKey); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob pub Key is " + bobPKey); - - aliceSas.setTheirPublicKey(bobPKey); - bobSas.setTheirPublicKey(alicePKey); - - int codeLength = 6; - byte[] alice_sas = aliceSas.generateShortCode("SAS", codeLength); - byte[] bob_sas = bobSas.generateShortCode("SAS", codeLength); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice SAS is " + new String(alice_sas, "UTF-8")); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob SAS is " + new String(bob_sas, "UTF-8")); - - assertEquals(codeLength, alice_sas.length); - assertEquals(codeLength, bob_sas.length); - assertArrayEquals(alice_sas, bob_sas); - - String aliceMac = aliceSas.calculateMac("Hello world!", "SAS"); - String bobMac = bobSas.calculateMac("Hello world!", "SAS"); - - assertEquals(aliceMac, bobMac); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice Mac is " + aliceMac); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob Mac is " + bobMac); - - - String aliceLongKdfMac = aliceSas.calculateMacLongKdf("Hello world!", "SAS"); - String bobLongKdfMac = bobSas.calculateMacLongKdf("Hello world!", "SAS"); - - assertEquals("Mac should be the same", aliceLongKdfMac, bobLongKdfMac); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice lkdf Mac is " + aliceLongKdfMac); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob lkdf Mac is " + bobLongKdfMac); - - - } catch (Exception e) { - assertTrue("OlmSas init failed " + e.getMessage(), false); - e.printStackTrace(); - } finally { - if (aliceSas != null) { - aliceSas.releaseSas(); - } - if (bobSas != null) { - bobSas.releaseSas(); - } - } - } - -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java deleted file mode 100644 index e08b151..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java +++ /dev/null @@ -1,1014 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations 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.text.TextUtils; -import android.util.Log; - -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.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Map; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -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 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; - - @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; - OlmAccount bobAccount = null; - OlmAccount aliceAccount = null; - - // ALICE & BOB ACCOUNTS CREATION - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> 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 - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - String clearMsg = "Heloo bob , this is alice!"; - OlmMessage encryptedMsgToBob = null; - try { - encryptedMsgToBob = aliceSession.encryptMessage(clearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsgToBob); - assertNotNull(encryptedMsgToBob.mCipherText); - Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - try { - bobSession.initInboundSession(bobAccount, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - String decryptedMsg = null; - try { - decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(clearMsg.equals(decryptedMsg)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - // release accounts - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - // release sessions - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - - /** - * 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 - * - alice encrypts a message - * - bob decrypts the encrypted message - */ - @Test - public void test02AliceToBobBackAndForth() { - String bobIdentityKey; - String bobOneTimeKey; - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - - // creates alice & bob accounts - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> 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 - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String helloClearMsg = "Hello I'm Alice!"; - - OlmMessage encryptedAliceToBobMsg1 = null; - - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(encryptedAliceToBobMsg1); - assertNotNull(encryptedAliceToBobMsg1.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - - assertTrue(0!=bobSession.getOlmSessionId()); - - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // DECRYPT MESSAGE FROM ALICE - String decryptedMsg01 = null; - try { - decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg01); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(helloClearMsg.equals(decryptedMsg01)); - - // BACK/FORTH MESSAGE COMPARISON - String clearMsg1 = "Hello I'm Bob!"; - String clearMsg2 = "Isn't life grand?"; - String clearMsg3 = "Let's go to the opera."; - - // bob encrypts messages - OlmMessage encryptedMsg1 = null; - try { - encryptedMsg1 = bobSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - OlmMessage encryptedMsg2 = null; - try { - encryptedMsg2 = bobSession.encryptMessage(clearMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg2); - - - OlmMessage encryptedMsg3 = null; - try { - encryptedMsg3 = bobSession.encryptMessage(clearMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg3); - - // alice decrypts bob's messages - String decryptedMsg1 = null; - try { - decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg1); - String decryptedMsg2 = null; - try { - decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg2); - String decryptedMsg3 = null; - try { - decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg3); - - // comparison tests - assertTrue(clearMsg1.equals(decryptedMsg1)); - assertTrue(clearMsg2.equals(decryptedMsg2)); - assertTrue(clearMsg3.equals(decryptedMsg3)); - - // and one more from alice to bob - clearMsg1 = "another message from Alice to Bob!!"; - encryptedMsg1 = null; - - try { - encryptedMsg1 = aliceSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - decryptedMsg1 = null; - try { - decryptedMsg1 = bobSession.decryptMessage(encryptedMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg1); - assertTrue(clearMsg1.equals(decryptedMsg1)); - - // comparison test - assertTrue(clearMsg1.equals(decryptedMsg1)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - - @Test - public void test03AliceBobSessionId() { - // creates alice & bob accounts - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // CREATE ALICE SESSION - - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE SESSION - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue(e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - String aliceSessionId = null; - try { - aliceSessionId = aliceSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(aliceSessionId); - - String bobSessionId = null; - try { - bobSessionId = bobSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(bobSessionId); - - // must be the same for both ends of the conversation - assertTrue(aliceSessionId.equals(bobSessionId)); - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - @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 - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - Map<String, String> 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 - 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<String, Map<String, String>> 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 - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String aliceClearMsg = "hello helooo to bob!"; - OlmMessage encryptedAliceToBobMsg1 = null; - - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText)); - - // init bob session with alice PRE KEY - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // 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 - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - aliceSession.releaseSession(); - bobSession.releaseSession(); - assertTrue(aliceSession.isReleased()); - assertTrue(bobSession.isReleased()); - } - - // ******************************************************** - // ************* SERIALIZATION TEST *********************** - // ******************************************************** - /** - * Same as {@link #test02AliceToBobBackAndForth()}, but alice's session - * is serialized and de-serialized before performing the final - * comparison (encrypt vs ) - */ - @Test - public void test05SessionSerialization() { - final int ONE_TIME_KEYS_NUMBER = 1; - String bobIdentityKey; - String bobOneTimeKey; - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - OlmSession aliceSessionDeserial = null; - - // creates alice & bob accounts - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> 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 - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String helloClearMsg = "Hello I'm Alice!"; - - OlmMessage encryptedAliceToBobMsg1 = null; - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedAliceToBobMsg1); - assertNotNull(encryptedAliceToBobMsg1.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - // init bob session with alice PRE KEY - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // DECRYPT MESSAGE FROM ALICE - String decryptedMsg01 = null; - - try { - decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg01); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(helloClearMsg.equals(decryptedMsg01)); - - // BACK/FORTH MESSAGE COMPARISON - String clearMsg1 = "Hello I'm Bob!"; - String clearMsg2 = "Isn't life grand?"; - String clearMsg3 = "Let's go to the opera."; - - // bob encrypts messages - OlmMessage encryptedMsg1 = null; - try { - encryptedMsg1 = bobSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - OlmMessage encryptedMsg2 = null; - try { - encryptedMsg2 = bobSession.encryptMessage(clearMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg2); - - OlmMessage encryptedMsg3 = null; - try { - encryptedMsg3 = bobSession.encryptMessage(clearMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg3); - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(aliceSession); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - aliceSessionDeserial = (OlmSession) objectInput.readObject(); - objectInput.close(); - - // test deserialize return value - assertNotNull(aliceSessionDeserial); - - // de-serialized alice session decrypts bob's messages - String decryptedMsg1 = aliceSessionDeserial.decryptMessage(encryptedMsg1); - assertNotNull(decryptedMsg1); - String decryptedMsg2 = aliceSessionDeserial.decryptMessage(encryptedMsg2); - assertNotNull(decryptedMsg2); - String decryptedMsg3 = aliceSessionDeserial.decryptMessage(encryptedMsg3); - assertNotNull(decryptedMsg3); - - // comparison tests - assertTrue(clearMsg1.equals(decryptedMsg1)); - assertTrue(clearMsg2.equals(decryptedMsg2)); - assertTrue(clearMsg3.equals(decryptedMsg3)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - aliceSessionDeserial.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - assertTrue(aliceSessionDeserial.isReleased()); - } - catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } - catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - catch (IOException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - /*catch (OlmException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage()); - }*/ - catch (Exception e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - - // **************************************************** - // *************** 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 - Map<String, String> 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 - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(bobOneTimeKeys); - String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,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() - String errorMessage = null; - try { - aliceSession.initOutboundSession(null, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, null, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(null, null, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - // init properly - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null == errorMessage); - - // SANITY CHECK TESTS FOR: encryptMessage() - OlmMessage message = null; - try { - message = aliceSession.encryptMessage(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertTrue(null==message); - - // encrypt properly - OlmMessage encryptedMsgToBob = null; - try { - encryptedMsgToBob = aliceSession.encryptMessage("A message for bob"); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsgToBob); - - // SANITY CHECK TESTS FOR: initInboundSessionWithAccount() - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - errorMessage = null; - try { - bobSession.initInboundSession(null, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, INVALID_PRE_KEY); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - // init properly - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(TextUtils.isEmpty(errorMessage)); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - - // SANITY CHECK TESTS FOR: decryptMessage() - String decryptedMsg = null; - try { - decryptedMsg = aliceSession.decryptMessage(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertTrue(null==decryptedMsg); - - // SANITY CHECK TESTS FOR: matchesInboundSession() - assertTrue(!aliceSession.matchesInboundSession(null)); - - // SANITY CHECK TESTS FOR: matchesInboundSessionFrom() - assertTrue(!aliceSession.matchesInboundSessionFrom(null,null)); - - // release objects - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - aliceSession.releaseSession(); - bobSession.releaseSession(); - assertTrue(aliceSession.isReleased()); - assertTrue(bobSession.isReleased()); - } - -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java deleted file mode 100644 index b560bff..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations 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.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.Map; - -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 = null; - String message = "{\"algorithms\":[\"m.megolm.v1.aes-sha2\",\"m.olm.v1.curve25519-aes-sha2\"],\"device_id\":\"YMBYCWTWCG\",\"keys\":{\"curve25519:YMBYCWTWCG\":\"KZFa5YUXV2EOdhK8dcGMMHWB67stdgAP4+xwiS69mCU\",\"ed25519:YMBYCWTWCG\":\"0cEgQJJqjgtXUGp4ZXQQmh36RAxwxr8HJw2E9v1gvA0\"},\"user_id\":\"@mxBob14774891254276b253f42-f267-43ec-bad9-767142bfea30:localhost:8480\"}"; - OlmAccount account = null; - - // create account - try { - account = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - assertNotNull(account); - - // sign 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<String, String> 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)); - - // instantiate utility object - OlmUtility utility = null; - - try { - utility = new OlmUtility(); - } catch (Exception e) { - assertTrue("failed to create OlmUtility", false); - } - - // verify signature - errorMsg = null; - try { - utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(TextUtils.isEmpty(errorMsg)); - - // check a bad signature is detected => errorMsg = BAD_MESSAGE_MAC - String badSignature = "Bad signature Bad signature Bad signature.."; - - errorMsg = null; - try { - utility.verifyEd25519Signature(badSignature, fingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(!TextUtils.isEmpty(errorMsg)); - - // check bad fingerprint size => errorMsg = INVALID_BASE64 - String badSizeFingerPrintKey = fingerPrintKey.substring(fingerPrintKey.length()/2); - - errorMsg = null; - try { - utility.verifyEd25519Signature(messageSignature, badSizeFingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(!TextUtils.isEmpty(errorMsg)); - - utility.releaseUtility(); - assertTrue(utility.isReleased()); - - account.releaseAccount(); - assertTrue(account.isReleased()); - } - - @Test - public void test02sha256() { - OlmUtility utility = null; - - try { - utility = new OlmUtility(); - } catch (Exception e) { - assertTrue("OlmUtility creation failed", false); - } - String msgToHash = "The quick brown fox jumps over the lazy dog"; - - String hashResult = utility.sha256(msgToHash); - assertFalse(TextUtils.isEmpty(hashResult)); - - utility.releaseUtility(); - assertTrue(utility.isReleased()); - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java deleted file mode 100644 index 4451f7a..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations 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 java.util.ArrayList; -import java.util.Map; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Helper class providing helper methods used in the Olm Android SDK unit tests. - */ -public class TestHelper { - - /** - * Return the identity key {@link OlmAccount#JSON_KEY_IDENTITY_KEY} from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()} - * @return identity key string if operation succeed, null otherwise - */ - static public String getIdentityKey(Map<String, String> aIdentityKeysMap){ - String idKey = null; - - try { - idKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_IDENTITY_KEY); - } catch (Exception e) { - assertTrue("Exception MSg=" + e.getMessage(), false); - } - return idKey; - } - - /** - * Return the fingerprint key {@link OlmAccount#JSON_KEY_FINGER_PRINT_KEY} from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()} - * @return fingerprint key string if operation succeed, null otherwise - */ - static public String getFingerprintKey(Map<String, String> aIdentityKeysMap) { - String fingerprintKey = null; - - try { - fingerprintKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); - } catch (Exception e) { - assertTrue("Exception MSg=" + e.getMessage(), false); - } - return fingerprintKey; - } - - /** - * Return the first one time key from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#oneTimeKeys()} - * @param aKeyPosition the position of the key to be retrieved - * @return one time key string if operation succeed, null otherwise - */ - static public String getOneTimeKey(Map<String, Map<String, String>> aIdentityKeysMap, int aKeyPosition) { - String firstOneTimeKey = null; - - try { - Map<String, String> generatedKeys = aIdentityKeysMap.get(OlmAccount.JSON_KEY_ONE_TIME_KEY); - assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", generatedKeys); - - firstOneTimeKey = (new ArrayList<>(generatedKeys.values())).get(aKeyPosition - 1); - } catch (Exception e) { - assertTrue("Exception Msg=" + e.getMessage(), false); - } - return firstOneTimeKey; - } -} |