1 /*
2  * Copyright (C) 2019 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.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.net.MacAddress;
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import com.android.modules.utils.build.SdkLevel;
29 
30 import org.junit.Test;
31 
32 import java.util.List;
33 
34 /**
35  * Unit tests for {@link android.net.wifi.SoftApInfo}.
36  */
37 @SmallTest
38 public class SoftApInfoTest {
39     private static final String TEST_AP_INSTANCE = "wlan1";
40     private static final int TEST_FREQUENCY = 2412;
41     private static final int TEST_BANDWIDTH = SoftApInfo.CHANNEL_WIDTH_20MHZ;
42     private static final int TEST_WIFI_STANDARD = ScanResult.WIFI_STANDARD_LEGACY;
43     private static final MacAddress TEST_AP_MAC = MacAddress.fromString("aa:bb:cc:dd:ee:ff");
44     private static final long TEST_SHUTDOWN_TIMEOUT_MILLIS = 100_000;
45     private static final List<OuiKeyedData> TEST_VENDOR_DATA =
46             OuiKeyedDataUtil.createTestOuiKeyedDataList(5);
47 
48     /**
49      * Verifies copy constructor.
50      */
51     @Test
testCopyOperator()52     public void testCopyOperator() throws Exception {
53         SoftApInfo info = new SoftApInfo();
54         info.setFrequency(TEST_FREQUENCY);
55         info.setBandwidth(TEST_BANDWIDTH);
56         info.setBssid(TEST_AP_MAC);
57         info.setWifiStandard(TEST_WIFI_STANDARD);
58         info.setApInstanceIdentifier(TEST_AP_INSTANCE);
59         if (SdkLevel.isAtLeastV()) {
60             info.setVendorData(TEST_VENDOR_DATA);
61         }
62 
63         SoftApInfo copiedInfo = new SoftApInfo(info);
64 
65         assertEquals(info, copiedInfo);
66         assertEquals(info.hashCode(), copiedInfo.hashCode());
67     }
68 
69     /**
70      * Verifies parcel serialization/deserialization.
71      */
72     @Test
testParcelOperation()73     public void testParcelOperation() throws Exception {
74         SoftApInfo info = new SoftApInfo();
75         info.setFrequency(TEST_FREQUENCY);
76         info.setBandwidth(TEST_BANDWIDTH);
77         info.setBssid(TEST_AP_MAC);
78         info.setWifiStandard(TEST_WIFI_STANDARD);
79         info.setApInstanceIdentifier(TEST_AP_INSTANCE);
80         if (SdkLevel.isAtLeastV()) {
81             info.setVendorData(TEST_VENDOR_DATA);
82         }
83 
84         Parcel parcelW = Parcel.obtain();
85         info.writeToParcel(parcelW, 0);
86         byte[] bytes = parcelW.marshall();
87         parcelW.recycle();
88 
89         Parcel parcelR = Parcel.obtain();
90         parcelR.unmarshall(bytes, 0, bytes.length);
91         parcelR.setDataPosition(0);
92         SoftApInfo fromParcel = SoftApInfo.CREATOR.createFromParcel(parcelR);
93 
94         assertEquals(info, fromParcel);
95         assertEquals(info.hashCode(), fromParcel.hashCode());
96     }
97 
98 
99     /**
100      * Verifies the initial value same as expected.
101      */
102     @Test
testInitialValue()103     public void testInitialValue() throws Exception {
104         SoftApInfo info = new SoftApInfo();
105         assertEquals(info.getFrequency(), 0);
106         assertEquals(info.getBandwidth(), SoftApInfo.CHANNEL_WIDTH_INVALID);
107         if (SdkLevel.isAtLeastS()) {
108             assertEquals(info.getBssid(), null);
109             assertEquals(info.getWifiStandard(), ScanResult.WIFI_STANDARD_UNKNOWN);
110             assertEquals(info.getApInstanceIdentifier(), null);
111         }
112         if (SdkLevel.isAtLeastV()) {
113             assertNotNull(info.getVendorData());
114         }
115     }
116 
117     /**
118      * Verifies the set/get method same as expected.
119      */
120     @Test
testGetXXXAlignedWithSetXXX()121     public void testGetXXXAlignedWithSetXXX() throws Exception {
122         SoftApInfo info = new SoftApInfo();
123         info.setFrequency(TEST_FREQUENCY);
124         info.setBandwidth(TEST_BANDWIDTH);
125         info.setBssid(TEST_AP_MAC);
126         info.setWifiStandard(TEST_WIFI_STANDARD);
127         info.setApInstanceIdentifier(TEST_AP_INSTANCE);
128         info.setAutoShutdownTimeoutMillis(TEST_SHUTDOWN_TIMEOUT_MILLIS);
129         assertEquals(info.getFrequency(), TEST_FREQUENCY);
130         assertEquals(info.getBandwidth(), TEST_BANDWIDTH);
131         if (SdkLevel.isAtLeastS()) {
132             assertEquals(info.getBssid(), TEST_AP_MAC);
133             assertEquals(info.getWifiStandard(), TEST_WIFI_STANDARD);
134             assertEquals(info.getApInstanceIdentifier(), TEST_AP_INSTANCE);
135         }
136         assertEquals(info.getAutoShutdownTimeoutMillis(), TEST_SHUTDOWN_TIMEOUT_MILLIS);
137         if (SdkLevel.isAtLeastV()) {
138             info.setVendorData(TEST_VENDOR_DATA);
139             assertTrue(TEST_VENDOR_DATA.equals(info.getVendorData()));
140         }
141     }
142 
143 }
144