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  */
16 
17 package android.net.wifi.nl80211;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.os.Parcel;
24 
25 import androidx.test.filters.SmallTest;
26 
27 import org.junit.Test;
28 
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 
32 /**
33  * Unit tests for {@link android.net.wifi.nl80211.NativeScanResult}.
34  */
35 @SmallTest
36 public class NativeScanResultTest {
37 
38     private static final byte[] TEST_SSID =
39             new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
40     private static final byte[] TEST_BSSID =
41             new byte[] {(byte) 0x12, (byte) 0xef, (byte) 0xa1,
42                         (byte) 0x2c, (byte) 0x97, (byte) 0x8b};
43     private static final byte[] TEST_INFO_ELEMENT =
44             new byte[] {(byte) 0x01, (byte) 0x03, (byte) 0x12, (byte) 0xbe, (byte) 0xff};
45     private static final int TEST_FREQUENCY = 2456;
46     private static final int TEST_SIGNAL_MBM = -45;
47     private static final long TEST_TSF = 34455441;
48     private static final int TEST_CAPABILITY = (0x1 << 2) | (0x1 << 5);
49     private static final boolean TEST_ASSOCIATED = true;
50     private static final int[] RADIO_CHAIN_IDS = { 0, 1 };
51     private static final int[] RADIO_CHAIN_LEVELS = { -56, -65 };
52 
53     /**
54      *  NativeScanResult object can be serialized and deserialized, while keeping the
55      *  values unchanged.
56      */
57     @Test
canSerializeAndDeserialize()58     public void canSerializeAndDeserialize() {
59         NativeScanResult scanResult = new NativeScanResult();
60         scanResult.ssid = TEST_SSID;
61         scanResult.bssid = TEST_BSSID;
62         scanResult.infoElement = TEST_INFO_ELEMENT;
63         scanResult.frequency = TEST_FREQUENCY;
64         scanResult.signalMbm = TEST_SIGNAL_MBM;
65         scanResult.tsf = TEST_TSF;
66         scanResult.capability = TEST_CAPABILITY;
67         scanResult.associated = TEST_ASSOCIATED;
68         scanResult.radioChainInfos = new ArrayList<>(Arrays.asList(
69                 new RadioChainInfo(RADIO_CHAIN_IDS[0], RADIO_CHAIN_LEVELS[0]),
70                 new RadioChainInfo(RADIO_CHAIN_IDS[1], RADIO_CHAIN_LEVELS[1])));
71         Parcel parcel = Parcel.obtain();
72         scanResult.writeToParcel(parcel, 0);
73         // Rewind the pointer to the head of the parcel.
74         parcel.setDataPosition(0);
75         NativeScanResult scanResultDeserialized = NativeScanResult.CREATOR.createFromParcel(parcel);
76 
77         assertArrayEquals(scanResult.ssid, scanResultDeserialized.ssid);
78         assertArrayEquals(scanResult.bssid, scanResultDeserialized.bssid);
79         assertArrayEquals(scanResult.infoElement, scanResultDeserialized.infoElement);
80         assertEquals(scanResult.frequency, scanResultDeserialized.frequency);
81         assertEquals(scanResult.signalMbm, scanResultDeserialized.signalMbm);
82         assertEquals(scanResult.tsf, scanResultDeserialized.tsf);
83         assertEquals(scanResult.capability, scanResultDeserialized.capability);
84         assertEquals(scanResult.associated, scanResultDeserialized.associated);
85         assertTrue(scanResult.radioChainInfos.containsAll(scanResultDeserialized.radioChainInfos));
86     }
87 }
88