1 /*
2  * Copyright (C) 2022 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.aware;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import androidx.test.filters.SmallTest;
23 
24 import org.junit.Test;
25 
26 /**
27  * Unit test harness for WifiAwareDataPathSecurityConfig class.
28  */
29 @SmallTest
30 public class WifiAwareDataPathSecurityConfigTest {
31     private static final String INVALID_PASSPHRASE = "0";
32     private static final String PASSPHRASE = "PASSPHRASE";
33     private static final byte[] PMK = "01234567890123456789012345678901".getBytes();
34     private static final byte[] INVALID_PMK = "0123456789012345678901234567890".getBytes();
35     private static final byte[] PMKID = "0123456789012345".getBytes();
36     private static final byte[] INVALID_PMKID = "012345678901234".getBytes();
37 
38     @Test(expected = IllegalArgumentException.class)
testBuilderWithInvalidPassphrase()39     public void testBuilderWithInvalidPassphrase() {
40         WifiAwareDataPathSecurityConfig.Builder builder =
41                 new WifiAwareDataPathSecurityConfig
42                         .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_128);
43         builder.setPskPassphrase(INVALID_PASSPHRASE);
44     }
45 
46     @Test(expected = IllegalArgumentException.class)
testBuilderWithInvalidPmk()47     public void testBuilderWithInvalidPmk() {
48         WifiAwareDataPathSecurityConfig.Builder builder =
49                 new WifiAwareDataPathSecurityConfig
50                         .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_128);
51         builder.setPmk(INVALID_PMK);
52     }
53 
54     @Test(expected = IllegalArgumentException.class)
testBuilderWithInvalidPmkId()55     public void testBuilderWithInvalidPmkId() {
56         WifiAwareDataPathSecurityConfig.Builder builder =
57                 new WifiAwareDataPathSecurityConfig
58                         .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_PK_128);
59         builder.setPmkId(INVALID_PMKID);
60     }
61 
62     @Test(expected = IllegalArgumentException.class)
testBuilderWithInvalidCipherSuite()63     public void testBuilderWithInvalidCipherSuite() {
64         WifiAwareDataPathSecurityConfig.Builder builder =
65                 new WifiAwareDataPathSecurityConfig
66                         .Builder(3);
67     }
68 
69     @Test
testBuilderWithSKCipherSuite()70     public void testBuilderWithSKCipherSuite() {
71         WifiAwareDataPathSecurityConfig.Builder builder =
72                 new WifiAwareDataPathSecurityConfig
73                         .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_128);
74         builder.setPskPassphrase(PASSPHRASE);
75         WifiAwareDataPathSecurityConfig securityConfig = builder.build();
76         assertTrue(securityConfig.isValid());
77         assertEquals(PASSPHRASE, securityConfig.getPskPassphrase());
78         assertEquals(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_128,
79                 securityConfig.getCipherSuite());
80 
81         builder = new WifiAwareDataPathSecurityConfig
82                 .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_256);
83         builder.setPmk(PMK);
84         securityConfig = builder.build();
85         assertTrue(securityConfig.isValid());
86         assertEquals(PMK, securityConfig.getPmk());
87         assertEquals(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_SK_256,
88                 securityConfig.getCipherSuite());
89     }
90 
91     @Test
testBuilderWithPKCipherSuite()92     public void testBuilderWithPKCipherSuite() {
93         WifiAwareDataPathSecurityConfig.Builder builder =
94                 new WifiAwareDataPathSecurityConfig
95                         .Builder(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_PK_128);
96         builder.setPmk(PMK);
97         builder.setPmkId(PMKID);
98         WifiAwareDataPathSecurityConfig securityConfig = builder.build();
99         assertTrue(securityConfig.isValid());
100         assertEquals(PMK, securityConfig.getPmk());
101         assertEquals(PMKID, securityConfig.getPmkId());
102         assertEquals(Characteristics.WIFI_AWARE_CIPHER_SUITE_NCS_PK_128,
103                 securityConfig.getCipherSuite());
104     }
105 
106 }
107