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
17package com.android.server.wifi.wificond;
18
19import static org.junit.Assert.assertArrayEquals;
20import static org.junit.Assert.assertEquals;
21import static org.junit.Assert.assertTrue;
22
23import android.os.Parcel;
24import android.support.test.filters.SmallTest;
25
26import org.junit.Test;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.BitSet;
31
32/**
33 * Unit tests for {@link com.android.server.wifi.wificond.NativeScanResult}.
34 */
35@SmallTest
36public 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 BitSet TEST_CAPABILITY = new BitSet(16) {{ set(2); set(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
58    public void canSerializeAndDeserialize() throws Exception {
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(
69                Arrays.asList(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        assertTrue(scanResult.capability.equals(scanResultDeserialized.capability));
84        assertEquals(scanResult.associated, scanResultDeserialized.associated);
85        assertTrue(scanResult.radioChainInfos.containsAll(scanResultDeserialized.radioChainInfos));
86    }
87}
88