NetworkStatsFactoryTest.java revision 2d6c580262ba1c19fc4f20c3566889e332fbdcb6
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 17package com.android.internal.net; 18 19import static android.net.NetworkStats.SET_ALL; 20import static android.net.NetworkStats.SET_DEFAULT; 21import static android.net.NetworkStats.SET_FOREGROUND; 22import static android.net.NetworkStats.TAG_NONE; 23import static android.net.NetworkStats.UID_ALL; 24import static com.android.server.NetworkManagementSocketTagger.kernelToTag; 25 26import android.content.res.Resources; 27import android.net.NetworkStats; 28import android.net.TrafficStats; 29import android.test.AndroidTestCase; 30 31import com.android.frameworks.coretests.R; 32 33import java.io.File; 34import java.io.FileOutputStream; 35import java.io.FileWriter; 36import java.io.InputStream; 37import java.io.OutputStream; 38 39import libcore.io.IoUtils; 40import libcore.io.Streams; 41 42/** 43 * Tests for {@link NetworkStatsFactory}. 44 */ 45public class NetworkStatsFactoryTest extends AndroidTestCase { 46 private File mTestProc; 47 private NetworkStatsFactory mFactory; 48 49 @Override 50 public void setUp() throws Exception { 51 super.setUp(); 52 53 mTestProc = new File(getContext().getFilesDir(), "proc"); 54 if (mTestProc.exists()) { 55 IoUtils.deleteContents(mTestProc); 56 } 57 58 mFactory = new NetworkStatsFactory(mTestProc); 59 } 60 61 @Override 62 public void tearDown() throws Exception { 63 mFactory = null; 64 65 if (mTestProc.exists()) { 66 IoUtils.deleteContents(mTestProc); 67 } 68 69 super.tearDown(); 70 } 71 72 public void testNetworkStatsDetail() throws Exception { 73 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); 74 75 final NetworkStats stats = mFactory.readNetworkStatsDetail(); 76 assertEquals(70, stats.size()); 77 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L); 78 assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L); 79 assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L); 80 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L); 81 assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L); 82 } 83 84 public void testNetworkStatsSummary() throws Exception { 85 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 86 87 final NetworkStats stats = mFactory.readNetworkStatsSummary(); 88 assertEquals(6, stats.size()); 89 assertStatsEntry(stats, "lo", UID_ALL, SET_ALL, TAG_NONE, 8308L, 8308L); 90 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L, 489339L); 91 assertStatsEntry(stats, "ifb0", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L); 92 assertStatsEntry(stats, "ifb1", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L); 93 assertStatsEntry(stats, "sit0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L); 94 assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L); 95 } 96 97 public void testNetworkStatsSummaryDown() throws Exception { 98 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 99 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active")); 100 stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes")); 101 stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets")); 102 stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes")); 103 stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets")); 104 105 final NetworkStats stats = mFactory.readNetworkStatsSummary(); 106 assertEquals(7, stats.size()); 107 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L, 489339L); 108 assertStatsEntry(stats, "wlan0", UID_ALL, SET_ALL, TAG_NONE, 1024L, 2048L); 109 } 110 111 public void testNetworkStatsCombined() throws Exception { 112 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 113 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 114 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 115 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 116 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 117 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 118 119 final NetworkStats stats = mFactory.readNetworkStatsSummary(); 120 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L + 10L, 121 2205L + 20L, 489339L + 30L, 2237L + 40L); 122 } 123 124 public void testNetworkStatsCombinedInactive() throws Exception { 125 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 126 stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 127 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 128 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 129 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 130 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 131 132 final NetworkStats stats = mFactory.readNetworkStatsSummary(); 133 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 10L, 20L, 30L, 40L); 134 } 135 136 public void testKernelTags() throws Exception { 137 assertEquals(0, kernelToTag("0x0000000000000000")); 138 assertEquals(0x32, kernelToTag("0x0000003200000000")); 139 assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); 140 assertEquals(0, kernelToTag("0x0000000000000000")); 141 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); 142 143 assertEquals(0, kernelToTag("0x0")); 144 assertEquals(0, kernelToTag("0xf00d")); 145 assertEquals(1, kernelToTag("0x100000000")); 146 assertEquals(14438007, kernelToTag("0xdc4e7700000000")); 147 assertEquals(TrafficStats.TAG_SYSTEM_DOWNLOAD, kernelToTag("0xffffff0100000000")); 148 } 149 150 public void testNetworkStatsWithSet() throws Exception { 151 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); 152 153 final NetworkStats stats = mFactory.readNetworkStatsDetail(); 154 assertEquals(70, stats.size()); 155 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 676L); 156 assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L); 157 } 158 159 public void testNetworkStatsSingle() throws Exception { 160 stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all")); 161 162 final NetworkStats stats = mFactory.readNetworkStatsSummary(); 163 assertEquals(6, stats.size()); 164 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L); 165 assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L); 166 assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L); 167 } 168 169 /** 170 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 171 * testing purposes. 172 */ 173 private void stageFile(int rawId, File file) throws Exception { 174 new File(file.getParent()).mkdirs(); 175 InputStream in = null; 176 OutputStream out = null; 177 try { 178 in = getContext().getResources().openRawResource(rawId); 179 out = new FileOutputStream(file); 180 Streams.copy(in, out); 181 } finally { 182 IoUtils.closeQuietly(in); 183 IoUtils.closeQuietly(out); 184 } 185 } 186 187 private void stageLong(long value, File file) throws Exception { 188 new File(file.getParent()).mkdirs(); 189 FileWriter out = null; 190 try { 191 out = new FileWriter(file); 192 out.write(Long.toString(value)); 193 } finally { 194 IoUtils.closeQuietly(out); 195 } 196 } 197 198 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 199 int tag, long rxBytes, long txBytes) { 200 final int i = stats.findIndex(iface, uid, set, tag); 201 final NetworkStats.Entry entry = stats.getValues(i, null); 202 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 203 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 204 } 205 206 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 207 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { 208 final int i = stats.findIndex(iface, uid, set, tag); 209 final NetworkStats.Entry entry = stats.getValues(i, null); 210 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 211 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 212 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 213 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 214 } 215 216} 217