From 1f1cbf2b3edff5b2f358e9170350566a564f80f4 Mon Sep 17 00:00:00 2001
From: pedroGitt <pedro.contreiras@amdocs.com>
Date: Thu, 27 Oct 2016 18:14:04 +0200
Subject: [PATCH] Add new tests for multiple creations of account and outbound
 group sessions: check random generation function in JNI works properly

---
 .../java/org/matrix/olm/OlmAccountTest.java   | 82 ++++++++++++++++--
 .../org/matrix/olm/OlmGroupSessionTest.java   | 86 +++++++++++++++++--
 2 files changed, 156 insertions(+), 12 deletions(-)

diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java
index 838fba2..a5cde5e 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
@@ -41,6 +41,7 @@ import java.io.ObjectOutputStream;
 import java.util.Iterator;
 
 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;
@@ -238,10 +239,10 @@ public class OlmAccountTest {
 
     @Test
     public void test13Serialization() {
-        FileOutputStream fileOutput = null;
-        ObjectOutputStream objectOutput = null;
+        FileOutputStream fileOutput;
+        ObjectOutputStream objectOutput;
         OlmAccount accountRef = null;
-        OlmAccount accountDeserial = null;
+        OlmAccount accountDeserial;
 
         try {
             accountRef = new OlmAccount();
@@ -348,11 +349,82 @@ public class OlmAccountTest {
         } catch (OlmException e) {
             assertTrue(e.getMessage(),false);
         }
-        String clearMsg = null;
-        String signedMsg  = olmAccount.signMessage(clearMsg);
+        String signedMsg  = olmAccount.signMessage(null);
         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();
+
+            JSONObject identityKeysJson1 = account1.identityKeys();
+            JSONObject identityKeysJson2 = account2.identityKeys();
+            JSONObject identityKeysJson3 = account3.identityKeys();
+            JSONObject identityKeysJson4 = account4.identityKeys();
+            JSONObject identityKeysJson5 = account5.identityKeys();
+            JSONObject identityKeysJson6 = account6.identityKeys();
+            JSONObject identityKeysJson7 = account7.identityKeys();
+            JSONObject identityKeysJson8 = account8.identityKeys();
+            JSONObject identityKeysJson9 = account9.identityKeys();
+            JSONObject identityKeysJson10 = account10.identityKeys();
+
+            String identityKey1 = TestHelper.getIdentityKey(identityKeysJson1);
+            String identityKey2 = TestHelper.getIdentityKey(identityKeysJson2);
+            assertFalse(identityKey1.equals(identityKey2));
+
+            String identityKey3 = TestHelper.getIdentityKey(identityKeysJson3);
+            assertFalse(identityKey2.equals(identityKey3));
+
+            String identityKey4 = TestHelper.getIdentityKey(identityKeysJson4);
+            assertFalse(identityKey3.equals(identityKey4));
+
+            String identityKey5 = TestHelper.getIdentityKey(identityKeysJson5);
+            assertFalse(identityKey4.equals(identityKey5));
+
+            String identityKey6 = TestHelper.getIdentityKey(identityKeysJson6);
+            assertFalse(identityKey5.equals(identityKey6));
+
+            String identityKey7 = TestHelper.getIdentityKey(identityKeysJson7);
+            assertFalse(identityKey6.equals(identityKey7));
+
+            String identityKey8 = TestHelper.getIdentityKey(identityKeysJson8);
+            assertFalse(identityKey7.equals(identityKey8));
+
+            String identityKey9 = TestHelper.getIdentityKey(identityKeysJson9);
+            assertFalse(identityKey8.equals(identityKey9));
+
+            String identityKey10 = TestHelper.getIdentityKey(identityKeysJson10);
+            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/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
index 5bd6838..eca070f 100644
--- 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
@@ -51,7 +51,7 @@ public class OlmGroupSessionTest {
     private static OlmOutboundGroupSession mAliceOutboundGroupSession;
     private static String mAliceSessionIdentifier;
     private static long mAliceMessageIndex;
-    public  static final String CLEAR_MESSAGE1 = "Hello!";
+    private static final String CLEAR_MESSAGE1 = "Hello!";
     private static String mAliceToBobMessage;
     private static OlmInboundGroupSession mBobInboundGroupSession;
     private static String mAliceOutboundSessionKey;
@@ -172,7 +172,7 @@ public class OlmGroupSessionTest {
     @Test
     public void test14SerializeOutboundSession() {
         OlmOutboundGroupSession outboundGroupSessionRef=null;
-        OlmOutboundGroupSession outboundGroupSessionSerial=null;
+        OlmOutboundGroupSession outboundGroupSessionSerial;
 
         // create one OUTBOUND GROUP SESSION
         try {
@@ -238,14 +238,13 @@ public class OlmGroupSessionTest {
         catch (Exception e) {
             Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage());
         }
-
     }
 
     @Test
     public void test15SerializeInboundSession() {
         OlmOutboundGroupSession aliceOutboundGroupSession=null;
         OlmInboundGroupSession bobInboundGroupSessionRef=null;
-        OlmInboundGroupSession bobInboundGroupSessionSerial=null;
+        OlmInboundGroupSession bobInboundGroupSessionSerial;
 
         // alice creates OUTBOUND GROUP SESSION
         try {
@@ -306,13 +305,86 @@ public class OlmGroupSessionTest {
         catch (IOException e) {
             Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage());
         }
-        /*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());
         }
+    }
+
+    /**
+     * 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 test16MultipleOutboundSession() {
+        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));
+
+
+        } catch (OlmException e) {
+            assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false);
+        }
     }
 
 }
-- 
GitLab