Home | History | Annotate | Download | only in scanner
      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 
     17 package com.android.server.wifi.scanner;
     18 
     19 import static com.android.server.wifi.ScanTestUtil.bandIs;
     20 import static com.android.server.wifi.ScanTestUtil.channelsAre;
     21 import static com.android.server.wifi.ScanTestUtil.channelsToSpec;
     22 import static com.android.server.wifi.ScanTestUtil.createRequest;
     23 
     24 import static org.hamcrest.MatcherAssert.assertThat;
     25 import static org.junit.Assert.assertEquals;
     26 import static org.junit.Assert.assertFalse;
     27 import static org.junit.Assert.assertNull;
     28 import static org.junit.Assert.assertTrue;
     29 
     30 import android.net.wifi.WifiScanner;
     31 import android.test.suitebuilder.annotation.SmallTest;
     32 
     33 import com.android.server.wifi.WifiNative;
     34 
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.experimental.runners.Enclosed;
     38 import org.junit.runner.RunWith;
     39 
     40 import java.util.Arrays;
     41 import java.util.Collections;
     42 import 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
     48 public 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             assertFalse(mChannelCollection.isAllChannels());
    294         }
    295 
    296         /**
    297          * Add something to a collection and then clear it and make sure nothing is in it
    298          */
    299         @Test
    300         public void clear() {
    301             mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
    302             mChannelCollection.clear();
    303 
    304             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    305             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    306             assertThat(bucketSettings, channelsAre());
    307 
    308             assertEquals(Collections.<Integer>emptySet(),
    309                     mChannelCollection.getSupplicantScanFreqs());
    310 
    311             assertTrue(mChannelCollection.isEmpty());
    312             assertFalse(mChannelCollection.containsChannel(2400));
    313             assertFalse(mChannelCollection.containsChannel(5150));
    314             assertFalse(mChannelCollection.isAllChannels());
    315         }
    316 
    317         /**
    318          * Add a single band to the collection
    319          */
    320         @Test
    321         public void addBand() {
    322             mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
    323 
    324             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    325             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    326             assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
    327 
    328             assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
    329                     mChannelCollection.getSupplicantScanFreqs());
    330 
    331             assertFalse(mChannelCollection.isEmpty());
    332             assertTrue(mChannelCollection.containsChannel(2400));
    333             assertFalse(mChannelCollection.containsChannel(5150));
    334             assertFalse(mChannelCollection.isAllChannels());
    335         }
    336 
    337         /**
    338          * Add a single channel to the collection
    339          */
    340         @Test
    341         public void addChannel_single() {
    342             mChannelCollection.addChannel(2400);
    343 
    344             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    345             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    346             assertThat(bucketSettings, channelsAre(2400));
    347 
    348             assertEquals(new HashSet<Integer>(Arrays.asList(2400)),
    349                     mChannelCollection.getSupplicantScanFreqs());
    350 
    351             assertFalse(mChannelCollection.isEmpty());
    352             assertTrue(mChannelCollection.containsChannel(2400));
    353             assertFalse(mChannelCollection.containsChannel(5150));
    354             assertFalse(mChannelCollection.isAllChannels());
    355         }
    356 
    357         /**
    358          * Add a multiple channels to the collection
    359          */
    360         @Test
    361         public void addChannel_multiple() {
    362             mChannelCollection.addChannel(2400);
    363             mChannelCollection.addChannel(2450);
    364 
    365             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    366             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    367             assertThat(bucketSettings, channelsAre(2400, 2450));
    368 
    369             assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
    370                     mChannelCollection.getSupplicantScanFreqs());
    371 
    372             assertFalse(mChannelCollection.isEmpty());
    373             assertTrue(mChannelCollection.containsChannel(2400));
    374             assertFalse(mChannelCollection.containsChannel(5150));
    375             assertFalse(mChannelCollection.isAllChannels());
    376         }
    377 
    378         /**
    379          * Add a band and channel that is on that band
    380          */
    381         @Test
    382         public void addChannel_and_addBand_sameBand() {
    383             mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
    384             mChannelCollection.addChannel(2400);
    385 
    386             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    387             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    388             assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
    389 
    390             assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
    391                     mChannelCollection.getSupplicantScanFreqs());
    392 
    393             assertFalse(mChannelCollection.isEmpty());
    394             assertTrue(mChannelCollection.containsChannel(2400));
    395             assertFalse(mChannelCollection.containsChannel(5150));
    396             assertFalse(mChannelCollection.isAllChannels());
    397         }
    398 
    399         /**
    400          * Add a band and channel that is not that band
    401          */
    402         @Test
    403         public void addChannel_and_addBand_withDifferentBandChannel() {
    404             mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
    405             mChannelCollection.addChannel(5150);
    406 
    407             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    408             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    409             assertThat(bucketSettings, channelsAre(2400, 2450, 5150));
    410 
    411             assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450, 5150)),
    412                     mChannelCollection.getSupplicantScanFreqs());
    413 
    414             assertFalse(mChannelCollection.isEmpty());
    415             assertTrue(mChannelCollection.containsChannel(2400));
    416             assertTrue(mChannelCollection.containsChannel(5150));
    417             assertFalse(mChannelCollection.isAllChannels());
    418         }
    419 
    420         /**
    421          * Add a band that should contain all channels
    422          */
    423         @Test
    424         public void addChannel_and_addBand_all() {
    425             mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS);
    426             mChannelCollection.addChannel(5150);
    427 
    428             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    429             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    430             assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS));
    431 
    432             assertNull(mChannelCollection.getSupplicantScanFreqs());
    433 
    434             assertFalse(mChannelCollection.isEmpty());
    435             assertTrue(mChannelCollection.containsChannel(2400));
    436             assertTrue(mChannelCollection.containsChannel(5150));
    437             assertTrue(mChannelCollection.containsChannel(5600));
    438             assertTrue(mChannelCollection.isAllChannels());
    439         }
    440 
    441         /**
    442          * Add enough channels on a single band that the max channels is exceeded
    443          */
    444         @Test
    445         public void addChannel_exceedMaxChannels() {
    446             mChannelCollection.addChannel(5600);
    447             mChannelCollection.addChannel(5650);
    448             mChannelCollection.addChannel(5660);
    449 
    450             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    451             mChannelCollection.fillBucketSettings(bucketSettings, 2);
    452             assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY));
    453             assertFalse(mChannelCollection.isAllChannels());
    454         }
    455 
    456         /**
    457          * Add enough channels across multiple bands that the max channels is exceeded
    458          */
    459         @Test
    460         public void addChannel_exceedMaxChannelsOnMultipleBands() {
    461             mChannelCollection.addChannel(2400);
    462             mChannelCollection.addChannel(2450);
    463             mChannelCollection.addChannel(5150);
    464 
    465             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    466             mChannelCollection.fillBucketSettings(bucketSettings, 2);
    467             assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH));
    468             assertFalse(mChannelCollection.isAllChannels());
    469         }
    470 
    471 
    472         /**
    473          * Add enough channels across all bands that the max channels is exceeded
    474          */
    475         @Test
    476         public void addChannel_addAllAvailableChannels() {
    477             mChannelCollection.addChannel(2400);
    478             mChannelCollection.addChannel(2450);
    479             mChannelCollection.addChannel(5150);
    480             mChannelCollection.addChannel(5175);
    481             mChannelCollection.addChannel(5600);
    482             mChannelCollection.addChannel(5650);
    483             mChannelCollection.addChannel(5660);
    484 
    485             WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
    486             mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
    487             assertThat(bucketSettings, channelsAre(2400, 2450, 5150, 5175, 5600, 5650, 5660));
    488             assertTrue(mChannelCollection.isAllChannels());
    489         }
    490     }
    491 }
    492