NetworkPolicyManagerServiceTest.java revision 4414cea13908b8230640f84ef39603d68ff9c377
19599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey/* 29599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project 39599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * 49599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 59599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * you may not use this file except in compliance with the License. 69599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * You may obtain a copy of the License at 79599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * 89599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 99599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * 109599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 119599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 129599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * See the License for the specific language governing permissions and 149599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * limitations under the License. 159599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey */ 169599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 179599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeypackage com.android.server; 189599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 19b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkeyimport static android.content.Intent.ACTION_UID_REMOVED; 20b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkeyimport static android.content.Intent.EXTRA_UID; 2121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport static android.net.ConnectivityManager.CONNECTIVITY_ACTION; 2221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIFI; 239599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static android.net.NetworkPolicyManager.POLICY_NONE; 24fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkeyimport static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; 259599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; 26fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkeyimport static android.net.NetworkPolicyManager.RULE_REJECT_METERED; 27cd2ca4038a027315832c38c68be5076000bc4b53Jeff Sharkeyimport static android.net.NetworkPolicyManager.computeLastCycleBoundary; 281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.NetworkStats.TAG_NONE; 2921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport static android.net.NetworkStats.UID_ALL; 301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.NetworkTemplate.MATCH_WIFI; 312ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkeyimport static org.easymock.EasyMock.anyInt; 32fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkeyimport static org.easymock.EasyMock.aryEq; 339599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static org.easymock.EasyMock.capture; 349599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static org.easymock.EasyMock.createMock; 359599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static org.easymock.EasyMock.eq; 369599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static org.easymock.EasyMock.expect; 379599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport static org.easymock.EasyMock.expectLastCall; 382ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkeyimport static org.easymock.EasyMock.isA; 399599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 409599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.app.IActivityManager; 412ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkeyimport android.app.INotificationManager; 429599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.app.IProcessObserver; 439599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.content.Intent; 444414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.content.pm.PackageInfo; 459599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.content.pm.PackageManager; 464414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.content.pm.Signature; 479599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.net.ConnectivityManager; 4821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.IConnectivityManager; 499599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.net.INetworkPolicyListener; 5075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport android.net.INetworkStatsService; 5121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.LinkProperties; 5221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.NetworkInfo; 5321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.NetworkInfo.DetailedState; 5421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.NetworkPolicy; 5521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.NetworkState; 5621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.net.NetworkStats; 571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.net.NetworkTemplate; 589599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.os.Binder; 599599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.os.IPowerManager; 609599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.test.AndroidTestCase; 619599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.test.mock.MockPackageManager; 629599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.test.suitebuilder.annotation.LargeTest; 639599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport android.test.suitebuilder.annotation.Suppress; 6421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.text.format.Time; 6521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport android.util.TrustedTime; 669599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 679599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport com.android.server.net.NetworkPolicyManagerService; 684414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport com.google.common.util.concurrent.AbstractFuture; 699599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 709599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport org.easymock.Capture; 719599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport org.easymock.EasyMock; 724414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport org.easymock.IAnswer; 739599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 7421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkeyimport java.io.File; 754414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.util.concurrent.ExecutionException; 769599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeyimport java.util.concurrent.Future; 774414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.util.concurrent.TimeUnit; 784414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.util.concurrent.TimeoutException; 799599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 809599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey/** 819599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey * Tests for {@link NetworkPolicyManagerService}. 829599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey */ 839599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey@LargeTest 849599cc5f21152860af9d18015b1398b50743da76Jeff Sharkeypublic class NetworkPolicyManagerServiceTest extends AndroidTestCase { 859599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private static final String TAG = "NetworkPolicyManagerServiceTest"; 869599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 8721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private static final long TEST_START = 1194220800000L; 8821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private static final String TEST_IFACE = "test0"; 8921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null); 911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 929599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private BroadcastInterceptingContext mServiceContext; 9321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private File mPolicyDir; 949599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 959599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private IActivityManager mActivityManager; 969599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private IPowerManager mPowerManager; 9775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey private INetworkStatsService mStatsService; 989599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private INetworkPolicyListener mPolicyListener; 9921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private TrustedTime mTime; 10021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private IConnectivityManager mConnManager; 1012ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey private INotificationManager mNotifManager; 1029599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1039599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private NetworkPolicyManagerService mService; 1049599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private IProcessObserver mProcessObserver; 1059599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1069599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private Binder mStubBinder = new Binder(); 1079599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1082ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey private static final int UID_A = android.os.Process.FIRST_APPLICATION_UID + 800; 1092ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey private static final int UID_B = android.os.Process.FIRST_APPLICATION_UID + 801; 1109599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1119599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private static final int PID_1 = 400; 1129599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private static final int PID_2 = 401; 1139599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private static final int PID_3 = 402; 1149599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1159599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey @Override 1169599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void setUp() throws Exception { 1179599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey super.setUp(); 1189599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1199599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // intercept various broadcasts, and pretend that uids have packages 1209599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mServiceContext = new BroadcastInterceptingContext(getContext()) { 1219599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey @Override 1229599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public PackageManager getPackageManager() { 1239599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey return new MockPackageManager() { 1249599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey @Override 1259599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public String[] getPackagesForUid(int uid) { 1269599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey return new String[] { "com.example" }; 1279599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 1284414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 1294414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey @Override 1304414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey public PackageInfo getPackageInfo(String packageName, int flags) { 1314414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey final PackageInfo info = new PackageInfo(); 1324414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey final Signature signature; 1334414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey if ("android".equals(packageName)) { 1344414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey signature = new Signature("F00D"); 1354414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } else { 1364414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey signature = new Signature("DEAD"); 1374414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 1384414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey info.signatures = new Signature[] { signature }; 1394414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey return info; 1404414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 1419599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey }; 1429599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 1439599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey }; 1449599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 14521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mPolicyDir = getContext().getFilesDir(); 1464414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey for (File file : mPolicyDir.listFiles()) { 1474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey file.delete(); 1484414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 14921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 1509599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mActivityManager = createMock(IActivityManager.class); 1519599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mPowerManager = createMock(IPowerManager.class); 15275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey mStatsService = createMock(INetworkStatsService.class); 1539599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mPolicyListener = createMock(INetworkPolicyListener.class); 15421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mTime = createMock(TrustedTime.class); 15521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mConnManager = createMock(IConnectivityManager.class); 1562ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mNotifManager = createMock(INotificationManager.class); 1579599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1589599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService = new NetworkPolicyManagerService( 15921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mServiceContext, mActivityManager, mPowerManager, mStatsService, mTime, mPolicyDir); 16021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mService.bindConnectivityManager(mConnManager); 1612ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mService.bindNotificationManager(mNotifManager); 1629599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1639599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // RemoteCallbackList needs a binder to use as key 1649599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce(); 1659599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 1669599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService.registerListener(mPolicyListener); 1679599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 1689599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1699599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // catch the registered IProcessObserver during systemReady() 1709599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>(); 1719599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mActivityManager.registerProcessObserver(capture(processObserver)); 1729599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey expectLastCall().atLeastOnce(); 1739599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1749599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // expect to answer screen status during systemReady() 1759599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce(); 1762ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey expectTime(System.currentTimeMillis()); 1779599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1789599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 1799599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService.systemReady(); 1809599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 1819599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1829599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver = processObserver.getValue(); 1839599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1849599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 1859599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1869599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey @Override 1879599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void tearDown() throws Exception { 18821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey for (File file : mPolicyDir.listFiles()) { 18921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey file.delete(); 19021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 19121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 1929599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mServiceContext = null; 19321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mPolicyDir = null; 1949599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 1959599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mActivityManager = null; 1969599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mPowerManager = null; 19775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey mStatsService = null; 1989599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mPolicyListener = null; 19921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mTime = null; 2009599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2019599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService = null; 2029599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver = null; 2039599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2049599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey super.tearDown(); 2059599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 2069599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2079599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey @Suppress 2089599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testPolicyChangeTriggersBroadcast() throws Exception { 2099599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_NONE); 2109599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2119599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // change background policy and expect broadcast 2129599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent( 2139599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); 2149599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 215fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); 2169599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2179599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey backgroundChanged.get(); 2189599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 2199599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2209599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testPidForegroundCombined() throws Exception { 2219599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // push all uid into background 2229599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); 2239599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); 2249599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false); 2259599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertFalse(mService.isUidForeground(UID_A)); 2269599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertFalse(mService.isUidForeground(UID_B)); 2279599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2289599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // push one of the shared pids into foreground 2299599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true); 2309599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertTrue(mService.isUidForeground(UID_A)); 2319599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertFalse(mService.isUidForeground(UID_B)); 2329599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2339599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // and swap another uid into foreground 2349599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); 2359599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true); 2369599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertFalse(mService.isUidForeground(UID_A)); 2379599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertTrue(mService.isUidForeground(UID_B)); 2389599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2399599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // push both pid into foreground 2409599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); 2419599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true); 2429599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertTrue(mService.isUidForeground(UID_A)); 2439599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2449599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // pull one out, should still be foreground 2459599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); 2469599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertTrue(mService.isUidForeground(UID_A)); 2479599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2489599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // pull final pid out, should now be background 2499599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); 2509599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey assertFalse(mService.isUidForeground(UID_A)); 2519599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 2529599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2539599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testScreenChangesRules() throws Exception { 2544414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 2554414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 2564414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 2579599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 2589599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); 2594414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 2604414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey verifyAndReset(); 2614414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 2624414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey // push strict policy for foreground uid, verify ALLOW rule 2634414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 2644414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey replay(); 265fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); 2664414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 2679599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 2689599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2699599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // now turn screen off and verify REJECT rule 2709599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey expect(mPowerManager.isScreenOn()).andReturn(false).atLeastOnce(); 2714414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_REJECT_METERED); 2729599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 2739599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF)); 2744414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 2759599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 2769599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2779599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // and turn screen back on, verify ALLOW rule restored 2789599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce(); 2794414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 2809599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 2819599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON)); 2824414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 2839599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 2849599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 2859599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 2869599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testPolicyNone() throws Exception { 2874414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 2884414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 2894414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 2904414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey replay(); 2914414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); 2924414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 2934414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey verifyAndReset(); 2944414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 2959599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_NONE should RULE_ALLOW in foreground 2964414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 2979599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 2989599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_NONE); 2994414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3009599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3019599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3029599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_NONE should RULE_ALLOW in background 3034414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 3049599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 3059599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); 3064414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3079599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3089599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 3099599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3109599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testPolicyReject() throws Exception { 3114414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 3124414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 3139599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_REJECT should RULE_ALLOW in background 3144414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_REJECT_METERED); 3159599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 316fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); 3174414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3189599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3199599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3209599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_REJECT should RULE_ALLOW in foreground 3214414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 3229599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 3239599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); 3244414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3259599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3269599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3279599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_REJECT should RULE_REJECT in background 3284414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_REJECT_METERED); 3299599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 3309599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); 3314414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3329599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3339599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 3349599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3359599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey public void testPolicyRejectAddRemove() throws Exception { 3364414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 3374414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 3389599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // POLICY_NONE should have RULE_ALLOW in background 3394414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 3409599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 3419599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); 3424414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_NONE); 3434414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3449599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3459599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3469599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // adding POLICY_REJECT should cause RULE_REJECT 3474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_REJECT_METERED); 3489599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 349fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); 3504414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3519599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3529599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 3539599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey // removing POLICY_REJECT should return us to RULE_ALLOW 3544414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 3559599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey replay(); 3569599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey mService.setUidPolicy(UID_A, POLICY_NONE); 3574414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 3589599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey verifyAndReset(); 3599599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 3609599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 36121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey public void testLastCycleBoundaryThisMonth() throws Exception { 36221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // assume cycle day of "5th", which should be in same month 36321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); 36421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z"); 36521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 3661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 5, 1024L, 1024L); 36721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long actualCycle = computeLastCycleBoundary(currentTime, policy); 36821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey assertEquals(expectedCycle, actualCycle); 36921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 37021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 37121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey public void testLastCycleBoundaryLastMonth() throws Exception { 37221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // assume cycle day of "20th", which should be in last month 37321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long currentTime = parseTime("2007-11-14T00:00:00.000Z"); 37421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z"); 37521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 3761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 20, 1024L, 1024L); 37721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long actualCycle = computeLastCycleBoundary(currentTime, policy); 37821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey assertEquals(expectedCycle, actualCycle); 37921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 38021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 38121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey public void testLastCycleBoundaryThisMonthFebruary() throws Exception { 38221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // assume cycle day of "30th" in february; should go to january 38321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long currentTime = parseTime("2007-02-14T00:00:00.000Z"); 38421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z"); 38521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 3861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 30, 1024L, 1024L); 38721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long actualCycle = computeLastCycleBoundary(currentTime, policy); 38821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey assertEquals(expectedCycle, actualCycle); 38921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 39021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 39121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey public void testLastCycleBoundaryLastMonthFebruary() throws Exception { 39221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // assume cycle day of "30th" in february, which should clamp 39321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long currentTime = parseTime("2007-03-14T00:00:00.000Z"); 39421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long expectedCycle = parseTime("2007-03-01T00:00:00.000Z"); 39521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 3961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkPolicy policy = new NetworkPolicy(sTemplateWifi, 30, 1024L, 1024L); 39721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long actualCycle = computeLastCycleBoundary(currentTime, policy); 39821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey assertEquals(expectedCycle, actualCycle); 39921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 40021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 40121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey public void testNetworkPolicyAppliedCycleLastMonth() throws Exception { 40221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey long elapsedRealtime = 0; 40321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey NetworkState[] state = null; 40421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey NetworkStats stats = null; 4054414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 40621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 40721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long TIME_FEB_15 = 1171497600000L; 40821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final long TIME_MAR_10 = 1173484800000L; 40921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final int CYCLE_DAY = 15; 41021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 41121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // first, pretend that wifi network comes online. no policy active, 41221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // which means we shouldn't push limit to interface. 41321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey state = new NetworkState[] { buildWifi() }; 41421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); 41521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expectTime(TIME_MAR_10 + elapsedRealtime); 4164414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectMeteredIfacesChanged(); 41721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 41821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey replay(); 41921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); 4204414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 42121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey verifyAndReset(); 42221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 42321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // now change cycle to be on 15th, and test in early march, to verify we 42421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // pick cycle day in previous month. 42521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); 42621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expectTime(TIME_MAR_10 + elapsedRealtime); 42721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 42821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // pretend that 512 bytes total have happened 4294a97122ebf4d92a3f94402041729d77905e6c0c0Jeff Sharkey stats = new NetworkStats(elapsedRealtime, 1) 4301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 256L, 256L); 4311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey expect(mStatsService.getSummaryForNetwork(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)) 43221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey .andReturn(stats).atLeastOnce(); 43321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 43421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // expect that quota remaining should be 1536 bytes 43521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey // TODO: write up NetworkManagementService mock 43621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 4372ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey expectClearNotifications(); 4384414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectMeteredIfacesChanged(TEST_IFACE); 4392ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey 44021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey replay(); 4411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, 1024L, 2048L)); 4424414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 44321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey verifyAndReset(); 44421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 44521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 446b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey public void testUidRemovedPolicyCleared() throws Exception { 4474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey Future<Void> future; 4484414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 449b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey // POLICY_REJECT should RULE_REJECT in background 4504414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_REJECT_METERED); 451b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey replay(); 452b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey mService.setUidPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND); 4534414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 454b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey verifyAndReset(); 455b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey 456b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey // uninstall should clear RULE_REJECT 4574414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); 458b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey replay(); 459b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey final Intent intent = new Intent(ACTION_UID_REMOVED); 460b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey intent.putExtra(EXTRA_UID, UID_A); 461b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey mServiceContext.sendBroadcast(intent); 4624414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey future.get(); 463b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey verifyAndReset(); 464b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey } 465b09540f33a6cabe50edec0ef32d0b1d0b0d96fffJeff Sharkey 46621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private static long parseTime(String time) { 46721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final Time result = new Time(); 46821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey result.parse3339(time); 46921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey return result.toMillis(true); 47021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 47121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 472af11d4859582a9736aa204562f0beac5a7d60934Jeff Sharkey private void setNetworkPolicies(NetworkPolicy... policies) { 473af11d4859582a9736aa204562f0beac5a7d60934Jeff Sharkey mService.setNetworkPolicies(policies); 474af11d4859582a9736aa204562f0beac5a7d60934Jeff Sharkey } 475af11d4859582a9736aa204562f0beac5a7d60934Jeff Sharkey 47621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey private static NetworkState buildWifi() { 47721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); 47821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey info.setDetailedState(DetailedState.CONNECTED, null, null); 47921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey final LinkProperties prop = new LinkProperties(); 48021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey prop.setInterfaceName(TEST_IFACE); 48121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey return new NetworkState(info, prop, null); 48221c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 48321c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 4842ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey private void expectTime(long currentTime) throws Exception { 48521c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mTime.forceRefresh()).andReturn(false).anyTimes(); 48621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mTime.hasCache()).andReturn(true).anyTimes(); 48721c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes(); 48821c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); 48921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); 49021c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey } 49121c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey 4922ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey private void expectClearNotifications() throws Exception { 4932ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mNotifManager.cancelNotificationWithTag(isA(String.class), isA(String.class), anyInt()); 4942ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey expectLastCall().anyTimes(); 4952ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey } 4962ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey 4974414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey private Future<Void> expectRulesChanged(int uid, int policy) throws Exception { 4984414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey final FutureAnswer future = new FutureAnswer(); 499fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mPolicyListener.onUidRulesChanged(eq(uid), eq(policy)); 5004414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey expectLastCall().andAnswer(future); 5014414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey return future; 502fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey } 503fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey 5044414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception { 5054414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey final FutureAnswer future = new FutureAnswer(); 506fdfef57f498e3021a34342538aef9f1c7ccbae78Jeff Sharkey mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces)); 5074414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey expectLastCall().andAnswer(future); 5084414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey return future; 5094414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 5104414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 5114414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey private static class FutureAnswer extends AbstractFuture<Void> implements IAnswer<Void> { 5124414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey @Override 5134414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey public Void get() throws InterruptedException, ExecutionException { 5144414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey try { 5154414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey return get(5, TimeUnit.SECONDS); 5164414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } catch (TimeoutException e) { 5174414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey throw new RuntimeException(e); 5184414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 5194414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 5204414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey 5214414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey @Override 5224414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey public Void answer() { 5234414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey set(null); 5244414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey return null; 5254414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey } 5269599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 5279599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 5289599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private void replay() { 52921c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener, mTime, 5302ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mConnManager, mNotifManager); 5319599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 5329599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey 5339599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey private void verifyAndReset() { 53421c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener, mTime, 5352ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mConnManager, mNotifManager); 53621c9c45e5caf62b935354b74392fb40c4bf18529Jeff Sharkey EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener, mTime, 5372ef2aeb18d9d1ca7e25ad705c4da559f3682805aJeff Sharkey mConnManager, mNotifManager); 5389599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey } 5399599cc5f21152860af9d18015b1398b50743da76Jeff Sharkey} 540