From 867ef94ced8d3d32c63e7d6dac74574c6d7da813 Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Thu, 20 Oct 2016 14:40:59 +0200 Subject: First update with serialization mechanism --- .../java/org/matrix/olm/OlmAccountTest.java | 90 +++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) 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 45f6e63..19b8b63 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 @@ -1,5 +1,8 @@ package org.matrix.olm; +import android.accounts.Account; +import android.content.Context; +import android.content.SharedPreferences; import android.support.test.runner.AndroidJUnit4; import android.text.TextUtils; import android.util.Log; @@ -15,9 +18,19 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import java.io.File; +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.Iterator; +import static android.support.test.InstrumentationRegistry.getInstrumentation; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @RunWith(AndroidJUnit4.class) @@ -106,7 +119,7 @@ public class OlmAccountTest { } //**************************************************** - //** ************** One time keys TESTS ************** + //***************** ONE TIME KEYS TESTS ************** //**************************************************** @Test public void test06MaxOneTimeKeys() { @@ -175,4 +188,79 @@ public class OlmAccountTest { assertNotNull(signedMsg); // additional tests are performed in test01VerifyEd25519Signing() } + + + // ******************************************************** + // ************* SERIALIZATION TEST *********************** + // ******************************************************** + + @Test + public void test13Serialization() { + FileOutputStream fileOutput = null; + ObjectOutputStream objectOutput = null; + OlmAccount accountRef = new OlmAccount(); + OlmAccount accountDeserial = null; + OlmException exception; + + int retValue = accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); + assertTrue(0==retValue); + + JSONObject identityKeysRef = accountRef.identityKeys(); + JSONObject oneTimeKeysRef = accountRef.oneTimeKeys(); + final String FILE_NAME = "testfile"; + + /*Context context = getInstrumentation().getContext(); + SharedPreferences sharedPref = context.getSharedPreferences("TestPref",Context.MODE_PRIVATE); + SharedPreferences.Editor editPref = sharedPref.edit(); + editPref.putLong();*/ + + try { + Context context = getInstrumentation().getContext(); + context.getFilesDir(); + //File serialFile = new File(FILE_NAME); + //fileOutput = new FileOutputStream(serialFile); + fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); + + objectOutput = new ObjectOutputStream(fileOutput); + objectOutput.writeObject(accountRef); + objectOutput.flush(); + objectOutput.close(); + + //FileInputStream fileInput = new FileInputStream(serialFile); + FileInputStream fileInput = context.openFileInput(FILE_NAME); + ObjectInputStream objectInput = new ObjectInputStream(fileInput); + accountDeserial = (OlmAccount) objectInput.readObject(); + objectInput.close(); + + assertNotNull(accountDeserial); + + JSONObject identityKeys2 = accountDeserial.identityKeys(); + JSONObject oneTimeKeys2 = accountDeserial.oneTimeKeys(); + assertEquals(identityKeysRef, identityKeys2); + assertEquals(oneTimeKeysRef, oneTimeKeys2); + + accountRef.releaseAccount(); + accountDeserial.releaseAccount(); + + } + + catch (FileNotFoundException e) { + Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage()); + } + catch (ClassNotFoundException e) { + Log.e(LOG_TAG, "## test13Serialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); + } + catch (IOException e) { + Log.e(LOG_TAG, "## test13Serialization(): Exception IOException Msg==" + e.getMessage()); + } + /*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()); + } + + } + + } -- cgit v1.2.3