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.net; 18 19 import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; 20 import static android.net.NetworkStats.DEFAULT_NETWORK_NO; 21 import static android.net.NetworkStats.DEFAULT_NETWORK_YES; 22 import static android.net.NetworkStats.METERED_ALL; 23 import static android.net.NetworkStats.METERED_NO; 24 import static android.net.NetworkStats.METERED_YES; 25 import static android.net.NetworkStats.ROAMING_ALL; 26 import static android.net.NetworkStats.ROAMING_NO; 27 import static android.net.NetworkStats.ROAMING_YES; 28 import static android.net.NetworkStats.SET_ALL; 29 import static android.net.NetworkStats.SET_DEFAULT; 30 import static android.net.NetworkStats.SET_FOREGROUND; 31 import static android.net.NetworkStats.TAG_NONE; 32 33 import static org.junit.Assert.assertEquals; 34 35 import android.net.NetworkStats; 36 import android.net.UnderlyingNetworkInfo; 37 38 import java.util.Arrays; 39 40 /** Superclass with utilities for NetworkStats(Service|Factory)Test */ 41 abstract class NetworkStatsBaseTest { 42 static final String TEST_IFACE = "test0"; 43 static final String TEST_IFACE2 = "test1"; 44 static final String TEST_IFACE3 = "test2"; 45 static final String TUN_IFACE = "test_nss_tun0"; 46 static final String TUN_IFACE2 = "test_nss_tun1"; 47 48 static final int UID_RED = 1001; 49 static final int UID_BLUE = 1002; 50 static final int UID_GREEN = 1003; 51 static final int UID_VPN = 1004; 52 assertValues(NetworkStats stats, String iface, int uid, long rxBytes, long rxPackets, long txBytes, long txPackets)53 void assertValues(NetworkStats stats, String iface, int uid, long rxBytes, 54 long rxPackets, long txBytes, long txPackets) { 55 assertValues( 56 stats, iface, uid, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, 57 rxBytes, rxPackets, txBytes, txPackets, 0); 58 } 59 assertValues(NetworkStats stats, String iface, int uid, int set, int tag, int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets, long txBytes, long txPackets, long operations)60 void assertValues(NetworkStats stats, String iface, int uid, int set, int tag, 61 int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets, 62 long txBytes, long txPackets, long operations) { 63 final NetworkStats.Entry entry = new NetworkStats.Entry(); 64 final int[] sets; 65 if (set == SET_ALL) { 66 sets = new int[] {SET_ALL, SET_DEFAULT, SET_FOREGROUND}; 67 } else { 68 sets = new int[] {set}; 69 } 70 71 final int[] roamings; 72 if (roaming == ROAMING_ALL) { 73 roamings = new int[] {ROAMING_ALL, ROAMING_YES, ROAMING_NO}; 74 } else { 75 roamings = new int[] {roaming}; 76 } 77 78 final int[] meterings; 79 if (metered == METERED_ALL) { 80 meterings = new int[] {METERED_ALL, METERED_YES, METERED_NO}; 81 } else { 82 meterings = new int[] {metered}; 83 } 84 85 final int[] defaultNetworks; 86 if (defaultNetwork == DEFAULT_NETWORK_ALL) { 87 defaultNetworks = 88 new int[] {DEFAULT_NETWORK_ALL, DEFAULT_NETWORK_YES, DEFAULT_NETWORK_NO}; 89 } else { 90 defaultNetworks = new int[] {defaultNetwork}; 91 } 92 93 for (int s : sets) { 94 for (int r : roamings) { 95 for (int m : meterings) { 96 for (int d : defaultNetworks) { 97 final int i = stats.findIndex(iface, uid, s, tag, m, r, d); 98 if (i != -1) { 99 entry.add(stats.getValues(i, null)); 100 } 101 } 102 } 103 } 104 } 105 106 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 107 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 108 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 109 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 110 assertEquals("unexpected operations", operations, entry.operations); 111 } 112 createVpnInfo(String[] underlyingIfaces)113 static UnderlyingNetworkInfo createVpnInfo(String[] underlyingIfaces) { 114 return createVpnInfo(TUN_IFACE, underlyingIfaces); 115 } 116 createVpnInfo(String vpnIface, String[] underlyingIfaces)117 static UnderlyingNetworkInfo createVpnInfo(String vpnIface, String[] underlyingIfaces) { 118 return new UnderlyingNetworkInfo(UID_VPN, vpnIface, Arrays.asList(underlyingIfaces)); 119 } 120 } 121