Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -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 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 19 | import static android.content.Intent.ACTION_UID_REMOVED; |
| 20 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 22 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 24 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 25 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 26 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 27 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 28 | import static android.net.TrafficStats.UID_REMOVED; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 29 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
| 30 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 31 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
| 32 | import static android.text.format.DateUtils.WEEK_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 33 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL; |
| 34 | import static org.easymock.EasyMock.anyLong; |
| 35 | import static org.easymock.EasyMock.createMock; |
| 36 | import static org.easymock.EasyMock.eq; |
| 37 | import static org.easymock.EasyMock.expect; |
| 38 | import static org.easymock.EasyMock.expectLastCall; |
| 39 | import static org.easymock.EasyMock.isA; |
| 40 | |
| 41 | import android.app.AlarmManager; |
| 42 | import android.app.IAlarmManager; |
| 43 | import android.app.PendingIntent; |
| 44 | import android.content.Intent; |
| 45 | import android.net.IConnectivityManager; |
| 46 | import android.net.LinkProperties; |
| 47 | import android.net.NetworkInfo; |
| 48 | import android.net.NetworkInfo.DetailedState; |
| 49 | import android.net.NetworkState; |
| 50 | import android.net.NetworkStats; |
| 51 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 52 | import android.net.NetworkTemplate; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 53 | import android.os.INetworkManagementService; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 54 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 55 | import android.test.AndroidTestCase; |
| 56 | import android.test.suitebuilder.annotation.LargeTest; |
| 57 | import android.util.TrustedTime; |
| 58 | |
| 59 | import com.android.server.net.NetworkStatsService; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 60 | import com.android.server.net.NetworkStatsService.NetworkStatsSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 61 | |
| 62 | import org.easymock.EasyMock; |
| 63 | |
| 64 | import java.io.File; |
| 65 | |
| 66 | /** |
| 67 | * Tests for {@link NetworkStatsService}. |
| 68 | */ |
| 69 | @LargeTest |
| 70 | public class NetworkStatsServiceTest extends AndroidTestCase { |
| 71 | private static final String TAG = "NetworkStatsServiceTest"; |
| 72 | |
| 73 | private static final String TEST_IFACE = "test0"; |
| 74 | private static final long TEST_START = 1194220800000L; |
| 75 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 76 | private static final String IMSI_1 = "310004"; |
| 77 | private static final String IMSI_2 = "310260"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 78 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 79 | private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null); |
| 80 | private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1); |
| 81 | private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2); |
| 82 | |
| 83 | private static final int TEST_UID_RED = 1001; |
| 84 | private static final int TEST_UID_BLUE = 1002; |
| 85 | private static final int TEST_UID_GREEN = 1003; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 87 | private BroadcastInterceptingContext mServiceContext; |
| 88 | private File mStatsDir; |
| 89 | |
| 90 | private INetworkManagementService mNetManager; |
| 91 | private IAlarmManager mAlarmManager; |
| 92 | private TrustedTime mTime; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 93 | private NetworkStatsSettings mSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 94 | private IConnectivityManager mConnManager; |
| 95 | |
| 96 | private NetworkStatsService mService; |
| 97 | |
| 98 | @Override |
| 99 | public void setUp() throws Exception { |
| 100 | super.setUp(); |
| 101 | |
| 102 | mServiceContext = new BroadcastInterceptingContext(getContext()); |
| 103 | mStatsDir = getContext().getFilesDir(); |
| 104 | |
| 105 | mNetManager = createMock(INetworkManagementService.class); |
| 106 | mAlarmManager = createMock(IAlarmManager.class); |
| 107 | mTime = createMock(TrustedTime.class); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 108 | mSettings = createMock(NetworkStatsSettings.class); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 109 | mConnManager = createMock(IConnectivityManager.class); |
| 110 | |
| 111 | mService = new NetworkStatsService( |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 112 | mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 113 | mService.bindConnectivityManager(mConnManager); |
| 114 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 115 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 116 | expectSystemReady(); |
| 117 | |
| 118 | replay(); |
| 119 | mService.systemReady(); |
| 120 | verifyAndReset(); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void tearDown() throws Exception { |
| 126 | for (File file : mStatsDir.listFiles()) { |
| 127 | file.delete(); |
| 128 | } |
| 129 | |
| 130 | mServiceContext = null; |
| 131 | mStatsDir = null; |
| 132 | |
| 133 | mNetManager = null; |
| 134 | mAlarmManager = null; |
| 135 | mTime = null; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 136 | mSettings = null; |
| 137 | mConnManager = null; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 138 | |
| 139 | mService = null; |
| 140 | |
| 141 | super.tearDown(); |
| 142 | } |
| 143 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 144 | public void testNetworkStatsWifi() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 145 | long elapsedRealtime = 0; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 146 | |
| 147 | // pretend that wifi network comes online; service should ask about full |
| 148 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 149 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 150 | expectDefaultSettings(); |
| 151 | expectNetworkState(buildWifiState()); |
| 152 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 153 | |
| 154 | replay(); |
| 155 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 156 | |
| 157 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 158 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 159 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 160 | |
| 161 | // modify some number on wifi, and trigger poll event |
| 162 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 163 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 164 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 165 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 166 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 167 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 168 | |
| 169 | replay(); |
| 170 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 171 | |
| 172 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 173 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 174 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 175 | |
| 176 | // and bump forward again, with counters going higher. this is |
| 177 | // important, since polling should correctly subtract last snapshot. |
| 178 | elapsedRealtime += DAY_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 179 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 180 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 181 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 182 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 183 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 184 | |
| 185 | replay(); |
| 186 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 187 | |
| 188 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 189 | assertNetworkTotal(sTemplateWifi, 4096L, 8192L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 190 | verifyAndReset(); |
| 191 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 194 | public void testStatsRebootPersist() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 195 | long elapsedRealtime = 0; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 196 | assertStatsFilesExist(false); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 197 | |
| 198 | // pretend that wifi network comes online; service should ask about full |
| 199 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 200 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 201 | expectDefaultSettings(); |
| 202 | expectNetworkState(buildWifiState()); |
| 203 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 204 | |
| 205 | replay(); |
| 206 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 207 | |
| 208 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 209 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 210 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 211 | |
| 212 | // modify some number on wifi, and trigger poll event |
| 213 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 214 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 215 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 216 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 217 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 218 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2) |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 219 | .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 512L, 256L) |
| 220 | .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 128L, 128L)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 221 | |
| 222 | replay(); |
| 223 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 224 | |
| 225 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 226 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 227 | assertUidTotal(sTemplateWifi, TEST_UID_RED, 512L, 256L); |
| 228 | assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 229 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 230 | |
| 231 | // graceful shutdown system, which should trigger persist of stats, and |
| 232 | // clear any values in memory. |
| 233 | mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN)); |
| 234 | |
| 235 | // talk with zombie service to assert stats have gone; and assert that |
| 236 | // we persisted them to file. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 237 | expectDefaultSettings(); |
| 238 | replay(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 239 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 240 | verifyAndReset(); |
| 241 | |
| 242 | assertStatsFilesExist(true); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 243 | |
| 244 | // boot through serviceReady() again |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 245 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 246 | expectSystemReady(); |
| 247 | |
| 248 | replay(); |
| 249 | mService.systemReady(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 250 | |
| 251 | // after systemReady(), we should have historical stats loaded again |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 252 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 253 | assertUidTotal(sTemplateWifi, TEST_UID_RED, 512L, 256L); |
| 254 | assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 255 | verifyAndReset(); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | public void testStatsBucketResize() throws Exception { |
| 260 | long elapsedRealtime = 0; |
| 261 | NetworkStatsHistory history = null; |
| 262 | long[] total = null; |
| 263 | |
| 264 | assertStatsFilesExist(false); |
| 265 | |
| 266 | // pretend that wifi network comes online; service should ask about full |
| 267 | // network state, and poll any existing interfaces before updating. |
| 268 | expectTime(TEST_START + elapsedRealtime); |
| 269 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 270 | expectNetworkState(buildWifiState()); |
| 271 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 272 | |
| 273 | replay(); |
| 274 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 275 | verifyAndReset(); |
| 276 | |
| 277 | // modify some number on wifi, and trigger poll event |
| 278 | elapsedRealtime += 2 * HOUR_IN_MILLIS; |
| 279 | expectTime(TEST_START + elapsedRealtime); |
| 280 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 281 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 282 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 283 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 284 | |
| 285 | replay(); |
| 286 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 287 | |
| 288 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 289 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 290 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 291 | assertEquals(512L, total[0]); |
| 292 | assertEquals(512L, total[1]); |
| 293 | assertEquals(HOUR_IN_MILLIS, history.bucketDuration); |
| 294 | assertEquals(2, history.bucketCount); |
| 295 | verifyAndReset(); |
| 296 | |
| 297 | // now change bucket duration setting and trigger another poll with |
| 298 | // exact same values, which should resize existing buckets. |
| 299 | expectTime(TEST_START + elapsedRealtime); |
| 300 | expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS); |
| 301 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 302 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 303 | |
| 304 | replay(); |
| 305 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 306 | |
| 307 | // verify identical stats, but spread across 4 buckets now |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 308 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 309 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 310 | assertEquals(512L, total[0]); |
| 311 | assertEquals(512L, total[1]); |
| 312 | assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration); |
| 313 | assertEquals(4, history.bucketCount); |
| 314 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 315 | |
| 316 | } |
| 317 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 318 | public void testUidStatsAcrossNetworks() throws Exception { |
| 319 | long elapsedRealtime = 0; |
| 320 | |
| 321 | // pretend first mobile network comes online |
| 322 | expectTime(TEST_START + elapsedRealtime); |
| 323 | expectDefaultSettings(); |
| 324 | expectNetworkState(buildMobile3gState(IMSI_1)); |
| 325 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 326 | |
| 327 | replay(); |
| 328 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 329 | verifyAndReset(); |
| 330 | |
| 331 | // create some traffic on first network |
| 332 | elapsedRealtime += HOUR_IN_MILLIS; |
| 333 | expectTime(TEST_START + elapsedRealtime); |
| 334 | expectDefaultSettings(); |
| 335 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 336 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 512L)); |
| 337 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 3) |
| 338 | .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 1024L, 0L) |
| 339 | .addEntry(TEST_IFACE, TEST_UID_RED, 0xF00D, 512L, 512L) |
| 340 | .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 512L, 0L)); |
| 341 | |
| 342 | replay(); |
| 343 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 344 | |
| 345 | // verify service recorded history |
| 346 | assertNetworkTotal(sTemplateImsi1, 2048L, 512L); |
| 347 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
| 348 | assertUidTotal(sTemplateImsi1, TEST_UID_RED, 1536L, 512L); |
| 349 | assertUidTotal(sTemplateImsi1, TEST_UID_BLUE, 512L, 0L); |
| 350 | verifyAndReset(); |
| 351 | |
| 352 | // now switch networks; this also tests that we're okay with interfaces |
| 353 | // disappearing, to verify we don't count backwards. |
| 354 | elapsedRealtime += HOUR_IN_MILLIS; |
| 355 | expectTime(TEST_START + elapsedRealtime); |
| 356 | expectDefaultSettings(); |
| 357 | expectNetworkState(buildMobile3gState(IMSI_2)); |
| 358 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 359 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 360 | |
| 361 | replay(); |
| 362 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 363 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 364 | verifyAndReset(); |
| 365 | |
| 366 | // create traffic on second network |
| 367 | elapsedRealtime += HOUR_IN_MILLIS; |
| 368 | expectTime(TEST_START + elapsedRealtime); |
| 369 | expectDefaultSettings(); |
| 370 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 371 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1024L)); |
| 372 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 373 | .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 128L, 1024L)); |
| 374 | |
| 375 | replay(); |
| 376 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 377 | |
| 378 | // verify original history still intact |
| 379 | assertNetworkTotal(sTemplateImsi1, 2048L, 512L); |
| 380 | assertUidTotal(sTemplateImsi1, TEST_UID_RED, 1536L, 512L); |
| 381 | assertUidTotal(sTemplateImsi1, TEST_UID_BLUE, 512L, 0L); |
| 382 | |
| 383 | // and verify new history also recorded under different template, which |
| 384 | // verifies that we didn't cross the streams. |
| 385 | assertNetworkTotal(sTemplateImsi2, 128L, 1024L); |
| 386 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
| 387 | assertUidTotal(sTemplateImsi2, TEST_UID_BLUE, 128L, 1024L); |
| 388 | verifyAndReset(); |
| 389 | |
| 390 | } |
| 391 | |
| 392 | public void testUidRemovedIsMoved() throws Exception { |
| 393 | long elapsedRealtime = 0; |
| 394 | |
| 395 | // pretend that network comes online |
| 396 | expectTime(TEST_START + elapsedRealtime); |
| 397 | expectDefaultSettings(); |
| 398 | expectNetworkState(buildWifiState()); |
| 399 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 400 | |
| 401 | replay(); |
| 402 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 403 | verifyAndReset(); |
| 404 | |
| 405 | // create some traffic |
| 406 | elapsedRealtime += HOUR_IN_MILLIS; |
| 407 | expectTime(TEST_START + elapsedRealtime); |
| 408 | expectDefaultSettings(); |
| 409 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 410 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 544L)); |
| 411 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 412 | .addEntry(TEST_IFACE, TEST_UID_RED, TAG_NONE, 16L, 16L) |
| 413 | .addEntry(TEST_IFACE, TEST_UID_BLUE, TAG_NONE, 4096L, 512L) |
| 414 | .addEntry(TEST_IFACE, TEST_UID_GREEN, TAG_NONE, 16L, 16L)); |
| 415 | |
| 416 | replay(); |
| 417 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 418 | |
| 419 | // verify service recorded history |
| 420 | assertNetworkTotal(sTemplateWifi, 4128L, 544L); |
| 421 | assertUidTotal(sTemplateWifi, TEST_UID_RED, 16L, 16L); |
| 422 | assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 4096L, 512L); |
| 423 | assertUidTotal(sTemplateWifi, TEST_UID_GREEN, 16L, 16L); |
| 424 | verifyAndReset(); |
| 425 | |
| 426 | // now pretend two UIDs are uninstalled, which should migrate stats to |
| 427 | // special "removed" bucket. |
| 428 | expectDefaultSettings(); |
| 429 | replay(); |
| 430 | final Intent intent = new Intent(ACTION_UID_REMOVED); |
| 431 | intent.putExtra(EXTRA_UID, TEST_UID_BLUE); |
| 432 | mServiceContext.sendBroadcast(intent); |
| 433 | intent.putExtra(EXTRA_UID, TEST_UID_RED); |
| 434 | mServiceContext.sendBroadcast(intent); |
| 435 | |
| 436 | // existing uid and total should remain unchanged; but removed UID |
| 437 | // should be gone completely. |
| 438 | assertNetworkTotal(sTemplateWifi, 4128L, 544L); |
| 439 | assertUidTotal(sTemplateWifi, TEST_UID_RED, 0L, 0L); |
| 440 | assertUidTotal(sTemplateWifi, TEST_UID_BLUE, 0L, 0L); |
| 441 | assertUidTotal(sTemplateWifi, TEST_UID_GREEN, 16L, 16L); |
| 442 | assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 528L); |
| 443 | verifyAndReset(); |
| 444 | |
| 445 | } |
| 446 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 447 | private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 448 | final NetworkStatsHistory history = mService.getHistoryForNetwork(template); |
| 449 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 450 | assertEquals(rx, total[0]); |
| 451 | assertEquals(tx, total[1]); |
| 452 | } |
| 453 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 454 | private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) { |
| 455 | final NetworkStatsHistory history = mService.getHistoryForUid(template, uid); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 456 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 457 | assertEquals(rx, total[0]); |
| 458 | assertEquals(tx, total[1]); |
| 459 | } |
| 460 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 461 | private void expectSystemReady() throws Exception { |
| 462 | mAlarmManager.remove(isA(PendingIntent.class)); |
| 463 | expectLastCall().anyTimes(); |
| 464 | |
| 465 | mAlarmManager.setInexactRepeating( |
| 466 | eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class)); |
| 467 | expectLastCall().atLeastOnce(); |
| 468 | } |
| 469 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 470 | private void expectNetworkState(NetworkState... state) throws Exception { |
| 471 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
| 472 | } |
| 473 | |
| 474 | private void expectNetworkStatsSummary(NetworkStats summary) throws Exception { |
| 475 | expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce(); |
| 476 | } |
| 477 | |
| 478 | private void expectNetworkStatsDetail(NetworkStats detail) throws Exception { |
| 479 | expect(mNetManager.getNetworkStatsDetail()).andReturn(detail).atLeastOnce(); |
| 480 | } |
| 481 | |
| 482 | private void expectDefaultSettings() throws Exception { |
| 483 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 484 | } |
| 485 | |
| 486 | private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory) |
| 487 | throws Exception { |
| 488 | expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes(); |
| 489 | expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes(); |
| 490 | expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 491 | expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes(); |
| 492 | expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 493 | expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes(); |
| 494 | expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes(); |
| 495 | } |
| 496 | |
| 497 | private void expectTime(long currentTime) throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 498 | expect(mTime.forceRefresh()).andReturn(false).anyTimes(); |
| 499 | expect(mTime.hasCache()).andReturn(true).anyTimes(); |
| 500 | expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes(); |
| 501 | expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); |
| 502 | expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); |
| 503 | } |
| 504 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 505 | private void assertStatsFilesExist(boolean exist) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 506 | final File networkFile = new File(mStatsDir, "netstats.bin"); |
| 507 | final File uidFile = new File(mStatsDir, "netstats_uid.bin"); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 508 | if (exist) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 509 | assertTrue(networkFile.exists()); |
| 510 | assertTrue(uidFile.exists()); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 511 | } else { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 512 | assertFalse(networkFile.exists()); |
| 513 | assertFalse(uidFile.exists()); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | private static NetworkState buildWifiState() { |
| 518 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 519 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 520 | final LinkProperties prop = new LinkProperties(); |
| 521 | prop.setInterfaceName(TEST_IFACE); |
| 522 | return new NetworkState(info, prop, null); |
| 523 | } |
| 524 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame^] | 525 | private static NetworkState buildMobile3gState(String subscriberId) { |
| 526 | final NetworkInfo info = new NetworkInfo( |
| 527 | TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null); |
| 528 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 529 | final LinkProperties prop = new LinkProperties(); |
| 530 | prop.setInterfaceName(TEST_IFACE); |
| 531 | return new NetworkState(info, prop, null, subscriberId); |
| 532 | } |
| 533 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 534 | private static NetworkStats buildEmptyStats(long elapsedRealtime) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 535 | return new NetworkStats(elapsedRealtime, 0); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 538 | private void replay() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 539 | EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | private void verifyAndReset() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 543 | EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
| 544 | EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 545 | } |
| 546 | } |