Home | History | Annotate | Download | only in wifi
      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 package com.android.server.wifi;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 import static org.mockito.Mockito.*;
     22 
     23 import android.net.wifi.WifiConfiguration;
     24 import android.support.test.filters.SmallTest;
     25 
     26 import org.junit.Test;
     27 
     28 /**
     29  * Unit tests for {@link com.android.server.wifi.ScanResultMatchInfoTest}.
     30  */
     31 @SmallTest
     32 public class ScanResultMatchInfoTest {
     33     /**
     34      * Tests that equivalent ScanResultMatchInfo objects are created for WifiConfigurations and
     35      * their associated ScanResult
     36      */
     37     @Test
     38     public void testScanResultMatchesWifiConfiguration() {
     39         WifiConfiguration conf =
     40                 WifiConfigurationTestUtil.createPskNetwork("\"PrettyFlyForAWifi\"");
     41         ScanDetail scan = createScanDetailForNetwork(conf, "AA:AA:AA:AA:AA:AA");
     42         assertEquals(ScanResultMatchInfo.fromWifiConfiguration(conf),
     43                 ScanResultMatchInfo.fromScanResult(scan.getScanResult()));
     44 
     45         conf = WifiConfigurationTestUtil.createOpenNetwork("\"WIFIght the inevitable\"");
     46         scan = createScanDetailForNetwork(conf, "BB:BB:BB:BB:BB:BB");
     47         assertEquals(ScanResultMatchInfo.fromWifiConfiguration(conf),
     48                 ScanResultMatchInfo.fromScanResult(scan.getScanResult()));
     49     }
     50 
     51     /**
     52      * Tests that multiple ScanResults with different BSSIDs will produce equivalent
     53      * ScanResultMatchInfo objects to their associated WifiConfiguration
     54      */
     55     @Test
     56     public void testDifferentBssidScanResultsMatch() {
     57         WifiConfiguration conf =
     58                 WifiConfigurationTestUtil.createPskNetwork("\"PrettyFlyForAWifi-5G\"");
     59         ScanDetail scan1 = createScanDetailForNetwork(conf, "AA:AA:AA:AA:AA:AA");
     60         ScanDetail scan2 = createScanDetailForNetwork(conf, "BB:BB:BB:BB:BB:BB");
     61         assertFalse(scan1.getScanResult().BSSID.equals(scan2.getScanResult().BSSID));
     62         assertEquals(ScanResultMatchInfo.fromScanResult(scan1.getScanResult()),
     63                 ScanResultMatchInfo.fromScanResult(scan2.getScanResult()));
     64     }
     65 
     66     /**
     67      * Tests that ScanResultMatchInfo objects created for different SSIDs or security types are not
     68      * equivalent
     69      */
     70     @Test
     71     public void testDifferentNetworkScanResultsDontMatch() {
     72         WifiConfiguration psk =
     73                 WifiConfigurationTestUtil.createPskNetwork("\"Series Of Tubes\"");
     74         WifiConfiguration open1 =
     75                 WifiConfigurationTestUtil.createOpenNetwork("\"Series Of Tubes\"");
     76         WifiConfiguration open2 =
     77                 WifiConfigurationTestUtil.createOpenNetwork("\"Mom, Click Here For Internet\"");
     78         ScanDetail scanOpen1 = createScanDetailForNetwork(open1, "AA:AA:AA:AA:AA:AA");
     79         ScanDetail scanOpen2 = createScanDetailForNetwork(open2, "BB:BB:BB:BB:BB:BB");
     80         ScanDetail scanPsk =   createScanDetailForNetwork(psk,   "CC:CC:CC:CC:CC:CC");
     81         assertTrue(ScanResultMatchInfo.fromScanResult(scanOpen1.getScanResult())
     82                 != ScanResultMatchInfo.fromScanResult(scanOpen2.getScanResult()));
     83         assertTrue(ScanResultMatchInfo.fromScanResult(scanOpen1.getScanResult())
     84                 != ScanResultMatchInfo.fromScanResult(scanPsk.getScanResult()));
     85     }
     86 
     87     /**
     88      * Creates a scan detail corresponding to the provided network and given BSSID
     89      */
     90     private ScanDetail createScanDetailForNetwork(
     91             WifiConfiguration configuration, String bssid) {
     92         return WifiConfigurationTestUtil.createScanDetailForNetwork(configuration, bssid, -40,
     93                 2402, 0, 0);
     94     }
     95 }
     96