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 | |
| 17 | package com.android.server; |
| 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; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 21 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 22 | import static android.net.NetworkPolicy.SNOOZE_NEVER; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 27 | import static android.net.NetworkPolicyManager.uidPoliciesToString; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 28 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 29 | import static android.net.TrafficStats.KB_IN_BYTES; |
| 30 | import static android.net.TrafficStats.MB_IN_BYTES; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 31 | import static android.telephony.CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 32 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_THRESHOLD_DISABLED; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 33 | import static android.telephony.CarrierConfigManager.DATA_CYCLE_USE_PLATFORM_DEFAULT; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 34 | import static android.telephony.CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG; |
| 35 | import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG; |
| 36 | import static android.telephony.CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 37 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 38 | import static android.text.format.Time.TIMEZONE_UTC; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 39 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 40 | import static com.android.server.net.NetworkPolicyManagerService.MAX_PROC_STATE_SEQ_HISTORY; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 41 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT; |
| 42 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED; |
| 43 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 44 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 45 | import static com.google.common.truth.Truth.assertThat; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 46 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertFalse; |
| 49 | import static org.junit.Assert.assertNotNull; |
| 50 | import static org.junit.Assert.assertNull; |
| 51 | import static org.junit.Assert.assertTrue; |
| 52 | import static org.junit.Assert.fail; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 53 | import static org.mockito.ArgumentMatchers.any; |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 54 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 55 | import static org.mockito.ArgumentMatchers.anyInt; |
| 56 | import static org.mockito.ArgumentMatchers.anyLong; |
| 57 | import static org.mockito.ArgumentMatchers.anyString; |
| 58 | import static org.mockito.ArgumentMatchers.eq; |
| 59 | import static org.mockito.ArgumentMatchers.isA; |
| 60 | import static org.mockito.ArgumentMatchers.isNull; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 61 | import static org.mockito.Mockito.atLeast; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 62 | import static org.mockito.Mockito.atLeastOnce; |
| 63 | import static org.mockito.Mockito.doAnswer; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 64 | import static org.mockito.Mockito.doReturn; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 65 | import static org.mockito.Mockito.mock; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 66 | import static org.mockito.Mockito.never; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 67 | import static org.mockito.Mockito.verify; |
| 68 | import static org.mockito.Mockito.when; |
| 69 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 70 | import android.Manifest; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 71 | import android.app.ActivityManager; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 72 | import android.app.ActivityManagerInternal; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 73 | import android.app.IActivityManager; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 74 | import android.app.INotificationManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 75 | import android.app.IUidObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 76 | import android.app.Notification; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 77 | import android.app.usage.UsageStatsManagerInternal; |
| 78 | import android.content.Context; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 79 | import android.content.Intent; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 80 | import android.content.pm.ApplicationInfo; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 81 | import android.content.pm.IPackageManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 82 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 83 | import android.content.pm.PackageManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 84 | import android.content.pm.Signature; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 85 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 86 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 87 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 88 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 89 | import android.net.INetworkStatsService; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 90 | import android.net.LinkProperties; |
Stephen Chen | a1c92b7 | 2017-02-06 13:18:11 -0800 | [diff] [blame] | 91 | import android.net.NetworkCapabilities; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 92 | import android.net.NetworkInfo; |
| 93 | import android.net.NetworkInfo.DetailedState; |
| 94 | import android.net.NetworkPolicy; |
| 95 | import android.net.NetworkState; |
| 96 | import android.net.NetworkStats; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 97 | import android.net.NetworkTemplate; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 98 | import android.os.Binder; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 99 | import android.os.INetworkManagementService; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 100 | import android.os.PersistableBundle; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 101 | import android.os.PowerManagerInternal; |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 102 | import android.os.PowerSaveState; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 103 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 104 | import android.os.UserHandle; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 105 | import android.support.test.InstrumentationRegistry; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 106 | import android.support.test.filters.MediumTest; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 107 | import android.support.test.runner.AndroidJUnit4; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 108 | import android.telephony.CarrierConfigManager; |
| 109 | import android.telephony.SubscriptionManager; |
| 110 | import android.telephony.TelephonyManager; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 111 | import android.text.TextUtils; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 112 | import android.text.format.Time; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 113 | import android.util.Log; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 114 | import android.util.Pair; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 115 | import android.util.RecurrenceRule; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 116 | import android.util.TrustedTime; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 117 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 118 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 119 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 281a17c | 2016-10-28 12:56:03 +0900 | [diff] [blame] | 120 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 121 | import com.android.internal.util.test.BroadcastInterceptingContext.FutureIntent; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 122 | import com.android.server.net.NetworkPolicyManagerInternal; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 123 | import com.android.server.net.NetworkPolicyManagerService; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 124 | import com.android.server.net.NetworkPolicyManagerService.ProcStateSeqHistory; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 125 | |
| 126 | import libcore.io.IoUtils; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 127 | import libcore.io.Streams; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 128 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 129 | import com.google.common.util.concurrent.AbstractFuture; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 130 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 131 | import org.junit.After; |
| 132 | import org.junit.Before; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 133 | import org.junit.Rule; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 134 | import org.junit.Test; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 135 | import org.junit.rules.MethodRule; |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 136 | import org.junit.runner.RunWith; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 137 | import org.junit.runners.model.FrameworkMethod; |
| 138 | import org.junit.runners.model.Statement; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 139 | import org.mockito.ArgumentCaptor; |
| 140 | import org.mockito.Mock; |
| 141 | import org.mockito.MockitoAnnotations; |
| 142 | import org.mockito.invocation.InvocationOnMock; |
| 143 | import org.mockito.stubbing.Answer; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 144 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 145 | import java.io.ByteArrayOutputStream; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 146 | import java.io.File; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 147 | import java.io.FileOutputStream; |
| 148 | import java.io.InputStream; |
| 149 | import java.io.OutputStream; |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 150 | import java.io.PrintWriter; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 151 | import java.lang.annotation.Annotation; |
| 152 | import java.lang.annotation.ElementType; |
| 153 | import java.lang.annotation.Retention; |
| 154 | import java.lang.annotation.RetentionPolicy; |
| 155 | import java.lang.annotation.Target; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 156 | import java.time.Clock; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 157 | import java.time.Instant; |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 158 | import java.time.Period; |
| 159 | import java.time.ZoneId; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 160 | import java.time.ZonedDateTime; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 161 | import java.util.Arrays; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 162 | import java.util.Calendar; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 163 | import java.util.Iterator; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 164 | import java.util.LinkedHashSet; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 165 | import java.util.List; |
| 166 | import java.util.concurrent.CountDownLatch; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 167 | import java.util.concurrent.ExecutionException; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 168 | import java.util.concurrent.Future; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 169 | import java.util.concurrent.TimeUnit; |
| 170 | import java.util.concurrent.TimeoutException; |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 171 | import java.util.stream.Collectors; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 172 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Tests for {@link NetworkPolicyManagerService}. |
Felipe Leme | b8d572e | 2016-12-05 13:41:43 -0800 | [diff] [blame] | 175 | * |
| 176 | * <p>Typical usage: |
| 177 | * |
| 178 | * <pre><code> |
| 179 | m -j32 FrameworksServicesTests && adb install -r -g \ |
| 180 | ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && \ |
| 181 | adb shell am instrument -e class "com.android.server.NetworkPolicyManagerServiceTest" -w \ |
| 182 | "com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner" |
| 183 | * </code></pre> |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 184 | */ |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 185 | @RunWith(AndroidJUnit4.class) |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 186 | @MediumTest |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 187 | public class NetworkPolicyManagerServiceTest { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 188 | private static final String TAG = "NetworkPolicyManagerServiceTest"; |
| 189 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 190 | private static final long TEST_START = 1194220800000L; |
| 191 | private static final String TEST_IFACE = "test0"; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 192 | private static final String TEST_SSID = "AndroidAP"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 193 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 194 | private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
| 195 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 196 | private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 197 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Path on assets where files used by {@link NetPolicyXml} are located. |
| 200 | */ |
| 201 | private static final String NETPOLICY_DIR = "NetworkPolicyManagerServiceTest/netpolicy"; |
| 202 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 203 | private BroadcastInterceptingContext mServiceContext; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 204 | private File mPolicyDir; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 205 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Relative path of the XML file that will be used as {@code netpolicy.xml}. |
| 208 | * |
| 209 | * <p>Typically set through a {@link NetPolicyXml} annotation in the test method. |
| 210 | */ |
| 211 | private String mNetpolicyXml; |
| 212 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 213 | private @Mock IActivityManager mActivityManager; |
| 214 | private @Mock INetworkStatsService mStatsService; |
| 215 | private @Mock INetworkManagementService mNetworkManager; |
| 216 | private @Mock TrustedTime mTime; |
| 217 | private @Mock IConnectivityManager mConnManager; |
| 218 | private @Mock INotificationManager mNotifManager; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 219 | private @Mock PackageManager mPackageManager; |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 220 | private @Mock IPackageManager mIpm; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 221 | private @Mock SubscriptionManager mSubscriptionManager; |
| 222 | private @Mock CarrierConfigManager mCarrierConfigManager; |
| 223 | private @Mock TelephonyManager mTelephonyManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 224 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 225 | private static ActivityManagerInternal mActivityManagerInternal; |
| 226 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 227 | private IUidObserver mUidObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 228 | private INetworkManagementEventObserver mNetworkObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 229 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 230 | private NetworkPolicyListenerAnswer mPolicyListener; |
| 231 | private NetworkPolicyManagerService mService; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 232 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 233 | /** |
| 234 | * In some of the tests while initializing NetworkPolicyManagerService, |
| 235 | * ACTION_RESTRICT_BACKGROUND_CHANGED is broadcasted. This is for capturing that broadcast. |
| 236 | */ |
| 237 | private FutureIntent mFutureIntent; |
| 238 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 239 | private long mStartTime; |
| 240 | private long mElapsedRealtime; |
| 241 | |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 242 | private static final int USER_ID = 0; |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 243 | private static final int FAKE_SUB_ID = 3737373; |
| 244 | private static final String FAKE_SUBSCRIBER_ID = "FAKE_SUB_ID"; |
| 245 | private static final int DEFAULT_CYCLE_DAY = 1; |
| 246 | private static final int INVALID_CARRIER_CONFIG_VALUE = -9999; |
| 247 | private long mDefaultWarningBytes; // filled in with the actual default before tests are run |
| 248 | private long mDefaultLimitBytes; // filled in with the actual default before tests are run |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 249 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 250 | private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 4; |
| 251 | private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 8; |
| 252 | private static final int APP_ID_C = android.os.Process.FIRST_APPLICATION_UID + 15; |
| 253 | private static final int APP_ID_D = android.os.Process.FIRST_APPLICATION_UID + 16; |
| 254 | private static final int APP_ID_E = android.os.Process.FIRST_APPLICATION_UID + 23; |
| 255 | 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] | 256 | |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 257 | private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A); |
| 258 | 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] | 259 | private static final int UID_C = UserHandle.getUid(USER_ID, APP_ID_C); |
| 260 | private static final int UID_D = UserHandle.getUid(USER_ID, APP_ID_D); |
| 261 | private static final int UID_E = UserHandle.getUid(USER_ID, APP_ID_E); |
| 262 | 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] | 263 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 264 | private static final String PKG_NAME_A = "name.is.A,pkg.A"; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 265 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 266 | public final @Rule NetPolicyMethodRule mNetPolicyXmlRule = new NetPolicyMethodRule(); |
| 267 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 268 | private void registerLocalServices() { |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 269 | addLocalServiceMock(DeviceIdleController.LocalService.class); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 270 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 271 | final UsageStatsManagerInternal usageStats = |
| 272 | addLocalServiceMock(UsageStatsManagerInternal.class); |
| 273 | when(usageStats.getIdleUidsForUser(anyInt())).thenReturn(new int[]{}); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 274 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 275 | mActivityManagerInternal = addLocalServiceMock(ActivityManagerInternal.class); |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 276 | |
| 277 | final PowerSaveState state = new PowerSaveState.Builder() |
| 278 | .setBatterySaverEnabled(false).build(); |
| 279 | final PowerManagerInternal pmInternal = addLocalServiceMock(PowerManagerInternal.class); |
| 280 | when(pmInternal.getLowPowerState(anyInt())).thenReturn(state); |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 281 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 282 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 283 | @Before |
| 284 | public void callSystemReady() throws Exception { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 285 | MockitoAnnotations.initMocks(this); |
| 286 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 287 | final Context context = InstrumentationRegistry.getContext(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 288 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 289 | setCurrentTimeMillis(TEST_START); |
| 290 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 291 | registerLocalServices(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 292 | // Intercept various broadcasts, and pretend that uids have packages. |
| 293 | // Also return mock service instances for a few critical services. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 294 | mServiceContext = new BroadcastInterceptingContext(context) { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 295 | @Override |
| 296 | public PackageManager getPackageManager() { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 297 | return mPackageManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 298 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 299 | |
| 300 | @Override |
| 301 | public void startActivity(Intent intent) { |
| 302 | // ignored |
| 303 | } |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 304 | |
| 305 | @Override |
| 306 | public Object getSystemService(String name) { |
| 307 | switch (name) { |
| 308 | case Context.TELEPHONY_SUBSCRIPTION_SERVICE: |
| 309 | return mSubscriptionManager; |
| 310 | case Context.CARRIER_CONFIG_SERVICE: |
| 311 | return mCarrierConfigManager; |
| 312 | case Context.TELEPHONY_SERVICE: |
| 313 | return mTelephonyManager; |
| 314 | default: |
| 315 | return super.getSystemService(name); |
| 316 | } |
| 317 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 320 | setNetpolicyXml(context); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 321 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 322 | doAnswer(new Answer<Void>() { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 323 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 324 | @Override |
| 325 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 326 | mUidObserver = (IUidObserver) invocation.getArguments()[0]; |
| 327 | Log.d(TAG, "set mUidObserver to " + mUidObserver); |
| 328 | return null; |
| 329 | } |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 330 | }).when(mActivityManager).registerUidObserver(any(), anyInt(), |
Felipe Leme | b8d572e | 2016-12-05 13:41:43 -0800 | [diff] [blame] | 331 | eq(ActivityManager.PROCESS_STATE_UNKNOWN), isNull(String.class)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 332 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 333 | mFutureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 334 | mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService, |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 335 | mNetworkManager, mIpm, mTime, mPolicyDir, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 336 | mService.bindConnectivityManager(mConnManager); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 337 | mService.bindNotificationManager(mNotifManager); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 338 | mPolicyListener = new NetworkPolicyListenerAnswer(mService); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 339 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 340 | // Sets some common expectations. |
| 341 | when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenAnswer( |
| 342 | new Answer<PackageInfo>() { |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 343 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public PackageInfo answer(InvocationOnMock invocation) throws Throwable { |
| 346 | final String packageName = (String) invocation.getArguments()[0]; |
| 347 | final PackageInfo info = new PackageInfo(); |
| 348 | final Signature signature; |
| 349 | if ("android".equals(packageName)) { |
| 350 | signature = new Signature("F00D"); |
| 351 | } else { |
| 352 | signature = new Signature("DEAD"); |
| 353 | } |
| 354 | info.signatures = new Signature[] { |
| 355 | signature |
| 356 | }; |
| 357 | return info; |
| 358 | } |
| 359 | }); |
| 360 | when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) |
| 361 | .thenReturn(new ApplicationInfo()); |
| 362 | when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] {PKG_NAME_A}); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 363 | when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true); |
Sudheer Shanka | 543339f | 2017-07-28 15:18:07 -0700 | [diff] [blame] | 364 | when(mNetworkManager.setDataSaverModeEnabled(anyBoolean())).thenReturn(true); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 365 | expectCurrentTime(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 366 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 367 | // Prepare NPMS. |
Fyodor Kupolov | 311b9fa | 2016-12-02 16:24:35 -0800 | [diff] [blame] | 368 | mService.systemReady(mService.networkScoreAndNetworkManagementServiceReady()); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 369 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 370 | // catch INetworkManagementEventObserver during systemReady() |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 371 | final ArgumentCaptor<INetworkManagementEventObserver> networkObserver = |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 372 | ArgumentCaptor.forClass(INetworkManagementEventObserver.class); |
| 373 | verify(mNetworkManager).registerObserver(networkObserver.capture()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 374 | mNetworkObserver = networkObserver.getValue(); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 375 | |
| 376 | NetworkPolicy defaultPolicy = mService.buildDefaultMobilePolicy(0, ""); |
| 377 | mDefaultWarningBytes = defaultPolicy.warningBytes; |
| 378 | mDefaultLimitBytes = defaultPolicy.limitBytes; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 381 | @After |
| 382 | public void removeFiles() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 383 | for (File file : mPolicyDir.listFiles()) { |
| 384 | file.delete(); |
| 385 | } |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 386 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 387 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 388 | @After |
| 389 | public void unregisterLocalServices() throws Exception { |
| 390 | // Registered by NetworkPolicyManagerService's constructor. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 391 | LocalServices.removeServiceForTest(NetworkPolicyManagerInternal.class); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 392 | |
| 393 | // Added in registerLocalServices() |
| 394 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 395 | LocalServices.removeServiceForTest(PowerManagerInternal.class); |
| 396 | LocalServices.removeServiceForTest(DeviceIdleController.LocalService.class); |
| 397 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 400 | @After |
| 401 | public void resetClock() throws Exception { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 402 | RecurrenceRule.sClock = Clock.systemDefaultZone(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 403 | } |
| 404 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 405 | @Test |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 406 | public void testTurnRestrictBackgroundOn() throws Exception { |
| 407 | assertRestrictBackgroundOff(); // Sanity check. |
| 408 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 409 | setRestrictBackground(true); |
| 410 | assertRestrictBackgroundChangedReceived(futureIntent, null); |
| 411 | } |
| 412 | |
| 413 | @Test |
| 414 | @NetPolicyXml("restrict-background-on.xml") |
| 415 | public void testTurnRestrictBackgroundOff() throws Exception { |
| 416 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 417 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 418 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 419 | setRestrictBackground(false); |
| 420 | assertRestrictBackgroundChangedReceived(futureIntent, null); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Adds whitelist when restrict background is on - app should receive an intent. |
| 425 | */ |
| 426 | @Test |
| 427 | @NetPolicyXml("restrict-background-on.xml") |
| 428 | public void testAddRestrictBackgroundWhitelist_restrictBackgroundOn() throws Exception { |
| 429 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 430 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 431 | addRestrictBackgroundWhitelist(true); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Adds whitelist when restrict background is off - app should not receive an intent. |
| 436 | */ |
| 437 | @Test |
| 438 | public void testAddRestrictBackgroundWhitelist_restrictBackgroundOff() throws Exception { |
| 439 | assertRestrictBackgroundOff(); // Sanity check. |
| 440 | addRestrictBackgroundWhitelist(false); |
| 441 | } |
| 442 | |
| 443 | private void addRestrictBackgroundWhitelist(boolean expectIntent) throws Exception { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 444 | // Sanity checks. |
| 445 | assertWhitelistUids(); |
| 446 | assertUidPolicy(UID_A, POLICY_NONE); |
| 447 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 448 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 449 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 450 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 451 | mService.setUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 452 | |
| 453 | assertWhitelistUids(UID_A); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 454 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 455 | mPolicyListener.waitAndVerify() |
| 456 | .onUidPoliciesChanged(APP_ID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 457 | if (expectIntent) { |
| 458 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 459 | } else { |
| 460 | futureIntent.assertNotReceived(); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Removes whitelist when restrict background is on - app should receive an intent. |
| 466 | */ |
| 467 | @Test |
| 468 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
| 469 | public void testRemoveRestrictBackgroundWhitelist_restrictBackgroundOn() throws Exception { |
| 470 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 471 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 472 | removeRestrictBackgroundWhitelist(true); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Removes whitelist when restrict background is off - app should not receive an intent. |
| 477 | */ |
| 478 | @Test |
| 479 | @NetPolicyXml("uidA-whitelisted-restrict-background-off.xml") |
| 480 | public void testRemoveRestrictBackgroundWhitelist_restrictBackgroundOff() throws Exception { |
| 481 | assertRestrictBackgroundOff(); // Sanity check. |
| 482 | removeRestrictBackgroundWhitelist(false); |
| 483 | } |
| 484 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 485 | @Test |
| 486 | public void testLowPowerModeObserver_ListenersRegistered() |
| 487 | throws Exception { |
| 488 | PowerManagerInternal pmInternal = LocalServices.getService(PowerManagerInternal.class); |
| 489 | |
| 490 | verify(pmInternal, atLeast(2)).registerLowPowerModeObserver(any()); |
| 491 | } |
| 492 | |
| 493 | @Test |
| 494 | public void updateRestrictBackgroundByLowPowerMode_RestrictOnBeforeBsm_RestrictOnAfterBsm() |
| 495 | throws Exception { |
| 496 | setRestrictBackground(true); |
| 497 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 498 | .setGlobalBatterySaverEnabled(true) |
| 499 | .setBatterySaverEnabled(false) |
| 500 | .build(); |
| 501 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 502 | |
| 503 | // RestrictBackground should be on even though battery saver want to turn it off |
| 504 | assertThat(mService.getRestrictBackground()).isTrue(); |
| 505 | |
| 506 | PowerSaveState stateOff = new PowerSaveState.Builder() |
| 507 | .setGlobalBatterySaverEnabled(false) |
| 508 | .setBatterySaverEnabled(false) |
| 509 | .build(); |
| 510 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 511 | |
| 512 | // RestrictBackground should be on, following its previous state |
| 513 | assertThat(mService.getRestrictBackground()).isTrue(); |
| 514 | } |
| 515 | |
| 516 | @Test |
| 517 | public void updateRestrictBackgroundByLowPowerMode_RestrictOffBeforeBsm_RestrictOffAfterBsm() |
| 518 | throws Exception { |
| 519 | setRestrictBackground(false); |
| 520 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 521 | .setGlobalBatterySaverEnabled(true) |
| 522 | .setBatterySaverEnabled(true) |
| 523 | .build(); |
| 524 | |
jackqdyulei | 29c82ab | 2017-03-10 14:09:16 -0800 | [diff] [blame] | 525 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 526 | |
| 527 | // RestrictBackground should be turned on because of battery saver |
| 528 | assertThat(mService.getRestrictBackground()).isTrue(); |
| 529 | |
| 530 | PowerSaveState stateOff = new PowerSaveState.Builder() |
| 531 | .setGlobalBatterySaverEnabled(false) |
| 532 | .setBatterySaverEnabled(false) |
| 533 | .build(); |
| 534 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 535 | |
| 536 | // RestrictBackground should be off, following its previous state |
| 537 | assertThat(mService.getRestrictBackground()).isFalse(); |
| 538 | } |
| 539 | |
| 540 | @Test |
| 541 | public void updateRestrictBackgroundByLowPowerMode_StatusChangedInBsm_DoNotRestore() |
| 542 | throws Exception { |
| 543 | setRestrictBackground(true); |
| 544 | PowerSaveState stateOn = new PowerSaveState.Builder() |
| 545 | .setGlobalBatterySaverEnabled(true) |
| 546 | .setBatterySaverEnabled(true) |
| 547 | .build(); |
| 548 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOn); |
| 549 | |
| 550 | // RestrictBackground should still be on |
| 551 | assertThat(mService.getRestrictBackground()).isTrue(); |
| 552 | |
| 553 | // User turns off RestrictBackground manually |
| 554 | setRestrictBackground(false); |
| 555 | PowerSaveState stateOff = new PowerSaveState.Builder().setBatterySaverEnabled( |
| 556 | false).build(); |
| 557 | mService.updateRestrictBackgroundByLowPowerModeUL(stateOff); |
| 558 | |
| 559 | // RestrictBackground should be off because user changes it manually |
| 560 | assertThat(mService.getRestrictBackground()).isFalse(); |
| 561 | } |
| 562 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 563 | private void removeRestrictBackgroundWhitelist(boolean expectIntent) throws Exception { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 564 | // Sanity checks. |
| 565 | assertWhitelistUids(UID_A); |
| 566 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
| 567 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 568 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 569 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 570 | |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 571 | mService.setUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 572 | |
| 573 | assertWhitelistUids(); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 574 | assertUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 575 | mPolicyListener.waitAndVerify().onUidPoliciesChanged(APP_ID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 576 | if (expectIntent) { |
| 577 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 578 | } else { |
| 579 | futureIntent.assertNotReceived(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 584 | * 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] | 585 | */ |
| 586 | @Test |
| 587 | @NetPolicyXml("restrict-background-on.xml") |
| 588 | public void testAddRestrictBackgroundBlacklist_restrictBackgroundOn() throws Exception { |
| 589 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 590 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 591 | addRestrictBackgroundBlacklist(false); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Adds blacklist when restrict background is off - app should receive an intent. |
| 596 | */ |
| 597 | @Test |
| 598 | public void testAddRestrictBackgroundBlacklist_restrictBackgroundOff() throws Exception { |
| 599 | assertRestrictBackgroundOff(); // Sanity check. |
| 600 | addRestrictBackgroundBlacklist(true); |
| 601 | } |
| 602 | |
| 603 | private void addRestrictBackgroundBlacklist(boolean expectIntent) throws Exception { |
| 604 | assertUidPolicy(UID_A, POLICY_NONE); // Sanity check. |
| 605 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 606 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 607 | |
| 608 | mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 609 | |
| 610 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 611 | mPolicyListener.waitAndVerify() |
| 612 | .onUidPoliciesChanged(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 613 | if (expectIntent) { |
| 614 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 615 | } else { |
| 616 | futureIntent.assertNotReceived(); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 621 | * 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] | 622 | */ |
| 623 | @Test |
| 624 | @NetPolicyXml("uidA-blacklisted-restrict-background-on.xml") |
| 625 | public void testRemoveRestrictBackgroundBlacklist_restrictBackgroundOn() throws Exception { |
| 626 | assertRestrictBackgroundOn(); // Sanity check. |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 627 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 628 | removeRestrictBackgroundBlacklist(false); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Removes blacklist when restrict background is off - app should receive an intent. |
| 633 | */ |
| 634 | @Test |
| 635 | @NetPolicyXml("uidA-blacklisted-restrict-background-off.xml") |
| 636 | public void testRemoveRestrictBackgroundBlacklist_restrictBackgroundOff() throws Exception { |
| 637 | assertRestrictBackgroundOff(); // Sanity check. |
| 638 | removeRestrictBackgroundBlacklist(true); |
| 639 | } |
| 640 | |
| 641 | private void removeRestrictBackgroundBlacklist(boolean expectIntent) throws Exception { |
| 642 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); // Sanity check. |
| 643 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 644 | mPolicyListener.expect().onUidPoliciesChanged(anyInt(), anyInt()); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 645 | |
| 646 | mService.setUidPolicy(UID_A, POLICY_NONE); |
| 647 | |
| 648 | assertUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 649 | mPolicyListener.waitAndVerify() |
| 650 | .onUidPoliciesChanged(APP_ID_A, POLICY_NONE); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 651 | if (expectIntent) { |
| 652 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 653 | } else { |
| 654 | futureIntent.assertNotReceived(); |
| 655 | } |
| 656 | } |
| 657 | |
Felipe Leme | 51a6364 | 2016-08-30 12:15:09 -0700 | [diff] [blame] | 658 | @Test |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 659 | @NetPolicyXml("uidA-blacklisted-restrict-background-on.xml") |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 660 | public void testBlacklistedAppIsNotNotifiedWhenRestrictBackgroundIsOn() throws Exception { |
| 661 | // Sanity checks. |
| 662 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 663 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 664 | assertUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 665 | |
| 666 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 667 | setRestrictBackground(true); |
| 668 | futureIntent.assertNotReceived(); |
| 669 | } |
| 670 | |
Felipe Leme | 51a6364 | 2016-08-30 12:15:09 -0700 | [diff] [blame] | 671 | @Test |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 672 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 673 | public void testWhitelistedAppIsNotNotifiedWhenRestrictBackgroundIsOn() throws Exception { |
| 674 | // Sanity checks. |
Felipe Leme | c08ecdf | 2016-08-30 12:57:35 -0700 | [diff] [blame] | 675 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 676 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 677 | assertWhitelistUids(UID_A); |
| 678 | |
| 679 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 680 | setRestrictBackground(true); |
| 681 | futureIntent.assertNotReceived(); |
| 682 | } |
| 683 | |
Andreas Gampe | 84d156b | 2016-09-03 10:50:38 -0700 | [diff] [blame] | 684 | @Test |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 685 | @NetPolicyXml("uidA-whitelisted-restrict-background-on.xml") |
| 686 | public void testWhitelistedAppIsNotifiedWhenBlacklisted() throws Exception { |
| 687 | // Sanity checks. |
| 688 | assertRestrictBackgroundOn(); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 689 | assertRestrictBackgroundChangedReceived(mFutureIntent, null); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 690 | assertWhitelistUids(UID_A); |
| 691 | |
| 692 | final FutureIntent futureIntent = newRestrictBackgroundChangedFuture(); |
| 693 | mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); |
| 694 | assertRestrictBackgroundChangedReceived(futureIntent, PKG_NAME_A); |
| 695 | } |
| 696 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 697 | @Test |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 698 | @NetPolicyXml("restrict-background-lists-whitelist-format.xml") |
| 699 | public void testRestrictBackgroundLists_whitelistFormat() throws Exception { |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 700 | restrictBackgroundListsTest(); |
| 701 | } |
| 702 | |
| 703 | @Test |
| 704 | @NetPolicyXml("restrict-background-lists-uid-policy-format.xml") |
| 705 | public void testRestrictBackgroundLists_uidPolicyFormat() throws Exception { |
| 706 | restrictBackgroundListsTest(); |
| 707 | } |
| 708 | |
| 709 | private void restrictBackgroundListsTest() throws Exception { |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 710 | // UIds that are whitelisted. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 711 | assertWhitelistUids(UID_A, UID_B, UID_C); |
| 712 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
| 713 | assertUidPolicy(UID_B, POLICY_ALLOW_METERED_BACKGROUND); |
| 714 | assertUidPolicy(UID_C, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 715 | |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 716 | // UIDs that are blacklisted. |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 717 | assertUidPolicy(UID_D, POLICY_NONE); |
| 718 | assertUidPolicy(UID_E, POLICY_REJECT_METERED_BACKGROUND); |
Felipe Leme | 8546a44 | 2016-08-23 09:38:20 -0700 | [diff] [blame] | 719 | |
| 720 | // UIDS that have legacy policies. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 721 | assertUidPolicy(UID_F, 2); // POLICY_ALLOW_BACKGROUND_BATTERY_SAVE |
| 722 | |
| 723 | // Remove whitelist. |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 724 | mService.setUidPolicy(UID_A, POLICY_NONE); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 725 | assertUidPolicy(UID_A, POLICY_NONE); |
| 726 | assertWhitelistUids(UID_B, UID_C); |
| 727 | |
| 728 | // Add whitelist when blacklisted. |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 729 | mService.setUidPolicy(UID_E, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 730 | assertUidPolicy(UID_E, POLICY_ALLOW_METERED_BACKGROUND); |
| 731 | assertWhitelistUids(UID_B, UID_C, UID_E); |
| 732 | |
| 733 | // Add blacklist when whitelisted. |
| 734 | mService.setUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); |
| 735 | assertUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); |
| 736 | assertWhitelistUids(UID_C, UID_E); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Tests scenario where an UID had {@code restrict-background} and {@code uid-policy} tags. |
| 741 | */ |
| 742 | @Test |
| 743 | @NetPolicyXml("restrict-background-lists-mixed-format.xml") |
| 744 | public void testRestrictBackgroundLists_mixedFormat() throws Exception { |
| 745 | assertWhitelistUids(UID_A, UID_C, UID_D); |
| 746 | assertUidPolicy(UID_A, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 747 | assertUidPolicy(UID_B, POLICY_REJECT_METERED_BACKGROUND); // Blacklist prevails. |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 748 | assertUidPolicy(UID_C, (POLICY_ALLOW_METERED_BACKGROUND | 2)); |
| 749 | assertUidPolicy(UID_D, POLICY_ALLOW_METERED_BACKGROUND); |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 752 | @Test |
| 753 | @NetPolicyXml("uids-with-mixed-policies.xml") |
| 754 | public void testGetUidsWithPolicy() throws Exception { |
Felipe Leme | 03f9029 | 2016-09-08 18:10:32 -0700 | [diff] [blame] | 755 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_NONE)); |
Felipe Leme | 6f51a0a | 2016-08-24 15:11:51 -0700 | [diff] [blame] | 756 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND), |
| 757 | UID_B, UID_D); |
| 758 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND), |
| 759 | UID_E, UID_F); |
| 760 | // Legacy (POLICY_ALLOW_BACKGROUND_BATTERY_SAVE) |
| 761 | assertContainsInAnyOrder(mService.getUidsWithPolicy(2), |
| 762 | UID_C, UID_D, UID_F); |
| 763 | } |
| 764 | |
Felipe Leme | 0ecfcd1 | 2016-09-06 12:49:48 -0700 | [diff] [blame] | 765 | // NOTE: testPolicyChangeTriggersListener() is too superficial, they |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 766 | // don't check for side-effects (like calls to NetworkManagementService) neither cover all |
| 767 | // different modes (Data Saver, Battery Saver, Doze, App idle, etc...). |
| 768 | // These scenarios are extensively tested on CTS' HostsideRestrictBackgroundNetworkTests. |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 769 | @Test |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 770 | public void testUidForeground() throws Exception { |
| 771 | // push all uids into background |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 772 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, 0); |
| 773 | callOnUidStateChanged(UID_B, ActivityManager.PROCESS_STATE_SERVICE, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 774 | assertFalse(mService.isUidForeground(UID_A)); |
| 775 | assertFalse(mService.isUidForeground(UID_B)); |
| 776 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 777 | // push one of the uids into foreground |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 778 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_TOP, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 779 | assertTrue(mService.isUidForeground(UID_A)); |
| 780 | assertFalse(mService.isUidForeground(UID_B)); |
| 781 | |
| 782 | // and swap another uid into foreground |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 783 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, 0); |
| 784 | callOnUidStateChanged(UID_B, ActivityManager.PROCESS_STATE_TOP, 0); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 785 | assertFalse(mService.isUidForeground(UID_A)); |
| 786 | assertTrue(mService.isUidForeground(UID_B)); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 789 | private static long computeLastCycleBoundary(long currentTime, NetworkPolicy policy) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 790 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTime), |
| 791 | ZoneId.systemDefault()); |
| 792 | final Iterator<Pair<ZonedDateTime, ZonedDateTime>> it = policy.cycleIterator(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 793 | while (it.hasNext()) { |
| 794 | final Pair<ZonedDateTime, ZonedDateTime> cycle = it.next(); |
| 795 | if (cycle.first.toInstant().toEpochMilli() < currentTime) { |
| 796 | return cycle.first.toInstant().toEpochMilli(); |
| 797 | } |
| 798 | } |
| 799 | throw new IllegalStateException( |
| 800 | "Failed to find current cycle for " + policy + " at " + currentTime); |
| 801 | } |
| 802 | |
| 803 | private static long computeNextCycleBoundary(long currentTime, NetworkPolicy policy) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 804 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTime), |
| 805 | ZoneId.systemDefault()); |
| 806 | return policy.cycleIterator().next().second.toInstant().toEpochMilli(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 807 | } |
| 808 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 809 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 810 | public void testLastCycleBoundaryThisMonth() throws Exception { |
| 811 | // assume cycle day of "5th", which should be in same month |
| 812 | final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); |
| 813 | final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z"); |
| 814 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 815 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 816 | sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 817 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 818 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 821 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 822 | public void testLastCycleBoundaryLastMonth() throws Exception { |
| 823 | // assume cycle day of "20th", which should be in last month |
| 824 | final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); |
| 825 | final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z"); |
| 826 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 827 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 828 | sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 829 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 830 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 833 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 834 | public void testLastCycleBoundaryThisMonthFebruary() throws Exception { |
| 835 | // assume cycle day of "30th" in february; should go to january |
| 836 | final long currentTime = parseTime("2007-02-14T00:00:00.000Z"); |
| 837 | final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z"); |
| 838 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 839 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 840 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 841 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 842 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 845 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 846 | public void testLastCycleBoundaryLastMonthFebruary() throws Exception { |
| 847 | // assume cycle day of "30th" in february, which should clamp |
| 848 | final long currentTime = parseTime("2007-03-14T00:00:00.000Z"); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 849 | final long expectedCycle = parseTime("2007-02-28T23:59:59.999Z"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 850 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 851 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 852 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 853 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 854 | assertTimeEquals(expectedCycle, actualCycle); |
| 855 | } |
| 856 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 857 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 858 | public void testCycleBoundaryLeapYear() throws Exception { |
| 859 | final NetworkPolicy policy = new NetworkPolicy( |
| 860 | sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false); |
| 861 | |
| 862 | assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"), |
| 863 | computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy)); |
| 864 | assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"), |
| 865 | computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy)); |
| 866 | assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"), |
| 867 | computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy)); |
| 868 | assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"), |
| 869 | computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy)); |
| 870 | |
| 871 | assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"), |
| 872 | computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy)); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 873 | assertTimeEquals(parseTime("2007-02-28T23:59:59.999Z"), |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 874 | computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy)); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 875 | assertTimeEquals(parseTime("2007-02-28T23:59:59.999Z"), |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 876 | computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy)); |
| 877 | assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"), |
| 878 | computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy)); |
| 879 | } |
| 880 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 881 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 882 | public void testNextCycleTimezoneAfterUtc() throws Exception { |
| 883 | // US/Central is UTC-6 |
| 884 | final NetworkPolicy policy = new NetworkPolicy( |
| 885 | sTemplateWifi, 10, "US/Central", 1024L, 1024L, false); |
| 886 | assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"), |
| 887 | computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy)); |
| 888 | } |
| 889 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 890 | @Test |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 891 | public void testNextCycleTimezoneBeforeUtc() throws Exception { |
| 892 | // Israel is UTC+2 |
| 893 | final NetworkPolicy policy = new NetworkPolicy( |
| 894 | sTemplateWifi, 10, "Israel", 1024L, 1024L, false); |
| 895 | assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"), |
| 896 | computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy)); |
| 897 | } |
| 898 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 899 | @Test |
Jeff Sharkey | 1539905 | 2013-01-15 14:15:53 -0800 | [diff] [blame] | 900 | public void testCycleTodayJanuary() throws Exception { |
| 901 | final NetworkPolicy policy = new NetworkPolicy( |
| 902 | sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false); |
| 903 | |
| 904 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 905 | computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy)); |
| 906 | assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), |
| 907 | computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy)); |
| 908 | assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"), |
| 909 | computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy)); |
| 910 | |
| 911 | assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"), |
| 912 | computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy)); |
| 913 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 914 | computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy)); |
| 915 | assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"), |
| 916 | computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy)); |
| 917 | } |
| 918 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 919 | @Test |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 920 | public void testNetworkPolicyAppliedCycleLastMonth() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 921 | NetworkState[] state = null; |
| 922 | NetworkStats stats = null; |
| 923 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 924 | final int CYCLE_DAY = 15; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 925 | final long NOW = parseTime("2007-03-10T00:00Z"); |
| 926 | final long CYCLE_START = parseTime("2007-02-15T00:00Z"); |
| 927 | final long CYCLE_END = parseTime("2007-03-15T00:00Z"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 928 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 929 | setCurrentTimeMillis(NOW); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 930 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 931 | // first, pretend that wifi network comes online. no policy active, |
| 932 | // which means we shouldn't push limit to interface. |
| 933 | state = new NetworkState[] { buildWifi() }; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 934 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 935 | expectCurrentTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 936 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 937 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 938 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 939 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 940 | |
| 941 | // now change cycle to be on 15th, and test in early march, to verify we |
| 942 | // pick cycle day in previous month. |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 943 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 944 | expectCurrentTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 945 | |
| 946 | // pretend that 512 bytes total have happened |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 947 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 948 | .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 949 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, CYCLE_END)) |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 950 | .thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 951 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 952 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 953 | setNetworkPolicies(new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 954 | 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] | 955 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 956 | |
| 957 | // TODO: consider making strongly ordered mock |
| 958 | verifyPolicyDataEnable(TYPE_WIFI, true); |
| 959 | verifyRemoveInterfaceQuota(TEST_IFACE); |
| 960 | verifySetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 963 | @Test |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 964 | public void testOverWarningLimitNotification() throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 965 | NetworkState[] state = null; |
| 966 | NetworkStats stats = null; |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 967 | Future<String> tagFuture = null; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 968 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 969 | final int CYCLE_DAY = 15; |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 970 | final long NOW = parseTime("2007-03-10T00:00Z"); |
| 971 | final long CYCLE_START = parseTime("2007-02-15T00:00Z"); |
| 972 | final long CYCLE_END = parseTime("2007-03-15T00:00Z"); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 973 | |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 974 | setCurrentTimeMillis(NOW); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 975 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 976 | // assign wifi policy |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 977 | state = new NetworkState[] {}; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 978 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 979 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 980 | |
| 981 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 982 | expectCurrentTime(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 983 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 984 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, |
| 985 | CYCLE_END)).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 986 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 987 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 988 | setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 |
| 989 | * MB_IN_BYTES, 2 * MB_IN_BYTES, false)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 990 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(any()); |
| 991 | verifyPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | // bring up wifi network |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 995 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 996 | state = new NetworkState[] { buildWifi() }; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 997 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 998 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 999 | |
| 1000 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1001 | expectCurrentTime(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1002 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1003 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, |
| 1004 | CYCLE_END)).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1005 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1006 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 1007 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1008 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 1009 | |
| 1010 | verifyPolicyDataEnable(TYPE_WIFI, true); |
| 1011 | verifyRemoveInterfaceQuota(TEST_IFACE); |
| 1012 | verifySetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | // go over warning, which should kick notification |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1016 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 1017 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1018 | .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1019 | |
| 1020 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1021 | expectCurrentTime(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1022 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, |
| 1023 | CYCLE_END)).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1024 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1025 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1026 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1027 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1028 | assertNotificationType(TYPE_WARNING, tagFuture.get()); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1029 | verifyPolicyDataEnable(TYPE_WIFI, true); |
| 1030 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // go over limit, which should kick notification and dialog |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1034 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 1035 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 1036 | .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1037 | |
| 1038 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1039 | expectCurrentTime(); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1040 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, |
| 1041 | CYCLE_END)).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1042 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1043 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1044 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1045 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1046 | assertNotificationType(TYPE_LIMIT, tagFuture.get()); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1047 | verifyPolicyDataEnable(TYPE_WIFI, false); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | // now snooze policy, which should remove quota |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1051 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1052 | |
| 1053 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1054 | expectCurrentTime(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1055 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1056 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, CYCLE_START, |
| 1057 | CYCLE_END)).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1058 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1059 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1060 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 1061 | mService.snoozeLimit(sTemplateWifi); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1062 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 1063 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1064 | assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get()); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1065 | // snoozed interface still has high quota so background data is |
| 1066 | // still restricted. |
| 1067 | verifyRemoveInterfaceQuota(TEST_IFACE); |
| 1068 | verifySetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
| 1069 | verifyPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 1073 | @Test |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1074 | public void testMeteredNetworkWithoutLimit() throws Exception { |
| 1075 | NetworkState[] state = null; |
| 1076 | NetworkStats stats = null; |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1077 | |
| 1078 | final long TIME_FEB_15 = 1171497600000L; |
| 1079 | final long TIME_MAR_10 = 1173484800000L; |
| 1080 | final int CYCLE_DAY = 15; |
| 1081 | |
| 1082 | setCurrentTimeMillis(TIME_MAR_10); |
| 1083 | |
| 1084 | // bring up wifi network with metered policy |
| 1085 | state = new NetworkState[] { buildWifi() }; |
| 1086 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 1087 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
| 1088 | |
| 1089 | { |
| 1090 | expectCurrentTime(); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1091 | when(mConnManager.getAllNetworkState()).thenReturn(state); |
| 1092 | when(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, |
| 1093 | currentTimeMillis())).thenReturn(stats.getTotalBytes()); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1094 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1095 | mPolicyListener.expect().onMeteredIfacesChanged(any()); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 1096 | setNetworkPolicies(new NetworkPolicy( |
| 1097 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, |
| 1098 | true)); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1099 | mPolicyListener.waitAndVerify().onMeteredIfacesChanged(eq(new String[]{TEST_IFACE})); |
| 1100 | |
| 1101 | verifyPolicyDataEnable(TYPE_WIFI, true); |
| 1102 | verifyRemoveInterfaceQuota(TEST_IFACE); |
| 1103 | verifySetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1107 | @Test |
| 1108 | public void testOnUidStateChanged_notifyAMS() throws Exception { |
| 1109 | final long procStateSeq = 222; |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 1110 | callOnUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE, procStateSeq); |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1111 | verify(mActivityManagerInternal).notifyNetworkPolicyRulesUpdated(UID_A, procStateSeq); |
| 1112 | |
| 1113 | final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 1114 | final IndentingPrintWriter writer = new IndentingPrintWriter( |
| 1115 | new PrintWriter(outputStream), " "); |
| 1116 | mService.mObservedHistory.dumpUL(writer); |
| 1117 | writer.flush(); |
| 1118 | assertEquals(ProcStateSeqHistory.getString(UID_A, procStateSeq), |
| 1119 | outputStream.toString().trim()); |
| 1120 | } |
| 1121 | |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 1122 | private void callOnUidStateChanged(int uid, int procState, long procStateSeq) |
| 1123 | throws Exception { |
| 1124 | mUidObserver.onUidStateChanged(uid, procState, procStateSeq); |
| 1125 | final CountDownLatch latch = new CountDownLatch(1); |
| 1126 | mService.mUidEventHandler.post(() -> { |
| 1127 | latch.countDown(); |
| 1128 | }); |
| 1129 | latch.await(2, TimeUnit.SECONDS); |
| 1130 | } |
| 1131 | |
Sudheer Shanka | e736185 | 2017-03-07 11:51:46 -0800 | [diff] [blame] | 1132 | @Test |
| 1133 | public void testProcStateHistory() { |
| 1134 | // Verify dump works correctly with no elements added. |
| 1135 | verifyProcStateHistoryDump(0); |
| 1136 | |
| 1137 | // Add items upto half of the max capacity and verify that dump works correctly. |
| 1138 | verifyProcStateHistoryDump(MAX_PROC_STATE_SEQ_HISTORY / 2); |
| 1139 | |
| 1140 | // Add items upto the max capacity and verify that dump works correctly. |
| 1141 | verifyProcStateHistoryDump(MAX_PROC_STATE_SEQ_HISTORY); |
| 1142 | |
| 1143 | // Add more items than max capacity and verify that dump works correctly. |
| 1144 | verifyProcStateHistoryDump(MAX_PROC_STATE_SEQ_HISTORY + MAX_PROC_STATE_SEQ_HISTORY / 2); |
| 1145 | |
| 1146 | } |
| 1147 | |
| 1148 | private void verifyProcStateHistoryDump(int count) { |
| 1149 | final ProcStateSeqHistory history = new ProcStateSeqHistory(MAX_PROC_STATE_SEQ_HISTORY); |
| 1150 | final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 1151 | final IndentingPrintWriter writer = new IndentingPrintWriter( |
| 1152 | new PrintWriter(outputStream), " "); |
| 1153 | |
| 1154 | if (count == 0) { |
| 1155 | // Verify with no uid info written to history. |
| 1156 | history.dumpUL(writer); |
| 1157 | writer.flush(); |
| 1158 | assertEquals("When no uid info is there, dump should contain NONE", |
| 1159 | "NONE", outputStream.toString().trim()); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | int uid = 111; |
| 1164 | long procStateSeq = 222; |
| 1165 | // Add count items and verify dump works correctly. |
| 1166 | for (int i = 0; i < count; ++i) { |
| 1167 | uid++; |
| 1168 | procStateSeq++; |
| 1169 | history.addProcStateSeqUL(uid, procStateSeq); |
| 1170 | } |
| 1171 | history.dumpUL(writer); |
| 1172 | writer.flush(); |
| 1173 | final String[] uidsDump = outputStream.toString().split(LINE_SEPARATOR); |
| 1174 | // Dump will have at most MAX_PROC_STATE_SEQ_HISTORY items. |
| 1175 | final int expectedCount = (count < MAX_PROC_STATE_SEQ_HISTORY) |
| 1176 | ? count : MAX_PROC_STATE_SEQ_HISTORY; |
| 1177 | assertEquals(expectedCount, uidsDump.length); |
| 1178 | for (int i = 0; i < expectedCount; ++i) { |
| 1179 | assertEquals(ProcStateSeqHistory.getString(uid, procStateSeq), uidsDump[i]); |
| 1180 | uid--; |
| 1181 | procStateSeq--; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1185 | private void assertCycleDayAsExpected(PersistableBundle config, int carrierCycleDay, |
| 1186 | boolean expectValid) { |
| 1187 | config.putInt(KEY_MONTHLY_DATA_CYCLE_DAY_INT, carrierCycleDay); |
| 1188 | int actualCycleDay = mService.getCycleDayFromCarrierConfig(config, |
| 1189 | INVALID_CARRIER_CONFIG_VALUE); |
| 1190 | if (expectValid) { |
| 1191 | assertEquals(carrierCycleDay, actualCycleDay); |
| 1192 | } else { |
| 1193 | // INVALID_CARRIER_CONFIG_VALUE is returned for invalid values |
| 1194 | assertEquals(INVALID_CARRIER_CONFIG_VALUE, actualCycleDay); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | @Test |
| 1199 | public void testGetCycleDayFromCarrierConfig() { |
| 1200 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1201 | final Calendar cal = Calendar.getInstance(); |
| 1202 | int actualCycleDay; |
| 1203 | |
| 1204 | config.putInt(KEY_MONTHLY_DATA_CYCLE_DAY_INT, DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1205 | actualCycleDay = mService.getCycleDayFromCarrierConfig(config, DEFAULT_CYCLE_DAY); |
| 1206 | assertEquals(DEFAULT_CYCLE_DAY, actualCycleDay); |
| 1207 | |
| 1208 | // null config returns a default value |
| 1209 | actualCycleDay = mService.getCycleDayFromCarrierConfig(null, DEFAULT_CYCLE_DAY); |
| 1210 | assertEquals(DEFAULT_CYCLE_DAY, actualCycleDay); |
| 1211 | |
| 1212 | // Sane, non-default values |
| 1213 | assertCycleDayAsExpected(config, 1, true); |
| 1214 | assertCycleDayAsExpected(config, cal.getMaximum(Calendar.DAY_OF_MONTH), true); |
| 1215 | assertCycleDayAsExpected(config, cal.getMinimum(Calendar.DAY_OF_MONTH), true); |
| 1216 | |
| 1217 | // Invalid values |
| 1218 | assertCycleDayAsExpected(config, 0, false); |
| 1219 | assertCycleDayAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, false); |
| 1220 | assertCycleDayAsExpected(config, cal.getMaximum(Calendar.DAY_OF_MONTH) + 1, false); |
| 1221 | assertCycleDayAsExpected(config, cal.getMinimum(Calendar.DAY_OF_MONTH) - 5, false); |
| 1222 | } |
| 1223 | |
| 1224 | private void assertWarningBytesAsExpected(PersistableBundle config, long carrierWarningBytes, |
| 1225 | long expected) { |
| 1226 | config.putLong(KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, carrierWarningBytes); |
| 1227 | long actualWarning = mService.getWarningBytesFromCarrierConfig(config, |
| 1228 | INVALID_CARRIER_CONFIG_VALUE); |
| 1229 | assertEquals(expected, actualWarning); |
| 1230 | } |
| 1231 | |
| 1232 | @Test |
| 1233 | public void testGetWarningBytesFromCarrierConfig() { |
| 1234 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1235 | long actualWarningBytes; |
| 1236 | |
| 1237 | assertWarningBytesAsExpected(config, DATA_CYCLE_USE_PLATFORM_DEFAULT, |
| 1238 | mDefaultWarningBytes); |
| 1239 | assertWarningBytesAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, WARNING_DISABLED); |
| 1240 | assertWarningBytesAsExpected(config, 0, 0); |
| 1241 | // not a valid value |
| 1242 | assertWarningBytesAsExpected(config, -1000, INVALID_CARRIER_CONFIG_VALUE); |
| 1243 | |
| 1244 | // null config returns a default value |
| 1245 | actualWarningBytes = mService.getWarningBytesFromCarrierConfig(null, mDefaultWarningBytes); |
| 1246 | assertEquals(mDefaultWarningBytes, actualWarningBytes); |
| 1247 | } |
| 1248 | |
| 1249 | private void assertLimitBytesAsExpected(PersistableBundle config, long carrierWarningBytes, |
| 1250 | long expected) { |
| 1251 | config.putLong(KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, carrierWarningBytes); |
| 1252 | long actualWarning = mService.getLimitBytesFromCarrierConfig(config, |
| 1253 | INVALID_CARRIER_CONFIG_VALUE); |
| 1254 | assertEquals(expected, actualWarning); |
| 1255 | } |
| 1256 | |
| 1257 | @Test |
| 1258 | public void testGetLimitBytesFromCarrierConfig() { |
| 1259 | PersistableBundle config = CarrierConfigManager.getDefaultConfig(); |
| 1260 | long actualLimitBytes; |
| 1261 | |
| 1262 | assertLimitBytesAsExpected(config, DATA_CYCLE_USE_PLATFORM_DEFAULT, |
| 1263 | mDefaultLimitBytes); |
| 1264 | assertLimitBytesAsExpected(config, DATA_CYCLE_THRESHOLD_DISABLED, LIMIT_DISABLED); |
| 1265 | assertLimitBytesAsExpected(config, 0, 0); |
| 1266 | // not a valid value |
| 1267 | assertLimitBytesAsExpected(config, -1000, INVALID_CARRIER_CONFIG_VALUE); |
| 1268 | |
| 1269 | // null config returns a default value |
| 1270 | actualLimitBytes = mService.getWarningBytesFromCarrierConfig(null, mDefaultLimitBytes); |
| 1271 | assertEquals(mDefaultLimitBytes, actualLimitBytes); |
| 1272 | } |
| 1273 | |
| 1274 | private PersistableBundle setupUpdateMobilePolicyCycleTests() throws RemoteException { |
| 1275 | when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]); |
| 1276 | when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID}); |
| 1277 | when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID); |
| 1278 | PersistableBundle bundle = CarrierConfigManager.getDefaultConfig(); |
| 1279 | when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(bundle); |
| 1280 | setNetworkPolicies(buildDefaultFakeMobilePolicy()); |
| 1281 | return bundle; |
| 1282 | } |
| 1283 | |
| 1284 | @Test |
| 1285 | public void testUpdateMobilePolicyCycleWithNullConfig() throws RemoteException { |
| 1286 | when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]); |
| 1287 | when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID}); |
| 1288 | when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID); |
| 1289 | when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(null); |
| 1290 | setNetworkPolicies(buildDefaultFakeMobilePolicy()); |
| 1291 | // smoke test to make sure no errors are raised |
| 1292 | mServiceContext.sendBroadcast( |
| 1293 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1294 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1295 | ); |
| 1296 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1297 | true); |
| 1298 | } |
| 1299 | |
| 1300 | @Test |
| 1301 | public void testUpdateMobilePolicyCycleWithInvalidConfig() throws RemoteException { |
| 1302 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1303 | // Test with an invalid CarrierConfig, there should be no changes or crashes. |
| 1304 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, -100); |
| 1305 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, -100); |
| 1306 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, -100); |
| 1307 | mServiceContext.sendBroadcast( |
| 1308 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1309 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1310 | ); |
| 1311 | |
| 1312 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1313 | true); |
| 1314 | } |
| 1315 | |
| 1316 | @Test |
| 1317 | public void testUpdateMobilePolicyCycleWithDefaultConfig() throws RemoteException { |
| 1318 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1319 | // Test that we respect the platform values when told to |
| 1320 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, |
| 1321 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1322 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1323 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1324 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1325 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1326 | mServiceContext.sendBroadcast( |
| 1327 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1328 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1329 | ); |
| 1330 | |
| 1331 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1332 | true); |
| 1333 | } |
| 1334 | |
| 1335 | @Test |
| 1336 | public void testUpdateMobilePolicyCycleWithUserOverrides() throws RemoteException { |
| 1337 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1338 | |
| 1339 | // inferred = false implies that a user manually modified this policy. |
| 1340 | NetworkPolicy policy = buildDefaultFakeMobilePolicy(); |
| 1341 | policy.inferred = false; |
| 1342 | setNetworkPolicies(policy); |
| 1343 | |
| 1344 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1345 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, 9999); |
| 1346 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1347 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1348 | mServiceContext.sendBroadcast( |
| 1349 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1350 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1351 | ); |
| 1352 | |
| 1353 | // The policy still shouldn't change, because we don't want to overwrite user settings. |
| 1354 | assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1355 | false); |
| 1356 | } |
| 1357 | |
| 1358 | @Test |
| 1359 | public void testUpdateMobilePolicyCycleUpdatesDataCycle() throws RemoteException { |
| 1360 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1361 | |
| 1362 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1363 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, 9999); |
| 1364 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, 9999); |
| 1365 | mServiceContext.sendBroadcast( |
| 1366 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1367 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1368 | ); |
| 1369 | |
| 1370 | assertNetworkPolicyEquals(31, 9999, 9999, true); |
| 1371 | } |
| 1372 | |
| 1373 | @Test |
| 1374 | public void testUpdateMobilePolicyCycleDisableThresholds() throws RemoteException { |
| 1375 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1376 | |
| 1377 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1378 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1379 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1380 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1381 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1382 | mServiceContext.sendBroadcast( |
| 1383 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1384 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1385 | ); |
| 1386 | |
| 1387 | assertNetworkPolicyEquals(31, WARNING_DISABLED, LIMIT_DISABLED, true); |
| 1388 | } |
| 1389 | |
| 1390 | @Test |
| 1391 | public void testUpdateMobilePolicyCycleRevertsToDefault() throws RemoteException { |
| 1392 | PersistableBundle bundle = setupUpdateMobilePolicyCycleTests(); |
| 1393 | |
| 1394 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, 31); |
| 1395 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1396 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1397 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1398 | DATA_CYCLE_THRESHOLD_DISABLED); |
| 1399 | mServiceContext.sendBroadcast( |
| 1400 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1401 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1402 | ); |
| 1403 | assertNetworkPolicyEquals(31, WARNING_DISABLED, LIMIT_DISABLED, true); |
| 1404 | |
| 1405 | // If the user switches carriers to one that doesn't use a CarrierConfig, we should revert |
| 1406 | // to the default data limit and warning. The cycle date doesn't need to revert as it's |
| 1407 | // arbitrary anyways. |
| 1408 | bundle.putInt(CarrierConfigManager.KEY_MONTHLY_DATA_CYCLE_DAY_INT, |
| 1409 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1410 | bundle.putLong(CarrierConfigManager.KEY_DATA_WARNING_THRESHOLD_BYTES_LONG, |
| 1411 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1412 | bundle.putLong(CarrierConfigManager.KEY_DATA_LIMIT_THRESHOLD_BYTES_LONG, |
| 1413 | DATA_CYCLE_USE_PLATFORM_DEFAULT); |
| 1414 | mServiceContext.sendBroadcast( |
| 1415 | new Intent(ACTION_CARRIER_CONFIG_CHANGED) |
| 1416 | .putExtra(PhoneConstants.SUBSCRIPTION_KEY, FAKE_SUB_ID) |
| 1417 | ); |
| 1418 | |
| 1419 | assertNetworkPolicyEquals(31, mDefaultWarningBytes, mDefaultLimitBytes, |
| 1420 | true); |
| 1421 | } |
| 1422 | |
| 1423 | private NetworkPolicy buildDefaultFakeMobilePolicy() { |
| 1424 | NetworkPolicy p = mService.buildDefaultMobilePolicy(FAKE_SUB_ID, FAKE_SUBSCRIBER_ID); |
| 1425 | // set a deterministic cycle date |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1426 | p.cycleRule = new RecurrenceRule( |
| 1427 | p.cycleRule.start.withDayOfMonth(DEFAULT_CYCLE_DAY), |
| 1428 | p.cycleRule.end, Period.ofMonths(1)); |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1429 | return p; |
| 1430 | } |
| 1431 | |
| 1432 | private static NetworkPolicy buildFakeMobilePolicy(int cycleDay, long warningBytes, |
| 1433 | long limitBytes, boolean inferred){ |
| 1434 | final NetworkTemplate template = buildTemplateMobileAll(FAKE_SUBSCRIBER_ID); |
Sudheer Shanka | ed25ce6 | 2017-03-29 20:46:30 -0700 | [diff] [blame] | 1435 | return new NetworkPolicy(template, cycleDay, new Time().timezone, warningBytes, |
Ammar Aijazi | 6ce48e2 | 2017-03-28 15:43:22 -0700 | [diff] [blame] | 1436 | limitBytes, SNOOZE_NEVER, SNOOZE_NEVER, true, inferred); |
| 1437 | } |
| 1438 | |
| 1439 | private void assertNetworkPolicyEquals(int expectedCycleDay, long expectedWarningBytes, |
| 1440 | long expectedLimitBytes, boolean expectedInferred) { |
| 1441 | NetworkPolicy[] policies = mService.getNetworkPolicies( |
| 1442 | mServiceContext.getOpPackageName()); |
| 1443 | assertEquals("Unexpected number of network policies", 1, policies.length); |
| 1444 | NetworkPolicy actualPolicy = policies[0]; |
| 1445 | NetworkPolicy expectedPolicy = buildFakeMobilePolicy(expectedCycleDay, expectedWarningBytes, |
| 1446 | expectedLimitBytes, expectedInferred); |
| 1447 | assertEquals(expectedPolicy, actualPolicy); |
| 1448 | } |
| 1449 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1450 | private static long parseTime(String time) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1451 | return ZonedDateTime.parse(time).toInstant().toEpochMilli(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 1454 | private void setNetworkPolicies(NetworkPolicy... policies) { |
| 1455 | mService.setNetworkPolicies(policies); |
| 1456 | } |
| 1457 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1458 | private static NetworkState buildWifi() { |
| 1459 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 1460 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 1461 | final LinkProperties prop = new LinkProperties(); |
| 1462 | prop.setInterfaceName(TEST_IFACE); |
Stephen Chen | a1c92b7 | 2017-02-06 13:18:11 -0800 | [diff] [blame] | 1463 | final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); |
| 1464 | return new NetworkState(info, prop, networkCapabilities, null, null, TEST_SSID); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1467 | private void expectCurrentTime() throws Exception { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1468 | when(mTime.forceRefresh()).thenReturn(false); |
| 1469 | when(mTime.hasCache()).thenReturn(true); |
| 1470 | when(mTime.currentTimeMillis()).thenReturn(currentTimeMillis()); |
| 1471 | when(mTime.getCacheAge()).thenReturn(0L); |
| 1472 | when(mTime.getCacheCertainty()).thenReturn(0L); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1475 | private Future<String> expectEnqueueNotification() throws Exception { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1476 | final FutureAnswer<String> futureAnswer = new FutureAnswer<String>(2); |
| 1477 | doAnswer(futureAnswer).when(mNotifManager).enqueueNotificationWithTag( |
| 1478 | anyString(), anyString(), anyString() /* capture here (index 2)*/, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1479 | anyInt(), isA(Notification.class), anyInt()); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1480 | return futureAnswer; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1483 | private void expectHasInternetPermission(int uid, boolean hasIt) throws Exception { |
| 1484 | when(mIpm.checkUidPermission(Manifest.permission.INTERNET, uid)).thenReturn( |
| 1485 | hasIt ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED); |
| 1486 | } |
| 1487 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1488 | private void verifySetInterfaceQuota(String iface, long quotaBytes) throws Exception { |
| 1489 | verify(mNetworkManager, atLeastOnce()).setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1492 | private void verifyRemoveInterfaceQuota(String iface) throws Exception { |
| 1493 | verify(mNetworkManager, atLeastOnce()).removeInterfaceQuota(iface); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1496 | private Future<Void> verifyPolicyDataEnable(int type, boolean enabled) throws Exception { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1497 | // TODO: bring back this test |
| 1498 | return null; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1501 | private void verifyAdvisePersistThreshold() throws Exception { |
| 1502 | verify(mStatsService).advisePersistThreshold(anyLong()); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1505 | private static class TestAbstractFuture<T> extends AbstractFuture<T> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1506 | @Override |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1507 | public T get() throws InterruptedException, ExecutionException { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1508 | try { |
| 1509 | return get(5, TimeUnit.SECONDS); |
| 1510 | } catch (TimeoutException e) { |
| 1511 | throw new RuntimeException(e); |
| 1512 | } |
| 1513 | } |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 1514 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1515 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1516 | private static class FutureAnswer<T> extends TestAbstractFuture<T> implements Answer<Void> { |
| 1517 | private final int index; |
| 1518 | |
| 1519 | FutureAnswer(int index) { |
| 1520 | this.index = index; |
| 1521 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1522 | @Override |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1523 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 1524 | @SuppressWarnings("unchecked") |
| 1525 | T captured = (T) invocation.getArguments()[index]; |
| 1526 | set(captured); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 1527 | return null; |
| 1528 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1531 | private static void assertTimeEquals(long expected, long actual) { |
| 1532 | if (expected != actual) { |
| 1533 | fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual)); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | private static String formatTime(long millis) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1538 | return Instant.ofEpochMilli(millis) + " [" + millis + "]"; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) { |
| 1542 | final long low = expected - fuzzy; |
| 1543 | final long high = expected + fuzzy; |
| 1544 | if (actual < low || actual > high) { |
Jeff Sharkey | 53313d7 | 2017-07-13 16:47:32 -0600 | [diff] [blame] | 1545 | fail("value " + formatTime(actual) + " is outside [" + formatTime(low) + "," |
| 1546 | + formatTime(high) + "]"); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | private static void assertUnique(LinkedHashSet<Long> seen, Long value) { |
| 1551 | if (!seen.add(value)) { |
| 1552 | fail("found duplicate time " + value + " in series " + seen.toString()); |
| 1553 | } |
| 1554 | } |
| 1555 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1556 | private static void assertNotificationType(int expected, String actualTag) { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1557 | assertEquals("notification type mismatch for '" + actualTag +"'", |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1558 | Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1)); |
| 1559 | } |
| 1560 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1561 | private void assertUidPolicy(int uid, int expected) { |
| 1562 | final int actual = mService.getUidPolicy(uid); |
| 1563 | if (expected != actual) { |
| 1564 | fail("Wrong policy for UID " + uid + ": expected " + uidPoliciesToString(expected) |
| 1565 | + ", actual " + uidPoliciesToString(actual)); |
| 1566 | } |
| 1567 | } |
| 1568 | |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1569 | private void assertWhitelistUids(int... uids) { |
Felipe Leme | 57e3d31 | 2016-08-23 14:42:52 -0700 | [diff] [blame] | 1570 | assertContainsInAnyOrder(mService.getUidsWithPolicy(POLICY_ALLOW_METERED_BACKGROUND), uids); |
Felipe Leme | 46b451f | 2016-08-19 08:46:17 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1573 | private void assertRestrictBackgroundOn() throws Exception { |
| 1574 | assertTrue("restrictBackground should be set", mService.getRestrictBackground()); |
| 1575 | } |
| 1576 | |
| 1577 | private void assertRestrictBackgroundOff() throws Exception { |
| 1578 | assertFalse("restrictBackground should not be set", mService.getRestrictBackground()); |
| 1579 | } |
| 1580 | |
| 1581 | private FutureIntent newRestrictBackgroundChangedFuture() { |
| 1582 | return mServiceContext |
| 1583 | .nextBroadcastIntent(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 1584 | } |
| 1585 | |
| 1586 | private void assertRestrictBackgroundChangedReceived(Future<Intent> future, |
| 1587 | String expectedPackage) throws Exception { |
| 1588 | final String action = ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; |
| 1589 | final Intent intent = future.get(5, TimeUnit.SECONDS); |
| 1590 | assertNotNull("Didn't get a " + action + "intent in 5 seconds"); |
| 1591 | assertEquals("Wrong package on " + action + " intent", expectedPackage, intent.getPackage()); |
| 1592 | } |
| 1593 | |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1594 | // TODO: replace by Truth, Hamcrest, or a similar tool. |
| 1595 | private void assertContainsInAnyOrder(int[] actual, int...expected) { |
| 1596 | final StringBuilder errors = new StringBuilder(); |
| 1597 | if (actual.length != expected.length) { |
| 1598 | errors.append("\tsize does not match\n"); |
| 1599 | } |
| 1600 | final List<Integer> actualList = |
| 1601 | Arrays.stream(actual).boxed().collect(Collectors.<Integer>toList()); |
| 1602 | final List<Integer> expectedList = |
| 1603 | Arrays.stream(expected).boxed().collect(Collectors.<Integer>toList()); |
| 1604 | if (!actualList.containsAll(expectedList)) { |
| 1605 | errors.append("\tmissing elements on actual list\n"); |
| 1606 | } |
| 1607 | if (!expectedList.containsAll(actualList)) { |
| 1608 | errors.append("\tmissing elements on expected list\n"); |
| 1609 | } |
| 1610 | if (errors.length() > 0) { |
| 1611 | fail("assertContainsInAnyOrder(expected=" + Arrays.toString(expected) |
| 1612 | + ", actual=" + Arrays.toString(actual) +") failed: \n" + errors); |
| 1613 | } |
| 1614 | } |
| 1615 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1616 | private long getElapsedRealtime() { |
| 1617 | return mElapsedRealtime; |
| 1618 | } |
| 1619 | |
| 1620 | private void setCurrentTimeMillis(long currentTimeMillis) { |
Jeff Sharkey | 17bebd2 | 2017-07-19 21:00:38 -0600 | [diff] [blame] | 1621 | RecurrenceRule.sClock = Clock.fixed(Instant.ofEpochMilli(currentTimeMillis), |
| 1622 | ZoneId.systemDefault()); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1623 | mStartTime = currentTimeMillis; |
| 1624 | mElapsedRealtime = 0L; |
| 1625 | } |
| 1626 | |
| 1627 | private long currentTimeMillis() { |
| 1628 | return mStartTime + mElapsedRealtime; |
| 1629 | } |
| 1630 | |
| 1631 | private void incrementCurrentTime(long duration) { |
| 1632 | mElapsedRealtime += duration; |
| 1633 | } |
| 1634 | |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1635 | private FutureIntent mRestrictBackgroundChanged; |
| 1636 | |
| 1637 | private void setRestrictBackground(boolean flag) throws Exception { |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1638 | mService.setRestrictBackground(flag); |
| 1639 | // Sanity check. |
| 1640 | assertEquals("restrictBackground not set", flag, mService.getRestrictBackground()); |
| 1641 | } |
| 1642 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1643 | /** |
| 1644 | * Creates a mock and registers it to {@link LocalServices}. |
| 1645 | */ |
Felipe Leme | 9f27e7a | 2016-08-12 15:19:40 -0700 | [diff] [blame] | 1646 | private static <T> T addLocalServiceMock(Class<T> clazz) { |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1647 | final T mock = mock(clazz); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1648 | LocalServices.addService(clazz, mock); |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1649 | return mock; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1653 | * Custom Mockito answer used to verify async {@link INetworkPolicyListener} calls. |
| 1654 | * |
| 1655 | * <p>Typical usage: |
| 1656 | * <pre><code> |
| 1657 | * mPolicyListener.expect().someCallback(any()); |
| 1658 | * // do something on objects under test |
| 1659 | * mPolicyListener.waitAndVerify().someCallback(eq(expectedValue)); |
| 1660 | * </code></pre> |
| 1661 | */ |
| 1662 | final class NetworkPolicyListenerAnswer implements Answer<Void> { |
| 1663 | private CountDownLatch latch; |
| 1664 | private final INetworkPolicyListener listener; |
| 1665 | |
| 1666 | NetworkPolicyListenerAnswer(NetworkPolicyManagerService service) { |
| 1667 | this.listener = mock(INetworkPolicyListener.class); |
| 1668 | // RemoteCallbackList needs a binder to use as key |
| 1669 | when(listener.asBinder()).thenReturn(new Binder()); |
| 1670 | service.registerListener(listener); |
| 1671 | } |
| 1672 | |
| 1673 | @Override |
| 1674 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 1675 | Log.d(TAG,"counting down on answer: " + invocation); |
| 1676 | latch.countDown(); |
| 1677 | return null; |
| 1678 | } |
| 1679 | |
| 1680 | INetworkPolicyListener expect() { |
| 1681 | assertNull("expect() called before waitAndVerify()", latch); |
| 1682 | latch = new CountDownLatch(1); |
| 1683 | return doAnswer(this).when(listener); |
| 1684 | } |
| 1685 | |
| 1686 | INetworkPolicyListener waitAndVerify() { |
| 1687 | assertNotNull("waitAndVerify() called before expect()", latch); |
| 1688 | try { |
| 1689 | assertTrue("callback not called in 5 seconds", latch.await(5, TimeUnit.SECONDS)); |
| 1690 | } catch (InterruptedException e) { |
| 1691 | fail("Thread interrupted before callback called"); |
| 1692 | } finally { |
| 1693 | latch = null; |
| 1694 | } |
| 1695 | return verify(listener, atLeastOnce()); |
| 1696 | } |
Felipe Leme | 3d3308d | 2016-08-23 17:41:47 -0700 | [diff] [blame] | 1697 | |
| 1698 | INetworkPolicyListener verifyNotCalled() { |
| 1699 | return verify(listener, never()); |
| 1700 | } |
| 1701 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1702 | } |
Felipe Leme | e88729d | 2016-08-17 16:43:01 -0700 | [diff] [blame] | 1703 | |
| 1704 | private void setNetpolicyXml(Context context) throws Exception { |
| 1705 | mPolicyDir = context.getFilesDir(); |
| 1706 | if (mPolicyDir.exists()) { |
| 1707 | IoUtils.deleteContents(mPolicyDir); |
| 1708 | } |
| 1709 | if (!TextUtils.isEmpty(mNetpolicyXml)) { |
| 1710 | final String assetPath = NETPOLICY_DIR + "/" + mNetpolicyXml; |
| 1711 | final File netConfigFile = new File(mPolicyDir, "netpolicy.xml"); |
| 1712 | Log.d(TAG, "Creating " + netConfigFile + " from asset " + assetPath); |
| 1713 | try (final InputStream in = context.getResources().getAssets().open(assetPath); |
| 1714 | final OutputStream out = new FileOutputStream(netConfigFile)) { |
| 1715 | Streams.copy(in, out); |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | /** |
| 1721 | * Annotation used to define the relative path of the {@code netpolicy.xml} file. |
| 1722 | */ |
| 1723 | @Retention(RetentionPolicy.RUNTIME) |
| 1724 | @Target(ElementType.METHOD) |
| 1725 | public @interface NetPolicyXml { |
| 1726 | |
| 1727 | public String value() default ""; |
| 1728 | |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * Rule used to set {@code mNetPolicyXml} according to the {@link NetPolicyXml} annotation. |
| 1733 | */ |
| 1734 | public static class NetPolicyMethodRule implements MethodRule { |
| 1735 | |
| 1736 | @Override |
| 1737 | public Statement apply(Statement base, FrameworkMethod method, Object target) { |
| 1738 | for (Annotation annotation : method.getAnnotations()) { |
| 1739 | if ((annotation instanceof NetPolicyXml)) { |
| 1740 | final String path = ((NetPolicyXml) annotation).value(); |
| 1741 | if (!path.isEmpty()) { |
| 1742 | ((NetworkPolicyManagerServiceTest) target).mNetpolicyXml = path; |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | return base; |
| 1748 | } |
| 1749 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1750 | } |