aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
blob: 311d6a86092f53348758cb9ae7c9c76cac374647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package org.matrix.olm;

import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

import java.util.Iterator;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OlmSessionTest {
    private static final String LOG_TAG = "OlmSessionTest";

    private static OlmManager mOlmManager;

    @BeforeClass
    public static void setUpClass(){
        // load native lib
        mOlmManager = new OlmManager();

        String version = mOlmManager.getOlmLibVersion();
        assertNotNull(version);
        Log.d(LOG_TAG, "## setUpClass(): lib version="+version);
    }

    /**
     * Basic test:
     * - alice creates an account
     * - bob creates an account
     * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey)
     * - alice encrypts a message with its session
     * - bob creates an inbound session based on alice's encrypted message
     * - bob decrypts the encrypted message with its session
     */
    @Test
    public void test01AliceToBob() {
        final int ONE_TIME_KEYS_NUMBER = 5;
        String bobIdentityKey = null;
        String bobOneTimeKey=null;

        // creates alice & bob accounts
        OlmAccount aliceAccount = new OlmAccount();
        OlmAccount bobAccount = new OlmAccount();

        // test accounts creation
        assertTrue(0!=bobAccount.getOlmAccountId());
        assertTrue(0!=aliceAccount.getOlmAccountId());

        // get bob identity key
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        assertNotNull(bobIdentityKeysJson);
        try {
            bobIdentityKey = bobIdentityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
            assertTrue(null!=bobIdentityKey);
        } catch (JSONException e) {
            assertTrue("Exception MSg="+e.getMessage(), false);
        }

        // get bob one time keys
        assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        assertNotNull(bobOneTimeKeysJsonObj);
        try {
            JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
            assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);

            Iterator<String> generatedKeysIt = generatedKeys.keys();
            if(generatedKeysIt.hasNext()) {
                bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
            }
            assertNotNull(bobOneTimeKey);
        } catch (JSONException e) {
            assertTrue("Exception MSg="+e.getMessage(), false);
        }

        // CREATE ALICE SESSION
        OlmSession aliceSession = new OlmSession();
        assertTrue(0!=aliceSession.getOlmSessionId());

        // CREATE ALICE OUTBOUND SESSION and encrypt message to bob
        assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
        String clearMsg = "Heloo bob , this is alice!";
        OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg);
        assertNotNull(encryptedMsgToBob);
        Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText);

        // CREATE BOB INBOUND SESSION and decrypt message from alice
        OlmSession bobSession = new OlmSession();
        assertTrue(0!=bobSession.getOlmSessionId());
        assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
        String decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob);
        assertNotNull(decryptedMsg);

        // MESSAGE COMPARISON: decrypted vs encrypted
        assertTrue(clearMsg.equals(decryptedMsg));

        // clean objects..
        assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
        // release accounts
        bobAccount.releaseAccount();
        aliceAccount.releaseAccount();
        // release sessions
        bobSession.releaseSession();
        aliceSession.releaseSession();
    }


    /**
     * Same as test01AliceToBob but with bob who's encrypting messages
     * to alice and alice decrypt them.<br>
     * - alice creates an account
     * - bob creates an account
     * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey)
     * - alice encrypts a message with its own session
     * - bob creates an inbound session based on alice's encrypted message
     * - bob decrypts the encrypted message with its own session
     * - bob encrypts messages with its own session
     * - alice decrypts bob's messages with its own message
     */
    @Test
    public void test02AliceToBobBackAndForth() {
        final int ONE_TIME_KEYS_NUMBER = 1;
        String bobIdentityKey = null;
        String bobOneTimeKey=null;

        // creates alice & bob accounts
        OlmAccount aliceAccount = new OlmAccount();
        OlmAccount bobAccount = new OlmAccount();

        // test accounts creation
        assertTrue(0!=bobAccount.getOlmAccountId());
        assertTrue(0!=aliceAccount.getOlmAccountId());

        // get bob identity key
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        assertNotNull(bobIdentityKeysJson);
        try {
            bobIdentityKey = bobIdentityKeysJson.getString(OlmAccount.JSON_KEY_IDENTITY_KEY);
            assertTrue(null!=bobIdentityKey);
        } catch (JSONException e) {
            assertTrue("Exception MSg="+e.getMessage(), false);
        }

        // get bob one time keys
        assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        assertNotNull(bobOneTimeKeysJsonObj);
        try {
            JSONObject generatedKeys = bobOneTimeKeysJsonObj.getJSONObject(OlmAccount.JSON_KEY_ONE_TIME_KEY);
            assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", generatedKeys);

            Iterator<String> generatedKeysIt = generatedKeys.keys();
            if(generatedKeysIt.hasNext()) {
                bobOneTimeKey = generatedKeys.getString(generatedKeysIt.next());
            }
            assertNotNull(bobOneTimeKey);
        } catch (JSONException e) {
            assertTrue("Exception MSg="+e.getMessage(), false);
        }

        // CREATE ALICE SESSION
        OlmSession aliceSession = new OlmSession();
        assertTrue(0!=aliceSession.getOlmSessionId());

        // CREATE ALICE OUTBOUND SESSION and encrypt message to bob
        assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
        String helloClearMsg = "Hello I'm Alice!";

        OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
        assertNotNull(encryptedAliceToBobMsg1);

        // CREATE BOB INBOUND SESSION and decrypt message from alice
        OlmSession bobSession = new OlmSession();
        assertTrue(0!=bobSession.getOlmSessionId());
        assertNotNull(bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));

        // DECRYPT MESSAGE FROM ALICE
        String decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1);
        assertNotNull(decryptedMsg01);

        // MESSAGE COMPARISON: decrypted vs encrypted
        assertTrue(helloClearMsg.equals(decryptedMsg01));

        assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));

        // BACK/FORTH MESSAGE COMPARISON
        String clearMsg1 = "Hello I'm Bob!";
        String clearMsg2 = "Isn't life grand?";
        String clearMsg3 = "Let's go to the opera.";

        OlmMessage encryptedMsg1 = bobSession.encryptMessage(clearMsg1);
        assertNotNull(encryptedMsg1);
        OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
        assertNotNull(encryptedMsg2);
        OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
        assertNotNull(encryptedMsg3);

        String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
        assertNotNull(decryptedMsg1);
        String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
        assertNotNull(decryptedMsg2);
        String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
        assertNotNull(decryptedMsg3);

        assertTrue(clearMsg1.equals(decryptedMsg1));
        assertTrue(clearMsg2.equals(decryptedMsg2));
        assertTrue(clearMsg3.equals(decryptedMsg3));

        // clean objects..
        bobAccount.releaseAccount();
        aliceAccount.releaseAccount();
        bobSession.releaseSession();
        aliceSession.releaseSession();
    }

    @Test
    public void test03AliceBobSessionId() {
        // creates alice & bob accounts
        OlmAccount aliceAccount = new OlmAccount();
        OlmAccount bobAccount = new OlmAccount();

        // test accounts creation
        assertTrue(0!=bobAccount.getOlmAccountId());
        assertTrue(0!=aliceAccount.getOlmAccountId());

        // CREATE ALICE SESSION
        OlmSession aliceSession = new OlmSession();
        assertTrue(0!=aliceSession.getOlmSessionId());

        // CREATE BOB INBOUND SESSION and decrypt message from alice
        OlmSession bobSession = new OlmSession();
        assertTrue(0!=bobSession.getOlmSessionId());

        String aliceSessionId = aliceSession.sessionIdentifier();
        assertNotNull(aliceSessionId);

        String bobSessionId = bobSession.sessionIdentifier();
        assertNotNull(bobSessionId);

        // must be the same for both ends of the conversation
        assertTrue(aliceSessionId.equals(bobSessionId));
    }
}