Home | History | Annotate | Download | only in net
      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.METERED_ALL;
     20 import static android.net.NetworkStats.METERED_NO;
     21 import static android.net.NetworkStats.METERED_YES;
     22 import static android.net.NetworkStats.ROAMING_ALL;
     23 import static android.net.NetworkStats.ROAMING_NO;
     24 import static android.net.NetworkStats.ROAMING_YES;
     25 import static android.net.NetworkStats.SET_DEFAULT;
     26 import static android.net.NetworkStats.SET_FOREGROUND;
     27 import static android.net.NetworkStats.SET_DBG_VPN_IN;
     28 import static android.net.NetworkStats.SET_DBG_VPN_OUT;
     29 import static android.net.NetworkStats.SET_ALL;
     30 import static android.net.NetworkStats.IFACE_ALL;
     31 import static android.net.NetworkStats.TAG_NONE;
     32 import static android.net.NetworkStats.UID_ALL;
     33 
     34 import android.test.suitebuilder.annotation.SmallTest;
     35 
     36 import com.google.android.collect.Sets;
     37 
     38 import junit.framework.TestCase;
     39 
     40 import java.util.HashSet;
     41 
     42 @SmallTest
     43 public class NetworkStatsTest extends TestCase {
     44 
     45     private static final String TEST_IFACE = "test0";
     46     private static final String TEST_IFACE2 = "test2";
     47     private static final int TEST_UID = 1001;
     48     private static final long TEST_START = 1194220800000L;
     49 
     50     public void testFindIndex() throws Exception {
     51         final NetworkStats stats = new NetworkStats(TEST_START, 5)
     52                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
     53                         8L, 0L, 0L, 10)
     54                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L,
     55                         1024L, 8L, 11)
     56                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 0L, 0L,
     57                         1024L, 8L, 11)
     58                 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
     59                         8L, 1024L, 8L, 12)
     60                 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 1024L,
     61                         8L, 1024L, 8L, 12);
     62 
     63         assertEquals(4, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_YES,
     64                 ROAMING_YES));
     65         assertEquals(3, stats.findIndex(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO,
     66                 ROAMING_NO));
     67         assertEquals(2, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES,
     68                 ROAMING_NO));
     69         assertEquals(1, stats.findIndex(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO,
     70                 ROAMING_NO));
     71         assertEquals(0, stats.findIndex(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO,
     72                 ROAMING_NO));
     73         assertEquals(-1, stats.findIndex(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE, METERED_NO,
     74                 ROAMING_NO));
     75     }
     76 
     77     public void testFindIndexHinted() {
     78         final NetworkStats stats = new NetworkStats(TEST_START, 3)
     79                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
     80                         8L, 0L, 0L, 10)
     81                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L,
     82                         1024L, 8L, 11)
     83                 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
     84                         8L, 1024L, 8L, 12)
     85                 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
     86                         1024L, 8L, 0L, 0L, 10)
     87                 .addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 0L, 0L,
     88                         1024L, 8L, 11)
     89                 .addValues(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO, 0L, 0L,
     90                         1024L, 8L, 11)
     91                 .addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1024L,
     92                         8L, 1024L, 8L, 12)
     93                 .addValues(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 1024L,
     94                         8L, 1024L, 8L, 12);
     95 
     96         // verify that we correctly find across regardless of hinting
     97         for (int hint = 0; hint < stats.size(); hint++) {
     98             assertEquals(0, stats.findIndexHinted(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE,
     99                     METERED_NO, ROAMING_NO, hint));
    100             assertEquals(1, stats.findIndexHinted(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE,
    101                     METERED_NO, ROAMING_NO, hint));
    102             assertEquals(2, stats.findIndexHinted(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE,
    103                     METERED_NO, ROAMING_NO, hint));
    104             assertEquals(3, stats.findIndexHinted(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE,
    105                     METERED_NO, ROAMING_NO, hint));
    106             assertEquals(4, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D,
    107                     METERED_NO, ROAMING_NO, hint));
    108             assertEquals(5, stats.findIndexHinted(TEST_IFACE2, 101, SET_DEFAULT, 0xF00D,
    109                     METERED_YES, ROAMING_NO, hint));
    110             assertEquals(6, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE,
    111                     METERED_NO, ROAMING_NO, hint));
    112             assertEquals(7, stats.findIndexHinted(TEST_IFACE2, 102, SET_DEFAULT, TAG_NONE,
    113                     METERED_YES, ROAMING_YES, hint));
    114             assertEquals(-1, stats.findIndexHinted(TEST_IFACE, 6, SET_DEFAULT, TAG_NONE,
    115                     METERED_NO, ROAMING_NO, hint));
    116         }
    117     }
    118 
    119     public void testAddEntryGrow() throws Exception {
    120         final NetworkStats stats = new NetworkStats(TEST_START, 4);
    121 
    122         assertEquals(0, stats.size());
    123         assertEquals(4, stats.internalSize());
    124 
    125         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1L, 1L,
    126                 2L, 2L, 3);
    127         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 2L, 2L,
    128                 2L, 2L, 4);
    129         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 3L,
    130                 3L, 2L, 2L, 5);
    131         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 3L,
    132                 3L, 2L, 2L, 5);
    133 
    134         assertEquals(4, stats.size());
    135         assertEquals(4, stats.internalSize());
    136 
    137         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 4L,
    138                 40L, 4L, 40L, 7);
    139         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 5L,
    140                 50L, 4L, 40L, 8);
    141         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 6L,
    142                 60L, 5L, 50L, 10);
    143         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 7L,
    144                 70L, 5L, 50L, 11);
    145         stats.addValues(TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_YES, 7L,
    146                 70L, 5L, 50L, 11);
    147 
    148         assertEquals(9, stats.size());
    149         assertTrue(stats.internalSize() >= 9);
    150 
    151         assertValues(stats, 0, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    152                 1L, 1L, 2L, 2L, 3);
    153         assertValues(stats, 1, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    154                 2L, 2L, 2L, 2L, 4);
    155         assertValues(stats, 2, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
    156                 3L, 3L, 2L, 2L, 5);
    157         assertValues(stats, 3, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES,
    158                 ROAMING_YES, 3L, 3L, 2L, 2L, 5);
    159         assertValues(stats, 4, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    160                 4L, 40L, 4L, 40L, 7);
    161         assertValues(stats, 5, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    162                 5L, 50L, 4L, 40L, 8);
    163         assertValues(stats, 6, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    164                 6L, 60L, 5L, 50L, 10);
    165         assertValues(stats, 7, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES,
    166                 7L, 70L, 5L, 50L, 11);
    167         assertValues(stats, 8, TEST_IFACE, TEST_UID, SET_DEFAULT, TAG_NONE, METERED_YES,
    168                 ROAMING_YES, 7L, 70L, 5L, 50L, 11);
    169     }
    170 
    171     public void testCombineExisting() throws Exception {
    172         final NetworkStats stats = new NetworkStats(TEST_START, 10);
    173 
    174         stats.addValues(TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 10);
    175         stats.addValues(TEST_IFACE, 1001, SET_DEFAULT, 0xff, 128L, 1L, 128L, 1L, 2);
    176         stats.combineValues(TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, -128L, -1L,
    177                 -128L, -1L, -1);
    178 
    179         assertValues(stats, 0, TEST_IFACE, 1001, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    180                 384L, 3L, 128L, 1L, 9);
    181         assertValues(stats, 1, TEST_IFACE, 1001, SET_DEFAULT, 0xff, METERED_NO, ROAMING_NO, 128L,
    182                 1L, 128L, 1L, 2);
    183 
    184         // now try combining that should create row
    185         stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3);
    186         assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    187                 128L, 1L, 128L, 1L, 3);
    188         stats.combineValues(TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 3);
    189         assertValues(stats, 2, TEST_IFACE, 5005, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    190                 256L, 2L, 256L, 2L, 6);
    191     }
    192 
    193     public void testSubtractIdenticalData() throws Exception {
    194         final NetworkStats before = new NetworkStats(TEST_START, 2)
    195                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
    196                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
    197 
    198         final NetworkStats after = new NetworkStats(TEST_START, 2)
    199                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
    200                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
    201 
    202         final NetworkStats result = after.subtract(before);
    203 
    204         // identical data should result in zero delta
    205         assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L,
    206                 0L, 0L, 0L, 0);
    207         assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L,
    208                 0L, 0L, 0L, 0);
    209     }
    210 
    211     public void testSubtractIdenticalRows() throws Exception {
    212         final NetworkStats before = new NetworkStats(TEST_START, 2)
    213                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
    214                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
    215 
    216         final NetworkStats after = new NetworkStats(TEST_START, 2)
    217                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1025L, 9L, 2L, 1L, 15)
    218                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 3L, 1L, 1028L, 9L, 20);
    219 
    220         final NetworkStats result = after.subtract(before);
    221 
    222         // expect delta between measurements
    223         assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1L,
    224                 1L, 2L, 1L, 4);
    225         assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 3L,
    226                 1L, 4L, 1L, 8);
    227     }
    228 
    229     public void testSubtractNewRows() throws Exception {
    230         final NetworkStats before = new NetworkStats(TEST_START, 2)
    231                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
    232                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
    233 
    234         final NetworkStats after = new NetworkStats(TEST_START, 3)
    235                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
    236                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 0L, 0L, 1024L, 8L, 12)
    237                 .addValues(TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 20);
    238 
    239         final NetworkStats result = after.subtract(before);
    240 
    241         // its okay to have new rows
    242         assertValues(result, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L,
    243                 0L, 0L, 0L, 0);
    244         assertValues(result, 1, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 0L,
    245                 0L, 0L, 0L, 0);
    246         assertValues(result, 2, TEST_IFACE, 102, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    247                 1024L, 8L, 1024L, 8L, 20);
    248     }
    249 
    250     public void testSubtractMissingRows() throws Exception {
    251         final NetworkStats before = new NetworkStats(TEST_START, 2)
    252                 .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 0L, 0L, 0L, 0)
    253                 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 2048L, 0L, 0L, 0L, 0);
    254 
    255         final NetworkStats after = new NetworkStats(TEST_START, 1)
    256                 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 2049L, 2L, 3L, 4L, 0);
    257 
    258         final NetworkStats result = after.subtract(before);
    259 
    260         // should silently drop omitted rows
    261         assertEquals(1, result.size());
    262         assertValues(result, 0, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO,
    263                 ROAMING_NO, 1L, 2L, 3L, 4L, 0);
    264         assertEquals(4L, result.getTotalBytes());
    265     }
    266 
    267     public void testTotalBytes() throws Exception {
    268         final NetworkStats iface = new NetworkStats(TEST_START, 2)
    269                 .addValues(TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 128L, 0L, 0L, 0L, 0L)
    270                 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, 256L, 0L, 0L, 0L, 0L);
    271         assertEquals(384L, iface.getTotalBytes());
    272 
    273         final NetworkStats uidSet = new NetworkStats(TEST_START, 3)
    274                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L)
    275                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 32L, 0L, 0L, 0L, 0L)
    276                 .addValues(TEST_IFACE, 101, SET_FOREGROUND, TAG_NONE, 32L, 0L, 0L, 0L, 0L);
    277         assertEquals(96L, uidSet.getTotalBytes());
    278 
    279         final NetworkStats uidTag = new NetworkStats(TEST_START, 6)
    280                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L)
    281                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L)
    282                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 8L, 0L, 0L, 0L, 0L)
    283                 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 16L, 0L, 0L, 0L, 0L)
    284                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 16L, 0L, 0L, 0L, 0L)
    285                 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 8L, 0L, 0L, 0L, 0L);
    286         assertEquals(64L, uidTag.getTotalBytes());
    287 
    288         final NetworkStats uidMetered = new NetworkStats(TEST_START, 3)
    289                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L, 0L,
    290                         0L, 0L, 0L)
    291                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
    292                         0L, 0L, 0L)
    293                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
    294                         0L, 0L, 0L);
    295         assertEquals(96L, uidMetered.getTotalBytes());
    296 
    297         final NetworkStats uidRoaming = new NetworkStats(TEST_START, 3)
    298                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L, 0L,
    299                         0L, 0L, 0L)
    300                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
    301                         0L, 0L, 0L)
    302                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 32L, 0L,
    303                         0L, 0L, 0L);
    304         assertEquals(96L, uidRoaming.getTotalBytes());
    305     }
    306 
    307     public void testGroupedByIfaceEmpty() throws Exception {
    308         final NetworkStats uidStats = new NetworkStats(TEST_START, 3);
    309         final NetworkStats grouped = uidStats.groupedByIface();
    310 
    311         assertEquals(0, uidStats.size());
    312         assertEquals(0, grouped.size());
    313     }
    314 
    315     public void testGroupedByIfaceAll() throws Exception {
    316         final NetworkStats uidStats = new NetworkStats(TEST_START, 3)
    317                 .addValues(IFACE_ALL, 100, SET_ALL, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L, 0L,
    318                         2L, 20L)
    319                 .addValues(IFACE_ALL, 101, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_NO, 128L,
    320                         8L, 0L, 2L, 20L)
    321                 .addValues(IFACE_ALL, 101, SET_ALL, TAG_NONE, METERED_NO, ROAMING_YES, 128L, 8L, 0L,
    322                         2L, 20L);
    323         final NetworkStats grouped = uidStats.groupedByIface();
    324 
    325         assertEquals(3, uidStats.size());
    326         assertEquals(1, grouped.size());
    327 
    328         assertValues(grouped, 0, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
    329                 384L, 24L, 0L, 6L, 0L);
    330     }
    331 
    332     public void testGroupedByIface() throws Exception {
    333         final NetworkStats uidStats = new NetworkStats(TEST_START, 7)
    334                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L,
    335                         0L, 2L, 20L)
    336                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 512L,
    337                         32L, 0L, 0L, 0L)
    338                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, 4L,
    339                         0L, 0L, 0L)
    340                 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 512L,
    341                         32L, 0L, 0L, 0L)
    342                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L,
    343                         0L, 0L, 0L)
    344                 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_YES, ROAMING_NO, 128L, 8L,
    345                         0L, 0L, 0L)
    346                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 128L,
    347                         8L, 0L, 0L, 0L);
    348 
    349         final NetworkStats grouped = uidStats.groupedByIface();
    350 
    351         assertEquals(7, uidStats.size());
    352 
    353         assertEquals(2, grouped.size());
    354         assertValues(grouped, 0, TEST_IFACE, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
    355                 384L, 24L, 0L, 2L, 0L);
    356         assertValues(grouped, 1, TEST_IFACE2, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
    357                 1024L, 64L, 0L, 0L, 0L);
    358     }
    359 
    360     public void testAddAllValues() {
    361         final NetworkStats first = new NetworkStats(TEST_START, 5)
    362                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
    363                         0L, 0L, 0L)
    364                 .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 32L,
    365                         0L, 0L, 0L, 0L)
    366                 .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES, 32L,
    367                         0L, 0L, 0L, 0L);
    368 
    369         final NetworkStats second = new NetworkStats(TEST_START, 2)
    370                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 32L, 0L,
    371                         0L, 0L, 0L)
    372                 .addValues(TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 32L,
    373                         0L, 0L, 0L, 0L)
    374                 .addValues(TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES, 32L,
    375                         0L, 0L, 0L, 0L);
    376 
    377         first.combineAllValues(second);
    378 
    379         assertEquals(4, first.size());
    380         assertValues(first, 0, TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 64L,
    381                 0L, 0L, 0L, 0L);
    382         assertValues(first, 1, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
    383                 32L, 0L, 0L, 0L, 0L);
    384         assertValues(first, 2, TEST_IFACE, 100, SET_FOREGROUND, TAG_NONE, METERED_YES, ROAMING_YES,
    385                 64L, 0L, 0L, 0L, 0L);
    386         assertValues(first, 3, TEST_IFACE2, UID_ALL, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    387                 32L, 0L, 0L, 0L, 0L);
    388     }
    389 
    390     public void testGetTotal() {
    391         final NetworkStats stats = new NetworkStats(TEST_START, 7)
    392                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 128L, 8L,
    393                         0L, 2L, 20L)
    394                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 512L,
    395                         32L, 0L, 0L, 0L)
    396                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 64L, 4L,
    397                         0L, 0L, 0L)
    398                 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 512L,
    399                         32L, 0L, 0L, 0L)
    400                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, 128L,
    401                         8L, 0L, 0L, 0L)
    402                 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 128L, 8L,
    403                         0L, 0L, 0L)
    404                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_YES, 128L,
    405                         8L, 0L, 0L, 0L);
    406 
    407         assertValues(stats.getTotal(null), 1408L, 88L, 0L, 2L, 20L);
    408         assertValues(stats.getTotal(null, 100), 1280L, 80L, 0L, 2L, 20L);
    409         assertValues(stats.getTotal(null, 101), 128L, 8L, 0L, 0L, 0L);
    410 
    411         final HashSet<String> ifaces = Sets.newHashSet();
    412         assertValues(stats.getTotal(null, ifaces), 0L, 0L, 0L, 0L, 0L);
    413 
    414         ifaces.add(TEST_IFACE2);
    415         assertValues(stats.getTotal(null, ifaces), 1024L, 64L, 0L, 0L, 0L);
    416     }
    417 
    418     public void testWithoutUid() throws Exception {
    419         final NetworkStats before = new NetworkStats(TEST_START, 3)
    420                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
    421                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L)
    422                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, 0xF00D, 64L, 4L, 0L, 0L, 0L)
    423                 .addValues(TEST_IFACE2, 100, SET_FOREGROUND, TAG_NONE, 512L, 32L, 0L, 0L, 0L)
    424                 .addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L)
    425                 .addValues(TEST_IFACE, 101, SET_DEFAULT, 0xF00D, 128L, 8L, 0L, 0L, 0L);
    426 
    427         final NetworkStats after = before.withoutUids(new int[] { 100 });
    428         assertEquals(6, before.size());
    429         assertEquals(2, after.size());
    430         assertValues(after, 0, TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    431                 128L, 8L, 0L, 0L, 0L);
    432         assertValues(after, 1, TEST_IFACE, 101, SET_DEFAULT, 0xF00D, METERED_NO, ROAMING_NO, 128L,
    433                 8L, 0L, 0L, 0L);
    434     }
    435 
    436     public void testClone() throws Exception {
    437         final NetworkStats original = new NetworkStats(TEST_START, 5)
    438                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
    439                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L);
    440 
    441         // make clone and mutate original
    442         final NetworkStats clone = original.clone();
    443         original.addValues(TEST_IFACE, 101, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 0L, 0L);
    444 
    445         assertEquals(3, original.size());
    446         assertEquals(2, clone.size());
    447 
    448         assertEquals(128L + 512L + 128L, original.getTotalBytes());
    449         assertEquals(128L + 512L, clone.getTotalBytes());
    450     }
    451 
    452     public void testAddWhenEmpty() throws Exception {
    453         final NetworkStats red = new NetworkStats(TEST_START, -1);
    454         final NetworkStats blue = new NetworkStats(TEST_START, 5)
    455                 .addValues(TEST_IFACE, 100, SET_DEFAULT, TAG_NONE, 128L, 8L, 0L, 2L, 20L)
    456                 .addValues(TEST_IFACE2, 100, SET_DEFAULT, TAG_NONE, 512L, 32L, 0L, 0L, 0L);
    457 
    458         // We're mostly checking that we don't crash
    459         red.combineAllValues(blue);
    460     }
    461 
    462     public void testMigrateTun() throws Exception {
    463         final int tunUid = 10030;
    464         final String tunIface = "tun0";
    465         final String underlyingIface = "wlan0";
    466         final int testTag1 = 8888;
    467         NetworkStats delta = new NetworkStats(TEST_START, 17)
    468             .addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 39605L, 46L,
    469                     12259L, 55L, 0L)
    470             .addValues(tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L,
    471                     0L, 0L, 0L)
    472             .addValues(tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 72667L, 197L,
    473                     43909L, 241L, 0L)
    474             .addValues(tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 9297L,
    475                     17L, 4128L, 21L, 0L)
    476             // VPN package also uses some traffic through unprotected network.
    477             .addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 4983L, 10L,
    478                     1801L, 12L, 0L)
    479             .addValues(tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L, 0L,
    480                     0L, 0L, 0L)
    481             // Tag entries
    482             .addValues(tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO, 21691L, 41L,
    483                     13820L, 51L, 0L)
    484             .addValues(tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO, 1281L, 2L,
    485                     665L, 2L, 0L)
    486             // Irrelevant entries
    487             .addValues(TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1685L, 5L,
    488                     2070L, 6L, 0L)
    489             // Underlying Iface entries
    490             .addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 5178L,
    491                     8L, 2139L, 11L, 0L)
    492             .addValues(underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO, 0L,
    493                     0L, 0L, 0L, 0L)
    494             .addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    495                     149873L, 287L, 59217L /* smaller than sum(tun0) */,
    496                     299L /* smaller than sum(tun0) */, 0L)
    497             .addValues(underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
    498                     0L, 0L, 0L, 0L, 0L);
    499 
    500         assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface));
    501         assertEquals(20, delta.size());
    502 
    503         // tunIface and TEST_IFACE entries are not changed.
    504         assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    505                 39605L, 46L, 12259L, 55L, 0L);
    506         assertValues(delta, 1, tunIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
    507                 0L, 0L, 0L, 0L, 0L);
    508         assertValues(delta, 2, tunIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    509                 72667L, 197L, 43909L, 241L, 0L);
    510         assertValues(delta, 3, tunIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
    511                 9297L, 17L, 4128L, 21L, 0L);
    512         assertValues(delta, 4, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    513                 4983L, 10L, 1801L, 12L, 0L);
    514         assertValues(delta, 5, tunIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO, ROAMING_NO,
    515                 0L, 0L, 0L, 0L, 0L);
    516         assertValues(delta, 6, tunIface, 10120, SET_DEFAULT, testTag1, METERED_NO, ROAMING_NO,
    517                 21691L, 41L, 13820L, 51L, 0L);
    518         assertValues(delta, 7, tunIface, 10120, SET_FOREGROUND, testTag1, METERED_NO, ROAMING_NO,
    519                 1281L, 2L, 665L, 2L, 0L);
    520         assertValues(delta, 8, TEST_IFACE, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    521                 1685L, 5L, 2070L, 6L, 0L);
    522 
    523         // Existing underlying Iface entries are updated
    524         assertValues(delta, 9, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO,
    525                 ROAMING_NO, 44783L, 54L, 14178L, 62L, 0L);
    526         assertValues(delta, 10, underlyingIface, 10100, SET_FOREGROUND, TAG_NONE, METERED_NO,
    527                 ROAMING_NO, 0L, 0L, 0L, 0L, 0L);
    528 
    529         // VPN underlying Iface entries are updated
    530         assertValues(delta, 11, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO,
    531                 ROAMING_NO, 28304L, 27L, 1L, 2L, 0L);
    532         assertValues(delta, 12, underlyingIface, tunUid, SET_FOREGROUND, TAG_NONE, METERED_NO,
    533                 ROAMING_NO, 0L, 0L, 0L, 0L, 0L);
    534 
    535         // New entries are added for new application's underlying Iface traffic
    536         assertContains(delta, underlyingIface, 10120, SET_DEFAULT, TAG_NONE, METERED_NO,
    537                 ROAMING_NO, 72667L, 197L, 43123L, 227L, 0L);
    538         assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, TAG_NONE, METERED_NO,
    539                 ROAMING_NO, 9297L, 17L, 4054, 19L, 0L);
    540         assertContains(delta, underlyingIface, 10120, SET_DEFAULT, testTag1, METERED_NO,
    541                 ROAMING_NO, 21691L, 41L, 13572L, 48L, 0L);
    542         assertContains(delta, underlyingIface, 10120, SET_FOREGROUND, testTag1, METERED_NO,
    543                 ROAMING_NO, 1281L, 2L, 653L, 1L, 0L);
    544 
    545         // New entries are added for debug purpose
    546         assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
    547                 ROAMING_NO, 39605L, 46L, 12039, 51, 0);
    548         assertContains(delta, underlyingIface, 10120, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
    549                 ROAMING_NO, 81964, 214, 47177, 246, 0);
    550         assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL,
    551                 ROAMING_ALL, 121569, 260, 59216, 297, 0);
    552 
    553     }
    554 
    555     // Tests a case where all of the data received by the tun0 interface is echo back into the tun0
    556     // interface by the vpn app before it's sent out of the underlying interface. The VPN app should
    557     // not be charged for the echoed data but it should still be charged for any extra data it sends
    558     // via the underlying interface.
    559     public void testMigrateTun_VpnAsLoopback() {
    560         final int tunUid = 10030;
    561         final String tunIface = "tun0";
    562         final String underlyingIface = "wlan0";
    563         NetworkStats delta = new NetworkStats(TEST_START, 9)
    564             // 2 different apps sent/receive data via tun0.
    565             .addValues(tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 50000L, 25L,
    566                     100000L, 50L, 0L)
    567             .addValues(tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 500L, 2L,
    568                     200L, 5L, 0L)
    569             // VPN package resends data through the tunnel (with exaggerated overhead)
    570             .addValues(tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 240000,
    571                     100L, 120000L, 60L, 0L)
    572             // 1 app already has some traffic on the underlying interface, the other doesn't yet
    573             .addValues(underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO, 1000L,
    574                     10L, 2000L, 20L, 0L)
    575             // Traffic through the underlying interface via the vpn app.
    576             // This test should redistribute this data correctly.
    577             .addValues(underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    578                     75500L, 37L, 130000L, 70L, 0L);
    579 
    580         assertTrue(delta.migrateTun(tunUid, tunIface, underlyingIface));
    581         assertEquals(9, delta.size());
    582 
    583         // tunIface entries should not be changed.
    584         assertValues(delta, 0, tunIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    585                 50000L, 25L, 100000L, 50L, 0L);
    586         assertValues(delta, 1, tunIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    587                 500L, 2L, 200L, 5L, 0L);
    588         assertValues(delta, 2, tunIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO, ROAMING_NO,
    589                 240000L, 100L, 120000L, 60L, 0L);
    590 
    591         // Existing underlying Iface entries are updated
    592         assertValues(delta, 3, underlyingIface, 10100, SET_DEFAULT, TAG_NONE, METERED_NO,
    593                 ROAMING_NO, 51000L, 35L, 102000L, 70L, 0L);
    594 
    595         // VPN underlying Iface entries are updated
    596         assertValues(delta, 4, underlyingIface, tunUid, SET_DEFAULT, TAG_NONE, METERED_NO,
    597                 ROAMING_NO, 25000L, 10L, 29800L, 15L, 0L);
    598 
    599         // New entries are added for new application's underlying Iface traffic
    600         assertContains(delta, underlyingIface, 20100, SET_DEFAULT, TAG_NONE, METERED_NO,
    601                 ROAMING_NO, 500L, 2L, 200L, 5L, 0L);
    602 
    603         // New entries are added for debug purpose
    604         assertContains(delta, underlyingIface, 10100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
    605                 ROAMING_NO, 50000L, 25L, 100000L, 50L, 0L);
    606         assertContains(delta, underlyingIface, 20100, SET_DBG_VPN_IN, TAG_NONE, METERED_NO,
    607                 ROAMING_NO, 500, 2L, 200L, 5L, 0L);
    608         assertContains(delta, underlyingIface, tunUid, SET_DBG_VPN_OUT, TAG_NONE, METERED_ALL,
    609                 ROAMING_ALL, 50500L, 27L, 100200L, 55, 0);
    610     }
    611 
    612     private static void assertContains(NetworkStats stats,  String iface, int uid, int set,
    613             int tag, int metered, int roaming, long rxBytes, long rxPackets, long txBytes,
    614             long txPackets, long operations) {
    615         int index = stats.findIndex(iface, uid, set, tag, metered, roaming);
    616         assertTrue(index != -1);
    617         assertValues(stats, index, iface, uid, set, tag, metered, roaming,
    618                 rxBytes, rxPackets, txBytes, txPackets, operations);
    619     }
    620 
    621     private static void assertValues(NetworkStats stats, int index, String iface, int uid, int set,
    622             int tag, int metered, int roaming, long rxBytes, long rxPackets, long txBytes,
    623             long txPackets, long operations) {
    624         final NetworkStats.Entry entry = stats.getValues(index, null);
    625         assertValues(entry, iface, uid, set, tag, metered, roaming);
    626         assertValues(entry, rxBytes, rxPackets, txBytes, txPackets, operations);
    627     }
    628 
    629     private static void assertValues(
    630             NetworkStats.Entry entry, String iface, int uid, int set, int tag, int metered,
    631             int roaming) {
    632         assertEquals(iface, entry.iface);
    633         assertEquals(uid, entry.uid);
    634         assertEquals(set, entry.set);
    635         assertEquals(tag, entry.tag);
    636         assertEquals(metered, entry.metered);
    637         assertEquals(roaming, entry.roaming);
    638     }
    639 
    640     private static void assertValues(NetworkStats.Entry entry, long rxBytes, long rxPackets,
    641             long txBytes, long txPackets, long operations) {
    642         assertEquals(rxBytes, entry.rxBytes);
    643         assertEquals(rxPackets, entry.rxPackets);
    644         assertEquals(txBytes, entry.txBytes);
    645         assertEquals(txPackets, entry.txPackets);
    646         assertEquals(operations, entry.operations);
    647     }
    648 
    649 }
    650