1 /*
2  * Copyright (C) 2018 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.assertNull;
22 import static org.mockito.Mockito.validateMockitoUsage;
23 
24 import android.os.Parcel;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.MockitoAnnotations;
32 
33 /**
34  * Unit tests for {@link android.net.wifi.WifiScanner}.
35  */
36 @SmallTest
37 public class ScanResultTest {
38     public static final String TEST_SSID = "\"test_ssid\"";
39     public static final String TEST_BSSID = "04:ac:fe:45:34:10";
40     public static final String TEST_CAPS = "CCMP";
41     public static final int TEST_LEVEL = -56;
42     public static final int TEST_FREQUENCY = 2412;
43     public static final long TEST_TSF = 04660l;
44 
45     /**
46      * Setup before tests.
47      */
48     @Before
setUp()49     public void setUp() throws Exception {
50         MockitoAnnotations.initMocks(this);
51     }
52 
53     /**
54      * Clean up after tests.
55      */
56     @After
cleanup()57     public void cleanup() {
58         validateMockitoUsage();
59     }
60 
61     /**
62      * Verify parcel read/write for ScanResult.
63      */
64     @Test
verifyScanResultParcelWithoutRadioChainInfo()65     public void verifyScanResultParcelWithoutRadioChainInfo() throws Exception {
66         ScanResult writeScanResult = createScanResult();
67         ScanResult readScanResult = parcelReadWrite(writeScanResult);
68         assertScanResultEquals(writeScanResult, readScanResult);
69     }
70 
71     /**
72      * Verify parcel read/write for ScanResult.
73      */
74     @Test
verifyScanResultParcelWithZeroRadioChainInfo()75     public void verifyScanResultParcelWithZeroRadioChainInfo() throws Exception {
76         ScanResult writeScanResult = createScanResult();
77         writeScanResult.radioChainInfos = new ScanResult.RadioChainInfo[0];
78         ScanResult readScanResult = parcelReadWrite(writeScanResult);
79         assertNull(readScanResult.radioChainInfos);
80     }
81 
82     /**
83      * Verify parcel read/write for ScanResult.
84      */
85     @Test
verifyScanResultParcelWithRadioChainInfo()86     public void verifyScanResultParcelWithRadioChainInfo() throws Exception {
87         ScanResult writeScanResult = createScanResult();
88         writeScanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
89         writeScanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
90         writeScanResult.radioChainInfos[0].id = 0;
91         writeScanResult.radioChainInfos[0].level = -45;
92         writeScanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
93         writeScanResult.radioChainInfos[1].id = 1;
94         writeScanResult.radioChainInfos[1].level = -54;
95         ScanResult readScanResult = parcelReadWrite(writeScanResult);
96         assertScanResultEquals(writeScanResult, readScanResult);
97     }
98 
99     /**
100      * Verify copy constructor for ScanResult.
101      */
102     @Test
verifyScanResultCopyWithoutRadioChainInfo()103     public void verifyScanResultCopyWithoutRadioChainInfo() throws Exception {
104         ScanResult scanResult = createScanResult();
105         ScanResult copyScanResult = new ScanResult(scanResult);
106         assertScanResultEquals(scanResult, copyScanResult);
107     }
108 
109     /**
110      * Verify copy constructor for ScanResult.
111      */
112     @Test
verifyScanResultCopyWithRadioChainInfo()113     public void verifyScanResultCopyWithRadioChainInfo() throws Exception {
114         ScanResult scanResult = createScanResult();
115         scanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
116         scanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
117         scanResult.radioChainInfos[0].id = 0;
118         scanResult.radioChainInfos[0].level = -45;
119         scanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
120         scanResult.radioChainInfos[1].id = 1;
121         scanResult.radioChainInfos[1].level = -54;
122         ScanResult copyScanResult = new ScanResult(scanResult);
123         assertScanResultEquals(scanResult, copyScanResult);
124     }
125 
126     /**
127      * Verify toString for ScanResult.
128      */
129     @Test
verifyScanResultToStringWithoutRadioChainInfo()130     public void verifyScanResultToStringWithoutRadioChainInfo() throws Exception {
131         ScanResult scanResult = createScanResult();
132         assertEquals("SSID: \"test_ssid\", BSSID: 04:ac:fe:45:34:10, capabilities: CCMP, " +
133                 "level: -56, frequency: 2412, timestamp: 2480, distance: 0(cm), distanceSd: 0(cm), " +
134                 "passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, " +
135                 "80211mcResponder: is not supported, Carrier AP: no, " +
136                 "Carrier AP EAP Type: 0, Carrier name: null, " +
137                 "Radio Chain Infos: null", scanResult.toString());
138     }
139 
140     /**
141      * Verify toString for ScanResult.
142      */
143     @Test
verifyScanResultToStringWithRadioChainInfo()144     public void verifyScanResultToStringWithRadioChainInfo() throws Exception {
145         ScanResult scanResult = createScanResult();
146         scanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
147         scanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
148         scanResult.radioChainInfos[0].id = 0;
149         scanResult.radioChainInfos[0].level = -45;
150         scanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
151         scanResult.radioChainInfos[1].id = 1;
152         scanResult.radioChainInfos[1].level = -54;
153         assertEquals("SSID: \"test_ssid\", BSSID: 04:ac:fe:45:34:10, capabilities: CCMP, " +
154                 "level: -56, frequency: 2412, timestamp: 2480, distance: 0(cm), distanceSd: 0(cm), " +
155                 "passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, " +
156                 "80211mcResponder: is not supported, Carrier AP: no, " +
157                 "Carrier AP EAP Type: 0, Carrier name: null, " +
158                 "Radio Chain Infos: [RadioChainInfo: id=0, level=-45, " +
159                 "RadioChainInfo: id=1, level=-54]", scanResult.toString());
160     }
161 
162     /**
163      * Write the provided {@link ScanResult} to a parcel and deserialize it.
164      */
parcelReadWrite(ScanResult writeResult)165     private static ScanResult parcelReadWrite(ScanResult writeResult) throws Exception {
166         Parcel parcel = Parcel.obtain();
167         writeResult.writeToParcel(parcel, 0);
168         parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
169         return ScanResult.CREATOR.createFromParcel(parcel);
170     }
171 
createScanResult()172     private static ScanResult createScanResult() {
173         ScanResult result = new ScanResult();
174         result.wifiSsid = WifiSsid.createFromAsciiEncoded(TEST_SSID);
175         result.BSSID = TEST_BSSID;
176         result.capabilities = TEST_CAPS;
177         result.level = TEST_LEVEL;
178         result.frequency = TEST_FREQUENCY;
179         result.timestamp = TEST_TSF;
180         return result;
181     }
182 
assertScanResultEquals(ScanResult expected, ScanResult actual)183     private static void assertScanResultEquals(ScanResult expected, ScanResult actual) {
184         assertEquals(expected.SSID, actual.SSID);
185         assertEquals(expected.BSSID, actual.BSSID);
186         assertEquals(expected.capabilities, actual.capabilities);
187         assertEquals(expected.level, actual.level);
188         assertEquals(expected.frequency, actual.frequency);
189         assertEquals(expected.timestamp, actual.timestamp);
190         assertArrayEquals(expected.radioChainInfos, actual.radioChainInfos);
191     }
192 }
193