blob: 142769f61335c1d9d9c05d2c4b9eba096ab37ad5 [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;
Chalard Jean05ab6812018-05-02 21:14:54 +090039import android.net.NetworkFactory;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090040import android.net.NetworkInfo;
41import android.net.NetworkMisc;
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;
77 @Mock NetworkMisc mMisc;
78 @Mock NetworkNotificationManager mNotifier;
79 @Mock Resources mResources;
80
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090081 @Before
Hugo Benichia43a0952016-08-30 10:01:15 +090082 public void setUp() {
83 MockitoAnnotations.initMocks(this);
84 when(mCtx.getResources()).thenReturn(mResources);
85 when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
Hugo Benichia43a0952016-08-30 10:01:15 +090086
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090087 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
Hugo Benichia43a0952016-08-30 10:01:15 +090088 }
89
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090090 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +090091 public void testTransitions() {
92 setNotificationSwitch(transition(WIFI, CELLULAR));
93 NetworkAgentInfo nai1 = wifiNai(100);
94 NetworkAgentInfo nai2 = cellNai(101);
95
96 assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
97 assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
98 }
99
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900100 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900101 public void testNotificationOnLinger() {
102 setNotificationSwitch(transition(WIFI, CELLULAR));
103 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
104 NetworkAgentInfo from = wifiNai(100);
105 NetworkAgentInfo to = cellNai(101);
106
107 mMonitor.noteLingerDefaultNetwork(from, to);
108 verifyNotification(from, to);
109 }
110
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900111 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900112 public void testToastOnLinger() {
113 setNotificationSwitch(transition(WIFI, CELLULAR));
114 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
115 NetworkAgentInfo from = wifiNai(100);
116 NetworkAgentInfo to = cellNai(101);
117
118 mMonitor.noteLingerDefaultNetwork(from, to);
119 verifyToast(from, to);
120 }
121
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900122 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900123 public void testNotificationClearedAfterDisconnect() {
124 setNotificationSwitch(transition(WIFI, CELLULAR));
125 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
126 NetworkAgentInfo from = wifiNai(100);
127 NetworkAgentInfo to = cellNai(101);
128
129 mMonitor.noteLingerDefaultNetwork(from, to);
130 verifyNotification(from, to);
131
132 mMonitor.noteDisconnect(to);
133 verify(mNotifier, times(1)).clearNotification(100);
134 }
135
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900136 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900137 public void testNotificationClearedAfterSwitchingBack() {
138 setNotificationSwitch(transition(WIFI, CELLULAR));
139 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
140 NetworkAgentInfo from = wifiNai(100);
141 NetworkAgentInfo to = cellNai(101);
142
143 mMonitor.noteLingerDefaultNetwork(from, to);
144 verifyNotification(from, to);
145
146 mMonitor.noteLingerDefaultNetwork(to, from);
147 verify(mNotifier, times(1)).clearNotification(100);
148 }
149
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900150 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900151 public void testUniqueToast() {
152 setNotificationSwitch(transition(WIFI, CELLULAR));
153 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
154 NetworkAgentInfo from = wifiNai(100);
155 NetworkAgentInfo to = cellNai(101);
156
157 mMonitor.noteLingerDefaultNetwork(from, to);
158 verifyToast(from, to);
159
160 mMonitor.noteLingerDefaultNetwork(to, from);
161 verify(mNotifier, times(1)).clearNotification(100);
162
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900163 reset(mNotifier);
Hugo Benichia43a0952016-08-30 10:01:15 +0900164 mMonitor.noteLingerDefaultNetwork(from, to);
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900165 verifyNoNotifications();
166 }
167
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900168 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900169 public void testMultipleNotifications() {
170 setNotificationSwitch(transition(WIFI, CELLULAR));
171 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
172 NetworkAgentInfo wifi1 = wifiNai(100);
173 NetworkAgentInfo wifi2 = wifiNai(101);
174 NetworkAgentInfo cell = cellNai(102);
175
176 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
177 verifyNotification(wifi1, cell);
178
179 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
180 verify(mNotifier, times(1)).clearNotification(100);
181
182 reset(mNotifier);
183 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
184 verifyNotification(wifi2, cell);
185 }
186
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900187 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900188 public void testRateLimiting() throws InterruptedException {
189 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
190
191 setNotificationSwitch(transition(WIFI, CELLULAR));
192 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
193 NetworkAgentInfo wifi1 = wifiNai(100);
194 NetworkAgentInfo wifi2 = wifiNai(101);
195 NetworkAgentInfo wifi3 = wifiNai(102);
196 NetworkAgentInfo cell = cellNai(103);
197
198 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
199 verifyNotification(wifi1, cell);
200 reset(mNotifier);
201
202 Thread.sleep(50);
203 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
204 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
205 verifyNoNotifications();
206
207 Thread.sleep(50);
208 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
209 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
210 verifyNoNotifications();
211 }
212
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900213 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900214 public void testDailyLimiting() throws InterruptedException {
215 mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
216
217 setNotificationSwitch(transition(WIFI, CELLULAR));
218 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
219 NetworkAgentInfo wifi1 = wifiNai(100);
220 NetworkAgentInfo wifi2 = wifiNai(101);
221 NetworkAgentInfo wifi3 = wifiNai(102);
222 NetworkAgentInfo cell = cellNai(103);
223
224 mMonitor.noteLingerDefaultNetwork(wifi1, cell);
225 verifyNotification(wifi1, cell);
226 reset(mNotifier);
227
228 Thread.sleep(50);
229 mMonitor.noteLingerDefaultNetwork(cell, wifi2);
230 mMonitor.noteLingerDefaultNetwork(wifi2, cell);
231 verifyNotification(wifi2, cell);
232 reset(mNotifier);
233
234 Thread.sleep(50);
235 mMonitor.noteLingerDefaultNetwork(cell, wifi3);
236 mMonitor.noteLingerDefaultNetwork(wifi3, cell);
237 verifyNoNotifications();
Hugo Benichia43a0952016-08-30 10:01:15 +0900238 }
239
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900240 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900241 public void testUniqueNotification() {
242 setNotificationSwitch(transition(WIFI, CELLULAR));
243 setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
244 NetworkAgentInfo from = wifiNai(100);
245 NetworkAgentInfo to = cellNai(101);
246
247 mMonitor.noteLingerDefaultNetwork(from, to);
248 verifyNotification(from, to);
249
250 mMonitor.noteLingerDefaultNetwork(to, from);
251 verify(mNotifier, times(1)).clearNotification(100);
252
253 mMonitor.noteLingerDefaultNetwork(from, to);
254 verifyNotification(from, to);
255 }
256
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900257 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900258 public void testIgnoreNeverValidatedNetworks() {
Hugo Benichia43a0952016-08-30 10:01:15 +0900259 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
260 setNotificationSwitch(transition(WIFI, CELLULAR));
261 NetworkAgentInfo from = wifiNai(100);
262 NetworkAgentInfo to = cellNai(101);
263 from.everValidated = false;
264
265 mMonitor.noteLingerDefaultNetwork(from, to);
266 verifyNoNotifications();
267 }
268
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900269 @Test
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900270 public void testIgnoreCurrentlyValidatedNetworks() {
271 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
272 setNotificationSwitch(transition(WIFI, CELLULAR));
273 NetworkAgentInfo from = wifiNai(100);
274 NetworkAgentInfo to = cellNai(101);
275 from.lastValidated = true;
276
277 mMonitor.noteLingerDefaultNetwork(from, to);
278 verifyNoNotifications();
279 }
280
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900281 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900282 public void testNoNotificationType() {
283 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
284 setNotificationSwitch();
285 NetworkAgentInfo from = wifiNai(100);
286 NetworkAgentInfo to = cellNai(101);
287
288 mMonitor.noteLingerDefaultNetwork(from, to);
289 verifyNoNotifications();
290 }
291
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900292 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900293 public void testNoTransitionToNotify() {
294 setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
295 setNotificationSwitch(transition(WIFI, CELLULAR));
296 NetworkAgentInfo from = wifiNai(100);
297 NetworkAgentInfo to = cellNai(101);
298
299 mMonitor.noteLingerDefaultNetwork(from, to);
300 verifyNoNotifications();
301 }
302
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900303 @Test
Hugo Benichia43a0952016-08-30 10:01:15 +0900304 public void testDifferentTransitionToNotify() {
305 setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
306 setNotificationSwitch(transition(CELLULAR, WIFI));
307 NetworkAgentInfo from = wifiNai(100);
308 NetworkAgentInfo to = cellNai(101);
309
310 mMonitor.noteLingerDefaultNetwork(from, to);
311 verifyNoNotifications();
312 }
313
314 void setNotificationSwitch(String... transitions) {
315 when(mResources.getStringArray(R.array.config_networkNotifySwitches))
316 .thenReturn(transitions);
317 }
318
319 String transition(String from, String to) {
320 return from + "-" + to;
321 }
322
323 void setNotificationType(int type) {
324 when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
325 }
326
327 void verifyNoToast() {
328 verify(mNotifier, never()).showToast(any(), any());
329 }
330
331 void verifyNoNotification() {
332 verify(mNotifier, never())
333 .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
334 }
335
336 void verifyNoNotifications() {
337 verifyNoToast();
338 verifyNoNotification();
Hugo Benichia43a0952016-08-30 10:01:15 +0900339 }
340
341 void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
342 verifyNoNotification();
343 verify(mNotifier, times(1)).showToast(from, to);
344 }
345
346 void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
347 verifyNoToast();
348 verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
349 eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
350 }
351
352 NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
353 NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
354 NetworkCapabilities caps = new NetworkCapabilities();
355 caps.addCapability(0);
356 caps.addTransportType(transport);
357 NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
Luke Huang65914772019-03-16 00:31:46 +0800358 caps, 50, mCtx, null, mMisc, mConnService, mNetd, mDnsResolver, mNMS,
Chalard Jean05ab6812018-05-02 21:14:54 +0900359 NetworkFactory.SerialNumber.NONE);
Hugo Benichia43a0952016-08-30 10:01:15 +0900360 nai.everValidated = true;
361 return nai;
362 }
363
364 NetworkAgentInfo wifiNai(int netId) {
365 return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
366 ConnectivityManager.TYPE_WIFI, WIFI);
367 }
368
369 NetworkAgentInfo cellNai(int netId) {
370 return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
371 ConnectivityManager.TYPE_MOBILE, CELLULAR);
372 }
373
374 public static class TestableLingerMonitor extends LingerMonitor {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900375 public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
376 super(c, n, l, r);
Hugo Benichia43a0952016-08-30 10:01:15 +0900377 }
378 @Override protected PendingIntent createNotificationIntent() {
379 return null;
380 }
381 }
382}