summaryrefslogtreecommitdiff
path: root/robotests/src/com/android/networkrecommendation/util/ScanResultUtilTest.java
blob: a55a59875b555c5c8c47af3ec4df8166db49e2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.networkrecommendation.util;

import static com.android.networkrecommendation.PlatformTestObjectFactory.createOpenNetworkScanResult;
import static com.android.networkrecommendation.util.SsidUtil.quoteSsid;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.net.NetworkKey;
import android.net.WifiKey;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import com.android.networkrecommendation.config.Flag;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

/** Unit tests for {@link ScanResultUtil}. */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "packages/services/NetworkRecommendation/AndroidManifest.xml", sdk = 23)
public class ScanResultUtilTest {

    @Before
    public void setUp() {
        Flag.initForTest();
    }

    @Test
    public void testCreateNetworkKey() {
        ScanResult scanResult = createOpenNetworkScanResult("testSSID", "00:00:00:00:00:00");
        NetworkKey networkKey = ScanResultUtil.createNetworkKey(scanResult);

        assertEquals(quoteSsid(scanResult.SSID), networkKey.wifiKey.ssid);
        assertEquals(scanResult.BSSID, networkKey.wifiKey.bssid);
    }

    @Test
    public void testCreateWifiKey_validScanResult() {
        ScanResult scanResult = createOpenNetworkScanResult("testSSID", "00:00:00:00:00:00");
        WifiKey wifiKey = ScanResultUtil.createWifiKey(scanResult);

        assertEquals(quoteSsid(scanResult.SSID), wifiKey.ssid);
        assertEquals(scanResult.BSSID, wifiKey.bssid);
    }

    @Test
    public void testCreateWifiKey_invalidScanResultReturnsNull() {
        ScanResult scanResult1 = createOpenNetworkScanResult(null, "00:00:00:00:00:00");

        assertNull(ScanResultUtil.createWifiKey(scanResult1));

        ScanResult scanResult2 = createOpenNetworkScanResult("testSSID", null);

        assertNull(ScanResultUtil.createWifiKey(scanResult2));

        ScanResult scanResult3 = createOpenNetworkScanResult("testSSID", "invalidBSSID");

        assertNull(ScanResultUtil.createWifiKey(scanResult3));
    }

    @Test
    public void testDoesScanResultMatchWithNetwork_matching() {
        WifiConfiguration config = new WifiConfiguration();
        config.SSID = quoteSsid("testSSID");
        config.BSSID = "00:00:00:00:00:00";
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        ScanResult scanResult = createOpenNetworkScanResult(config.SSID, config.BSSID);

        assertTrue(ScanResultUtil.doesScanResultMatchWithNetwork(scanResult, config));
    }

    @Test
    public void testDoesScanResultMatchWithNetwork_onlyOneOpenNetworkDoesNotMatch() {
        WifiConfiguration config = new WifiConfiguration();
        config.SSID = quoteSsid("testSSID");
        config.BSSID = "00:00:00:00:00:00";
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        ScanResult scanResult = createOpenNetworkScanResult(config.SSID, config.BSSID);

        assertFalse(ScanResultUtil.doesScanResultMatchWithNetwork(scanResult, config));
    }
}