SavedNetworkEvaluatorTest.java revision 57c8c1165d5ea5c10cd96ea51652c11db7635302
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;
18
19import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE;
20import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK;
21
22import static org.junit.Assert.*;
23import static org.mockito.Mockito.*;
24
25import android.content.Context;
26import android.content.res.Resources;
27import android.net.wifi.ScanResult;
28import android.net.wifi.WifiConfiguration;
29import android.os.SystemClock;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import com.android.internal.R;
33import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs;
34
35import org.junit.After;
36import org.junit.Before;
37import org.junit.Test;
38
39import java.util.List;
40
41/**
42 * Unit tests for {@link com.android.server.wifi.SavedNetworkEvaluator}.
43 */
44@SmallTest
45public class SavedNetworkEvaluatorTest {
46
47    /** Sets up test. */
48    @Before
49    public void setUp() throws Exception {
50        mResource = getResource();
51        mContext = getContext();
52        mWifiConfigManager = getWifiConfigManager();
53
54        when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime());
55
56        mThresholdMinimumRssi2G = mResource.getInteger(
57                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz);
58        mThresholdMinimumRssi5G = mResource.getInteger(
59                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz);
60        mThresholdQualifiedRssi2G = mResource.getInteger(
61                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz);
62        mThresholdQualifiedRssi5G = mResource.getInteger(
63                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz);
64        mThresholdSaturatedRssi2G = mResource.getInteger(
65                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz);
66        mThresholdSaturatedRssi5G = mResource.getInteger(
67                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz);
68
69        mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mWifiConfigManager,
70                mClock, null);
71    }
72
73    /** Cleans up test. */
74    @After
75    public void cleanup() {
76        validateMockitoUsage();
77    }
78
79    private SavedNetworkEvaluator mSavedNetworkEvaluator;
80    private WifiConfigManager mWifiConfigManager;
81    private Context mContext;
82    private Resources mResource;
83    private Clock mClock = mock(Clock.class);
84    private int mThresholdMinimumRssi2G;
85    private int mThresholdMinimumRssi5G;
86    private int mThresholdQualifiedRssi2G;
87    private int mThresholdQualifiedRssi5G;
88    private int mThresholdSaturatedRssi2G;
89    private int mThresholdSaturatedRssi5G;
90    private static final String TAG = "Saved Network Evaluator Unit Test";
91
92    Context getContext() {
93        Context context = mock(Context.class);
94        Resources resource = mock(Resources.class);
95
96        when(context.getResources()).thenReturn(mResource);
97        return context;
98    }
99
100    Resources getResource() {
101        Resources resource = mock(Resources.class);
102
103        when(resource.getInteger(
104                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz))
105                .thenReturn(-70);
106        when(resource.getInteger(
107                R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz))
108                .thenReturn(-73);
109        when(resource.getInteger(
110                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz))
111                .thenReturn(-70);
112        when(resource.getInteger(
113                R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz))
114                .thenReturn(-73);
115        when(resource.getInteger(
116                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz))
117                .thenReturn(-82);
118        when(resource.getInteger(
119                R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz))
120                .thenReturn(-85);
121        when(resource.getInteger(
122                R.integer.config_wifi_framework_RSSI_SCORE_SLOPE))
123                .thenReturn(4);
124        when(resource.getInteger(
125                R.integer.config_wifi_framework_RSSI_SCORE_OFFSET))
126                .thenReturn(85);
127        when(resource.getInteger(
128                R.integer.config_wifi_framework_SAME_BSSID_AWARD))
129                .thenReturn(24);
130        when(resource.getInteger(
131                R.integer.config_wifi_framework_SECURITY_AWARD))
132                .thenReturn(80);
133        when(resource.getInteger(
134                R.integer.config_wifi_framework_5GHz_preference_boost_factor))
135                .thenReturn(16);
136        when(resource.getInteger(
137                R.integer.config_wifi_framework_current_network_boost))
138                .thenReturn(16);
139
140        return resource;
141    }
142
143    WifiConfigManager getWifiConfigManager() {
144        WifiConfigManager wifiConfigManager = mock(WifiConfigManager.class);
145        when(wifiConfigManager.getLastSelectedNetwork())
146                .thenReturn(WifiConfiguration.INVALID_NETWORK_ID);
147        return wifiConfigManager;
148    }
149
150
151    /**
152     * Between two 2G networks, choose the one with stronger RSSI value if other conditions
153     * are the same and the RSSI values are not satuarted.
154     */
155    @Test
156    public void chooseStrongerRssi2GNetwork() {
157        String[] ssids = {"\"test1\"", "\"test2\""};
158        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
159        int[] freqs = {2470, 2437};
160        String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"};
161        int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10};
162        int[] securities = {SECURITY_PSK, SECURITY_PSK};
163
164        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
165                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
166                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
167        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
168        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
169
170        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
171                null, null, true, false, null);
172
173        ScanResult chosenScanResult = scanDetails.get(1).getScanResult();
174        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate);
175        WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager,
176                chosenScanResult, candidate);
177    }
178
179    /**
180     * Between two 5G networks, choose the one with stronger RSSI value if other conditions
181     * are the same and the RSSI values are not satuarted.
182     */
183    @Test
184    public void chooseStrongerRssi5GNetwork() {
185        String[] ssids = {"\"test1\"", "\"test2\""};
186        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
187        int[] freqs = {5200, 5240};
188        String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"};
189        int[] levels = {mThresholdQualifiedRssi5G + 8, mThresholdQualifiedRssi5G + 10};
190        int[] securities = {SECURITY_PSK, SECURITY_PSK};
191
192        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
193                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
194                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
195        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
196        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
197
198        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
199                null, null, true, false, null);
200
201        ScanResult chosenScanResult = scanDetails.get(1).getScanResult();
202        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate);
203        WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager,
204                chosenScanResult, candidate);
205    }
206
207    /**
208     * Choose secure network over open network if other conditions are the same.
209     */
210    @Test
211    public void chooseSecureNetworkOverOpenNetwork() {
212        String[] ssids = {"\"test1\"", "\"test2\""};
213        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
214        int[] freqs = {5200, 5240};
215        String[] caps = {"[ESS]", "[WPA2-EAP-CCMP][ESS]"};
216        int[] levels = {mThresholdQualifiedRssi5G, mThresholdQualifiedRssi5G};
217        int[] securities = {SECURITY_NONE, SECURITY_PSK};
218
219        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
220                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
221                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
222        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
223        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
224
225        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
226                null, null, true, false, null);
227
228        ScanResult chosenScanResult = scanDetails.get(1).getScanResult();
229        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate);
230        WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager,
231                chosenScanResult, candidate);
232    }
233
234    /**
235     * Choose 5G network over 2G network if other conditions are the same.
236     */
237    @Test
238    public void choose5GNetworkOver2GNetwork() {
239        String[] ssids = {"\"test1\"", "\"test2\""};
240        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
241        int[] freqs = {2437, 5240};
242        String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"};
243        int[] levels = {mThresholdQualifiedRssi2G, mThresholdQualifiedRssi5G};
244        int[] securities = {SECURITY_PSK, SECURITY_PSK};
245
246        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
247                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
248                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
249        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
250        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
251
252        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
253                null, null, true, false, null);
254
255        ScanResult chosenScanResult = scanDetails.get(1).getScanResult();
256        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate);
257        WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager,
258                chosenScanResult, candidate);
259    }
260
261    /**
262     * Verify that we stick to the currently connected network if the other one is
263     * just slightly better scored.
264     */
265    @Test
266    public void stickToCurrentNetwork() {
267        String[] ssids = {"\"test1\"", "\"test2\""};
268        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
269        int[] freqs = {5200, 5240};
270        String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"};
271        // test2 has slightly stronger RSSI value than test1
272        int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 4};
273        int[] securities = {SECURITY_PSK, SECURITY_PSK};
274
275        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
276                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
277                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
278        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
279        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
280
281        // Simuluate we are connected to SSID test1 already.
282        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
283                savedConfigs[0], null, true, false, null);
284
285        // Even though test2 has higher RSSI value, test1 is chosen because of the
286        // currently connected network bonus.
287        ScanResult chosenScanResult = scanDetails.get(0).getScanResult();
288        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate);
289        WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager,
290                chosenScanResult, candidate);
291    }
292
293    /**
294     * Verify that we stick to the currently connected BSSID if the other one is
295     * just slightly better scored.
296     */
297    @Test
298    public void stickToCurrentBSSID() {
299        // Same SSID
300        String[] ssids = {"\"test1\"", "\"test1\""};
301        String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"};
302        int[] freqs = {5200, 5240};
303        String[] caps = {"[WPA2-EAP-CCMP][ESS]", "[WPA2-EAP-CCMP][ESS]"};
304        // test2 has slightly stronger RSSI value than test1
305        int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 6};
306        int[] securities = {SECURITY_PSK, SECURITY_PSK};
307
308        ScanDetailsAndWifiConfigs scanDetailsAndConfigs =
309                WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids,
310                    freqs, caps, levels, securities, mWifiConfigManager, mClock);
311        List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails();
312        WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs();
313
314        // Simuluate we are connected to BSSID "6c:f3:7f:ae:8c:f3" already
315        WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails,
316                null, bssids[0], true, false, null);
317
318        // Even though test2 has higher RSSI value, test1 is chosen because of the
319        // currently connected BSSID bonus.
320        ScanResult chosenScanResult = scanDetails.get(0).getScanResult();
321        WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate);
322    }
323}
324