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