summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/LinkProbeManagerTest.java
blob: 32eb78d8cd6ef280cbf06284ecd5af39d92acb13 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (C) 2019 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.server.wifi;

import static junit.framework.Assert.assertEquals;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.database.ContentObserver;
import android.net.wifi.WifiInfo;
import android.os.test.TestLooper;
import android.provider.Settings;

import androidx.test.filters.SmallTest;

import com.android.internal.R;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashSet;

/**
 * Unit tests for LinkProbeManager
 */
@SmallTest
public class LinkProbeManagerTest {

    private static final String TEST_IFACE_NAME = "testIfaceName";
    private static final String TEST_BSSID = "6c:f3:7f:ae:8c:f3";
    private static final int TEST_ELAPSED_TIME_MS = 100;

    private LinkProbeManager mLinkProbeManager;

    private WifiInfo mWifiInfo;
    private long mTimeMs;
    @Mock private Clock mClock;
    @Mock private WifiNative mWifiNative;
    @Mock private WifiMetrics mWifiMetrics;
    @Mock private FrameworkFacade mFrameworkFacade;
    @Mock private Context mContext;

    private TestLooper mLooper = new TestLooper();
    private ContentObserver mContentObserver;
    private MockResources mResources;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        mResources = new MockResources();
        mResources.setBoolean(R.bool.config_wifi_link_probing_supported, true);
        when(mContext.getResources()).thenReturn(mResources);

        initLinkProbeManager();

