Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 17 | package com.android.server.net; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 18 | |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 21 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 22 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 27 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 28 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 29 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
| 30 | import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED; |
| 31 | import static android.net.NetworkPolicyManager.RULE_NONE; |
| 32 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
| 33 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
| 34 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 35 | import static android.net.NetworkPolicyManager.uidPoliciesToString; |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 36 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 37 | import static android.net.NetworkStats.IFACE_ALL; |
| 38 | import static android.net.NetworkStats.SET_ALL; |
| 39 | import static android.net.NetworkStats.TAG_ALL; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 40 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 41 | import static android.net.NetworkTemplate.buildTemplateWifi; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 42 | import static android.net.TrafficStats.MB_IN_BYTES; |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 43 | import static android.os.Process.SYSTEM_UID; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 44 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 45 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_THRESHOLD_DISABLED; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 46 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_USE_PLATFORM_DEFAULT; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 47 | import static android.telephony.CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; |
| 48 | import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; |
| 49 | import static android.telephony.CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 50 | import static android.telephony.SubscriptionPlan.BYTES_UNLIMITED; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 51 | import static android.telephony.SubscriptionPlan.LIMIT_BEHAVIOR_DISABLED; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 52 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 53 | import static com.android.server.net.NetworkPolicyManagerInternal.QUOTA_TYPE_JOBS; |
| 54 | import static com.android.server.net.NetworkPolicyManagerInternal.QUOTA_TYPE_MULTIPATH; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 55 | import static com.android.server.net.NetworkPolicyManagerService.OPPORTUNISTIC_QUOTA_UNKNOWN; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 56 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT; |
| 57 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 58 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_RAPID; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 59 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 60 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 61 | import static org.junit.Assert.assertEquals; |
| 62 | import static org.junit.Assert.assertFalse; |
| 63 | import static org.junit.Assert.assertNotNull; |
| 64 | import static org.junit.Assert.assertNull; |
| 65 | import static org.junit.Assert.assertTrue; |
| 66 | import static org.junit.Assert.fail; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 67 | import static org.mockito.ArgumentMatchers.any; |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 68 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 69 | import static org.mockito.ArgumentMatchers.anyInt; |
| 70 | import static org.mockito.ArgumentMatchers.anyLong; |
| 71 | import static org.mockito.ArgumentMatchers.anyString; |
| 72 | import static org.mockito.ArgumentMatchers.eq; |
| 73 | import static org.mockito.ArgumentMatchers.isA; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 74 | import static org.mockito.Mockito.atLeast; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 75 | import static org.mockito.Mockito.atLeastOnce; |
| 76 | import static org.mockito.Mockito.doAnswer; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 77 | import static org.mockito.Mockito.doNothing; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 78 | import static org.mockito.Mockito.mock; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 79 | import static org.mockito.Mockito.never; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 80 | import static org.mockito.Mockito.reset; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 81 | import static org.mockito.Mockito.verify; |
| 82 | import static org.mockito.Mockito.when; |
| 83 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 84 | import android.Manifest; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 85 | import android.app.ActivityManager; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 86 | import android.app.ActivityManagerInternal; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 87 | import android.app.IActivityManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 88 | import android.app.IUidObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 89 | import android.app.Notification; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 90 | import android.app.NotificationManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 91 | import android.app.usage.UsageStatsManagerInternal; |
| 92 | import android.content.Context; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 93 | import android.content.Intent; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 94 | import android.content.pm.ApplicationInfo; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 95 | import android.content.pm.IPackageManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 96 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 97 | import android.content.pm.PackageManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 98 | import android.content.pm.Signature; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 99 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 100 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 101 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 102 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 103 | import android.net.LinkProperties; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 104 | import android.net.Network; |
Stephen Chen | a1c92b7 | 2017-02-06 13:18:11 -0800 | [diff] [blame] | 105 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 106 | import android.net.NetworkInfo; |
| 107 | import android.net.NetworkInfo.DetailedState; |
| 108 | import android.net.NetworkPolicy; |
Sudheer Shanka | 6a3c07e | 2018-09-11 15:21:22 -0700 | [diff] [blame] | 109 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 110 | import android.net.NetworkState; |
| 111 | import android.net.NetworkStats; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 112 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 113 | import android.net.NetworkTemplate; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 114 | import android.net.StringNetworkSpecifier; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 115 | import android.os.Binder; |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 116 | import android.os.Handler; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 117 | import android.os.INetworkManagementService; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 118 | import android.os.PersistableBundle; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 119 | import android.os.PowerManagerInternal; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 120 | import android.os.PowerSaveState; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 121 | import android.os.RemoteException; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 122 | import android.os.SimpleClock; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 123 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 124 | import android.os.UserHandle; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 125 | import android.telephony.CarrierConfigManager; |
| 126 | import android.telephony.SubscriptionManager; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 127 | import android.telephony.SubscriptionPlan; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 128 | import android.telephony.TelephonyManager; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 129 | import android.test.suitebuilder.annotation.MediumTest; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 130 | import android.text.TextUtils; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 131 | import android.util.DataUnit; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 132 | import android.util.Log; |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 133 | import android.util.Pair; |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 134 | import android.util.Range; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 135 | import android.util.RecurrenceRule; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 136 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 137 | import androidx.test.InstrumentationRegistry; |
| 138 | import androidx.test.runner.AndroidJUnit4; |
| 139 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 140 | import com.android.internal.telephony.PhoneConstants; |
Lorenzo Colitti | 281a17c | 2016-10-28 12:56:03 +0900 | [diff] [blame] | 141 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 142 | import com.android.internal.util.test.BroadcastInterceptingContext.FutureIntent; |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 143 | import com.android.server.DeviceIdleController; |
| 144 | import com.android.server.LocalServices; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 145 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 146 | import com.google.common.util.concurrent.AbstractFuture; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 147 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 148 | import libcore.io.IoUtils; |
| 149 | import libcore.io.Streams; |
| 150 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 151 | import org.junit.After; |
| 152 | import org.junit.Before; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 153 | import org.junit.Rule; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 154 | import org.junit.Test; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 155 | import org.junit.rules.MethodRule; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 156 | import org.junit.runner.RunWith; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 157 | import org.junit.runners.model.FrameworkMethod; |
| 158 | import org.junit.runners.model.Statement; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 159 | import org.mockito.ArgumentCaptor; |
| 160 | import org.mockito.Mock; |
| 161 | import org.mockito.MockitoAnnotations; |
| 162 | import org.mockito.invocation.InvocationOnMock; |
| 163 | import org.mockito.stubbing.Answer; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 164 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 165 | import java.io.File; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 166 | import java.io.FileOutputStream; |
| 167 | import java.io.InputStream; |
| 168 | import java.io.OutputStream; |
| 169 | import java.lang.annotation.Annotation; |
| 170 | import java.lang.annotation.ElementType; |
| 171 | import java.lang.annotation.Retention; |
| 172 | import java.lang.annotation.RetentionPolicy; |
| 173 | import java.lang.annotation.Target; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 174 | import java.time.Clock; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 175 | import java.time.Instant; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 176 | import java.time.Period; |
| 177 | import java.time.ZoneId; |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 178 | import java.time.ZoneOffset; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 179 | import java.time.ZonedDateTime; |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 180 | import java.util.ArrayList; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 181 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 182 | import java.util.Calendar; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 183 | import java.util.Iterator; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 184 | import java.util.LinkedHashSet; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 185 | import java.util.List; |
Neil Fuller | d5f0a39 | 2019-06-17 19:59:51 +0100 | [diff] [blame] | 186 | import java.util.TimeZone; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 187 | import java.util.concurrent.CountDownLatch; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 188 | import java.util.concurrent.ExecutionException; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 189 | import java.util.concurrent.Future; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 190 | import java.util.concurrent.TimeUnit; |
| 191 | import java.util.concurrent.TimeoutException; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 192 | import java.util.stream.Collectors; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 193 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Tests for {@link NetworkPolicyManagerService}. |
| 196 | */ |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 197 | @RunWith(AndroidJUnit4.class) |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 198 | @MediumTest |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 199 | public class NetworkPolicyManagerServiceTest { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 200 | private static final String TAG = "NetworkPolicyManagerServiceTest"; |
| 201 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 202 | private static final long TEST_START = 1194220800000L; |
| 203 | private static final String TEST_IFACE = "test0"; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 204 | private static final String TEST_SSID = "AndroidAP"; |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 205 | private static final String TEST_IMSI = "310210"; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 206 | private static final int TEST_SUB_ID = 42; |
| 207 | private static final int TEST_NET_ID = 24; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 208 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 209 | private static NetworkTemplate sTemplateWifi = buildTemplateWifi(TEST_SSID); |
| 210 | private static NetworkTemplate sTemplateMobileAll = buildTemplateMobileAll(TEST_IMSI); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 211 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Path on assets where files used by {@link NetPolicyXml} are located. |
| 214 | */ |
| 215 | private static final String NETPOLICY_DIR = "NetworkPolicyManagerServiceTest/netpolicy"; |
Neil Fuller | d5f0a39 | 2019-06-17 19:59:51 +0100 | [diff] [blame] | 216 | private static final String TIMEZONE_UTC = "UTC"; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 217 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 218 | private BroadcastInterceptingContext mServiceContext; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 219 | private File mPolicyDir; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 220 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Relative path of the XML file that will be used as {@code netpolicy.xml}. |
| 223 | * |
| 224 | * <p>Typically set through a {@link NetPolicyXml} annotation in the test method. |
| 225 | */ |
| 226 | private String mNetpolicyXml; |
| 227 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 228 | private @Mock IActivityManager mActivityManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 229 | private @Mock INetworkManagementService mNetworkManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 230 | private @Mock IConnectivityManager mConnManager; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 231 | private @Mock ConnectivityManager mConnectivityManager; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 232 | private @Mock NotificationManager mNotifManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 233 | private @Mock PackageManager mPackageManager; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 234 | private @Mock IPackageManager mIpm; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 235 | private @Mock SubscriptionManager mSubscriptionManager; |
| 236 | private @Mock CarrierConfigManager mCarrierConfigManager; |
| 237 | private @Mock TelephonyManager mTelephonyManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 238 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 239 | private ArgumentCaptor<ConnectivityManager.NetworkCallback> mNetworkCallbackCaptor = |
| 240 | ArgumentCaptor.forClass(ConnectivityManager.NetworkCallback.class); |
| 241 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 242 | private ActivityManagerInternal mActivityManagerInternal; |
| 243 | private NetworkStatsManagerInternal mStatsService; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 244 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 245 | private IUidObserver mUidObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 246 | private INetworkManagementEventObserver mNetworkObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 247 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 248 | private NetworkPolicyListenerAnswer mPolicyListener; |
| 249 | private NetworkPolicyManagerService mService; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 250 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 251 | /** |
| 252 | * In some of the tests while initializing NetworkPolicyManagerService, |
| 253 | * ACTION_RESTRICT_BACKGROUND_CHANGED is broadcasted. This is for capturing that broadcast. |
| 254 | */ |
| 255 | private FutureIntent mFutureIntent; |
| 256 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 257 | private long mStartTime; |
| 258 | private long mElapsedRealtime; |
| 259 | |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 260 | private static final int USER_ID = 0; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 261 | private static final int FAKE_SUB_ID = 3737373; |
| 262 | private static final String FAKE_SUBSCRIBER_ID = "FAKE_SUB_ID"; |
| 263 | private static final int DEFAULT_CYCLE_DAY = 1; |
| 264 | private static final int INVALID_CARRIER_CONFIG_VALUE = -9999; |
| 265 | private long mDefaultWarningBytes; // filled in with the actual default before tests are run |
| 266 | private long mDefaultLimitBytes; // filled in with the actual default before tests are run |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 267 | private PersistableBundle mCarrierConfig = CarrierConfigManager.getDefaultConfig(); |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 268 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 269 | private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 4; |
| 270 | private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 8; |
| 271 | private static final int APP_ID_C = android.os.Process.FIRST_APPLICATION_UID + 15; |
| 272 | private static final int APP_ID_D = android.os.Process.FIRST_APPLICATION_UID + 16; |
| 273 | private static final int APP_ID_E = android.os.Process.FIRST_APPLICATION_UID + 23; |
| 274 | private static final int APP_ID_F = android.os.Process.FIRST_APPLICATION_UID + 42; |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 275 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 276 | private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A); |
| 277 | private static final int UID_B = UserHandle.getUid(USER_ID, APP_ID_B); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 278 | private static final int UID_C = UserHandle.getUid(USER_ID, APP_ID_C); |
| 279 | private static final int UID_D = UserHandle.getUid(USER_ID, APP_ID_D); |
| 280 | private static final int UID_E = UserHandle.getUid(USER_ID, APP_ID_E); |
| 281 | private static final int UID_F = UserHandle.getUid(USER_ID, APP_ID_F); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 282 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 283 | private static final String PKG_NAME_A = "name.is.A,pkg.A"; |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 284 | private static final String PKG_NAME_B = "name.is.B,pkg.B"; |
| 285 | private static final String PKG_NAME_C = "name.is.C,pkg.C"; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 286 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 287 | public final @Rule NetPolicyMethodRule mNetPolicyXmlRule = new NetPolicyMethodRule(); |
| 288 | |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 289 | private final Clock mClock = new SimpleClock(ZoneOffset.UTC) { |
| 290 | @Override |
| 291 | public long millis() { |
| 292 | return currentTimeMillis(); |
| 293 | } |
| 294 | }; |
| 295 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 296 | private void registerLocalServices() { |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 297 | addLocalServiceMock(DeviceIdleController.LocalService.class); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 298 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 299 | final UsageStatsManagerInternal usageStats = |
| 300 | addLocalServiceMock(UsageStatsManagerInternal.class); |
| 301 | when(usageStats.getIdleUidsForUser(anyInt())).thenReturn(new int[]{}); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 302 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 303 | mActivityManagerInternal = addLocalServiceMock(ActivityManagerInternal.class); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 304 | |
| 305 | final PowerSaveState state = new PowerSaveState.Builder() |
| 306 | .setBatterySaverEnabled(false).build(); |
| 307 | final PowerManagerInternal pmInternal = addLocalServiceMock(PowerManagerInternal.class); |
| 308 | when(pmInternal.getLowPowerState(anyInt())).thenReturn(state); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 309 | |
| 310 | mStatsService = addLocalServiceMock(NetworkStatsManagerInternal.class); |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 311 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 312 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 313 | @Before |
| 314 | public void callSystemReady() throws Exception { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 315 | MockitoAnnotations.initMocks(this); |
| 316 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 317 | final Context context = InstrumentationRegistry.getContext(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 318 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 319 | setCurrentTimeMillis(TEST_START); |
| 320 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 321 | registerLocalServices(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 322 | // Intercept various broadcasts, and pretend that uids have packages. |
| 323 | // Also return mock service instances for a few critical services. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 324 | mServiceContext = new BroadcastInterceptingContext(context) { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public PackageManager getPackageManager() { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 327 | return mPackageManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 328 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 329 | |
| 330 | @Override |
| 331 | public void startActivity(Intent intent) { |
| 332 | // ignored |
| 333 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 334 | |
| 335 | @Override |
| 336 | public Object getSystemService(String name) { |
| 337 | switch (name) { |
| 338 | case Context.TELEPHONY_SUBSCRIPTION_SERVICE: |
| 339 | return mSubscriptionManager; |
| 340 | case Context.CARRIER_CONFIG_SERVICE: |
| 341 | return mCarrierConfigManager; |
| 342 | case Context.TELEPHONY_SERVICE: |
| 343 | return mTelephonyManager; |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 344 | case Context.NOTIFICATION_SERVICE: |
| 345 | return mNotifManager; |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 346 | case Context.CONNECTIVITY_SERVICE: |
| 347 | return mConnectivityManager; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 348 | default: |
| 349 | return super.getSystemService(name); |
| 350 | } |
| 351 | } |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 352 | |
| 353 | @Override |
| 354 | public void enforceCallingOrSelfPermission(String permission, String message) { |
| 355 | // Assume that we're AID_SYSTEM |
| 356 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 359 | setNetpolicyXml(context); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 360 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 361 | doAnswer(new Answer<Void>() { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 362 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 363 | @Override |
| 364 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 365 | mUidObserver = (IUidObserver) invocation.getArguments()[0]; |
| 366 | Log.d(TAG, "set mUidObserver to " + mUidObserver); |
| 367 | return null; |
| 368 | } |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 369 | }).when(mActivityManager).registerUidObserver(any(), anyInt(), |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 370 | eq(NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE), any(String.class)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 371 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 372 | mFutureIntent = newRestrictBackgroundChangedFuture(); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 373 | mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, |
Jeff Sharkey | 9911a28 | 2018-02-14 22:29:11 -0700 | [diff] [blame] | 374 | mNetworkManager, mIpm, mClock, mPolicyDir, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 375 | mService.bindConnectivityManager(mConnManager); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 376 | mPolicyListener = new NetworkPolicyListenerAnswer(mService); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 377 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 378 | // Sets some common expectations. |
| 379 | when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenAnswer( |
| 380 | new Answer<PackageInfo>() { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 381 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 382 | @Override |
| 383 | public PackageInfo answer(InvocationOnMock invocation) throws Throwable { |
| 384 | final String packageName = (String) invocation.getArguments()[0]; |
| 385 | final PackageInfo info = new PackageInfo(); |
| 386 | final Signature signature; |
| 387 | if ("android".equals(packageName)) { |
| 388 | signature = new Signature("F00D"); |
| 389 | } else { |
| 390 | signature = new Signature("DEAD"); |
| 391 | } |
| 392 | info.signatures = new Signature[] { |
| 393 | signature |
| 394 | }; |
| 395 | return info; |
| 396 | } |
| 397 | }); |
| 398 | when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
| 399 | .thenReturn(new ApplicationInfo()); |
| 400 | when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] {PKG_NAME_A}); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 401 | when(mPackageManager.getPackagesForUid(UID_B)).thenReturn(new String[] {PKG_NAME_B}); |
| 402 | when(mPackageManager.getPackagesForUid(UID_C)).thenReturn(new String[] {PKG_NAME_C}); |
| 403 | when(mPackageManager.getApplicationInfo(eq(PKG_NAME_A), anyInt())) |
| 404 | .thenReturn(buildApplicationInfo(PKG_NAME_A)); |
| 405 | when(mPackageManager.getApplicationInfo(eq(PKG_NAME_B), anyInt())) |
| 406 | .thenReturn(buildApplicationInfo(PKG_NAME_B)); |
| 407 | when(mPackageManager.getApplicationInfo(eq(PKG_NAME_C), anyInt())) |
| 408 | .thenReturn(buildApplicationInfo(PKG_NAME_C)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 409 | when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 410 | when(mNetworkManager.setDataSaverModeEnabled(anyBoolean())).thenReturn(true); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 411 | doNothing().when(mConnectivityManager) |
| 412 | .registerNetworkCallback(any(), mNetworkCallbackCaptor.capture()); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 413 | |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 414 | // Create the expected carrier config |
| 415 | mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 416 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 417 | // Prepare NPMS. |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 418 | mService.systemReady(mService.networkScoreAndNetworkManagementServiceReady()); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 419 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 420 | // catch INetworkManagementEventObserver during systemReady() |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 421 | final ArgumentCaptor<INetworkManagementEventObserver> networkObserver = |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 422 | ArgumentCaptor.forClass(INetworkManagementEventObserver.class); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 423 | verify(mNetworkManager).registerObserver(networkObserver.capture()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 424 | mNetworkObserver = networkObserver.getValue(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 425 | |
| 426 | NetworkPolicy defaultPolicy = mService.buildDefaultMobilePolicy(0, ""); |
| 427 | mDefaultWarningBytes = defaultPolicy.warningBytes; |
| 428 | mDefaultLimitBytes = defaultPolicy.limitBytes; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 431 | @After |
| 432 | public void removeFiles() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 433 | for (File file : mPolicyDir.listFiles()) { |
| 434 | file.delete(); |
| 435 | } |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 436 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 437 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 438 | @After |
| 439 | public void unregisterLocalServices() throws Exception { |
| 440 | // Registered by NetworkPolicyManagerService's constructor. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 441 | LocalServices.removeServiceForTest(NetworkPolicyManagerInternal.class); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 442 | |
| 443 | // Added in registerLocalServices() |
| 444 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 445 | LocalServices.removeServiceForTest(PowerManagerInternal.class); |
| 446 | LocalServices.removeServiceForTest(DeviceIdleController.LocalService.class); |
| 447 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 448 | LocalServices.removeServiceForTest(NetworkStatsManagerInternal.class); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 451 | @After |
| 452 | public void resetClock() throws Exception { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 453 | RecurrenceRule.sClock = Clock.systemDefaultZone(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 454 | } |
| 455 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 456 | @Test |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 457 | public void testTurnRestrictBackgroundOn() throws Exception { |
| 458 | assertRestrictBackgroundOff(); // Sanity check. |
| 459 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 460 | setRestrictBackground(true); |
| 461 | assertRestrictBackgroundChangedReceived(futureIntent, null); |
| 462 | } |
| 463 | |
| 464 | @Test |
| 465 | @NetPolicyXml("restrict-background-on.xml") |
| 466 | public void testTurnRestrictBackgroundOff() throws Exception { |
| 467 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 468 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 469 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 470 | setRestrictBackground(false); |
| 471 | assertRestrictBackgroundChangedReceived(futureIntent, null); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Adds whitelist when restrict background is on - app should receive an intent. |
| 476 | */ |
| 477 | @Test |
| 478 | @NetPolicyXml("restrict-background-on.xml") |
| 479 | public void testAddRestrictBackgroundWhitelist_restrictBackgroundOn() throws Exception { |
| 480 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 481 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 482 | addRestrictBackgroundWhitelist(true); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Adds whitelist when restrict background is off - app should not receive an intent. |
| 487 | */ |
| 488 | @Test |
| 489 | public void testAddRestrictBackgroundWhitelist_restrictBackgroundOff() throws Exception { |
| 490 | assertRestrictBackgroundOff(); // Sanity check. |
| 491 | addRestrictBackgroundWhitelist(false); |
| 492 | } |
| 493 | |
| 494 | private void addRestrictBackgroundWhitelist(boolean expectIntent) throws Exception { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 495 | // Sanity checks. |
| 496 | assertWhitelistUids(); |
| 497 | assertUidPolicy(UID_A, POLICY_NONE); |
| 498 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 499 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 500 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 501 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 502 | mService.setUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 503 | |
| 504 | assertWhitelistUids(UID_A); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 505 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 506 | mPolicyListener.waitAndVerify() |
| 507 | .onUidPoliciesChanged(APP_ID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 508 | if (expectIntent) { |
| 509 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 510 | } else { |
| 511 | futureIntent.assertNotReceived(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Removes whitelist when restrict background is on - app should receive an intent. |
| 517 | */ |
| 518 | @Test |
| 519 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
| 520 | public void testRemoveRestrictBackgroundWhitelist_restrictBackgroundOn() throws Exception { |
| 521 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 522 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 523 | removeRestrictBackgroundWhitelist(true); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Removes whitelist when restrict background is off - app should not receive an intent. |
| 528 | */ |
| 529 | @Test |
| 530 | @NetPolicyXml("uidA-whitelisted-restrict-background-off.xml") |
| 531 | public void testRemoveRestrictBackgroundWhitelist_restrictBackgroundOff() throws Exception { |
| 532 | assertRestrictBackgroundOff(); // Sanity check. |
| 533 | removeRestrictBackgroundWhitelist(false); |
| 534 | } |
| 535 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 536 | @Test |
| 537 | public void testLowPowerModeObserver_ListenersRegistered() |
| 538 | throws Exception { |
| 539 | PowerManagerInternal pmInternal = LocalServices.getService(PowerManagerInternal.class); |
| 540 | |
| 541 | verify(pmInternal, atLeast(2)).registerLowPowerModeObserver(any()); |
| 542 | } |
| 543 | |
| 544 | @Test |
| 545 | public void updateRestrictBackgroundByLowPowerMode_RestrictOnBeforeBsm_RestrictOnAfterBsm() |
| 546 | throws Exception { |
| 547 | setRestrictBackground(true); |
| 548 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 549 | .setGlobalBatterySaverEnabled(true) |
| 550 | .setBatterySaverEnabled(false) |
| 551 | .build(); |
| 552 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 553 | |
| 554 | // RestrictBackground should be on even though battery saver want to turn it off |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 555 | assertTrue(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 556 | |
| 557 | PowerSaveState stateOff = new PowerSaveState.Builder() |
| 558 | .setGlobalBatterySaverEnabled(false) |
| 559 | .setBatterySaverEnabled(false) |
| 560 | .build(); |
| 561 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 562 | |
| 563 | // RestrictBackground should be on, following its previous state |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 564 | assertTrue(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | @Test |
| 568 | public void updateRestrictBackgroundByLowPowerMode_RestrictOffBeforeBsm_RestrictOffAfterBsm() |
| 569 | throws Exception { |
| 570 | setRestrictBackground(false); |
| 571 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 572 | .setGlobalBatterySaverEnabled(true) |
| 573 | .setBatterySaverEnabled(true) |
| 574 | .build(); |
| 575 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 576 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 577 | |
| 578 | // RestrictBackground should be turned on because of battery saver |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 579 | assertTrue(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 580 | |
| 581 | PowerSaveState stateOff = new PowerSaveState.Builder() |
| 582 | .setGlobalBatterySaverEnabled(false) |
| 583 | .setBatterySaverEnabled(false) |
| 584 | .build(); |
| 585 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 586 | |
| 587 | // RestrictBackground should be off, following its previous state |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 588 | assertFalse(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | @Test |
| 592 | public void updateRestrictBackgroundByLowPowerMode_StatusChangedInBsm_DoNotRestore() |
| 593 | throws Exception { |
| 594 | setRestrictBackground(true); |
| 595 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 596 | .setGlobalBatterySaverEnabled(true) |
| 597 | .setBatterySaverEnabled(true) |
| 598 | .build(); |
| 599 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 600 | |
| 601 | // RestrictBackground should still be on |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 602 | assertTrue(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 603 | |
| 604 | // User turns off RestrictBackground manually |
| 605 | setRestrictBackground(false); |
| 606 | PowerSaveState stateOff = new PowerSaveState.Builder().setBatterySaverEnabled( |
| 607 | false).build(); |
| 608 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 609 | |
| 610 | // RestrictBackground should be off because user changes it manually |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 611 | assertFalse(mService.getRestrictBackground()); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 614 | private void removeRestrictBackgroundWhitelist(boolean expectIntent) throws Exception { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 615 | // Sanity checks. |
| 616 | assertWhitelistUids(UID_A); |
| 617 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
| 618 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 619 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 620 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 621 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 622 | mService.setUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 623 | |
| 624 | assertWhitelistUids(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 625 | assertUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 626 | mPolicyListener.waitAndVerify().onUidPoliciesChanged(APP_ID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 627 | if (expectIntent) { |
| 628 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 629 | } else { |
| 630 | futureIntent.assertNotReceived(); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 635 | * Adds blacklist when restrict background is on - app should not receive an intent. |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 636 | */ |
| 637 | @Test |
| 638 | @NetPolicyXml("restrict-background-on.xml") |
| 639 | public void testAddRestrictBackgroundBlacklist_restrictBackgroundOn() throws Exception { |
| 640 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 641 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 642 | addRestrictBackgroundBlacklist(false); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Adds blacklist when restrict background is off - app should receive an intent. |
| 647 | */ |
| 648 | @Test |
| 649 | public void testAddRestrictBackgroundBlacklist_restrictBackgroundOff() throws Exception { |
| 650 | assertRestrictBackgroundOff(); // Sanity check. |
| 651 | addRestrictBackgroundBlacklist(true); |
| 652 | } |
| 653 | |
| 654 | private void addRestrictBackgroundBlacklist(boolean expectIntent) throws Exception { |
| 655 | assertUidPolicy(UID_A, POLICY_NONE); // Sanity check. |
| 656 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 657 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 658 | |
| 659 | mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 660 | |
| 661 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 662 | mPolicyListener.waitAndVerify() |
| 663 | .onUidPoliciesChanged(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 664 | if (expectIntent) { |
| 665 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 666 | } else { |
| 667 | futureIntent.assertNotReceived(); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /** |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 672 | * Removes blacklist when restrict background is on - app should not receive an intent. |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 673 | */ |
| 674 | @Test |
| 675 | @NetPolicyXml("uidA-blacklisted-restrict-background-on.xml") |
| 676 | public void testRemoveRestrictBackgroundBlacklist_restrictBackgroundOn() throws Exception { |
| 677 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 678 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 679 | removeRestrictBackgroundBlacklist(false); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Removes blacklist when restrict background is off - app should receive an intent. |
| 684 | */ |
| 685 | @Test |
| 686 | @NetPolicyXml("uidA-blacklisted-restrict-background-off.xml") |
| 687 | public void testRemoveRestrictBackgroundBlacklist_restrictBackgroundOff() throws Exception { |
| 688 | assertRestrictBackgroundOff(); // Sanity check. |
| 689 | removeRestrictBackgroundBlacklist(true); |
| 690 | } |
| 691 | |
| 692 | private void removeRestrictBackgroundBlacklist(boolean expectIntent) throws Exception { |
| 693 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); // Sanity check. |
| 694 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 695 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 696 | |
| 697 | mService.setUidPolicy(UID_A, POLICY_NONE); |
| 698 | |
| 699 | assertUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 700 | mPolicyListener.waitAndVerify() |
| 701 | .onUidPoliciesChanged(APP_ID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 702 | if (expectIntent) { |
| 703 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 704 | } else { |
| 705 | futureIntent.assertNotReceived(); |
| 706 | } |
| 707 | } |
| 708 | |
Felipe Leme | 51a6364 | 2016-08-30 12:15:09 -0700 | [diff] [blame] | 709 | @Test |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 710 | @NetPolicyXml("uidA-blacklisted-restrict-background-on.xml") |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 711 | public void testBlacklistedAppIsNotNotifiedWhenRestrictBackgroundIsOn() throws Exception { |
| 712 | // Sanity checks. |
| 713 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 714 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 715 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 716 | |
| 717 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 718 | setRestrictBackground(true); |
| 719 | futureIntent.assertNotReceived(); |
| 720 | } |
| 721 | |
Felipe Leme | 51a6364 | 2016-08-30 12:15:09 -0700 | [diff] [blame] | 722 | @Test |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 723 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 724 | public void testWhitelistedAppIsNotNotifiedWhenRestrictBackgroundIsOn() throws Exception { |
| 725 | // Sanity checks. |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 726 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 727 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 728 | assertWhitelistUids(UID_A); |
| 729 | |
| 730 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 731 | setRestrictBackground(true); |
| 732 | futureIntent.assertNotReceived(); |
| 733 | } |
| 734 | |
Andreas Gampe | 84d156b | 2016-09-03 10:50:38 -0700 | [diff] [blame] | 735 | @Test |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 736 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
| 737 | public void testWhitelistedAppIsNotifiedWhenBlacklisted() throws Exception { |
| 738 | // Sanity checks. |
| 739 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 740 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 741 | assertWhitelistUids(UID_A); |
| 742 | |
| 743 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 744 | mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 745 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 746 | } |
| 747 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 748 | @Test |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 749 | @NetPolicyXml("restrict-background-lists-whitelist-format.xml") |
| 750 | public void testRestrictBackgroundLists_whitelistFormat() throws Exception { |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 751 | restrictBackgroundListsTest(); |
| 752 | } |
| 753 | |
| 754 | @Test |
| 755 | @NetPolicyXml("restrict-background-lists-uid-policy-format.xml") |
| 756 | public void testRestrictBackgroundLists_uidPolicyFormat() throws Exception { |
| 757 | restrictBackgroundListsTest(); |
| 758 | } |
| 759 | |
| 760 | private void restrictBackgroundListsTest() throws Exception { |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 761 | // UIds that are whitelisted. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 762 | assertWhitelistUids(UID_A, UID_B, UID_C); |
| 763 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
| 764 | assertUidPolicy(UID_B, POLICY_ALLOW_METERED_BACKGROUND); |
| 765 | assertUidPolicy(UID_C, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 766 | |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 767 | // UIDs that are blacklisted. |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 768 | assertUidPolicy(UID_D, POLICY_NONE); |
| 769 | assertUidPolicy(UID_E, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 770 | |
| 771 | // UIDS that have legacy policies. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 772 | assertUidPolicy(UID_F, 2); // POLICY_ALLOW_BACKGROUND_BATTERY_SAVE |
| 773 | |
| 774 | // Remove whitelist. |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 775 | mService.setUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 776 | assertUidPolicy(UID_A, POLICY_NONE); |
| 777 | assertWhitelistUids(UID_B, UID_C); |
| 778 | |
| 779 | // Add whitelist when blacklisted. |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 780 | mService.setUidPolicy(UID_E, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 781 | assertUidPolicy(UID_E, POLICY_ALLOW_METERED_BACKGROUND); |
| 782 | assertWhitelistUids(UID_B, UID_C, UID_E); |
| 783 | |
| 784 | // Add blacklist when whitelisted. |
| 785 | mService.setUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); |
| 786 | assertUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); |
| 787 | assertWhitelistUids(UID_C, UID_E); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Tests scenario where an UID had {@code restrict-background} and {@code uid-policy} tags. |
| 792 | */ |
| 793 | @Test |
| 794 | @NetPolicyXml("restrict-background-lists-mixed-format.xml") |
| 795 | public void testRestrictBackgroundLists_mixedFormat() throws Exception { |
| 796 | assertWhitelistUids(UID_A, UID_C, UID_D); |
| 797 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 798 | assertUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); // Blacklist prevails. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 799 | assertUidPolicy(UID_C, (POLICY_ALLOW_METERED_BACKGROUND | 2)); |
| 800 | assertUidPolicy(UID_D, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 803 | @Test |
| 804 | @NetPolicyXml("uids-with-mixed-policies.xml") |
| 805 | public void testGetUidsWithPolicy() throws Exception { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 806 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_NONE)); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 807 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND), |
| 808 | UID_B, UID_D); |
| 809 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND), |
| 810 | UID_E, UID_F); |
| 811 | // Legacy (POLICY_ALLOW_BACKGROUND_BATTERY_SAVE) |
| 812 | assertContainsInAnyOrder(mService.getUidsWithPolicy(2), |
| 813 | UID_C, UID_D, UID_F); |
| 814 | } |
| 815 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 816 | // NOTE: testPolicyChangeTriggersListener() is too superficial, they |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 817 | // don't check for side-effects (like calls to NetworkManagementService) neither cover all |
| 818 | // different modes (Data Saver, Battery Saver, Doze, App idle, etc...). |
| 819 | // These scenarios are extensively tested on CTS' HostsideRestrictBackgroundNetworkTests. |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 820 | @Test |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 821 | public void testUidForeground() throws Exception { |
| 822 | // push all uids into background |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 823 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, 0); |
| 824 | callOnUidStateChanged(UID_B, ActivityManager.PROCESS_STATE_SERVICE, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 825 | assertFalse(mService.isUidForeground(UID_A)); |
| 826 | assertFalse(mService.isUidForeground(UID_B)); |
| 827 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 828 | // push one of the uids into foreground |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 829 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_TOP, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 830 | assertTrue(mService.isUidForeground(UID_A)); |
| 831 | assertFalse(mService.isUidForeground(UID_B)); |
| 832 | |
| 833 | // and swap another uid into foreground |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 834 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, 0); |
| 835 | callOnUidStateChanged(UID_B, ActivityManager.PROCESS_STATE_TOP, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 836 | assertFalse(mService.isUidForeground(UID_A)); |
| 837 | assertTrue(mService.isUidForeground(UID_B)); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Kweku Adams | a9e55bc | 2018-11-19 14:59:15 -0800 | [diff] [blame] | 840 | @Test |
| 841 | public void testAppIdleTempWhitelisting() throws Exception { |
| 842 | mService.setAppIdleWhitelist(UID_A, true); |
| 843 | mService.setAppIdleWhitelist(UID_B, false); |
| 844 | int[] whitelistedIds = mService.getAppIdleWhitelist(); |
| 845 | assertTrue(Arrays.binarySearch(whitelistedIds, UID_A) >= 0); |
| 846 | assertTrue(Arrays.binarySearch(whitelistedIds, UID_B) < 0); |
| 847 | assertFalse(mService.isUidIdle(UID_A)); |
| 848 | // Can't currently guarantee UID_B's app idle state. |
| 849 | // TODO: expand with multiple app idle states. |
| 850 | } |
| 851 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 852 | private static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 853 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTime), |
| 854 | ZoneId.systemDefault()); |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 855 | final Iterator<Range<ZonedDateTime>> it = policy.cycleIterator(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 856 | while (it.hasNext()) { |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 857 | final Range<ZonedDateTime> cycle = it.next(); |
| 858 | if (cycle.getLower().toInstant().toEpochMilli() < currentTime) { |
| 859 | return cycle.getLower().toInstant().toEpochMilli(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | throw new IllegalStateException( |
| 863 | "Failed to find current cycle for " + policy + " at " + currentTime); |
| 864 | } |
| 865 | |
| 866 | private static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 867 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTime), |
| 868 | ZoneId.systemDefault()); |
Jeff Sharkey | 0fc6d03 | 2018-03-30 16:25:11 -0600 | [diff] [blame] | 869 | return policy.cycleIterator().next().getUpper().toInstant().toEpochMilli(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 870 | } |
| 871 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 872 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 873 | public void testLastCycleBoundaryThisMonth() throws Exception { |
| 874 | // assume cycle day of "5th", which should be in same month |
| 875 | final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); |
| 876 | final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z"); |
| 877 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 878 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 879 | sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 880 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 881 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 884 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 885 | public void testLastCycleBoundaryLastMonth() throws Exception { |
| 886 | // assume cycle day of "20th", which should be in last month |
| 887 | final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); |
| 888 | final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z"); |
| 889 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 890 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 891 | sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 892 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 893 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 896 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 897 | public void testLastCycleBoundaryThisMonthFebruary() throws Exception { |
| 898 | // assume cycle day of "30th" in february; should go to january |
| 899 | final long currentTime = parseTime("2007-02-14T00:00:00.000Z"); |
| 900 | final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z"); |
| 901 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 902 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 903 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 904 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 905 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 908 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 909 | public void testLastCycleBoundaryLastMonthFebruary() throws Exception { |
| 910 | // assume cycle day of "30th" in february, which should clamp |
| 911 | final long currentTime = parseTime("2007-03-14T00:00:00.000Z"); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 912 | final long expectedCycle = parseTime("2007-02-28T23:59:59.999Z"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 913 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 914 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 915 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 916 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 917 | assertTimeEquals(expectedCycle, actualCycle); |
| 918 | } |
| 919 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 920 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 921 | public void testCycleBoundaryLeapYear() throws Exception { |
| 922 | final NetworkPolicy policy = new NetworkPolicy( |
| 923 | sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false); |
| 924 | |
| 925 | assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"), |
| 926 | computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy)); |
| 927 | assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"), |
| 928 | computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy)); |
| 929 | assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"), |
| 930 | computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy)); |
| 931 | assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"), |
| 932 | computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy)); |
| 933 | |
| 934 | assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"), |
| 935 | computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy)); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 936 | assertTimeEquals(parseTime("2007-02-28T23:59:59.999Z"), |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 937 | computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy)); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 938 | assertTimeEquals(parseTime("2007-02-28T23:59:59.999Z"), |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 939 | computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy)); |
| 940 | assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"), |
| 941 | computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy)); |
| 942 | } |
| 943 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 944 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 945 | public void testNextCycleTimezoneAfterUtc() throws Exception { |
| 946 | // US/Central is UTC-6 |
| 947 | final NetworkPolicy policy = new NetworkPolicy( |
| 948 | sTemplateWifi, 10, "US/Central", 1024L, 1024L, false); |
| 949 | assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"), |
| 950 | computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy)); |
| 951 | } |
| 952 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 953 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 954 | public void testNextCycleTimezoneBeforeUtc() throws Exception { |
| 955 | // Israel is UTC+2 |
| 956 | final NetworkPolicy policy = new NetworkPolicy( |
| 957 | sTemplateWifi, 10, "Israel", 1024L, 1024L, false); |
| 958 | assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"), |
| 959 | computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy)); |
| 960 | } |
| 961 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 962 | @Test |
Jeff Sharkey | 1539905 | 2013-01-15 14:15:53 -0800 | [diff] [blame] | 963 | public void testCycleTodayJanuary() throws Exception { |
| 964 | final NetworkPolicy policy = new NetworkPolicy( |
| 965 | sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false); |
| 966 | |
| 967 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 968 | computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy)); |
| 969 | assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), |
| 970 | computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy)); |
| 971 | assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), |
| 972 | computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy)); |
| 973 | |
| 974 | assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"), |
| 975 | computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy)); |
| 976 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 977 | computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy)); |
| 978 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 979 | computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy)); |
| 980 | } |
| 981 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 982 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 983 | public void testNetworkPolicyAppliedCycleLastMonth() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 984 | NetworkState[] state = null; |
| 985 | NetworkStats stats = null; |
| 986 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 987 | final int CYCLE_DAY = 15; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 988 | final long NOW = parseTime("2007-03-10T00:00Z"); |
| 989 | final long CYCLE_START = parseTime("2007-02-15T00:00Z"); |
| 990 | final long CYCLE_END = parseTime("2007-03-15T00:00Z"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 991 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 992 | setCurrentTimeMillis(NOW); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 993 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 994 | // first, pretend that wifi network comes online. no policy active, |
| 995 | // which means we shouldn't push limit to interface. |
| 996 | state = new NetworkState[] { buildWifi() }; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 997 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 998 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 999 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 1000 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1001 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1002 | |
| 1003 | // now change cycle to be on 15th, and test in early march, to verify we |
| 1004 | // pick cycle day in previous month. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1005 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1006 | |
| 1007 | // pretend that 512 bytes total have happened |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1008 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 1009 | .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1010 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, CYCLE_END)) |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1011 | .thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1012 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1013 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1014 | setNetworkPolicies(new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1015 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1016 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 1017 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1018 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1019 | (2 * MB_IN_BYTES) - 512); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 1022 | @Test |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1023 | public void testNotificationWarningLimitSnooze() throws Exception { |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1024 | // Create a place to store fake usage |
| 1025 | final NetworkStatsHistory history = new NetworkStatsHistory(TimeUnit.HOURS.toMillis(1)); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1026 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 0); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1027 | when(mStatsService.getNetworkTotalBytes(any(), anyLong(), anyLong())) |
| 1028 | .thenAnswer(new Answer<Long>() { |
| 1029 | @Override |
| 1030 | public Long answer(InvocationOnMock invocation) throws Throwable { |
| 1031 | final NetworkStatsHistory.Entry entry = history.getValues( |
| 1032 | invocation.getArgument(1), invocation.getArgument(2), null); |
| 1033 | return entry.rxBytes + entry.txBytes; |
| 1034 | } |
| 1035 | }); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1036 | when(mStatsService.getNetworkUidBytes(any(), anyLong(), anyLong())) |
| 1037 | .thenAnswer(new Answer<NetworkStats>() { |
| 1038 | @Override |
| 1039 | public NetworkStats answer(InvocationOnMock invocation) throws Throwable { |
| 1040 | return stats; |
| 1041 | } |
| 1042 | }); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1043 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1044 | // Get active mobile network in place |
| 1045 | expectMobileDefaults(); |
| 1046 | mService.updateNetworks(); |
| 1047 | |
| 1048 | // Define simple data plan |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1049 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1050 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), DataUnit.MEGABYTES.toBytes(1800)); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1051 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan }, |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1052 | mServiceContext.getOpPackageName()); |
| 1053 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1054 | // We're 20% through the month (6 days) |
| 1055 | final long start = parseTime("2015-11-01T00:00Z"); |
| 1056 | final long end = parseTime("2015-11-07T00:00Z"); |
| 1057 | setCurrentTimeMillis(end); |
| 1058 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1059 | // Normal usage means no notification |
| 1060 | { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1061 | history.clear(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1062 | history.recordData(start, end, |
| 1063 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(360), 0L, 0L, 0L, 0)); |
| 1064 | |
| 1065 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1066 | expectMobileDefaults(); |
| 1067 | |
| 1068 | mService.updateNetworks(); |
| 1069 | |
| 1070 | verify(mTelephonyManager, atLeastOnce()).setPolicyDataEnabled(true, TEST_SUB_ID); |
| 1071 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1072 | DataUnit.MEGABYTES.toBytes(1800 - 360)); |
| 1073 | verify(mNotifManager, never()).notifyAsUser(any(), anyInt(), any(), any()); |
| 1074 | } |
| 1075 | |
| 1076 | // Push over warning |
| 1077 | { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1078 | history.clear(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1079 | history.recordData(start, end, |
| 1080 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(1799), 0L, 0L, 0L, 0)); |
| 1081 | |
| 1082 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1083 | expectMobileDefaults(); |
| 1084 | |
| 1085 | mService.updateNetworks(); |
| 1086 | |
| 1087 | verify(mTelephonyManager, atLeastOnce()).setPolicyDataEnabled(true, TEST_SUB_ID); |
| 1088 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1089 | DataUnit.MEGABYTES.toBytes(1800 - 1799)); |
| 1090 | verify(mNotifManager, atLeastOnce()).notifyAsUser(any(), eq(TYPE_WARNING), |
| 1091 | isA(Notification.class), eq(UserHandle.ALL)); |
| 1092 | } |
| 1093 | |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 1094 | // Push over warning, but with a config that isn't from an identified carrier |
| 1095 | { |
| 1096 | history.clear(); |
| 1097 | history.recordData(start, end, |
| 1098 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(1799), 0L, 0L, 0L, 0)); |
| 1099 | |
| 1100 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1101 | expectMobileDefaults(); |
| 1102 | expectDefaultCarrierConfig(); |
| 1103 | |
| 1104 | mService.updateNetworks(); |
| 1105 | |
| 1106 | verify(mTelephonyManager, atLeastOnce()).setPolicyDataEnabled(true, TEST_SUB_ID); |
| 1107 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1108 | DataUnit.MEGABYTES.toBytes(1800 - 1799)); |
| 1109 | // Since this isn't from the identified carrier, there should be no notifications |
| 1110 | verify(mNotifManager, never()).notifyAsUser(any(), anyInt(), any(), any()); |
| 1111 | } |
| 1112 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1113 | // Push over limit |
| 1114 | { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1115 | history.clear(); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1116 | history.recordData(start, end, |
| 1117 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(1810), 0L, 0L, 0L, 0)); |
| 1118 | |
| 1119 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1120 | expectMobileDefaults(); |
| 1121 | |
| 1122 | mService.updateNetworks(); |
| 1123 | |
| 1124 | verify(mTelephonyManager, atLeastOnce()).setPolicyDataEnabled(false, TEST_SUB_ID); |
| 1125 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, 1); |
| 1126 | verify(mNotifManager, atLeastOnce()).notifyAsUser(any(), eq(TYPE_LIMIT), |
| 1127 | isA(Notification.class), eq(UserHandle.ALL)); |
| 1128 | } |
| 1129 | |
| 1130 | // Snooze limit |
| 1131 | { |
| 1132 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1133 | expectMobileDefaults(); |
| 1134 | |
| 1135 | mService.snoozeLimit(NetworkTemplate.buildTemplateMobileAll(TEST_IMSI)); |
| 1136 | mService.updateNetworks(); |
| 1137 | |
| 1138 | verify(mTelephonyManager, atLeastOnce()).setPolicyDataEnabled(true, TEST_SUB_ID); |
| 1139 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1140 | Long.MAX_VALUE); |
| 1141 | verify(mNotifManager, atLeastOnce()).notifyAsUser(any(), eq(TYPE_LIMIT_SNOOZED), |
| 1142 | isA(Notification.class), eq(UserHandle.ALL)); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | @Test |
| 1147 | public void testNotificationRapid() throws Exception { |
| 1148 | // Create a place to store fake usage |
| 1149 | final NetworkStatsHistory history = new NetworkStatsHistory(TimeUnit.HOURS.toMillis(1)); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1150 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 0); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1151 | when(mStatsService.getNetworkTotalBytes(any(), anyLong(), anyLong())) |
| 1152 | .thenAnswer(new Answer<Long>() { |
| 1153 | @Override |
| 1154 | public Long answer(InvocationOnMock invocation) throws Throwable { |
| 1155 | final NetworkStatsHistory.Entry entry = history.getValues( |
| 1156 | invocation.getArgument(1), invocation.getArgument(2), null); |
| 1157 | return entry.rxBytes + entry.txBytes; |
| 1158 | } |
| 1159 | }); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1160 | when(mStatsService.getNetworkUidBytes(any(), anyLong(), anyLong())) |
| 1161 | .thenAnswer(new Answer<NetworkStats>() { |
| 1162 | @Override |
| 1163 | public NetworkStats answer(InvocationOnMock invocation) throws Throwable { |
| 1164 | return stats; |
| 1165 | } |
| 1166 | }); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1167 | |
| 1168 | // Get active mobile network in place |
| 1169 | expectMobileDefaults(); |
| 1170 | mService.updateNetworks(); |
| 1171 | |
| 1172 | // Define simple data plan which gives us effectively 60MB/day |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1173 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1174 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), DataUnit.MEGABYTES.toBytes(1800)); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1175 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan }, |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1176 | mServiceContext.getOpPackageName()); |
| 1177 | |
| 1178 | // We're 20% through the month (6 days) |
| 1179 | final long start = parseTime("2015-11-01T00:00Z"); |
| 1180 | final long end = parseTime("2015-11-07T00:00Z"); |
| 1181 | setCurrentTimeMillis(end); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1182 | |
| 1183 | // Using 20% data in 20% time is normal |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1184 | { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1185 | history.clear(); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1186 | history.recordData(start, end, |
| 1187 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(360), 0L, 0L, 0L, 0)); |
| 1188 | |
| 1189 | reset(mNotifManager); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1190 | mService.updateNetworks(); |
| 1191 | verify(mNotifManager, never()).notifyAsUser(any(), anyInt(), any(), any()); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1192 | } |
| 1193 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1194 | // Using 80% data in 20% time is alarming; but spread equally among |
| 1195 | // three UIDs means we get generic alert |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1196 | { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1197 | history.clear(); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1198 | history.recordData(start, end, |
| 1199 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(1440), 0L, 0L, 0L, 0)); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1200 | stats.clear(); |
| 1201 | stats.addValues(IFACE_ALL, UID_A, SET_ALL, TAG_ALL, |
| 1202 | DataUnit.MEGABYTES.toBytes(480), 0, 0, 0, 0); |
| 1203 | stats.addValues(IFACE_ALL, UID_B, SET_ALL, TAG_ALL, |
| 1204 | DataUnit.MEGABYTES.toBytes(480), 0, 0, 0, 0); |
| 1205 | stats.addValues(IFACE_ALL, UID_C, SET_ALL, TAG_ALL, |
| 1206 | DataUnit.MEGABYTES.toBytes(480), 0, 0, 0, 0); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1207 | |
| 1208 | reset(mNotifManager); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1209 | mService.updateNetworks(); |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1210 | |
| 1211 | final ArgumentCaptor<Notification> notif = ArgumentCaptor.forClass(Notification.class); |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1212 | verify(mNotifManager, atLeastOnce()).notifyAsUser(any(), eq(TYPE_RAPID), |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1213 | notif.capture(), eq(UserHandle.ALL)); |
| 1214 | |
| 1215 | final String text = notif.getValue().extras.getCharSequence(Notification.EXTRA_TEXT) |
| 1216 | .toString(); |
| 1217 | assertFalse(text.contains(PKG_NAME_A)); |
| 1218 | assertFalse(text.contains(PKG_NAME_B)); |
| 1219 | assertFalse(text.contains(PKG_NAME_C)); |
| 1220 | } |
| 1221 | |
| 1222 | // Using 80% data in 20% time is alarming; but mostly done by one UID |
| 1223 | // means we get specific alert |
| 1224 | { |
| 1225 | history.clear(); |
| 1226 | history.recordData(start, end, |
| 1227 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(1440), 0L, 0L, 0L, 0)); |
| 1228 | stats.clear(); |
| 1229 | stats.addValues(IFACE_ALL, UID_A, SET_ALL, TAG_ALL, |
| 1230 | DataUnit.MEGABYTES.toBytes(960), 0, 0, 0, 0); |
| 1231 | stats.addValues(IFACE_ALL, UID_B, SET_ALL, TAG_ALL, |
| 1232 | DataUnit.MEGABYTES.toBytes(480), 0, 0, 0, 0); |
| 1233 | |
| 1234 | reset(mNotifManager); |
| 1235 | mService.updateNetworks(); |
| 1236 | |
| 1237 | final ArgumentCaptor<Notification> notif = ArgumentCaptor.forClass(Notification.class); |
| 1238 | verify(mNotifManager, atLeastOnce()).notifyAsUser(any(), eq(TYPE_RAPID), |
| 1239 | notif.capture(), eq(UserHandle.ALL)); |
| 1240 | |
| 1241 | final String text = notif.getValue().extras.getCharSequence(Notification.EXTRA_TEXT) |
| 1242 | .toString(); |
| 1243 | assertTrue(text.contains(PKG_NAME_A)); |
| 1244 | assertFalse(text.contains(PKG_NAME_B)); |
| 1245 | assertFalse(text.contains(PKG_NAME_C)); |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | @Test |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1250 | public void testMeteredNetworkWithoutLimit() throws Exception { |
| 1251 | NetworkState[] state = null; |
| 1252 | NetworkStats stats = null; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1253 | |
| 1254 | final long TIME_FEB_15 = 1171497600000L; |
| 1255 | final long TIME_MAR_10 = 1173484800000L; |
| 1256 | final int CYCLE_DAY = 15; |
| 1257 | |
| 1258 | setCurrentTimeMillis(TIME_MAR_10); |
| 1259 | |
| 1260 | // bring up wifi network with metered policy |
| 1261 | state = new NetworkState[] { buildWifi() }; |
| 1262 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 1263 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
| 1264 | |
| 1265 | { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1266 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
| 1267 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, |
| 1268 | currentTimeMillis())).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1269 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1270 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1271 | setNetworkPolicies(new NetworkPolicy( |
| 1272 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, |
| 1273 | true)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1274 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 1275 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1276 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(TEST_IFACE, |
| 1277 | Long.MAX_VALUE); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1281 | @Test |
| 1282 | public void testOnUidStateChanged_notifyAMS() throws Exception { |
| 1283 | final long procStateSeq = 222; |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 1284 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, procStateSeq); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1285 | verify(mActivityManagerInternal).notifyNetworkPolicyRulesUpdated(UID_A, procStateSeq); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 1288 | private void callOnUidStateChanged(int uid, int procState, long procStateSeq) |
| 1289 | throws Exception { |
| 1290 | mUidObserver.onUidStateChanged(uid, procState, procStateSeq); |
| 1291 | final CountDownLatch latch = new CountDownLatch(1); |
| 1292 | mService.mUidEventHandler.post(() -> { |
| 1293 | latch.countDown(); |
| 1294 | }); |
| 1295 | latch.await(2, TimeUnit.SECONDS); |
| 1296 | } |
| 1297 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1298 | private void assertCycleDayAsExpected(PersistableBundle config, int carrierCycleDay, |
| 1299 | boolean expectValid) { |
| 1300 | config.putInt(KEY_MONTHLY_DATA_CYCLE_DAY_INT, carrierCycleDay); |
| 1301 | int actualCycleDay = mService.getCycleDayFromCarrierConfig(config, |
| 1302 | INVALID_CARRIER_CONFIG_VALUE); |
| 1303 | if (expectValid) { |
| 1304 | assertEquals(carrierCycleDay, actualCycleDay); |
| 1305 | } else { |
| 1306 | // INVALID_CARRIER_CONFIG_VALUE is returned for invalid values |
| 1307 | assertEquals(INVALID_CARRIER_CONFIG_VALUE, actualCycleDay); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | @Test |
| 1312 | public void testGetCycleDayFromCarrierConfig() { |
| 1313 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1314 | final Calendar cal = Calendar.getInstance(); |
| 1315 | int actualCycleDay; |
| 1316 | |
| 1317 | config.putInt(KEY_MONTHLY_DATA_CYCLE_DAY_INT, DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1318 | actualCycleDay = mService.getCycleDayFromCarrierConfig(config, DEFAULT_CYCLE_DAY); |
| 1319 | assertEquals(DEFAULT_CYCLE_DAY, actualCycleDay); |
| 1320 | |
| 1321 | // null config returns a default value |
| 1322 | actualCycleDay = mService.getCycleDayFromCarrierConfig(null, DEFAULT_CYCLE_DAY); |
| 1323 | assertEquals(DEFAULT_CYCLE_DAY, actualCycleDay); |
| 1324 | |
| 1325 | // Sane, non-default values |
| 1326 | assertCycleDayAsExpected(config, 1, true); |
| 1327 | assertCycleDayAsExpected(config, cal.getMaximum(Calendar.DAY_OF_MONTH), true); |
| 1328 | assertCycleDayAsExpected(config, cal.getMinimum(Calendar.DAY_OF_MONTH), true); |
| 1329 | |
| 1330 | // Invalid values |
| 1331 | assertCycleDayAsExpected(config, 0, false); |
| 1332 | assertCycleDayAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, false); |
| 1333 | assertCycleDayAsExpected(config, cal.getMaximum(Calendar.DAY_OF_MONTH) + 1, false); |
| 1334 | assertCycleDayAsExpected(config, cal.getMinimum(Calendar.DAY_OF_MONTH) - 5, false); |
| 1335 | } |
| 1336 | |
| 1337 | private void assertWarningBytesAsExpected(PersistableBundle config, long carrierWarningBytes, |
| 1338 | long expected) { |
| 1339 | config.putLong(KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, carrierWarningBytes); |
| 1340 | long actualWarning = mService.getWarningBytesFromCarrierConfig(config, |
| 1341 | INVALID_CARRIER_CONFIG_VALUE); |
| 1342 | assertEquals(expected, actualWarning); |
| 1343 | } |
| 1344 | |
| 1345 | @Test |
| 1346 | public void testGetWarningBytesFromCarrierConfig() { |
| 1347 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1348 | long actualWarningBytes; |
| 1349 | |
| 1350 | assertWarningBytesAsExpected(config, DATA_CYCLE_USE_PLATFORM_DEFAULT, |
| 1351 | mDefaultWarningBytes); |
| 1352 | assertWarningBytesAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, WARNING_DISABLED); |
| 1353 | assertWarningBytesAsExpected(config, 0, 0); |
| 1354 | // not a valid value |
| 1355 | assertWarningBytesAsExpected(config, -1000, INVALID_CARRIER_CONFIG_VALUE); |
| 1356 | |
| 1357 | // null config returns a default value |
| 1358 | actualWarningBytes = mService.getWarningBytesFromCarrierConfig(null, mDefaultWarningBytes); |
| 1359 | assertEquals(mDefaultWarningBytes, actualWarningBytes); |
| 1360 | } |
| 1361 | |
| 1362 | private void assertLimitBytesAsExpected(PersistableBundle config, long carrierWarningBytes, |
| 1363 | long expected) { |
| 1364 | config.putLong(KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, carrierWarningBytes); |
| 1365 | long actualWarning = mService.getLimitBytesFromCarrierConfig(config, |
| 1366 | INVALID_CARRIER_CONFIG_VALUE); |
| 1367 | assertEquals(expected, actualWarning); |
| 1368 | } |
| 1369 | |
| 1370 | @Test |
| 1371 | public void testGetLimitBytesFromCarrierConfig() { |
| 1372 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1373 | long actualLimitBytes; |
| 1374 | |
| 1375 | assertLimitBytesAsExpected(config, DATA_CYCLE_USE_PLATFORM_DEFAULT, |
| 1376 | mDefaultLimitBytes); |
| 1377 | assertLimitBytesAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, LIMIT_DISABLED); |
| 1378 | assertLimitBytesAsExpected(config, 0, 0); |
| 1379 | // not a valid value |
| 1380 | assertLimitBytesAsExpected(config, -1000, INVALID_CARRIER_CONFIG_VALUE); |
| 1381 | |
| 1382 | // null config returns a default value |
| 1383 | actualLimitBytes = mService.getWarningBytesFromCarrierConfig(null, mDefaultLimitBytes); |
| 1384 | assertEquals(mDefaultLimitBytes, actualLimitBytes); |
| 1385 | } |
| 1386 | |
| 1387 | private PersistableBundle setupUpdateMobilePolicyCycleTests() throws RemoteException { |
| 1388 | when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]); |
| 1389 | when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID}); |
| 1390 | when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1391 | when(mTelephonyManager.createForSubscriptionId(FAKE_SUB_ID)) |
| 1392 | .thenReturn(mock(TelephonyManager.class)); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1393 | PersistableBundle bundle = CarrierConfigManager.getDefaultConfig(); |
| 1394 | when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(bundle); |
| 1395 | setNetworkPolicies(buildDefaultFakeMobilePolicy()); |
| 1396 | return bundle; |
| 1397 | } |
| 1398 | |
| 1399 | @Test |
| 1400 | public void testUpdateMobilePolicyCycleWithNullConfig() throws RemoteException { |
| 1401 | when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]); |
| 1402 | when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID}); |
| 1403 | when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1404 | when(mTelephonyManager.createForSubscriptionId(FAKE_SUB_ID)) |
| 1405 | .thenReturn(mock(TelephonyManager.class)); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1406 | when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(null); |
| 1407 | setNetworkPolicies(buildDefaultFakeMobilePolicy()); |
| 1408 | // smoke test to make sure no errors are raised |
| 1409 | mServiceContext.sendBroadcast( |
| 1410 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1411 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1412 | ); |
| 1413 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1414 | true); |
| 1415 | } |
| 1416 | |
| 1417 | @Test |
| 1418 | public void testUpdateMobilePolicyCycleWithInvalidConfig() throws RemoteException { |
| 1419 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1420 | // Test with an invalid CarrierConfig, there should be no changes or crashes. |
| 1421 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, -100); |
| 1422 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, -100); |
| 1423 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, -100); |
| 1424 | mServiceContext.sendBroadcast( |
| 1425 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1426 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1427 | ); |
| 1428 | |
| 1429 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1430 | true); |
| 1431 | } |
| 1432 | |
| 1433 | @Test |
| 1434 | public void testUpdateMobilePolicyCycleWithDefaultConfig() throws RemoteException { |
| 1435 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1436 | // Test that we respect the platform values when told to |
| 1437 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, |
| 1438 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1439 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1440 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1441 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1442 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1443 | mServiceContext.sendBroadcast( |
| 1444 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1445 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1446 | ); |
| 1447 | |
| 1448 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1449 | true); |
| 1450 | } |
| 1451 | |
| 1452 | @Test |
| 1453 | public void testUpdateMobilePolicyCycleWithUserOverrides() throws RemoteException { |
| 1454 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1455 | |
| 1456 | // inferred = false implies that a user manually modified this policy. |
| 1457 | NetworkPolicy policy = buildDefaultFakeMobilePolicy(); |
| 1458 | policy.inferred = false; |
| 1459 | setNetworkPolicies(policy); |
| 1460 | |
| 1461 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1462 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, 9999); |
| 1463 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1464 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1465 | mServiceContext.sendBroadcast( |
| 1466 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1467 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1468 | ); |
| 1469 | |
| 1470 | // The policy still shouldn't change, because we don't want to overwrite user settings. |
| 1471 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1472 | false); |
| 1473 | } |
| 1474 | |
| 1475 | @Test |
| 1476 | public void testUpdateMobilePolicyCycleUpdatesDataCycle() throws RemoteException { |
| 1477 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1478 | |
| 1479 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1480 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, 9999); |
| 1481 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, 9999); |
| 1482 | mServiceContext.sendBroadcast( |
| 1483 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1484 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1485 | ); |
| 1486 | |
| 1487 | assertNetworkPolicyEquals(31, 9999, 9999, true); |
| 1488 | } |
| 1489 | |
| 1490 | @Test |
| 1491 | public void testUpdateMobilePolicyCycleDisableThresholds() throws RemoteException { |
| 1492 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1493 | |
| 1494 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1495 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1496 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1497 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1498 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1499 | mServiceContext.sendBroadcast( |
| 1500 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1501 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1502 | ); |
| 1503 | |
| 1504 | assertNetworkPolicyEquals(31, WARNING_DISABLED, LIMIT_DISABLED, true); |
| 1505 | } |
| 1506 | |
| 1507 | @Test |
| 1508 | public void testUpdateMobilePolicyCycleRevertsToDefault() throws RemoteException { |
| 1509 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1510 | |
| 1511 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1512 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1513 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1514 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1515 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1516 | mServiceContext.sendBroadcast( |
| 1517 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1518 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1519 | ); |
| 1520 | assertNetworkPolicyEquals(31, WARNING_DISABLED, LIMIT_DISABLED, true); |
| 1521 | |
| 1522 | // If the user switches carriers to one that doesn't use a CarrierConfig, we should revert |
| 1523 | // to the default data limit and warning. The cycle date doesn't need to revert as it's |
| 1524 | // arbitrary anyways. |
| 1525 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, |
| 1526 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1527 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1528 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1529 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1530 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1531 | mServiceContext.sendBroadcast( |
| 1532 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1533 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1534 | ); |
| 1535 | |
| 1536 | assertNetworkPolicyEquals(31, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1537 | true); |
| 1538 | } |
| 1539 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1540 | @Test |
| 1541 | public void testOpportunisticQuota() throws Exception { |
| 1542 | final Network net = new Network(TEST_NET_ID); |
| 1543 | final NetworkPolicyManagerInternal internal = LocalServices |
| 1544 | .getService(NetworkPolicyManagerInternal.class); |
| 1545 | |
| 1546 | // Create a place to store fake usage |
| 1547 | final NetworkStatsHistory history = new NetworkStatsHistory(TimeUnit.HOURS.toMillis(1)); |
| 1548 | final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 0); |
| 1549 | when(mStatsService.getNetworkTotalBytes(any(), anyLong(), anyLong())) |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1550 | .thenAnswer(invocation -> { |
| 1551 | final NetworkStatsHistory.Entry entry = history.getValues( |
| 1552 | invocation.getArgument(1), invocation.getArgument(2), null); |
| 1553 | return entry.rxBytes + entry.txBytes; |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1554 | }); |
| 1555 | when(mStatsService.getNetworkUidBytes(any(), anyLong(), anyLong())) |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1556 | .thenReturn(stats); |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1557 | |
| 1558 | // Get active mobile network in place |
| 1559 | expectMobileDefaults(); |
| 1560 | mService.updateNetworks(); |
| 1561 | |
| 1562 | // We're 20% through the month (6 days) |
| 1563 | final long start = parseTime("2015-11-01T00:00Z"); |
| 1564 | final long end = parseTime("2015-11-07T00:00Z"); |
| 1565 | setCurrentTimeMillis(end); |
| 1566 | |
| 1567 | // Get some data usage in place |
| 1568 | history.clear(); |
| 1569 | history.recordData(start, end, |
| 1570 | new NetworkStats.Entry(DataUnit.MEGABYTES.toBytes(360), 0L, 0L, 0L, 0)); |
| 1571 | |
| 1572 | // No data plan |
| 1573 | { |
| 1574 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1575 | expectMobileDefaults(); |
| 1576 | |
| 1577 | mService.updateNetworks(); |
| 1578 | |
| 1579 | // No quotas |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1580 | assertEquals(OPPORTUNISTIC_QUOTA_UNKNOWN, |
| 1581 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_JOBS)); |
| 1582 | assertEquals(OPPORTUNISTIC_QUOTA_UNKNOWN, |
| 1583 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_MULTIPATH)); |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | // Limited data plan |
| 1587 | { |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1588 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1589 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), |
| 1590 | DataUnit.MEGABYTES.toBytes(1800)); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1591 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan}, |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1592 | mServiceContext.getOpPackageName()); |
| 1593 | |
| 1594 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1595 | expectMobileDefaults(); |
| 1596 | |
| 1597 | mService.updateNetworks(); |
| 1598 | |
| 1599 | // We have 1440MB and 24 days left, which is 60MB/day; assuming 10% |
| 1600 | // for quota split equally between two types gives 3MB. |
| 1601 | assertEquals(DataUnit.MEGABYTES.toBytes(3), |
| 1602 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_JOBS)); |
| 1603 | assertEquals(DataUnit.MEGABYTES.toBytes(3), |
| 1604 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_MULTIPATH)); |
| 1605 | } |
| 1606 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1607 | // Limited data plan, over quota |
| 1608 | { |
| 1609 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1610 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), |
| 1611 | DataUnit.MEGABYTES.toBytes(100)); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1612 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan}, |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1613 | mServiceContext.getOpPackageName()); |
| 1614 | |
| 1615 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1616 | expectMobileDefaults(); |
| 1617 | |
| 1618 | mService.updateNetworks(); |
| 1619 | |
| 1620 | assertEquals(0L, internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_JOBS)); |
| 1621 | assertEquals(0L, internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_MULTIPATH)); |
| 1622 | } |
| 1623 | |
| 1624 | // Roaming |
| 1625 | { |
| 1626 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1627 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), BYTES_UNLIMITED); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1628 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan}, |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1629 | mServiceContext.getOpPackageName()); |
| 1630 | |
| 1631 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1632 | expectMobileDefaults(); |
| 1633 | expectNetworkState(true /* roaming */); |
| 1634 | |
| 1635 | mService.updateNetworks(); |
| 1636 | |
| 1637 | assertEquals(0L, internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_JOBS)); |
| 1638 | assertEquals(0L, internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_MULTIPATH)); |
| 1639 | } |
| 1640 | |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1641 | // Unlimited data plan |
| 1642 | { |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1643 | final SubscriptionPlan plan = buildMonthlyDataPlan( |
| 1644 | ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), BYTES_UNLIMITED); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1645 | setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan}, |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1646 | mServiceContext.getOpPackageName()); |
| 1647 | |
| 1648 | reset(mTelephonyManager, mNetworkManager, mNotifManager); |
| 1649 | expectMobileDefaults(); |
| 1650 | |
| 1651 | mService.updateNetworks(); |
| 1652 | |
| 1653 | // 20MB/day, split equally between two types gives 10MB. |
| 1654 | assertEquals(DataUnit.MEBIBYTES.toBytes(10), |
| 1655 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_JOBS)); |
| 1656 | assertEquals(DataUnit.MEBIBYTES.toBytes(10), |
| 1657 | internal.getSubscriptionOpportunisticQuota(net, QUOTA_TYPE_MULTIPATH)); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1658 | |
| 1659 | // Capabilities change to roaming |
| 1660 | final ConnectivityManager.NetworkCallback callback = mNetworkCallbackCaptor.getValue(); |
| 1661 | assertNotNull(callback); |
| 1662 | expectNetworkState(true /* roaming */); |
| 1663 | callback.onCapabilitiesChanged( |
| 1664 | new Network(TEST_NET_ID), |
| 1665 | buildNetworkCapabilities(TEST_SUB_ID, true /* roaming */)); |
| 1666 | |
| 1667 | assertEquals(0, internal.getSubscriptionOpportunisticQuota( |
| 1668 | new Network(TEST_NET_ID), NetworkPolicyManagerInternal.QUOTA_TYPE_MULTIPATH)); |
Jeff Sharkey | 36b414b | 2018-03-30 11:00:03 -0600 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
Annie Meng | 20b4d84 | 2018-05-18 15:00:49 +0100 | [diff] [blame] | 1672 | /** |
| 1673 | * Test that policy set of {null, NetworkPolicy, null} does not crash and restores the valid |
| 1674 | * NetworkPolicy. |
| 1675 | */ |
| 1676 | @Test |
| 1677 | public void testSetNetworkPolicies_withNullPolicies_doesNotThrow() { |
| 1678 | NetworkPolicy[] policies = new NetworkPolicy[3]; |
| 1679 | policies[1] = buildDefaultFakeMobilePolicy(); |
| 1680 | setNetworkPolicies(policies); |
| 1681 | |
| 1682 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1683 | true); |
| 1684 | } |
| 1685 | |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 1686 | /** |
| 1687 | * Exhaustively test isUidNetworkingBlocked to output the expected results based on external |
| 1688 | * conditions. |
| 1689 | */ |
| 1690 | @Test |
| 1691 | public void testIsUidNetworkingBlocked() { |
| 1692 | final ArrayList<Pair<Boolean, Integer>> expectedBlockedStates = new ArrayList<>(); |
| 1693 | |
| 1694 | // Metered network. Data saver on. |
| 1695 | expectedBlockedStates.add(new Pair<>(true, RULE_NONE)); |
| 1696 | expectedBlockedStates.add(new Pair<>(false, RULE_ALLOW_METERED)); |
| 1697 | expectedBlockedStates.add(new Pair<>(false, RULE_TEMPORARY_ALLOW_METERED)); |
| 1698 | expectedBlockedStates.add(new Pair<>(true, RULE_REJECT_METERED)); |
| 1699 | expectedBlockedStates.add(new Pair<>(true, RULE_ALLOW_ALL)); |
| 1700 | expectedBlockedStates.add(new Pair<>(true, RULE_REJECT_ALL)); |
| 1701 | verifyNetworkBlockedState( |
| 1702 | true /* metered */, true /* backgroundRestricted */, expectedBlockedStates); |
| 1703 | expectedBlockedStates.clear(); |
| 1704 | |
| 1705 | // Metered network. Data saver off. |
| 1706 | expectedBlockedStates.add(new Pair<>(false, RULE_NONE)); |
| 1707 | expectedBlockedStates.add(new Pair<>(false, RULE_ALLOW_METERED)); |
| 1708 | expectedBlockedStates.add(new Pair<>(false, RULE_TEMPORARY_ALLOW_METERED)); |
| 1709 | expectedBlockedStates.add(new Pair<>(true, RULE_REJECT_METERED)); |
| 1710 | expectedBlockedStates.add(new Pair<>(false, RULE_ALLOW_ALL)); |
| 1711 | expectedBlockedStates.add(new Pair<>(true, RULE_REJECT_ALL)); |
| 1712 | verifyNetworkBlockedState( |
| 1713 | true /* metered */, false /* backgroundRestricted */, expectedBlockedStates); |
| 1714 | expectedBlockedStates.clear(); |
| 1715 | |
| 1716 | // Non-metered network. Data saver on. |
| 1717 | expectedBlockedStates.add(new Pair<>(false, RULE_NONE)); |
| 1718 | expectedBlockedStates.add(new Pair<>(false, RULE_ALLOW_METERED)); |
| 1719 | expectedBlockedStates.add(new Pair<>(false, RULE_TEMPORARY_ALLOW_METERED)); |
| 1720 | expectedBlockedStates.add(new Pair<>(false, RULE_REJECT_METERED)); |
| 1721 | expectedBlockedStates.add(new Pair<>(false, RULE_ALLOW_ALL)); |
| 1722 | expectedBlockedStates.add(new Pair<>(true, RULE_REJECT_ALL)); |
| 1723 | verifyNetworkBlockedState( |
| 1724 | false /* metered */, true /* backgroundRestricted */, expectedBlockedStates); |
| 1725 | |
| 1726 | // Non-metered network. Data saver off. The result is the same as previous case since |
| 1727 | // the network is blocked only for RULE_REJECT_ALL regardless of data saver. |
| 1728 | verifyNetworkBlockedState( |
| 1729 | false /* metered */, false /* backgroundRestricted */, expectedBlockedStates); |
| 1730 | expectedBlockedStates.clear(); |
| 1731 | } |
| 1732 | |
| 1733 | private void verifyNetworkBlockedState(boolean metered, boolean backgroundRestricted, |
| 1734 | ArrayList<Pair<Boolean, Integer>> expectedBlockedStateForRules) { |
| 1735 | final NetworkPolicyManagerInternal npmi = LocalServices |
| 1736 | .getService(NetworkPolicyManagerInternal.class); |
| 1737 | |
| 1738 | for (Pair<Boolean, Integer> pair : expectedBlockedStateForRules) { |
| 1739 | final boolean expectedResult = pair.first; |
| 1740 | final int rule = pair.second; |
| 1741 | assertEquals(formatBlockedStateError(UID_A, rule, metered, backgroundRestricted), |
| 1742 | expectedResult, |
| 1743 | npmi.isUidNetworkingBlocked(UID_A, rule, metered, backgroundRestricted)); |
| 1744 | assertFalse(formatBlockedStateError(SYSTEM_UID, rule, metered, backgroundRestricted), |
| 1745 | npmi.isUidNetworkingBlocked(SYSTEM_UID, rule, metered, backgroundRestricted)); |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | private String formatBlockedStateError(int uid, int rule, boolean metered, |
| 1750 | boolean backgroundRestricted) { |
| 1751 | return String.format( |
| 1752 | "Unexpected BlockedState: (uid=%d, rule=%s, metered=%b, backgroundRestricted=%b)", |
| 1753 | uid, uidRulesToString(rule), metered, backgroundRestricted); |
| 1754 | } |
| 1755 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1756 | private SubscriptionPlan buildMonthlyDataPlan(ZonedDateTime start, long limitBytes) { |
| 1757 | return SubscriptionPlan.Builder |
| 1758 | .createRecurringMonthly(start) |
| 1759 | .setDataLimit(limitBytes, LIMIT_BEHAVIOR_DISABLED) |
| 1760 | .build(); |
| 1761 | } |
| 1762 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 1763 | private ApplicationInfo buildApplicationInfo(String label) { |
| 1764 | final ApplicationInfo ai = new ApplicationInfo(); |
| 1765 | ai.nonLocalizedLabel = label; |
| 1766 | return ai; |
| 1767 | } |
| 1768 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1769 | private NetworkInfo buildNetworkInfo() { |
| 1770 | final NetworkInfo ni = new NetworkInfo(ConnectivityManager.TYPE_MOBILE, |
| 1771 | TelephonyManager.NETWORK_TYPE_LTE, null, null); |
| 1772 | ni.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); |
| 1773 | return ni; |
| 1774 | } |
| 1775 | |
| 1776 | private LinkProperties buildLinkProperties(String iface) { |
| 1777 | final LinkProperties lp = new LinkProperties(); |
| 1778 | lp.setInterfaceName(iface); |
| 1779 | return lp; |
| 1780 | } |
| 1781 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1782 | private NetworkCapabilities buildNetworkCapabilities(int subId, boolean roaming) { |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1783 | final NetworkCapabilities nc = new NetworkCapabilities(); |
| 1784 | nc.addTransportType(TRANSPORT_CELLULAR); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1785 | if (!roaming) { |
| 1786 | nc.addCapability(NET_CAPABILITY_NOT_ROAMING); |
| 1787 | } |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1788 | nc.setNetworkSpecifier(new StringNetworkSpecifier(String.valueOf(subId))); |
| 1789 | return nc; |
| 1790 | } |
| 1791 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1792 | private NetworkPolicy buildDefaultFakeMobilePolicy() { |
| 1793 | NetworkPolicy p = mService.buildDefaultMobilePolicy(FAKE_SUB_ID, FAKE_SUBSCRIBER_ID); |
| 1794 | // set a deterministic cycle date |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1795 | p.cycleRule = new RecurrenceRule( |
| 1796 | p.cycleRule.start.withDayOfMonth(DEFAULT_CYCLE_DAY), |
| 1797 | p.cycleRule.end, Period.ofMonths(1)); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1798 | return p; |
| 1799 | } |
| 1800 | |
| 1801 | private static NetworkPolicy buildFakeMobilePolicy(int cycleDay, long warningBytes, |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1802 | long limitBytes, boolean inferred) { |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1803 | final NetworkTemplate template = buildTemplateMobileAll(FAKE_SUBSCRIBER_ID); |
Neil Fuller | d5f0a39 | 2019-06-17 19:59:51 +0100 | [diff] [blame] | 1804 | return new NetworkPolicy(template, cycleDay, TimeZone.getDefault().getID(), warningBytes, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1805 | limitBytes, SNOOZE_NEVER, SNOOZE_NEVER, true, inferred); |
| 1806 | } |
| 1807 | |
| 1808 | private void assertNetworkPolicyEquals(int expectedCycleDay, long expectedWarningBytes, |
| 1809 | long expectedLimitBytes, boolean expectedInferred) { |
| 1810 | NetworkPolicy[] policies = mService.getNetworkPolicies( |
| 1811 | mServiceContext.getOpPackageName()); |
| 1812 | assertEquals("Unexpected number of network policies", 1, policies.length); |
| 1813 | NetworkPolicy actualPolicy = policies[0]; |
| 1814 | NetworkPolicy expectedPolicy = buildFakeMobilePolicy(expectedCycleDay, expectedWarningBytes, |
| 1815 | expectedLimitBytes, expectedInferred); |
| 1816 | assertEquals(expectedPolicy, actualPolicy); |
| 1817 | } |
| 1818 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1819 | private static long parseTime(String time) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1820 | return ZonedDateTime.parse(time).toInstant().toEpochMilli(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 1823 | private void setNetworkPolicies(NetworkPolicy... policies) { |
| 1824 | mService.setNetworkPolicies(policies); |
| 1825 | } |
| 1826 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1827 | private static NetworkState buildWifi() { |
| 1828 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 1829 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 1830 | final LinkProperties prop = new LinkProperties(); |
| 1831 | prop.setInterfaceName(TEST_IFACE); |
Stephen Chen | a1c92b7 | 2017-02-06 13:18:11 -0800 | [diff] [blame] | 1832 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 1833 | return new NetworkState(info, prop, networkCapabilities, null, null, TEST_SSID); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1836 | private void expectHasInternetPermission(int uid, boolean hasIt) throws Exception { |
| 1837 | when(mIpm.checkUidPermission(Manifest.permission.INTERNET, uid)).thenReturn( |
| 1838 | hasIt ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED); |
| 1839 | } |
| 1840 | |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1841 | private void expectNetworkState(boolean roaming) throws Exception { |
Jeff Sharkey | 0a5570d | 2018-04-10 12:38:29 -0600 | [diff] [blame] | 1842 | when(mCarrierConfigManager.getConfigForSubId(eq(TEST_SUB_ID))) |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 1843 | .thenReturn(mCarrierConfig); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1844 | when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[] { |
| 1845 | new NetworkState(buildNetworkInfo(), |
| 1846 | buildLinkProperties(TEST_IFACE), |
| 1847 | buildNetworkCapabilities(TEST_SUB_ID, roaming), |
| 1848 | new Network(TEST_NET_ID), TEST_IMSI, null) |
| 1849 | }); |
| 1850 | } |
| 1851 | |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 1852 | private void expectDefaultCarrierConfig() throws Exception { |
| 1853 | when(mCarrierConfigManager.getConfigForSubId(eq(TEST_SUB_ID))) |
| 1854 | .thenReturn(CarrierConfigManager.getDefaultConfig()); |
| 1855 | } |
| 1856 | |
Jeff Sharkey | bfb43ea | 2018-02-03 12:08:16 -0700 | [diff] [blame] | 1857 | private void expectMobileDefaults() throws Exception { |
| 1858 | when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn( |
| 1859 | new int[] { TEST_SUB_ID }); |
| 1860 | when(mTelephonyManager.getSubscriberId(TEST_SUB_ID)).thenReturn(TEST_IMSI); |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1861 | when(mTelephonyManager.createForSubscriptionId(TEST_SUB_ID)) |
| 1862 | .thenReturn(mock(TelephonyManager.class)); |
Ammar Aijazi | b0941aa | 2019-04-26 18:39:50 -0700 | [diff] [blame] | 1863 | doNothing().when(mTelephonyManager).setPolicyDataEnabled(anyBoolean(), anyInt()); |
Remi NGUYEN VAN | ed6d2ca | 2018-04-04 11:12:51 +0900 | [diff] [blame] | 1864 | expectNetworkState(false /* roaming */); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1867 | private void verifyAdvisePersistThreshold() throws Exception { |
| 1868 | verify(mStatsService).advisePersistThreshold(anyLong()); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1871 | private static class TestAbstractFuture<T> extends AbstractFuture<T> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1872 | @Override |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1873 | public T get() throws InterruptedException, ExecutionException { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1874 | try { |
| 1875 | return get(5, TimeUnit.SECONDS); |
| 1876 | } catch (TimeoutException e) { |
| 1877 | throw new RuntimeException(e); |
| 1878 | } |
| 1879 | } |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1880 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1881 | |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1882 | private static void assertTimeEquals(long expected, long actual) { |
| 1883 | if (expected != actual) { |
| 1884 | fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual)); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | private static String formatTime(long millis) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1889 | return Instant.ofEpochMilli(millis) + " [" + millis + "]"; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) { |
| 1893 | final long low = expected - fuzzy; |
| 1894 | final long high = expected + fuzzy; |
| 1895 | if (actual < low || actual > high) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1896 | fail("value " + formatTime(actual) + " is outside [" + formatTime(low) + "," |
| 1897 | + formatTime(high) + "]"); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | private static void assertUnique(LinkedHashSet<Long> seen, Long value) { |
| 1902 | if (!seen.add(value)) { |
| 1903 | fail("found duplicate time " + value + " in series " + seen.toString()); |
| 1904 | } |
| 1905 | } |
| 1906 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1907 | private static void assertNotificationType(int expected, String actualTag) { |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1908 | assertEquals("notification type mismatch for '" + actualTag + "'", |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1909 | Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1)); |
| 1910 | } |
| 1911 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1912 | private void assertUidPolicy(int uid, int expected) { |
| 1913 | final int actual = mService.getUidPolicy(uid); |
| 1914 | if (expected != actual) { |
| 1915 | fail("Wrong policy for UID " + uid + ": expected " + uidPoliciesToString(expected) |
| 1916 | + ", actual " + uidPoliciesToString(actual)); |
| 1917 | } |
| 1918 | } |
| 1919 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1920 | private void assertWhitelistUids(int... uids) { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 1921 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND), uids); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1924 | private void assertRestrictBackgroundOn() throws Exception { |
| 1925 | assertTrue("restrictBackground should be set", mService.getRestrictBackground()); |
| 1926 | } |
| 1927 | |
| 1928 | private void assertRestrictBackgroundOff() throws Exception { |
| 1929 | assertFalse("restrictBackground should not be set", mService.getRestrictBackground()); |
| 1930 | } |
| 1931 | |
| 1932 | private FutureIntent newRestrictBackgroundChangedFuture() { |
| 1933 | return mServiceContext |
| 1934 | .nextBroadcastIntent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 1935 | } |
| 1936 | |
| 1937 | private void assertRestrictBackgroundChangedReceived(Future<Intent> future, |
| 1938 | String expectedPackage) throws Exception { |
| 1939 | final String action = ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; |
| 1940 | final Intent intent = future.get(5, TimeUnit.SECONDS); |
| 1941 | assertNotNull("Didn't get a " + action + "intent in 5 seconds"); |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1942 | assertEquals("Wrong package on " + action + " intent", |
| 1943 | expectedPackage, intent.getPackage()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1946 | // TODO: replace by Truth, Hamcrest, or a similar tool. |
| 1947 | private void assertContainsInAnyOrder(int[] actual, int...expected) { |
| 1948 | final StringBuilder errors = new StringBuilder(); |
| 1949 | if (actual.length != expected.length) { |
| 1950 | errors.append("\tsize does not match\n"); |
| 1951 | } |
| 1952 | final List<Integer> actualList = |
| 1953 | Arrays.stream(actual).boxed().collect(Collectors.<Integer>toList()); |
| 1954 | final List<Integer> expectedList = |
| 1955 | Arrays.stream(expected).boxed().collect(Collectors.<Integer>toList()); |
| 1956 | if (!actualList.containsAll(expectedList)) { |
| 1957 | errors.append("\tmissing elements on actual list\n"); |
| 1958 | } |
| 1959 | if (!expectedList.containsAll(actualList)) { |
| 1960 | errors.append("\tmissing elements on expected list\n"); |
| 1961 | } |
| 1962 | if (errors.length() > 0) { |
| 1963 | fail("assertContainsInAnyOrder(expected=" + Arrays.toString(expected) |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 1964 | + ", actual=" + Arrays.toString(actual) + ") failed: \n" + errors); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1965 | } |
| 1966 | } |
| 1967 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1968 | private long getElapsedRealtime() { |
| 1969 | return mElapsedRealtime; |
| 1970 | } |
| 1971 | |
| 1972 | private void setCurrentTimeMillis(long currentTimeMillis) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1973 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTimeMillis), |
| 1974 | ZoneId.systemDefault()); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1975 | mStartTime = currentTimeMillis; |
| 1976 | mElapsedRealtime = 0L; |
| 1977 | } |
| 1978 | |
| 1979 | private long currentTimeMillis() { |
| 1980 | return mStartTime + mElapsedRealtime; |
| 1981 | } |
| 1982 | |
| 1983 | private void incrementCurrentTime(long duration) { |
| 1984 | mElapsedRealtime += duration; |
| 1985 | } |
| 1986 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1987 | private FutureIntent mRestrictBackgroundChanged; |
| 1988 | |
Sudheer Shanka | 2d1ab90 | 2019-10-12 15:01:44 -0700 | [diff] [blame] | 1989 | private void postMsgAndWaitForCompletion() throws InterruptedException { |
| 1990 | final Handler handler = mService.getHandlerForTesting(); |
| 1991 | final CountDownLatch latch = new CountDownLatch(1); |
| 1992 | mService.getHandlerForTesting().post(latch::countDown); |
| 1993 | if (!latch.await(5, TimeUnit.SECONDS)) { |
| 1994 | fail("Timed out waiting for the test msg to be handled"); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | private void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage) |
| 1999 | throws InterruptedException { |
| 2000 | mService.setSubscriptionPlans(subId, plans, callingPackage); |
| 2001 | // setSubscriptionPlans() triggers async events, wait for those to be completed before |
| 2002 | // moving forward as they could interfere with the tests later. |
| 2003 | postMsgAndWaitForCompletion(); |
| 2004 | } |
| 2005 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 2006 | private void setRestrictBackground(boolean flag) throws Exception { |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 2007 | mService.setRestrictBackground(flag); |
| 2008 | // Sanity check. |
| 2009 | assertEquals("restrictBackground not set", flag, mService.getRestrictBackground()); |
| 2010 | } |
| 2011 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2012 | /** |
| 2013 | * Creates a mock and registers it to {@link LocalServices}. |
| 2014 | */ |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 2015 | private static <T> T addLocalServiceMock(Class<T> clazz) { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2016 | final T mock = mock(clazz); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2017 | LocalServices.addService(clazz, mock); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2018 | return mock; |
| 2019 | } |
| 2020 | |
| 2021 | /** |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2022 | * Custom Mockito answer used to verify async {@link INetworkPolicyListener} calls. |
| 2023 | * |
| 2024 | * <p>Typical usage: |
| 2025 | * <pre><code> |
| 2026 | * mPolicyListener.expect().someCallback(any()); |
| 2027 | * // do something on objects under test |
| 2028 | * mPolicyListener.waitAndVerify().someCallback(eq(expectedValue)); |
| 2029 | * </code></pre> |
| 2030 | */ |
| 2031 | final class NetworkPolicyListenerAnswer implements Answer<Void> { |
| 2032 | private CountDownLatch latch; |
| 2033 | private final INetworkPolicyListener listener; |
| 2034 | |
| 2035 | NetworkPolicyListenerAnswer(NetworkPolicyManagerService service) { |
| 2036 | this.listener = mock(INetworkPolicyListener.class); |
| 2037 | // RemoteCallbackList needs a binder to use as key |
| 2038 | when(listener.asBinder()).thenReturn(new Binder()); |
| 2039 | service.registerListener(listener); |
| 2040 | } |
| 2041 | |
| 2042 | @Override |
| 2043 | public Void answer(InvocationOnMock invocation) throws Throwable { |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 2044 | Log.d(TAG, "counting down on answer: " + invocation); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 2045 | latch.countDown(); |
| 2046 | return null; |
| 2047 | } |
| 2048 | |
| 2049 | INetworkPolicyListener expect() { |
| 2050 | assertNull("expect() called before waitAndVerify()", latch); |
| 2051 | latch = new CountDownLatch(1); |
| 2052 | return doAnswer(this).when(listener); |
| 2053 | } |
| 2054 | |
| 2055 | INetworkPolicyListener waitAndVerify() { |
| 2056 | assertNotNull("waitAndVerify() called before expect()", latch); |
| 2057 | try { |
| 2058 | assertTrue("callback not called in 5 seconds", latch.await(5, TimeUnit.SECONDS)); |
| 2059 | } catch (InterruptedException e) { |
| 2060 | fail("Thread interrupted before callback called"); |
| 2061 | } finally { |
| 2062 | latch = null; |
| 2063 | } |
| 2064 | return verify(listener, atLeastOnce()); |
| 2065 | } |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 2066 | |
| 2067 | INetworkPolicyListener verifyNotCalled() { |
| 2068 | return verify(listener, never()); |
| 2069 | } |
| 2070 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2071 | } |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 2072 | |
| 2073 | private void setNetpolicyXml(Context context) throws Exception { |
| 2074 | mPolicyDir = context.getFilesDir(); |
| 2075 | if (mPolicyDir.exists()) { |
| 2076 | IoUtils.deleteContents(mPolicyDir); |
| 2077 | } |
| 2078 | if (!TextUtils.isEmpty(mNetpolicyXml)) { |
| 2079 | final String assetPath = NETPOLICY_DIR + "/" + mNetpolicyXml; |
| 2080 | final File netConfigFile = new File(mPolicyDir, "netpolicy.xml"); |
| 2081 | Log.d(TAG, "Creating " + netConfigFile + " from asset " + assetPath); |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 2082 | try (InputStream in = context.getResources().getAssets().open(assetPath); |
| 2083 | OutputStream out = new FileOutputStream(netConfigFile)) { |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 2084 | Streams.copy(in, out); |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * Annotation used to define the relative path of the {@code netpolicy.xml} file. |
| 2091 | */ |
| 2092 | @Retention(RetentionPolicy.RUNTIME) |
| 2093 | @Target(ElementType.METHOD) |
| 2094 | public @interface NetPolicyXml { |
Sudheer Shanka | 8ab2299 | 2018-11-29 00:08:58 -0800 | [diff] [blame] | 2095 | String value() default ""; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * Rule used to set {@code mNetPolicyXml} according to the {@link NetPolicyXml} annotation. |
| 2100 | */ |
| 2101 | public static class NetPolicyMethodRule implements MethodRule { |
| 2102 | |
| 2103 | @Override |
| 2104 | public Statement apply(Statement base, FrameworkMethod method, Object target) { |
| 2105 | for (Annotation annotation : method.getAnnotations()) { |
| 2106 | if ((annotation instanceof NetPolicyXml)) { |
| 2107 | final String path = ((NetPolicyXml) annotation).value(); |
| 2108 | if (!path.isEmpty()) { |
| 2109 | ((NetworkPolicyManagerServiceTest) target).mNetpolicyXml = path; |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | return base; |
| 2115 | } |
| 2116 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 2117 | } |