summaryrefslogtreecommitdiff
path: root/tests/src/com/android/networkrecommendation/DefaultNetworkRecommendationProviderTest.java
blob: dc6c67345bb5258c73fda14211eb9db1cfdf7bdd (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (C) 2016 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;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.net.NetworkKey;
import android.net.NetworkRecommendationProvider;
import android.net.NetworkScoreManager;
import android.net.RecommendationRequest;
import android.net.RecommendationResult;
import android.net.RssiCurve;
import android.net.ScoredNetwork;
import android.net.WifiKey;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiSsid;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Tests the recommendation provider directly, to test internals of the provider rather than the
 * service's API.
 */
@RunWith(AndroidJUnit4.class)
public class DefaultNetworkRecommendationProviderTest {

    private static final String GOOD_METERED_NETWORK_STRING_UNQUOTED = "Metered";
    private static final String GOOD_METERED_NETWORK_STRING = "\"Metered\",aa:bb:cc:dd:ee:ff" +
            "|10,-128,-128,-128,-128,-128,-128,-128,-128,20,20,20,20,-128|1|0|4K";
    private static final RssiCurve GOOD_METERED_NETWORK_CURVE = new RssiCurve(
            DefaultNetworkRecommendationProvider.CONSTANT_CURVE_START, 10 /* bucketWidth */,
            new byte[]{-128, -128, -128, -128, -128, -128, -128, -128, 20, 20, 20, 20, -128},
            0 /* defaultActiveNetworkBoost */);
    private static final ScoredNetwork GOOD_METERED_NETWORK = new ScoredNetwork(
            new NetworkKey(new WifiKey("\"Metered\"", "aa:bb:cc:dd:ee:ff")),
            GOOD_METERED_NETWORK_CURVE, true /* meteredHint */, new Bundle());

    private static final String GOOD_CAPTIVE_NETWORK_STRING_UNQUOTED = "Captive";
    private static final String GOOD_CAPTIVE_NETWORK_STRING =
            "\"Captive\",ff:ee:dd:cc:bb:aa"
                    + "|18,-128,-128,-128,-128,-128,-128,21,21,21,-128|0|1|HD";
    private static final RssiCurve GOOD_CAPTIVE_NETWORK_CURVE = new RssiCurve(
            DefaultNetworkRecommendationProvider.CONSTANT_CURVE_START, 18 /* bucketWidth */,
            new byte[]{-128, -128, -128, -128, -128, -128, 21, 21, 21, -128},
            0 /* defaultActiveNetworkBoost */);
    private static final ScoredNetwork GOOD_CAPTIVE_NETWORK;
    static {
        Bundle attributes = new Bundle();
        attributes.putBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL, true);
        GOOD_CAPTIVE_NETWORK = new ScoredNetwork(
                new NetworkKey(new WifiKey("\"Captive\"", "ff:ee:dd:cc:bb:aa")),
                GOOD_CAPTIVE_NETWORK_CURVE, false /* meteredHint */, attributes);
    }

    private static final String ANY_NETWORK_STRING_UNQUOTED = "AnySsid";
    private static final String ANY_NETWORK_STRING =
            "\"AnySsid\",00:00:00:00:00:00"
                    + "|18,-128,-128,-128,-128,-128,-128,22,22,22,-128|0|0|NONE";
    private static final RssiCurve ANY_NETWORK_CURVE = new RssiCurve(
            DefaultNetworkRecommendationProvider.CONSTANT_CURVE_START, 18 /* bucketWidth */,
            new byte[]{-128, -128, -128, -128, -128, -128, 22, 22, 22, -128},
            0 /* defaultActiveNetworkBoost */);
    private static final ScoredNetwork ANY_NETWORK = new ScoredNetwork(
            new NetworkKey(new WifiKey("\"AnySsid\"", "ee:ee:ee:ee:ee:ee")),
            ANY_NETWORK_CURVE, false /* meteredHint */, new Bundle());

    private static final String ANY_NETWORK_SPECIFIC_STRING_UNQUOTED = "AnySsid";
    private static final String ANY_NETWORK_SPECIFIC_STRING =
            "\"AnySsid\",ee:ee:ee:ee:ee:ee"
                    + "|18,-128,-128,-128,-128,-128,-128,23,23,23,-128|0|0|NONE";
    private static final RssiCurve ANY_NETWORK_SPECIFIC_CURVE = new RssiCurve(
            DefaultNetworkRecommendationProvider.CONSTANT_CURVE_START, 18 /* bucketWidth */,
            new byte[]{-128, -128, -128, -128, -128, -128, 23, 23, 23, -128},
            0 /* defaultActiveNetworkBoost */);
    private static final ScoredNetwork ANY_NETWORK_SPECIFIC = new ScoredNetwork(
            new NetworkKey(new WifiKey("\"AnySsid\"", "ee:ee:ee:ee:ee:ee")),
            ANY_NETWORK_SPECIFIC_CURVE, false /* meteredHint */, new Bundle());

    private static final String BAD_NETWORK_STRING_UNQUOTED = "Bad";
    private static final String BAD_NETWORK_STRING =
            "\"Bad\",aa:bb:cc:dd:ee:ff"
                    + "|10,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128,-128"
                    + "|1|0|SD";
    private static final RssiCurve BAD_NETWORK_CURVE =
            new RssiCurve(
                    DefaultNetworkRecommendationProvider.CONSTANT_CURVE_START,
                    10 /* bucketWidth */,
                    new byte[] {-128, -128, -128, -128, -128, -128,
                            -128, -128, -128, -128, -128, -128, -128},
                    0 /* defaultActiveNetworkBoost */);
    private static final ScoredNetwork BAD_NETWORK =
            new ScoredNetwork(
                    new NetworkKey(new WifiKey("\"Bad\"", "aa:bb:cc:dd:ee:ff")),
                    BAD_NETWORK_CURVE,
                    true /* meteredHint */,
                    new Bundle());

    @Mock
    private NetworkRecommendationProvider.ResultCallback mCallback;

    @Mock
    private NetworkScoreManager mNetworkScoreManager;

    @Mock
    private Context mContext;

    private DefaultNetworkRecommendationProvider.ScoreStorage mStorage;
    private DefaultNetworkRecommendationProvider mProvider;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mStorage = new DefaultNetworkRecommendationProvider.ScoreStorage();
        final Handler handler = new Handler(Looper.getMainLooper());
        mProvider = new DefaultNetworkRecommendationProvider(mContext,
                handler::post, mNetworkScoreManager, mStorage);
    }

    @Test
    public void basicRecommendation() throws Exception {

        ScanResult[] scanResults = new ScanResult[6];
        for (int i = 0; i < 3; i++) {
            scanResults[i] = TestUtil.createMockScanResult(i);
        }

        // For now we add directly to storage, but when we start calling
        // networkScoreManager.updateScores, we'll have to adjust this test.
        mProvider.addScoreForTest(GOOD_METERED_NETWORK);
        {
            ScanResult scanResult = new ScanResult();
            scanResult.level = 115;
            scanResult.SSID = GOOD_METERED_NETWORK_STRING_UNQUOTED;
            scanResult.wifiSsid = WifiSsid.createFromAsciiEncoded(
                    GOOD_METERED_NETWORK_STRING_UNQUOTED);
            scanResult.BSSID = GOOD_METERED_NETWORK.networkKey.wifiKey.bssid;
            scanResult.capabilities = "[ESS]";
            scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
            scanResults[3] = scanResult;
        }

        for (int i = 4; i < 6; i++) {
            scanResults[i] = TestUtil.createMockScanResult(i);
        }

        RecommendationRequest request = new RecommendationRequest.Builder()
                .setScanResults(scanResults)
                .build();

        RecommendationResult result = verifyAndCaptureResult(request);
        assertEquals(GOOD_METERED_NETWORK.networkKey.wifiKey.ssid,
                result.getWifiConfiguration().SSID);
    }

    @Test
    public void recommendation_noScans_returnsCurrentConfig() throws Exception {
        ScanResult[] scanResults = new ScanResult[0];

        WifiConfiguration expectedConfig = new WifiConfiguration();
        expectedConfig.SSID = "ssid";
        expectedConfig.BSSID = "bssid";
        RecommendationRequest request = new RecommendationRequest.Builder()
                .setScanResults(scanResults)
                .setDefaultWifiConfig(expectedConfig)
                .build();

        RecommendationResult result = verifyAndCaptureResult(request);
        assertEquals(expectedConfig, result.getWifiConfiguration());
    }

    @Test
    public void recommendation_noScans_noCurrentConfig_returnsEmpty() throws Exception {
        ScanResult[] scanResults = new ScanResult[0];

        RecommendationRequest request = new RecommendationRequest.Builder()
                .setScanResults(scanResults)
                .build();

        RecommendationResult result = verifyAndCaptureResult(request);
        assertNull(result.getWifiConfiguration());
    }

    @Test
    public void scoreNetworks() throws Exception {
        NetworkKey[] keys =
                new NetworkKey[]{GOOD_METERED_NETWORK.networkKey, GOOD_CAPTIVE_NETWORK.networkKey};
        mProvider.onRequestScores(keys);

        verify(mNetworkScoreManager).updateScores(Mockito.any());
    }

    @Test
    public void scoreNetworks_empty() throws Exception {
        NetworkKey[] keys = new NetworkKey[]{};
        mProvider.onRequestScores(keys);

        verify(mNetworkScoreManager, times(0)).updateScores(Mockito.any());
    }

    @Test
    public void dumpAddScores_goodMetered() {
        String[] args = {"netrec", "addScore", GOOD_METERED_NETWORK_STRING};
        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()), args);

        ScoredNetwork[] scoredNetworks = verifyAndCaptureScoredNetworks();
        assertEquals(1, scoredNetworks.length);
        ScoredNetwork score = scoredNetworks[0];

        assertEquals(GOOD_METERED_NETWORK.networkKey.wifiKey.ssid, score.networkKey.wifiKey.ssid);
        assertEquals(GOOD_METERED_NETWORK.networkKey.wifiKey.bssid, score.networkKey.wifiKey.bssid);

        assertEquals(GOOD_METERED_NETWORK.meteredHint, score.meteredHint);
        assertEquals(
                GOOD_METERED_NETWORK.attributes.getBoolean(
                        ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL),
                score.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL));

        assertEquals("Network curve does not match", GOOD_METERED_NETWORK_CURVE, score.rssiCurve);
        assertEquals(
                "Badge curve does not match",
                DefaultNetworkRecommendationProvider.BADGE_CURVE_4K,
                (RssiCurve) score.attributes.getParcelable(
                        ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE));
    }

    @Test
    public void dumpAddScores_bad() {
        String[] args = {"netrec", "addScore", BAD_NETWORK_STRING};
        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()), args);

        ScoredNetwork[] scoredNetworks = verifyAndCaptureScoredNetworks();
        assertEquals(1, scoredNetworks.length);
        ScoredNetwork score = scoredNetworks[0];

        assertEquals(BAD_NETWORK.networkKey.wifiKey.ssid, score.networkKey.wifiKey.ssid);
        assertEquals(BAD_NETWORK.networkKey.wifiKey.bssid, score.networkKey.wifiKey.bssid);

        assertEquals(BAD_NETWORK.meteredHint, score.meteredHint);
        assertEquals(
                BAD_NETWORK.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL),
                score.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL));

        assertEquals("Network curve does not match", BAD_NETWORK_CURVE, score.rssiCurve);
        assertEquals(
                "Badge curve does not match",
                DefaultNetworkRecommendationProvider.BADGE_CURVE_SD,
                (RssiCurve) score.attributes.getParcelable(
                        ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE));
    }

    @Test
    public void dumpAddScores_goodCaptivePortal() {
        String[] args = {"addScore", GOOD_CAPTIVE_NETWORK_STRING};
        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()), args);

        ScoredNetwork[] scoredNetworks = verifyAndCaptureScoredNetworks();
        assertEquals(1, scoredNetworks.length);
        ScoredNetwork score = scoredNetworks[0];

        assertEquals(GOOD_CAPTIVE_NETWORK.networkKey.wifiKey.ssid, score.networkKey.wifiKey.ssid);
        assertEquals(GOOD_CAPTIVE_NETWORK.networkKey.wifiKey.bssid, score.networkKey.wifiKey.bssid);

        assertEquals(GOOD_CAPTIVE_NETWORK.meteredHint, score.meteredHint);

        assertEquals(
                GOOD_CAPTIVE_NETWORK.attributes.getBoolean(
                        ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL),
                score.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL));
        assertEquals("Network curve does not match.", GOOD_CAPTIVE_NETWORK_CURVE, score.rssiCurve);
        assertEquals(
                "Badge curve does not match",
                DefaultNetworkRecommendationProvider.BADGE_CURVE_HD,
                (RssiCurve) score.attributes.getParcelable(
                        ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE));
    }

    @Test
    public void dumpAddScores_anySsid() {
        String[] args = {"addScore", ANY_NETWORK_STRING};
        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()), args);

        // We don't update the platform with the any bssid score, but we do store it.
        verify(mNetworkScoreManager, times(0)).updateScores(Mockito.any());

        // We do store and serve the score, though:
        ScoredNetwork score = mStorage.get(ANY_NETWORK.networkKey);
        assertNotNull(score);

        assertEquals(ANY_NETWORK.networkKey, score.networkKey);
        assertEquals(ANY_NETWORK.meteredHint, score.meteredHint);
        assertEquals(
                ANY_NETWORK.attributes.getBoolean(
                    ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL),
                score.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL));
        assertEquals("Network curve does not match", ANY_NETWORK_CURVE, score.rssiCurve);
        assertNull(
                "Badge curve should not be set.",
                (RssiCurve) score.attributes.getParcelable(
                        ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE));
    }

    @Test
    public void dumpAddScores_anySsid_useMoreSpecific() {
        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()),
                new String[] {"addScore", ANY_NETWORK_STRING});
        verify(mNetworkScoreManager, times(0)).updateScores(Mockito.any());

        mProvider.dump(null /* fd */, new PrintWriter(new StringWriter()),
                new String[] {"addScore", ANY_NETWORK_SPECIFIC_STRING});
        verify(mNetworkScoreManager).updateScores(Mockito.any());

        // We don't update the platform with the any bssid score, but we do store it.
        ScoredNetwork score = mStorage.get(ANY_NETWORK.networkKey);
        assertNotNull(score);

        assertEquals(ANY_NETWORK_SPECIFIC.networkKey, score.networkKey);
        assertEquals(ANY_NETWORK_SPECIFIC.meteredHint, score.meteredHint);
        assertEquals(
                ANY_NETWORK_SPECIFIC.attributes.getBoolean(
                    ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL),
                score.attributes.getBoolean(ScoredNetwork.ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL));
        assertEquals("Network curve does not match", ANY_NETWORK_SPECIFIC_CURVE, score.rssiCurve);
        assertNull(
                "Badge curve should not be set.",
                (RssiCurve) score.attributes.getParcelable(
                        ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE));

    }

    private RecommendationResult verifyAndCaptureResult(
            RecommendationRequest request) {
        mProvider.onRequestRecommendation(request, mCallback);

        ArgumentCaptor<RecommendationResult> resultCaptor =
                ArgumentCaptor.forClass(RecommendationResult.class);
        verify(mCallback).onResult(resultCaptor.capture());

        return resultCaptor.getValue();
    }

    private ScoredNetwork[] verifyAndCaptureScoredNetworks() {
        ArgumentCaptor<ScoredNetwork[]> resultCaptor = ArgumentCaptor.forClass(
                ScoredNetwork[].class);
        verify(mNetworkScoreManager).updateScores(resultCaptor.capture());
        return resultCaptor.getValue();
    }
}