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