1/*
2 * Copyright (C) 2016 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.scanner;
18
19import static com.android.server.wifi.ScanTestUtil.bandIs;
20import static com.android.server.wifi.ScanTestUtil.channelsAre;
21import static com.android.server.wifi.ScanTestUtil.channelsToSpec;
22import static com.android.server.wifi.ScanTestUtil.createRequest;
23
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertNull;
28import static org.junit.Assert.assertTrue;
29
30import android.net.wifi.WifiScanner;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import com.android.server.wifi.WifiNative;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.experimental.runners.Enclosed;
38import org.junit.runner.RunWith;
39
40import java.util.Arrays;
41import java.util.Collections;
42import java.util.HashSet;
43
44/**
45 * Unit tests for {@link com.android.server.wifi.scanner.KnownBandsChannelHelper}.
46 */
47@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class
48public class KnownBandsChannelHelperTest {
49
50    private static final int[] CHANNELS_24_GHZ = new int[]{2400, 2450};
51    private static final int[] CHANNELS_5_GHZ = new int[]{5150, 5175};
52    private static final int[] CHANNELS_DFS = new int[]{5600, 5650, 5660};
53
54    /**
55     * Unit tests for
56     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.estimateScanDuration}.
57     */
58    @SmallTest
59    public static class EstimateScanDurationTest {
60        KnownBandsChannelHelper mChannelHelper;
61
62        /**
63         * Called before each test
64         * Create a channel helper
65         */
66        @Before
67        public void setUp() throws Exception {
68            mChannelHelper = new PresetKnownBandsChannelHelper(
69                    CHANNELS_24_GHZ,
70                    CHANNELS_5_GHZ,
71                    CHANNELS_DFS);
72        }
73
74        /**
75         * check a settings object with a few channels
76         */
77        @Test
78        public void fewChannels() {
79            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 2450, 5100),
80                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
81
82            assertEquals(ChannelHelper.SCAN_PERIOD_PER_CHANNEL_MS * 3,
83                    mChannelHelper.estimateScanDuration(testSettings));
84        }
85
86        /**
87         * check a settings object with a band
88         */
89        @Test
90        public void band() {
91            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ,
92                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
93
94            assertEquals(ChannelHelper.SCAN_PERIOD_PER_CHANNEL_MS * CHANNELS_24_GHZ.length,
95                    mChannelHelper.estimateScanDuration(testSettings));
96        }
97    }
98
99    /**
100     * Unit tests for
101     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.getAvailableScanChannels}.
102     */
103    @SmallTest
104    public static class GetAvailableScanChannelsTest {
105        KnownBandsChannelHelper mChannelHelper;
106
107        /**
108         * Called before each test
109         * Create a channel helper
110         */
111        @Before
112        public void setUp() throws Exception {
113            mChannelHelper = new PresetKnownBandsChannelHelper(
114                    CHANNELS_24_GHZ,
115                    CHANNELS_5_GHZ,
116                    CHANNELS_DFS);
117        }
118
119        private void testBand(int[] expectedChannels, int band) {
120            WifiScanner.ChannelSpec[] channels =
121                    mChannelHelper.getAvailableScanChannels(band);
122            assertEquals("nun channels", expectedChannels.length, channels.length);
123            for (int i = 0; i < channels.length; ++i) {
124                assertEquals("channels[" + i + "].frequency",
125                        expectedChannels[i], channels[i].frequency);
126            }
127        }
128
129        /**
130         * test the 2.4GHz band
131         */
132        @Test
133        public void channels24Ghz() {
134            testBand(CHANNELS_24_GHZ, WifiScanner.WIFI_BAND_24_GHZ);
135        }
136
137        /**
138         * test the 5GHz band
139         */
140        @Test
141        public void channels5Ghz() {
142            testBand(CHANNELS_5_GHZ, WifiScanner.WIFI_BAND_5_GHZ);
143        }
144
145        /**
146         * test the 5GHz DFS band
147         */
148        @Test
149        public void channelsDfs() {
150            testBand(CHANNELS_DFS, WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY);
151        }
152
153        /**
154         * test the 2.4GHz and 5GHz band
155         */
156        @Test
157        public void channels24GhzAnd5Ghz() {
158            int[] expectedChannels = new int[CHANNELS_24_GHZ.length + CHANNELS_5_GHZ.length];
159            System.arraycopy(CHANNELS_24_GHZ, 0, expectedChannels, 0, CHANNELS_24_GHZ.length);
160            System.arraycopy(CHANNELS_5_GHZ, 0, expectedChannels, CHANNELS_24_GHZ.length,
161                    CHANNELS_5_GHZ.length);
162            testBand(expectedChannels, WifiScanner.WIFI_BAND_BOTH);
163        }
164
165        /**
166         * test all bands
167         */
168        @Test
169        public void channelsAll() {
170            int[] expectedChannels =
171                    new int[CHANNELS_24_GHZ.length + CHANNELS_5_GHZ.length + CHANNELS_DFS.length];
172            System.arraycopy(CHANNELS_24_GHZ, 0, expectedChannels, 0, CHANNELS_24_GHZ.length);
173            System.arraycopy(CHANNELS_5_GHZ, 0, expectedChannels, CHANNELS_24_GHZ.length,
174                    CHANNELS_5_GHZ.length);
175            System.arraycopy(CHANNELS_DFS, 0, expectedChannels,
176                    CHANNELS_24_GHZ.length + CHANNELS_5_GHZ.length,
177                    CHANNELS_DFS.length);
178            testBand(expectedChannels, WifiScanner.WIFI_BAND_BOTH_WITH_DFS);
179        }
180    }
181
182    /**
183     * Unit tests for
184     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.settingsContainChannel}.
185     */
186    @SmallTest
187    public static class SettingsContainChannelTest {
188        KnownBandsChannelHelper mChannelHelper;
189
190        /**
191         * Called before each test
192         * Create a channel helper
193         */
194        @Before
195        public void setUp() throws Exception {
196            mChannelHelper = new PresetKnownBandsChannelHelper(
197                    CHANNELS_24_GHZ,
198                    CHANNELS_5_GHZ,
199                    CHANNELS_DFS);
200        }
201
202        /**
203         * check a settings object with no channels
204         */
205        @Test
206        public void emptySettings() {
207            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(),
208                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
209
210            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 2400));
211            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
212            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
213        }
214
215        /**
216         * check a settings object with some channels
217         */
218        @Test
219        public void settingsWithChannels() {
220            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 5650),
221                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
222
223            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
224            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
225            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650));
226        }
227
228        /**
229         * check a settings object with a band specified
230         */
231        @Test
232        public void settingsWithBand() {
233            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ,
234                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
235
236            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
237            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
238            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
239            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
240        }
241
242        /**
243         * check a settings object with multiple bands specified
244         */
245        @Test
246        public void settingsWithMultiBand() {
247            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_BOTH,
248                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
249
250            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
251            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
252            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150));
253            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
254        }
255    }
256
257    /**
258     * Unit tests for
259     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.KnownBandsChannelCollection}.
260     */
261    @SmallTest
262    public static class KnownBandsChannelCollectionTest {
263        ChannelHelper.ChannelCollection mChannelCollection;
264
265        /**
266         * Called before each test
267         * Create a collection to use for each test
268         */
269        @Before
270        public void setUp() throws Exception {
271            KnownBandsChannelHelper channelHelper = new PresetKnownBandsChannelHelper(
272                    CHANNELS_24_GHZ,
273                    CHANNELS_5_GHZ,
274                    CHANNELS_DFS);
275            mChannelCollection = channelHelper.createChannelCollection();
276        }
277
278        /**
279         * Create an empty collection
280         */
281        @Test
282        public void empty() {
283            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
284            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
285            assertThat(bucketSettings, channelsAre());
286
287            assertEquals(Collections.<Integer>emptySet(),
288                    mChannelCollection.getSupplicantScanFreqs());
289
290            assertTrue(mChannelCollection.isEmpty());
291            assertFalse(mChannelCollection.containsChannel(2400));
292            assertFalse(mChannelCollection.containsChannel(5150));
293        }
294
295        /**
296         * Add something to a collection and then clear it and make sure nothing is in it
297         */
298        @Test
299        public void clear() {
300            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
301            mChannelCollection.clear();
302
303            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
304            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
305            assertThat(bucketSettings, channelsAre());
306
307            assertEquals(Collections.<Integer>emptySet(),
308                    mChannelCollection.getSupplicantScanFreqs());
309
310            assertTrue(mChannelCollection.isEmpty());
311            assertFalse(mChannelCollection.containsChannel(2400));
312            assertFalse(mChannelCollection.containsChannel(5150));
313        }
314
315        /**
316         * Add a single band to the collection
317         */
318        @Test
319        public void addBand() {
320            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
321
322            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
323            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
324            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
325
326            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
327                    mChannelCollection.getSupplicantScanFreqs());
328
329            assertFalse(mChannelCollection.isEmpty());
330            assertTrue(mChannelCollection.containsChannel(2400));
331            assertFalse(mChannelCollection.containsChannel(5150));
332        }
333
334        /**
335         * Add a single channel to the collection
336         */
337        @Test
338        public void addChannel_single() {
339            mChannelCollection.addChannel(2400);
340
341            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
342            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
343            assertThat(bucketSettings, channelsAre(2400));
344
345            assertEquals(new HashSet<Integer>(Arrays.asList(2400)),
346                    mChannelCollection.getSupplicantScanFreqs());
347
348            assertFalse(mChannelCollection.isEmpty());
349            assertTrue(mChannelCollection.containsChannel(2400));
350            assertFalse(mChannelCollection.containsChannel(5150));
351        }
352
353        /**
354         * Add a multiple channels to the collection
355         */
356        @Test
357        public void addChannel_multiple() {
358            mChannelCollection.addChannel(2400);
359            mChannelCollection.addChannel(2450);
360
361            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
362            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
363            assertThat(bucketSettings, channelsAre(2400, 2450));
364
365            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
366                    mChannelCollection.getSupplicantScanFreqs());
367
368            assertFalse(mChannelCollection.isEmpty());
369            assertTrue(mChannelCollection.containsChannel(2400));
370            assertFalse(mChannelCollection.containsChannel(5150));
371        }
372
373        /**
374         * Add a band and channel that is on that band
375         */
376        @Test
377        public void addChannel_and_addBand_sameBand() {
378            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
379            mChannelCollection.addChannel(2400);
380
381            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
382            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
383            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
384
385            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
386                    mChannelCollection.getSupplicantScanFreqs());
387
388            assertFalse(mChannelCollection.isEmpty());
389            assertTrue(mChannelCollection.containsChannel(2400));
390            assertFalse(mChannelCollection.containsChannel(5150));
391        }
392
393        /**
394         * Add a band and channel that is not that band
395         */
396        @Test
397        public void addChannel_and_addBand_withDifferentBandChannel() {
398            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
399            mChannelCollection.addChannel(5150);
400
401            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
402            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
403            assertThat(bucketSettings, channelsAre(2400, 2450, 5150));
404
405            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450, 5150)),
406                    mChannelCollection.getSupplicantScanFreqs());
407
408            assertFalse(mChannelCollection.isEmpty());
409            assertTrue(mChannelCollection.containsChannel(2400));
410            assertTrue(mChannelCollection.containsChannel(5150));
411        }
412
413        /**
414         * Add a band that should contain all channels
415         */
416        @Test
417        public void addChannel_and_addBand_all() {
418            mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS);
419            mChannelCollection.addChannel(5150);
420
421            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
422            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
423            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS));
424
425            assertNull(mChannelCollection.getSupplicantScanFreqs());
426
427            assertFalse(mChannelCollection.isEmpty());
428            assertTrue(mChannelCollection.containsChannel(2400));
429            assertTrue(mChannelCollection.containsChannel(5150));
430            assertTrue(mChannelCollection.containsChannel(5600));
431        }
432
433        /**
434         * Add enough channels on a single band that the max channels is exceeded
435         */
436        @Test
437        public void addChannel_exceedMaxChannels() {
438            mChannelCollection.addChannel(5600);
439            mChannelCollection.addChannel(5650);
440            mChannelCollection.addChannel(5660);
441
442            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
443            mChannelCollection.fillBucketSettings(bucketSettings, 2);
444            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY));
445        }
446
447        /**
448         * Add enough channels across multiple bands that the max channels is exceeded
449         */
450        @Test
451        public void addChannel_exceedMaxChannelsOnMultipleBands() {
452            mChannelCollection.addChannel(2400);
453            mChannelCollection.addChannel(2450);
454            mChannelCollection.addChannel(5150);
455
456            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
457            mChannelCollection.fillBucketSettings(bucketSettings, 2);
458            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH));
459        }
460    }
461}
462