aboutsummaryrefslogtreecommitdiff
path: root/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java
blob: bbb50aeca81342e9922f7877db469cddf83a5a57 (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
/*
 * 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();
            }
        }
    }

}