aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
blob: 1aeb3fb66d1605a8a1111d44ac595d40013c33c9 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 * Copyright 2016 OpenMarket 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.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 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(){
        // enable UTF-8 specific conversion for pre Marshmallow(23) android versions,
        // due to issue described here: https://github.com/eclipsesource/J2V8/issues/142
        boolean isSpecificUtf8ConversionEnabled = android.os.Build.VERSION.SDK_INT < 23;

        // load native lib
        mOlmManager = new OlmManager(isSpecificUtf8ConversionEnabled);

        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
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
        assertTrue(null!=bobIdentityKey);

        // get bob one time keys
        assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,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
        assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
        String clearMsg = "Heloo bob , this is alice!";
        OlmMessage encryptedMsgToBob = aliceSession.encryptMessage(clearMsg);
        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());
        assertTrue(0==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();
        assertTrue(0==bobAccount.getUnreleasedCount());
        assertTrue(0==aliceAccount.getUnreleasedCount());

        // release sessions
        bobSession.releaseSession();
        aliceSession.releaseSession();
        assertTrue(0==bobSession.getUnreleasedCount());
        assertTrue(0==aliceSession.getUnreleasedCount());
    }


    /**
     * 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
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
        assertTrue(null!=bobIdentityKey);

        // get bob one time keys
        assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,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
        assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
        String helloClearMsg = "Hello I'm Alice!";

        OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
        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());
        assertTrue(0==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));

        // 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 = bobSession.encryptMessage(clearMsg1);
        assertNotNull(encryptedMsg1);
        OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
        assertNotNull(encryptedMsg2);
        OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
        assertNotNull(encryptedMsg3);

        // alice decrypts bob's messages
        String decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1);
        assertNotNull(decryptedMsg1);
        String decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2);
        assertNotNull(decryptedMsg2);
        String decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3);
        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 = aliceSession.encryptMessage(clearMsg1);
        assertNotNull(encryptedMsg1);
        decryptedMsg1 = bobSession.decryptMessage(encryptedMsg1);
        assertNotNull(decryptedMsg1);
        assertTrue(clearMsg1.equals(decryptedMsg1));

        // comparison test
        assertTrue(clearMsg1.equals(decryptedMsg1));

        // clean objects..
        assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
        bobAccount.releaseAccount();
        aliceAccount.releaseAccount();
        assertTrue(0==bobAccount.getUnreleasedCount());
        assertTrue(0==aliceAccount.getUnreleasedCount());

        bobSession.releaseSession();
        aliceSession.releaseSession();
        assertTrue(0==bobSession.getUnreleasedCount());
        assertTrue(0==aliceSession.getUnreleasedCount());
    }


    @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(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));

        aliceAccount.releaseAccount();
        bobAccount.releaseAccount();
        assertTrue(0==aliceAccount.getUnreleasedCount());
        assertTrue(0==bobAccount.getUnreleasedCount());

        bobSession.releaseSession();
        aliceSession.releaseSession();
        assertTrue(0==bobSession.getUnreleasedCount());
        assertTrue(0==aliceSession.getUnreleasedCount());
    }

    @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
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        JSONObject aliceIdentityKeysJson = aliceAccount.identityKeys();
        String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
        String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeysJson);

        // get bob/luke one time keys
        assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        assertTrue(0 == aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj, 1);

        // create alice inbound session for bob
        assertTrue(0==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey1));

        String aliceClearMsg = "hello helooo to bob!";
        OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg);
        assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText));

        // init bob session with alice PRE KEY
        assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedAliceToBobMsg1.mCipherText));

        // 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
        assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
        aliceAccount.releaseAccount();
        bobAccount.releaseAccount();
        assertTrue(0==aliceAccount.getUnreleasedCount());
        assertTrue(0==bobAccount.getUnreleasedCount());

        aliceSession.releaseSession();
        bobSession.releaseSession();
        assertTrue(0==aliceSession.getUnreleasedCount());
        assertTrue(0==bobSession.getUnreleasedCount());
    }

    // ********************************************************
    // ************* 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
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
        assertTrue(null!=bobIdentityKey);

        // get bob one time keys
        assertTrue(0==bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,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
        assertNotNull(aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));
        String helloClearMsg = "Hello I'm Alice!";

        OlmMessage encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg);
        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());
        assertTrue(0==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));

        // 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 = bobSession.encryptMessage(clearMsg1);
        assertNotNull(encryptedMsg1);
        OlmMessage encryptedMsg2 = bobSession.encryptMessage(clearMsg2);
        assertNotNull(encryptedMsg2);
        OlmMessage encryptedMsg3 = bobSession.encryptMessage(clearMsg3);
        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..
            assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
            bobAccount.releaseAccount();
            aliceAccount.releaseAccount();
            assertTrue(0==bobAccount.getUnreleasedCount());
            assertTrue(0==aliceAccount.getUnreleasedCount());

            bobSession.releaseSession();
            aliceSession.releaseSession();
            aliceSessionDeserial.releaseSession();
            assertTrue(0==bobSession.getUnreleasedCount());
            assertTrue(0==aliceSession.getUnreleasedCount());
            assertTrue(0==aliceSessionDeserial.getUnreleasedCount());
        }
        catch (FileNotFoundException e) {
            Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage());
        }
        catch (ClassNotFoundException e) {
            Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage());
        }
        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());
        }
    }


    // ****************************************************
    // *************** 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
        JSONObject bobIdentityKeysJson = bobAccount.identityKeys();
        String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeysJson);
        assertTrue(null != bobIdentityKey);

        // get bob one time keys
        assertTrue(0 == bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER));
        JSONObject bobOneTimeKeysJsonObj = bobAccount.oneTimeKeys();
        assertNotNull(bobOneTimeKeysJsonObj);
        String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeysJsonObj,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()
        assertTrue(-1==aliceSession.initOutboundSessionWithAccount(null, bobIdentityKey, bobOneTimeKey));
        assertTrue(-1==aliceSession.initOutboundSessionWithAccount(aliceAccount, null, bobOneTimeKey));
        assertTrue(-1==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, null));
        assertTrue(-1==aliceSession.initOutboundSessionWithAccount(null, null, null));

        // init properly
        assertTrue(0==aliceSession.initOutboundSessionWithAccount(aliceAccount, bobIdentityKey, bobOneTimeKey));

        // SANITY CHECK TESTS FOR: encryptMessage()
        assertTrue(null==aliceSession.encryptMessage(null));

        // encrypt properly
        OlmMessage encryptedMsgToBob = aliceSession.encryptMessage("A message for bob");
        assertNotNull(encryptedMsgToBob);

        // SANITY CHECK TESTS FOR: initInboundSessionWithAccount()
        OlmSession bobSession = null;
        try {
            bobSession = new OlmSession();
            assertTrue(-1==bobSession.initInboundSessionWithAccount(null, encryptedMsgToBob.mCipherText));
            assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, null));
            assertTrue(-1==bobSession.initInboundSessionWithAccount(bobAccount, INVALID_PRE_KEY));
            // init properly
            assertTrue(0==bobSession.initInboundSessionWithAccount(bobAccount, encryptedMsgToBob.mCipherText));
        } catch (OlmException e) {
            assertTrue("Exception Msg="+e.getMessage(), false);
        }

        // SANITY CHECK TESTS FOR: decryptMessage()
        String decryptedMsg = aliceSession.decryptMessage(null);
        assertTrue(null==decryptedMsg);

        // SANITY CHECK TESTS FOR: matchesInboundSession()
        assertTrue(!aliceSession.matchesInboundSession(null));

        // SANITY CHECK TESTS FOR: matchesInboundSessionFrom()
        assertTrue(!aliceSession.matchesInboundSessionFrom(null,null));

        // release objects
        assertTrue(0==bobAccount.removeOneTimeKeysForSession(bobSession));
        aliceAccount.releaseAccount();
        bobAccount.releaseAccount();
        assertTrue(0==aliceAccount.getUnreleasedCount());
        assertTrue(0==bobAccount.getUnreleasedCount());

        aliceSession.releaseSession();
        bobSession.releaseSession();
        assertTrue(0==aliceSession.getUnreleasedCount());
        assertTrue(0==bobSession.getUnreleasedCount());
    }

}