1 /*
2  * Copyright (C) 2016 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  */
16 
17 package android.net.wifi;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 
31 import java.security.PrivateKey;
32 import java.security.cert.X509Certificate;
33 
34 /**
35  * Unit tests for {@link android.net.wifi.ParcelUtil}.
36  */
37 @SmallTest
38 public class ParcelUtilTest {
39     private Parcel mParcel;
40 
41     @Before
setUp()42     public void setUp() throws Exception {
43         mParcel = Parcel.obtain();
44     }
45 
46     @Test
readWriteNullPrivateKey()47     public void readWriteNullPrivateKey() throws Exception {
48         ParcelUtil.writePrivateKey(mParcel, null);
49 
50         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
51         PrivateKey readKey = ParcelUtil.readPrivateKey(mParcel);
52         assertNull(readKey);
53     }
54 
55     @Test
readWriteValidPrivateKey()56     public void readWriteValidPrivateKey() throws Exception {
57         PrivateKey writeKey = FakeKeys.RSA_KEY1;
58         ParcelUtil.writePrivateKey(mParcel, writeKey);
59 
60         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
61         PrivateKey readKey = ParcelUtil.readPrivateKey(mParcel);
62         assertNotNull(readKey);
63         assertEquals(writeKey.getAlgorithm(), readKey.getAlgorithm());
64         assertArrayEquals(writeKey.getEncoded(), readKey.getEncoded());
65     }
66 
67     @Test
readWriteNullCertificate()68     public void readWriteNullCertificate() throws Exception {
69         ParcelUtil.writeCertificate(mParcel, null);
70 
71         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
72         X509Certificate readCert = ParcelUtil.readCertificate(mParcel);
73         assertNull(readCert);
74     }
75 
76     @Test
readWriteValidCertificate()77     public void readWriteValidCertificate() throws Exception {
78         X509Certificate writeCert = FakeKeys.CA_CERT1;
79         ParcelUtil.writeCertificate(mParcel, writeCert);
80 
81         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
82         X509Certificate readCert = ParcelUtil.readCertificate(mParcel);
83         assertNotNull(readCert);
84         assertArrayEquals(writeCert.getEncoded(), readCert.getEncoded());
85     }
86 
87     @Test
readWriteNullCertificates()88     public void readWriteNullCertificates() throws Exception {
89         ParcelUtil.writeCertificates(mParcel, null);
90 
91         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
92         X509Certificate[] readCerts = ParcelUtil.readCertificates(mParcel);
93         assertNull(readCerts);
94     }
95 
96     @Test
readWriteValidCertificates()97     public void readWriteValidCertificates() throws Exception {
98         X509Certificate[] writeCerts = new X509Certificate[2];
99         writeCerts[0] = FakeKeys.CA_CERT0;
100         writeCerts[1] = FakeKeys.CA_CERT1;
101         ParcelUtil.writeCertificates(mParcel, writeCerts);
102 
103         mParcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
104         X509Certificate[] readCerts = ParcelUtil.readCertificates(mParcel);
105         assertNotNull(readCerts);
106         assertEquals(writeCerts.length, readCerts.length);
107         for (int i = 0; i < writeCerts.length; i++) {
108             assertNotNull(readCerts[i]);
109             assertArrayEquals(writeCerts[i].getEncoded(), readCerts[i].getEncoded());
110         }
111     }
112 }
113