ScanTestUtil.java revision 72c639e8b97067e948eca8be50dfea3173121090
1/*
2 * Copyright (C) 2015 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;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.mockito.Mockito.when;
22
23import android.net.wifi.ScanResult;
24import android.net.wifi.WifiScanner;
25import android.net.wifi.WifiScanner.ChannelSpec;
26import android.net.wifi.WifiScanner.ScanData;
27import android.net.wifi.WifiScanner.ScanSettings;
28import android.net.wifi.WifiSsid;
29
30import com.android.server.wifi.WifiNative.BucketSettings;
31
32import java.lang.reflect.Field;
33import java.util.Arrays;
34import java.util.HashSet;
35import java.util.Set;
36
37/**
38 * Utilities for testing Wifi Scanning
39 */
40public class ScanTestUtil {
41
42    public static void installWlanWifiNative(WifiNative wifiNative) throws Exception {
43        Field field = WifiNative.class.getDeclaredField("wlanNativeInterface");
44        field.setAccessible(true);
45        field.set(null, wifiNative);
46
47        // Clear static state
48        WifiChannelHelper.clearChannelCache();
49    }
50
51    public static void setupMockChannels(WifiNative wifiNative, int[] channels24, int[] channels5,
52            int[] channelsDfs) throws Exception {
53        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_24_GHZ))
54                .thenReturn(channels24);
55        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_5_GHZ))
56                .thenReturn(channels5);
57        when(wifiNative.getChannelsForBand(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY))
58                .thenReturn(channelsDfs);
59    }
60
61    public static ScanSettings createRequest(WifiScanner.ChannelSpec[] channels, int period,
62            int batch, int bssidsPerScan, int reportEvents) {
63        ScanSettings request = new ScanSettings();
64        request.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
65        request.channels = channels;
66        request.periodInMs = period;
67        request.numBssidsPerScan = bssidsPerScan;
68        request.maxScansToCache = batch;
69        request.reportEvents = reportEvents;
70        return request;
71    }
72
73    public static ScanSettings createRequest(int band, int period, int batch, int bssidsPerScan,
74            int reportEvents) {
75        ScanSettings request = new ScanSettings();
76        request.band = band;
77        request.channels = null;
78        request.periodInMs = period;
79        request.numBssidsPerScan = bssidsPerScan;
80        request.maxScansToCache = batch;
81        request.reportEvents = reportEvents;
82        return request;
83    }
84
85    /**
86     * Builder to create WifiNative.ScanSettings objects for testing
87     */
88    public static class NativeScanSettingsBuilder {
89        private final WifiNative.ScanSettings mSettings = new WifiNative.ScanSettings();
90        public NativeScanSettingsBuilder() {
91            mSettings.buckets = new WifiNative.BucketSettings[0];
92            mSettings.num_buckets = 0;
93            mSettings.report_threshold_percent = 100;
94        }
95
96        public NativeScanSettingsBuilder withBasePeriod(int basePeriod) {
97            mSettings.base_period_ms = basePeriod;
98            return this;
99        }
100        public NativeScanSettingsBuilder withMaxApPerScan(int maxAp) {
101            mSettings.max_ap_per_scan = maxAp;
102            return this;
103        }
104        public NativeScanSettingsBuilder withMaxScansToCache(int maxScans) {
105            mSettings.report_threshold_num_scans = maxScans;
106            return this;
107        }
108
109        public NativeScanSettingsBuilder addBucketWithBand(
110                int period, int reportEvents, int band) {
111            WifiNative.BucketSettings bucket = new WifiNative.BucketSettings();
112            bucket.bucket = mSettings.num_buckets;
113            bucket.band = band;
114            bucket.period_ms = period;
115            bucket.report_events = reportEvents;
116            return addBucket(bucket);
117        }
118
119        public NativeScanSettingsBuilder addBucketWithChannels(
120                int period, int reportEvents, int... channels) {
121            WifiNative.BucketSettings bucket = new WifiNative.BucketSettings();
122            bucket.bucket = mSettings.num_buckets;
123            bucket.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
124            bucket.num_channels = channels.length;
125            bucket.channels = new WifiNative.ChannelSettings[channels.length];
126            for (int i = 0; i < channels.length; ++i) {
127                bucket.channels[i] = new WifiNative.ChannelSettings();
128                bucket.channels[i].frequency = channels[i];
129            }
130            bucket.period_ms = period;
131            bucket.report_events = reportEvents;
132            return addBucket(bucket);
133        }
134
135        public NativeScanSettingsBuilder addBucket(WifiNative.BucketSettings bucket) {
136            mSettings.buckets = Arrays.copyOf(mSettings.buckets, mSettings.num_buckets + 1);
137            mSettings.buckets[mSettings.num_buckets] = bucket;
138            mSettings.num_buckets = mSettings.num_buckets + 1;
139            return this;
140        }
141
142        public WifiNative.ScanSettings build() {
143            return mSettings;
144        }
145    }
146
147    public static Set<Integer> createFreqSet(int... elements) {
148        Set<Integer> set = new HashSet<>();
149        for (int e : elements) {
150            set.add(e);
151        }
152        return set;
153    }
154
155    public static ScanResult createScanResult(int freq) {
156        return new ScanResult(WifiSsid.createFromAsciiEncoded("AN SSID"), "00:00:00:00:00:00", "",
157                0, freq, 0);
158    }
159
160    public static ScanData createScanData(int... freqs) {
161        ScanResult[] results = new ScanResult[freqs.length];
162        for (int i = 0; i < freqs.length; ++i) {
163            results[i] = createScanResult(freqs[i]);
164        }
165        return new ScanData(0, 0, results);
166    }
167
168    public static ScanData[] createScanDatas(int[][] freqs) {
169        ScanData[] data = new ScanData[freqs.length];
170        for (int i = 0; i < freqs.length; ++i) {
171            data[i] = createScanData(freqs[i]);
172        }
173        return data;
174    }
175
176    private static void assertScanDataEquals(String prefix, ScanData expected, ScanData actual) {
177        assertNotNull(expected);
178        assertNotNull(actual);
179        assertEquals(prefix + "id", expected.getId(), actual.getId());
180        assertEquals(prefix + "flags", expected.getFlags(), actual.getFlags());
181        assertEquals(prefix + "results.length",
182                expected.getResults().length, actual.getResults().length);
183        for (int j = 0; j < expected.getResults().length; ++j) {
184            ScanResult expectedResult = expected.getResults()[j];
185            ScanResult actualResult = actual.getResults()[j];
186            assertEquals(prefix + "results[" + j + "].SSID",
187                    expectedResult.SSID, actualResult.SSID);
188            assertEquals(prefix + "results[" + j + "].wifiSsid",
189                    expectedResult.wifiSsid.toString(), actualResult.wifiSsid.toString());
190            assertEquals(prefix + "results[" + j + "].BSSID",
191                    expectedResult.BSSID, actualResult.BSSID);
192            assertEquals(prefix + "results[" + j + "].capabilities",
193                    expectedResult.capabilities, actualResult.capabilities);
194            assertEquals(prefix + "results[" + j + "].level",
195                    expectedResult.level, actualResult.level);
196            assertEquals(prefix + "results[" + j + "].frequency",
197                    expectedResult.frequency, actualResult.frequency);
198            assertEquals(prefix + "results[" + j + "].timestamp",
199                    expectedResult.timestamp, actualResult.timestamp);
200            assertEquals(prefix + "results[" + j + "].seen",
201                    expectedResult.seen, actualResult.seen);
202        }
203    }
204    public static void assertScanDataEquals(ScanData expected, ScanData actual) {
205        assertScanDataEquals("", expected, actual);
206    }
207
208    public static void assertScanDatasEquals(ScanData[] expected, ScanData[] actual) {
209        assertNotNull(expected);
210        assertNotNull(actual);
211        assertEquals("ScanData.length", expected.length, actual.length);
212        for (int i = 0; i < expected.length; ++i) {
213            assertScanDataEquals("ScanData[" + i + "].", expected[i], actual[i]);
214        }
215    }
216
217    public static WifiScanner.ChannelSpec[] channelsToSpec(int... channels) {
218        WifiScanner.ChannelSpec[] channelSpecs = new WifiScanner.ChannelSpec[channels.length];
219        for (int i = 0; i < channels.length; ++i) {
220            channelSpecs[i] = new WifiScanner.ChannelSpec(channels[i]);
221        }
222        return channelSpecs;
223    }
224
225    public static ChannelSpec[] getAllChannels(BucketSettings bucket) {
226        if (bucket.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
227            ChannelSpec[] channels = new ChannelSpec[bucket.num_channels];
228            for (int i = 0; i < bucket.num_channels; i++) {
229                channels[i] = new ChannelSpec(bucket.channels[i].frequency);
230            }
231            return channels;
232        } else {
233            return WifiChannelHelper.getChannelsForBand(bucket.band);
234        }
235    }
236    public static ChannelSpec[] getAllChannels(ScanSettings settings) {
237        if (settings.band == WifiScanner.WIFI_BAND_UNSPECIFIED) {
238            ChannelSpec[] channels = new ChannelSpec[settings.channels.length];
239            for (int i = 0; i < settings.channels.length; i++) {
240                channels[i] = new ChannelSpec(settings.channels[i].frequency);
241            }
242            return channels;
243        } else {
244            return WifiChannelHelper.getChannelsForBand(settings.band);
245        }
246    }
247}
248