blob: aafa18a532fa290eec2b3c3a5de7bfaf79d1fbc6 [file] [log] [blame]
Hugo Benichia43a0952016-08-30 10:01:15 +09001/*
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.connectivity;
18
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090019import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
Hugo Benichia43a0952016-08-30 10:01:15 +090021import static org.mockito.Mockito.any;
22import static org.mockito.Mockito.anyBoolean;
23import static org.mockito.Mockito.anyInt;
24import static org.mockito.Mockito.eq;
25import static org.mockito.Mockito.never;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090026import static org.mockito.Mockito.reset;
Hugo Benichia43a0952016-08-30 10:01:15 +090027import static org.mockito.Mockito.times;
28import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090031import android.app.PendingIntent;
32import android.content.Context;
33import android.content.res.Resources;
34import android.net.ConnectivityManager;
Luke Huang65914772019-03-16 00:31:46 +080035import android.net.IDnsResolver;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090036import android.net.INetd;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090037import android.net.Network;
38import android.net.NetworkCapabilities;
39import android.net.NetworkInfo;
Lorenzo Colitti6654b082020-01-10 00:40:28 +090040import android.net.NetworkProvider;
Cody Kesting0d8d6ac2020-05-12 18:47:10 +000041import android.os.Binder;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090042import android.os.INetworkManagementService;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090043import android.text.format.DateUtils;
44
Brett Chabot1ae2aa62019-03-04 14:14:56 -080045import androidx.test.filters.SmallTest;
46import androidx.test.runner.AndroidJUnit4;
47
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090048import com.android.internal.R;
49import com.android.server.ConnectivityService;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090050import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
51
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090052import org.junit.Before;
53import org.junit.Test;
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +090054import org.junit.runner.RunWith;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090055import org.mockito.Mock;
56import org.mockito.MockitoAnnotations;
57
58@RunWith(AndroidJUnit4.class)
59@SmallTest
60public class LingerMonitorTest {
Hugo Benichia43a0952016-08-30 10:01:15 +090061 static final String CELLULAR = "CELLULAR";
62 static final String WIFI = "WIFI";
63
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090064 static final long LOW_RATE_LIMIT = DateUtils.MINUTE_IN_MILLIS;
65 static final long HIGH_RATE_LIMIT = 0;
66
67 static final int LOW_DAILY_LIMIT = 2;
68 static final int HIGH_DAILY_LIMIT = 1000;
69
Hugo Benichia43a0952016-08-30 10:01:15 +090070 LingerMonitor mMonitor;
71
72 @Mock ConnectivityService mConnService;
Luke Huang65914772019-03-16 00:31:46 +080073 @Mock IDnsResolver mDnsResolver;
Lorenzo Colitti9307ca22019-01-12 01:54:23 +090074 @Mock INetd mNetd;
75 @Mock INetworkManagementService mNMS;
Hugo Benichia43a0952016-08-30 10:01:15 +090076 @Mock Context mCtx;
Hugo Benichia43a0952016-08-30 10:01:15 +090077 @Mock NetworkNotificationManager mNotifier;
78 @Mock Resources mResources;
79
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090080 @Before
Hugo Benichia43a0952016-08-30 10:01:15 +090081 public void setUp() {
82 MockitoAnnotations.initMocks(this);
83 when(mCtx.getResources()).thenReturn(mResources);
84 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
Hugo Benichia43a0952016-08-30 10:01:15 +090085
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090086 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090087 }
88
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090089 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090090 public void testTransitions() {
91 setNotificationSwitch(transition(WIFI, CELLULAR));
92 NetworkAgentInfo nai1 = wifiNai(100);
93 NetworkAgentInfo nai2 = cellNai(101);
94
95 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
96 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
97 }
98
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090099 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900100 public void testNotificationOnLinger() {
101 setNotificationSwitch(transition(WIFI, CELLULAR));
102 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
103 NetworkAgentInfo from = wifiNai(100);
104 NetworkAgentInfo to = cellNai(101);
105
106 mMonitor.noteLingerDefaultNetwork(from, to);
107 verifyNotification(from, to);
108 }
109
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900110 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900111 public void testToastOnLinger() {
112 setNotificationSwitch(transition(WIFI, CELLULAR));
113 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
114 NetworkAgentInfo from = wifiNai(100);
115 NetworkAgentInfo to = cellNai(101);
116
117 mMonitor.noteLingerDefaultNetwork(from, to);
118 verifyToast(from, to);
119 }
120
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900121 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900122 public void testNotificationClearedAfterDisconnect() {
123 setNotificationSwitch(transition(WIFI, CELLULAR));
124 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
125 NetworkAgentInfo from = wifiNai(100);
126 NetworkAgentInfo to = cellNai(101);
127
128 mMonitor.noteLingerDefaultNetwork(from, to);
129 verifyNotification(from, to);
130
131 mMonitor.noteDisconnect(to);
132 verify(mNotifier, times(1)).clearNotification(100);
133 }
134
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900135 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900136 public void testNotificationClearedAfterSwitchingBack() {
137 setNotificationSwitch(transition(WIFI, CELLULAR));
138 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
139 NetworkAgentInfo from = wifiNai(100);
140 NetworkAgentInfo to = cellNai(101);
141
142 mMonitor.noteLingerDefaultNetwork(from, to);
143 verifyNotification(from, to);
144
145 mMonitor.noteLingerDefaultNetwork(to, from);
146 verify(mNotifier, times(1)).clearNotification(100);
147 }
148
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900149 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900150 public void testUniqueToast() {
151 setNotificationSwitch(transition(WIFI, CELLULAR));
152 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
153 NetworkAgentInfo from = wifiNai(100);
154 NetworkAgentInfo to = cellNai(101);
155
156 mMonitor.noteLingerDefaultNetwork(from, to);
157 verifyToast(from, to);
158
159 mMonitor.noteLingerDefaultNetwork(to, from);
160 verify(mNotifier, times(1)).clearNotification(100);
161
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900162 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900163 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900164 verifyNoNotifications();
165 }
166
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900167 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900168 public void testMultipleNotifications() {
169 setNotificationSwitch(transition(WIFI, CELLULAR));
170 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
171 NetworkAgentInfo wifi1 = wifiNai(100);
172 NetworkAgentInfo wifi2 = wifiNai(101);
173 NetworkAgentInfo cell = cellNai(102);
174
175 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
176 verifyNotification(wifi1, cell);
177
178 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
179 verify(mNotifier, times(1)).clearNotification(100);
180
181 reset(mNotifier);
182 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
183 verifyNotification(wifi2, cell);
184 }
185
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900186 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900187 public void testRateLimiting() throws InterruptedException {
188 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
189
190 setNotificationSwitch(transition(WIFI, CELLULAR));
191 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
192 NetworkAgentInfo wifi1 = wifiNai(100);
193 NetworkAgentInfo wifi2 = wifiNai(101);
194 NetworkAgentInfo wifi3 = wifiNai(102);
195 NetworkAgentInfo cell = cellNai(103);
196
197 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
198 verifyNotification(wifi1, cell);
199 reset(mNotifier);
200
201 Thread.sleep(50);
202 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
203 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
204 verifyNoNotifications();
205
206 Thread.sleep(50);
207 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
208 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
209 verifyNoNotifications();
210 }
211
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900212 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900213 public void testDailyLimiting() throws InterruptedException {
214 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
215
216 setNotificationSwitch(transition(WIFI, CELLULAR));
217 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
218 NetworkAgentInfo wifi1 = wifiNai(100);
219 NetworkAgentInfo wifi2 = wifiNai(101);
220 NetworkAgentInfo wifi3 = wifiNai(102);
221 NetworkAgentInfo cell = cellNai(103);
222
223 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
224 verifyNotification(wifi1, cell);
225 reset(mNotifier);
226
227 Thread.sleep(50);
228 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
229 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
230 verifyNotification(wifi2, cell);
231 reset(mNotifier);
232
233 Thread.sleep(50);
234 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
235 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
236 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900237 }
238
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900239 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900240 public void testUniqueNotification() {
241 setNotificationSwitch(transition(WIFI, CELLULAR));
242 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
243 NetworkAgentInfo from = wifiNai(100);
244 NetworkAgentInfo to = cellNai(101);
245
246 mMonitor.noteLingerDefaultNetwork(from, to);
247 verifyNotification(from, to);
248
249 mMonitor.noteLingerDefaultNetwork(to, from);
250 verify(mNotifier, times(1)).clearNotification(100);
251
252 mMonitor.noteLingerDefaultNetwork(from, to);
253 verifyNotification(from, to);
254 }
255
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900256 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900257 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900258 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
259 setNotificationSwitch(transition(WIFI, CELLULAR));
260 NetworkAgentInfo from = wifiNai(100);
261 NetworkAgentInfo to = cellNai(101);
262 from.everValidated = false;
263
264 mMonitor.noteLingerDefaultNetwork(from, to);
265 verifyNoNotifications();
266 }
267
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900268 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900269 public void testIgnoreCurrentlyValidatedNetworks() {
270 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
271 setNotificationSwitch(transition(WIFI, CELLULAR));
272 NetworkAgentInfo from = wifiNai(100);
273 NetworkAgentInfo to = cellNai(101);
274 from.lastValidated = true;
275
276 mMonitor.noteLingerDefaultNetwork(from, to);
277 verifyNoNotifications();
278 }
279
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900280 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900281 public void testNoNotificationType() {
282 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
283 setNotificationSwitch();
284 NetworkAgentInfo from = wifiNai(100);
285 NetworkAgentInfo to = cellNai(101);
286
287 mMonitor.noteLingerDefaultNetwork(from, to);
288 verifyNoNotifications();
289 }
290
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900291 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900292 public void testNoTransitionToNotify() {
293 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
294 setNotificationSwitch(transition(WIFI, CELLULAR));
295 NetworkAgentInfo from = wifiNai(100);
296 NetworkAgentInfo to = cellNai(101);
297
298 mMonitor.noteLingerDefaultNetwork(from, to);
299 verifyNoNotifications();
300 }
301
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900302 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900303 public void testDifferentTransitionToNotify() {
304 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
305 setNotificationSwitch(transition(CELLULAR, WIFI));
306 NetworkAgentInfo from = wifiNai(100);
307 NetworkAgentInfo to = cellNai(101);
308
309 mMonitor.noteLingerDefaultNetwork(from, to);
310 verifyNoNotifications();
311 }
312
313 void setNotificationSwitch(String... transitions) {
314 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
315 .thenReturn(transitions);
316 }
317
318 String transition(String from, String to) {
319 return from + "-" + to;
320 }
321
322 void setNotificationType(int type) {
323 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
324 }
325
326 void verifyNoToast() {
327 verify(mNotifier, never()).showToast(any(), any());
328 }
329
330 void verifyNoNotification() {
331 verify(mNotifier, never())
332 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
333 }
334
335 void verifyNoNotifications() {
336 verifyNoToast();
337 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900338 }
339
340 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
341 verifyNoNotification();
342 verify(mNotifier, times(1)).showToast(from, to);
343 }
344
345 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
346 verifyNoToast();
347 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
348 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
349 }
350
351 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
352 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
353 NetworkCapabilities caps = new NetworkCapabilities();
354 caps.addCapability(0);
355 caps.addTransportType(transport);
356 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
Automerger Merge Worker3d40f5782020-03-08 06:07:47 +0000357 caps, 50, mCtx, null, null /* config */, mConnService, mNetd, mDnsResolver, mNMS,
Cody Kesting0d8d6ac2020-05-12 18:47:10 +0000358 NetworkProvider.ID_NONE, Binder.getCallingUid());
Hugo Benichia43a0952016-08-30 10:01:15 +0900359 nai.everValidated = true;
360 return nai;
361 }
362
363 NetworkAgentInfo wifiNai(int netId) {
364 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
365 ConnectivityManager.TYPE_WIFI, WIFI);
366 }
367
368 NetworkAgentInfo cellNai(int netId) {
369 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
370 ConnectivityManager.TYPE_MOBILE, CELLULAR);
371 }
372
373 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900374 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
375 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900376 }
377 @Override protected PendingIntent createNotificationIntent() {
378 return null;
379 }
380 }
381}