1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.internal.telephony;
17
18import static org.junit.Assert.assertEquals;
19
20import android.os.Parcel;
21import android.telephony.ImsiEncryptionInfo;
22import android.telephony.TelephonyManager;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.util.Base64;
25
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29
30import java.io.ByteArrayInputStream;
31import java.io.InputStream;
32import java.security.PublicKey;
33import java.security.cert.CertificateFactory;
34import java.util.Date;
35
36public class ImsiEncryptionInfoTest {
37    private ImsiEncryptionInfo mImsiEncryptionInfo;
38    private PublicKey mPublicKey;
39    private Date mDate = new Date(1496795015);
40
41    private static final String TEST_CERT = ""
42            + "MIIDsjCCAxugAwIBAgIJAPLf2gS0zYGUMA0GCSqGSIb3DQEBBQUAMIGYMQswCQYDVQQGEwJVUzET"
43            + "MBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEPMA0GA1UEChMGR29v"
44            + "Z2xlMRAwDgYDVQQLEwd0ZXN0aW5nMRYwFAYDVQQDEw1HZXJlbXkgQ29uZHJhMSEwHwYJKoZIhvcN"
45            + "AQkBFhJnY29uZHJhQGdvb2dsZS5jb20wHhcNMTIwNzE0MTc1MjIxWhcNMTIwODEzMTc1MjIxWjCB"
46            + "mDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDU1vdW50YWluIFZp"
47            + "ZXcxDzANBgNVBAoTBkdvb2dsZTEQMA4GA1UECxMHdGVzdGluZzEWMBQGA1UEAxMNR2VyZW15IENv"
48            + "bmRyYTEhMB8GCSqGSIb3DQEJARYSZ2NvbmRyYUBnb29nbGUuY29tMIGfMA0GCSqGSIb3DQEBAQUA"
49            + "A4GNADCBiQKBgQCjGGHATBYlmas+0sEECkno8LZ1KPglb/mfe6VpCT3GhSr+7br7NG/ZwGZnEhLq"
50            + "E7YIH4fxltHmQC3Tz+jM1YN+kMaQgRRjo/LBCJdOKaMwUbkVynAH6OYsKevjrOPk8lfM5SFQzJMG"
51            + "sA9+Tfopr5xg0BwZ1vA/+E3mE7Tr3M2UvwIDAQABo4IBADCB/TAdBgNVHQ4EFgQUhzkS9E6G+x8W"
52            + "L4EsmRjDxu28tHUwgc0GA1UdIwSBxTCBwoAUhzkS9E6G+x8WL4EsmRjDxu28tHWhgZ6kgZswgZgx"
53            + "CzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3"
54            + "MQ8wDQYDVQQKEwZHb29nbGUxEDAOBgNVBAsTB3Rlc3RpbmcxFjAUBgNVBAMTDUdlcmVteSBDb25k"
55            + "cmExITAfBgkqhkiG9w0BCQEWEmdjb25kcmFAZ29vZ2xlLmNvbYIJAPLf2gS0zYGUMAwGA1UdEwQF"
56            + "MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAYiugFDmbDOQ2U/+mqNt7o8ftlEo9SJrns6O8uTtK6AvR"
57            + "orDrR1AXTXkuxwLSbmVfedMGOZy7Awh7iZa8hw5x9XmUudfNxvmrKVEwGQY2DZ9PXbrnta/dwbhK"
58            + "mWfoepESVbo7CKIhJp8gRW0h1Z55ETXD57aGJRvQS4pxkP8ANhM=";
59
60    @After
61    public void tearDown() throws Exception {
62        mImsiEncryptionInfo = null;
63    }
64
65    @Before
66    public void setUp() throws Exception {
67
68        mPublicKey = createPublicKey(TEST_CERT);
69        mImsiEncryptionInfo = new ImsiEncryptionInfo("310", "270", TelephonyManager.KEY_TYPE_WLAN,
70                "key1=value", mPublicKey, mDate);
71    }
72
73    private static PublicKey createPublicKey(String cert) throws Exception {
74        byte[] derCert = Base64.decode(cert.getBytes(), Base64.DEFAULT);
75        InputStream istream = new ByteArrayInputStream(derCert);
76        CertificateFactory cf = CertificateFactory.getInstance("X.509");
77        return cf.generateCertificate(istream).getPublicKey();
78    }
79
80    /**
81     * Tests that all the class variables are set correctly.
82     */
83    @Test
84    @SmallTest
85    public void testSubProperties() {
86        assertEquals("310", mImsiEncryptionInfo.getMcc());
87        assertEquals("270", mImsiEncryptionInfo.getMnc());
88        assertEquals(TelephonyManager.KEY_TYPE_WLAN, mImsiEncryptionInfo.getKeyType());
89        assertEquals("key1=value", mImsiEncryptionInfo.getKeyIdentifier());
90        Date date = mImsiEncryptionInfo.getExpirationTime();
91        assertEquals(mDate, mImsiEncryptionInfo.getExpirationTime());
92    }
93
94    /**
95     * Tests the parceling/un-parceling of the object.
96     */
97    @Test
98    @SmallTest
99    public void testParcel() {
100        Parcel p = Parcel.obtain();
101        p.setDataPosition(0);
102        mImsiEncryptionInfo.writeToParcel(p, 0);
103        p.setDataPosition(0);
104        ImsiEncryptionInfo nw = new ImsiEncryptionInfo(p);
105        assertEquals("310", mImsiEncryptionInfo.getMcc());
106        assertEquals("270", mImsiEncryptionInfo.getMnc());
107        assertEquals(TelephonyManager.KEY_TYPE_WLAN, mImsiEncryptionInfo.getKeyType());
108        assertEquals("key1=value", mImsiEncryptionInfo.getKeyIdentifier());
109        assertEquals(mPublicKey, mImsiEncryptionInfo.getPublicKey());
110        assertEquals(mDate, mImsiEncryptionInfo.getExpirationTime());
111    }
112}
113