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 android.net; 18 19 import static android.net.NetworkStats.SET_DEFAULT; 20 import static android.net.NetworkStats.SET_FOREGROUND; 21 import static android.net.NetworkStats.SET_ALL; 22 import static android.net.NetworkStats.IFACE_ALL; 23 import static android.net.NetworkStats.TAG_NONE; 24 import static android.net.NetworkStats.UID_ALL; 25 26 import android.test.suitebuilder.annotation.SmallTest; 27 28 import com.google.android.collect.Sets; 29 30 import junit.framework.TestCase; 31 32 import java.util.HashSet; 33 34 @SmallTest 35 public class NetworkStatsTest extends TestCase { 36 37 private static final String TEST_IFACE = "test0"; 38 private static final String TEST_IFACE2 = "test2"; 39 private static final int TEST_UID = 1001; 40 private static final long TEST_START = 1194220800000L; 41 42 public void testFindIndex() throws Exception { 43 final NetworkStats stats = new NetworkStats(TEST_START, 3) 44 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 10) 45 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 11) 46 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 12); 47 48 assertEquals(2, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE)); 49 assertEquals(2, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE)); 50 assertEquals(0, stats.findIndex(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE)); 51 assertEquals(-1, stats.findIndex(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE)); 52 } 53 54 public void testFindIndexHinted() { 55 final NetworkStats stats = new NetworkStats(TEST_START, 3) 56 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 10) 57 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 11) 58 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 12) 59 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 1024L, 8L, 0L, 0L, 10) 60 .addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, 0L, 0L, 1024L, 8L, 11) 61 .addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 12); 62 63 // verify that we correctly find across regardless of hinting 64 for (int hint = 0; hint < stats.size(); hint++) { 65 assertEquals(0, stats.findIndexHinted(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, hint)); 66 assertEquals(1, stats.findIndexHinted(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, hint)); 67 assertEquals(2, stats.findIndexHinted(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, hint)); 68 assertEquals(3, stats.findIndexHinted(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, hint)); 69 assertEquals(4, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, hint)); 70 assertEquals(5, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, hint)); 71 assertEquals(-1, stats.findIndexHinted(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE, hint)); 72 } 73 } 74 75 public void testAddEntryGrow() throws Exception { 76 final NetworkStats stats = new NetworkStats(TEST_START, 2); 77 78 assertEquals(0, stats.size()); 79 assertEquals(2, stats.internalSize()); 80 81 stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 1L, 1L, 2L, 2L, 3); 82 stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 2L, 2L, 2L, 2L, 4); 83 84 assertEquals(2, stats.size()); 85 assertEquals(2, stats.internalSize()); 86 87 stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 3L, 30L, 4L, 40L, 7); 88 stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 4L, 40L, 4L, 40L, 8); 89 stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 5L, 50L, 5L, 50L, 10); 90 91 assertEquals(5, stats.size()); 92 assertTrue(stats.internalSize() >= 5); 93 94 assertValues(stats, 0, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 1L, 1L, 2L, 2L, 3); 95 assertValues(stats, 1, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 2L, 2L, 2L, 2L, 4); 96 assertValues(stats, 2, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 3L, 30L, 4L, 40L, 7); 97 assertValues(stats, 3, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 4L, 40L, 4L, 40L, 8); 98 assertValues(stats, 4, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, 5L, 50L, 5L, 50L, 10); 99 } 100 101 public void testCombineExisting() throws Exception { 102 final NetworkStats stats = new NetworkStats(TEST_START, 10); 103 104 stats.addValues(TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 10); 105 stats.addValues(TEST_IFACE, 1001, SET_DEFAULT, 0xff, 128L, 1L, 128L, 1L, 2); 106 stats.combineValues(TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, -128L, -1L, -128L, -1L, -1); 107 108 assertValues(stats, 0, TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, 384L, 3L, 128L, 1L, 9); 109 assertValues(stats, 1, TEST_IFACE, 1001, SET_DEFAULT, 0xff, 128L, 1L, 128L, 1L, 2); 110 111 // now try combining that should create row 112 stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3); 113 assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3); 114 stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3); 115 assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 256L, 2L, 256L, 2L, 6); 116 } 117 118 public void testSubtractIdenticalData() throws Exception { 119 final NetworkStats before = new NetworkStats(TEST_START, 2) 120 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11) 121 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12); 122 123 final NetworkStats after = new NetworkStats(TEST_START, 2) 124 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11) 125 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12); 126 127 final NetworkStats result = after.subtract(before); 128 129 // identical data should result in zero delta 130 assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0); 131 assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0); 132 } 133 134 public void testSubtractIdenticalRows() throws Exception { 135 final NetworkStats before = new NetworkStats(TEST_START, 2) 136 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11) 137 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12); 138 139 final NetworkStats after = new NetworkStats(TEST_START, 2) 140 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1025L, 9L, 2L, 1L, 15) 141 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 3L, 1L, 1028L, 9L, 20); 142 143 final NetworkStats result = after.subtract(before); 144 145 // expect delta between measurements 146 assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1L, 1L, 2L, 1L, 4); 147 assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 3L, 1L, 4L, 1L, 8); 148 } 149 150 public void testSubtractNewRows() throws Exception { 151 final NetworkStats before = new NetworkStats(TEST_START, 2) 152 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11) 153 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12); 154 155 final NetworkStats after = new NetworkStats(TEST_START, 3) 156 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11) 157 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12) 158 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 20); 159 160 final NetworkStats result = after.subtract(before); 161 162 // its okay to have new rows 163 assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0); 164 assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 0L, 0L, 0); 165 assertValues(result, 2, TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 20); 166 } 167 168 public void testSubtractMissingRows() throws Exception { 169 final NetworkStats before = new NetworkStats(TEST_START, 2) 170 .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 0L, 0L, 0L, 0) 171 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 2048L, 0L, 0L, 0L, 0); 172 173 final NetworkStats after = new NetworkStats(TEST_START, 1) 174 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 2049L, 2L, 3L, 4L, 0); 175 176 final NetworkStats result = after.subtract(before); 177 178 // should silently drop omitted rows 179 assertEquals(1, result.size()); 180 assertValues(result, 0, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L, 0); 181 assertEquals(4L, result.getTotalBytes()); 182 } 183 184 public void testTotalBytes() throws Exception { 185 final NetworkStats iface = new NetworkStats(TEST_START, 2) 186 .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 128L, 0L, 0L, 0L, 0L) 187 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 256L, 0L, 0L, 0L, 0L); 188 assertEquals(384L, iface.getTotalBytes()); 189 190 final NetworkStats uidSet = new NetworkStats(TEST_START, 3) 191 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L) 192 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L) 193 .addValues(TEST_IFACE, 101, SET_FOREGROUND, TAG_NONE, 32L, 0L, 0L, 0L, 0L); 194 assertEquals(96L, uidSet.getTotalBytes()); 195 196 final NetworkStats uidTag = new NetworkStats(TEST_START, 3) 197 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L) 198 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L) 199 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 8L, 0L, 0L, 0L, 0L) 200 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 16L, 0L, 0L, 0L, 0L) 201 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L) 202 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 8L, 0L, 0L, 0L, 0L); 203 assertEquals(64L, uidTag.getTotalBytes()); 204 } 205 206 public void testGroupedByIfaceEmpty() throws Exception { 207 final NetworkStats uidStats = new NetworkStats(TEST_START, 3); 208 final NetworkStats grouped = uidStats.groupedByIface(); 209 210 assertEquals(0, uidStats.size()); 211 assertEquals(0, grouped.size()); 212 } 213 214 public void testGroupedByIfaceAll() throws Exception { 215 final NetworkStats uidStats = new NetworkStats(TEST_START, 3) 216 .addValues(IFACE_ALL, 100, SET_ALL, TAG_NONE, 128L, 8L, 0L, 2L, 20L) 217 .addValues(IFACE_ALL, 101, SET_FOREGROUND, TAG_NONE, 128L, 8L, 0L, 2L, 20L); 218 final NetworkStats grouped = uidStats.groupedByIface(); 219 220 assertEquals(2, uidStats.size()); 221 assertEquals(1, grouped.size()); 222 223 assertValues(grouped, 0, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, 256L, 16L, 0L, 4L, 0L); 224 } 225 226 public void testGroupedByIface() throws Exception { 227 final NetworkStats uidStats = new NetworkStats(TEST_START, 3) 228 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L) 229 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 230 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L) 231 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 232 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L) 233 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L); 234 235 final NetworkStats grouped = uidStats.groupedByIface(); 236 237 assertEquals(6, uidStats.size()); 238 239 assertEquals(2, grouped.size()); 240 assertValues(grouped, 0, TEST_IFACE, UID_ALL, SET_ALL, TAG_NONE, 256L, 16L, 0L, 2L, 0L); 241 assertValues(grouped, 1, TEST_IFACE2, UID_ALL, SET_ALL, TAG_NONE, 1024L, 64L, 0L, 0L, 0L); 242 } 243 244 public void testAddAllValues() { 245 final NetworkStats first = new NetworkStats(TEST_START, 5) 246 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L) 247 .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, 32L, 0L, 0L, 0L, 0L); 248 249 final NetworkStats second = new NetworkStats(TEST_START, 2) 250 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L) 251 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L); 252 253 first.combineAllValues(second); 254 255 assertEquals(3, first.size()); 256 assertValues(first, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 64L, 0L, 0L, 0L, 0L); 257 assertValues(first, 1, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, 32L, 0L, 0L, 0L, 0L); 258 assertValues(first, 2, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L); 259 } 260 261 public void testGetTotal() { 262 final NetworkStats stats = new NetworkStats(TEST_START, 3) 263 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L) 264 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 265 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L) 266 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 267 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L) 268 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L); 269 270 assertValues(stats.getTotal(null), 1280L, 80L, 0L, 2L, 20L); 271 assertValues(stats.getTotal(null, 100), 1152L, 72L, 0L, 2L, 20L); 272 assertValues(stats.getTotal(null, 101), 128L, 8L, 0L, 0L, 0L); 273 274 final HashSet<String> ifaces = Sets.newHashSet(); 275 assertValues(stats.getTotal(null, ifaces), 0L, 0L, 0L, 0L, 0L); 276 277 ifaces.add(TEST_IFACE2); 278 assertValues(stats.getTotal(null, ifaces), 1024L, 64L, 0L, 0L, 0L); 279 } 280 281 public void testWithoutUid() throws Exception { 282 final NetworkStats before = new NetworkStats(TEST_START, 3) 283 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L) 284 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 285 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L) 286 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L) 287 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L) 288 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L); 289 290 final NetworkStats after = before.withoutUid(100); 291 assertEquals(6, before.size()); 292 assertEquals(2, after.size()); 293 assertValues(after, 0, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L); 294 assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L); 295 } 296 297 public void testClone() throws Exception { 298 final NetworkStats original = new NetworkStats(TEST_START, 5) 299 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L) 300 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L); 301 302 // make clone and mutate original 303 final NetworkStats clone = original.clone(); 304 original.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L); 305 306 assertEquals(3, original.size()); 307 assertEquals(2, clone.size()); 308 309 assertEquals(128L + 512L + 128L, original.getTotalBytes()); 310 assertEquals(128L + 512L, clone.getTotalBytes()); 311 } 312 313 private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set, 314 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations) { 315 final NetworkStats.Entry entry = stats.getValues(index, null); 316 assertValues(entry, iface, uid, set, tag); 317 assertValues(entry, rxBytes, rxPackets, txBytes, txPackets, operations); 318 } 319 320 private static void assertValues( 321 NetworkStats.Entry entry, String iface, int uid, int set, int tag) { 322 assertEquals(iface, entry.iface); 323 assertEquals(uid, entry.uid); 324 assertEquals(set, entry.set); 325 assertEquals(tag, entry.tag); 326 } 327 328 private static void assertValues(NetworkStats.Entry entry, long rxBytes, long rxPackets, 329 long txBytes, long txPackets, long operations) { 330 assertEquals(rxBytes, entry.rxBytes); 331 assertEquals(rxPackets, entry.rxPackets); 332 assertEquals(txBytes, entry.txBytes); 333 assertEquals(txPackets, entry.txPackets); 334 assertEquals(operations, entry.operations); 335 } 336 337 } 338