        mWifiInfo = new WifiInfo();
        mWifiInfo.setBSSID(TEST_BSSID);
        mTimeMs = 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);

        ArgumentCaptor<ContentObserver> observerCaptor = ArgumentCaptor.forClass(
                ContentObserver.class);
        verify(mFrameworkFacade).registerContentObserver(eq(mContext),
                eq(Settings.Global.getUriFor(Settings.Global.WIFI_LINK_PROBING_ENABLED)),
                eq(false), observerCaptor.capture());
        mContentObserver = observerCaptor.getValue();
        when(mFrameworkFacade.getIntegerSetting(eq(mContext),
                eq(Settings.Global.WIFI_LINK_PROBING_ENABLED), anyInt())).thenReturn(1);
        mContentObserver.onChange(false);
    }

    private void initLinkProbeManager() {
        mLinkProbeManager = new LinkProbeManager(mClock, mWifiNative, mWifiMetrics,
                mFrameworkFacade, mLooper.getLooper(), mContext);
    }

    /**
     * Tests that link probing is correctly triggered when the required conditions are met, and an
     * ACK was received from the AP.
     */
    @Test
    public void testLinkProbeTriggeredAndAcked() throws Exception {
        // initialize tx success counter
        mWifiInfo.txSuccess = 50;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());
        verify(mWifiMetrics, never()).incrementLinkProbeExperimentProbeCount(any());

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 50;
        // below RSSI threshold
        int rssi = LinkProbeManager.RSSI_THRESHOLD - 5;
        mWifiInfo.setRssi(rssi);
        // above link speed threshold
        int linkSpeed = LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10;
        mWifiInfo.setLinkSpeed(linkSpeed);
        // more than DELAY_AFTER_TX_SUCCESS_MS passed
        long timeDelta = LinkProbeManager.DELAY_AFTER_TX_SUCCESS_MS + 20000;
        mTimeMs += timeDelta;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        ArgumentCaptor<WifiNative.SendMgmtFrameCallback> callbackCaptor =
                ArgumentCaptor.forClass(WifiNative.SendMgmtFrameCallback.class);
        verify(mWifiNative).probeLink(eq(TEST_IFACE_NAME), any(), callbackCaptor.capture(),
                anyInt());
        ArgumentCaptor<String> experimentIdCaptor = ArgumentCaptor.forClass(String.class);
        verify(mWifiMetrics, atLeastOnce()).incrementLinkProbeExperimentProbeCount(
                experimentIdCaptor.capture());
        int numExperimentIds = LinkProbeManager.EXPERIMENT_DELAYS_MS.length
                * LinkProbeManager.EXPERIMENT_RSSIS.length
                * LinkProbeManager.EXPERIMENT_LINK_SPEEDS.length;
        assertEquals(numExperimentIds, new HashSet<>(experimentIdCaptor.getAllValues()).size());

        callbackCaptor.getValue().onAck(TEST_ELAPSED_TIME_MS);
        verify(mWifiMetrics).logLinkProbeSuccess(timeDelta, rssi, linkSpeed, TEST_ELAPSED_TIME_MS);
    }

    /**
     * Tests that link probing is correctly triggered when the required conditions are met, but
     * no ACK was received from the AP.
     */
    @Test
    public void testLinkProbeTriggeredAndFailed() throws Exception {
        // initialize tx success counter
        mWifiInfo.txSuccess = 50;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 50;
        // above RSSI threshold
        int rssi = LinkProbeManager.RSSI_THRESHOLD + 5;
        mWifiInfo.setRssi(rssi);
        // below link speed threshold
        int linkSpeed = LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS - 2;
        mWifiInfo.setLinkSpeed(linkSpeed);
        // more than DELAY_BETWEEN_PROBES_MS passed
        long timeDelta = LinkProbeManager.DELAY_BETWEEN_PROBES_MS + 1000;
        mTimeMs += timeDelta;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        ArgumentCaptor<WifiNative.SendMgmtFrameCallback> callbackCaptor =
                ArgumentCaptor.forClass(WifiNative.SendMgmtFrameCallback.class);
        verify(mWifiNative).probeLink(eq(TEST_IFACE_NAME), any(), callbackCaptor.capture(),
                anyInt());

        callbackCaptor.getValue().onFailure(WifiNative.SEND_MGMT_FRAME_ERROR_NO_ACK);
        verify(mWifiMetrics).logLinkProbeFailure(timeDelta, rssi, linkSpeed,
                WifiNative.SEND_MGMT_FRAME_ERROR_NO_ACK);
    }

    /**
     * Tests that link probing is not triggered more than once every DELAY_BETWEEN_PROBES_MS
     */
    @Test
    public void testLinkProbeNotTriggeredTooFrequently() throws Exception {
        testLinkProbeTriggeredAndAcked();

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 50;
        // below RSSI threshold
        mWifiInfo.setRssi(LinkProbeManager.RSSI_THRESHOLD - 5);
        // above link speed threshold
        mWifiInfo.setLinkSpeed(LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10);
        // *** but less than DELAY_BETWEEN_PROBES_MS has passed since last probe ***
        mTimeMs += LinkProbeManager.DELAY_BETWEEN_PROBES_MS - 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe
        verifyNoMoreInteractions(mWifiNative);
    }

    /**
     * Tests that link probing is not triggered when Tx has succeeded within the last
     * DELAY_AFTER_TX_SUCCESS_MS.
     */
    @Test
    public void testLinkProbeNotTriggeredWhenTxSucceeded() throws Exception {
        // initialize tx success counter
        mWifiInfo.txSuccess = 50;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx succeeded
        mWifiInfo.txSuccess = 55;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 55;
        // below RSSI threshold
        mWifiInfo.setRssi(LinkProbeManager.RSSI_THRESHOLD - 5);
        // above link speed threshold
        mWifiInfo.setLinkSpeed(LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10);
        // *** but less than DELAY_AFTER_TX_SUCCESS_MS has passed since last tx success ***
        mTimeMs += LinkProbeManager.DELAY_AFTER_TX_SUCCESS_MS - 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());
    }

    /**
     * Tests that link probing is not triggered when screen was turned on within the last
     * {@link LinkProbeManager#SCREEN_ON_DELAY_MS}.
     */
    @Test
    public void testLinkProbeNotTriggeredWhenScreenJustTurnedOn() throws Exception {
        mTimeMs += 30 * 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.resetOnScreenTurnedOn();
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since initialization
        mWifiInfo.txSuccess = 0;
        // below RSSI threshold
        mWifiInfo.setRssi(LinkProbeManager.RSSI_THRESHOLD - 5);
        // above link speed threshold
        mWifiInfo.setLinkSpeed(LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10);
        // *** but less than SCREEN_ON_DELAY_MS has passed since last screen on ***
        mTimeMs += LinkProbeManager.SCREEN_ON_DELAY_MS - 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());
    }

    /**
     * Tests that link probing is triggered when screen was turned on more than
     * {@link LinkProbeManager#SCREEN_ON_DELAY_MS} ago.
     */
    @Test
    public void testLinkProbeTriggeredAfterScreenTurnedOn() throws Exception {
        mTimeMs += 30 * 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.resetOnScreenTurnedOn();
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since initialization
        mWifiInfo.txSuccess = 0;
        // below RSSI threshold
        mWifiInfo.setRssi(LinkProbeManager.RSSI_THRESHOLD - 5);
        // above link speed threshold
        mWifiInfo.setLinkSpeed(LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10);
        // *** more than SCREEN_ON_DELAY_MS has passed since last screen on ***
        mTimeMs += LinkProbeManager.SCREEN_ON_DELAY_MS + 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        verify(mWifiNative).probeLink(eq(TEST_IFACE_NAME), any(), any(), anyInt());
    }

    /**
     * Tests when link probing feature flag is disabled, no probes should run.
     */
    @Test
    public void testLinkProbeFeatureDisabled() throws Exception {
        when(mFrameworkFacade.getIntegerSetting(eq(mContext),
                eq(Settings.Global.WIFI_LINK_PROBING_ENABLED), anyInt())).thenReturn(0);
        mContentObserver.onChange(false);

        // initialize tx success counter
        mWifiInfo.txSuccess = 50;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 50;
        // below RSSI threshold
        int rssi = LinkProbeManager.RSSI_THRESHOLD - 5;
        mWifiInfo.setRssi(rssi);
        // above link speed threshold
        int linkSpeed = LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10;
        mWifiInfo.setLinkSpeed(linkSpeed);
        // more than DELAY_AFTER_TX_SUCCESS_MS passed
        long timeDelta = LinkProbeManager.DELAY_AFTER_TX_SUCCESS_MS + 1000;
        mTimeMs += timeDelta;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        verify(mWifiNative, never()).probeLink(any() , any(), any(), anyInt());
    }

    /**
     * Tests when link probing feature is not supported by the device, no probes should run.
     */
    @Test
    public void testLinkProbeFeatureUnsupported() throws Exception {
        mResources.setBoolean(R.bool.config_wifi_link_probing_supported, false);
        initLinkProbeManager();

        // initialize tx success counter
        mWifiInfo.txSuccess = 50;
        mTimeMs += 3000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since last update
        mWifiInfo.txSuccess = 50;
        // below RSSI threshold
        int rssi = LinkProbeManager.RSSI_THRESHOLD - 5;
        mWifiInfo.setRssi(rssi);
        // above link speed threshold
        int linkSpeed = LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10;
        mWifiInfo.setLinkSpeed(linkSpeed);
        // more than DELAY_AFTER_TX_SUCCESS_MS passed
        long timeDelta = LinkProbeManager.DELAY_AFTER_TX_SUCCESS_MS + 1000;
        mTimeMs += timeDelta;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        verify(mWifiNative, never()).probeLink(any() , any(), any(), anyInt());
    }

    /**
     * Tests exhausting the daily link probe quota and verify that no more link probes are made
     * after the limit is reached. Tests that the quota is reset upon entering a new day.
     */
    @Test
    public void testLinkProbeQuotaExceeded() {
        mTimeMs += 30 * 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
        // should not probe yet
        verify(mWifiNative, never()).probeLink(any(), any(), any(), anyInt());

        // tx success counter did not change since initialization
        mWifiInfo.txSuccess = 0;
        // below RSSI threshold
        mWifiInfo.setRssi(LinkProbeManager.RSSI_THRESHOLD - 5);
        // above link speed threshold
        mWifiInfo.setLinkSpeed(LinkProbeManager.LINK_SPEED_THRESHOLD_MBPS + 10);

        // exhaust quota
        for (int i = 1; i <= LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD; i++) {
            mTimeMs += LinkProbeManager.DELAY_BETWEEN_PROBES_MS + 1000;
            when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
            mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        }
        verify(mWifiNative, times((int) LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD))
                .probeLink(eq(TEST_IFACE_NAME), any(), any(), anyInt());
        // verify no more quota
        for (int i = 0; i < 10; i++) {
            mTimeMs += LinkProbeManager.DELAY_BETWEEN_PROBES_MS + 1000;
            when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
            mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        }
        verify(mWifiNative, times((int) LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD))
                .probeLink(eq(TEST_IFACE_NAME), any(), any(), anyInt());

        // start new period
        mTimeMs += LinkProbeManager.PERIOD_MILLIS + 1000;
        when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);

        // exhaust quota again
        for (int i = 1; i <= LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD; i++) {
            mTimeMs += LinkProbeManager.DELAY_BETWEEN_PROBES_MS + 1000;
            when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
            mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        }
        verify(mWifiNative, times((int) (2 * LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD)))
                .probeLink(eq(TEST_IFACE_NAME), any(), any(), anyInt());
        // verify no more quota again
        for (int i = 0; i < 10; i++) {
            mTimeMs += LinkProbeManager.DELAY_BETWEEN_PROBES_MS + 1000;
            when(mClock.getElapsedSinceBootMillis()).thenReturn(mTimeMs);
            mLinkProbeManager.updateConnectionStats(mWifiInfo, TEST_IFACE_NAME);
        }
        verify(mWifiNative, times((int) (2 * LinkProbeManager.MAX_PROBE_COUNT_IN_PERIOD)))
                .probeLink(eq(TEST_IFACE_NAME), any(), any(), anyInt());
    }
}