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.assertEquals;
20 
21 import android.os.Parcel;
22 
23 import androidx.test.filters.SmallTest;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 
32 /**
33  * Unit tests for {@link android.net.wifi.nl80211.SingleScanSettingsResult}.
34  */
35 @SmallTest
36 public class SingleScanSettingsTest {
37 
38     private static final byte[] TEST_SSID_1 =
39             new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
40     private static final byte[] TEST_SSID_2 =
41             new byte[] {'A', 'n', 'd', 'r', 'o', 'i', 'd', 'T', 'e', 's', 't'};
42     private static final int TEST_FREQUENCY_1 = 2456;
43     private static final int TEST_FREQUENCY_2 = 5215;
44     private static final int TEST_VALUE = 42;
45 
46     private ChannelSettings mChannelSettings1;
47     private ChannelSettings mChannelSettings2;
48     private HiddenNetwork mHiddenNetwork1;
49     private HiddenNetwork mHiddenNetwork2;
50     private byte[] mVendorIes;
51 
52     @Before
setUp()53     public void setUp() {
54         mChannelSettings1 = new ChannelSettings();
55         mChannelSettings1.frequency = TEST_FREQUENCY_1;
56         mChannelSettings2 = new ChannelSettings();
57         mChannelSettings2.frequency = TEST_FREQUENCY_2;
58 
59         mHiddenNetwork1 = new HiddenNetwork();
60         mHiddenNetwork1.ssid = TEST_SSID_1;
61         mHiddenNetwork2 = new HiddenNetwork();
62         mHiddenNetwork2.ssid = TEST_SSID_2;
63 
64         mVendorIes = new byte[]{(byte) 0xdd, 0x7, 0x00, 0x50, (byte) 0xf2, 0x08, 0x11, 0x22, 0x33,
65                 (byte) 0xdd, 0x7, 0x00, 0x50, (byte) 0xf2, 0x08, 0x44, 0x55, 0x66};
66     }
67 
68     /**
69      *  SingleScanSettings object can be serialized and deserialized, while keeping the
70      *  values unchanged.
71      */
72     @Test
canSerializeAndDeserialize()73     public void canSerializeAndDeserialize() {
74         SingleScanSettings scanSettings = new SingleScanSettings();
75         scanSettings.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
76         scanSettings.channelSettings =
77                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
78         scanSettings.hiddenNetworks =
79                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
80         scanSettings.enable6GhzRnr = true;
81         scanSettings.vendorIes = mVendorIes;
82 
83         Parcel parcel = Parcel.obtain();
84         scanSettings.writeToParcel(parcel, 0);
85         // Rewind the pointer to the head of the parcel.
86         parcel.setDataPosition(0);
87         SingleScanSettings scanSettingsDeserialized =
88                 SingleScanSettings.CREATOR.createFromParcel(parcel);
89 
90         assertEquals(scanSettings, scanSettingsDeserialized);
91         assertEquals(scanSettings.hashCode(), scanSettingsDeserialized.hashCode());
92     }
93 
94     /**
95      * Tests usage of {@link SingleScanSettings} as a HashMap key type.
96      */
97     @Test
testAsHashMapKey()98     public void testAsHashMapKey() {
99         SingleScanSettings scanSettings1 = new SingleScanSettings();
100         scanSettings1.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
101         scanSettings1.channelSettings =
102                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
103         scanSettings1.hiddenNetworks =
104                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
105         scanSettings1.vendorIes = mVendorIes;
106 
107         SingleScanSettings scanSettings2 = new SingleScanSettings();
108         scanSettings2.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
109         scanSettings2.channelSettings =
110                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
111         scanSettings2.hiddenNetworks =
112                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
113         scanSettings2.vendorIes = mVendorIes;
114 
115         assertEquals(scanSettings1, scanSettings2);
116         assertEquals(scanSettings1.hashCode(), scanSettings2.hashCode());
117 
118         HashMap<SingleScanSettings, Integer> map = new HashMap<>();
119         map.put(scanSettings1, TEST_VALUE);
120 
121         assertEquals(TEST_VALUE, map.get(scanSettings2).intValue());
122     }
123 }
124