NetworkStatsFactoryTest.java revision b6a920124f28422877f59bfb32719099a0067d76
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.DEFAULT_NETWORK_NO; 20import static android.net.NetworkStats.METERED_NO; 21import static android.net.NetworkStats.ROAMING_NO; 22import static android.net.NetworkStats.SET_ALL; 23import static android.net.NetworkStats.SET_DEFAULT; 24import static android.net.NetworkStats.SET_FOREGROUND; 25import static android.net.NetworkStats.TAG_NONE; 26import static android.net.NetworkStats.UID_ALL; 27import static com.android.server.NetworkManagementSocketTagger.kernelToTag; 28import static org.junit.Assert.assertEquals; 29import static org.junit.Assert.fail; 30 31import android.content.res.Resources; 32import android.net.NetworkStats; 33import android.net.TrafficStats; 34import android.support.test.InstrumentationRegistry; 35import android.support.test.filters.SmallTest; 36import android.support.test.runner.AndroidJUnit4; 37 38import com.android.frameworks.tests.net.R; 39 40import java.io.File; 41import java.io.FileOutputStream; 42import java.io.FileWriter; 43import java.io.InputStream; 44import java.io.OutputStream; 45 46import libcore.io.IoUtils; 47import libcore.io.Streams; 48 49import org.junit.runner.RunWith; 50import org.junit.After; 51import org.junit.Before; 52import org.junit.Test; 53 54/** 55 * Tests for {@link NetworkStatsFactory}. 56 */ 57@RunWith(AndroidJUnit4.class) 58@SmallTest 59public class NetworkStatsFactoryTest { 60 private File mTestProc; 61 private NetworkStatsFactory mFactory; 62 63 @Before 64 public void setUp() throws Exception { 65 mTestProc = new File(InstrumentationRegistry.getContext().getFilesDir(), "proc"); 66 if (mTestProc.exists()) { 67 IoUtils.deleteContents(mTestProc); 68 } 69 70 mFactory = new NetworkStatsFactory(mTestProc, false); 71 } 72 73 @After 74 public void tearDown() throws Exception { 75 mFactory = null; 76 77 if (mTestProc.exists()) { 78 IoUtils.deleteContents(mTestProc); 79 } 80 } 81 82 @Test 83 public void testNetworkStatsDetail() throws Exception { 84 final NetworkStats stats = parseDetailedStats(R.raw.xt_qtaguid_typical); 85 86 assertEquals(70, stats.size()); 87 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L); 88 assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L); 89 assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L); 90 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L); 91 assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L); 92 } 93 94 @Test 95 public void testKernelTags() throws Exception { 96 assertEquals(0, kernelToTag("0x0000000000000000")); 97 assertEquals(0x32, kernelToTag("0x0000003200000000")); 98 assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); 99 assertEquals(0, kernelToTag("0x0000000000000000")); 100 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); 101 102 assertEquals(0, kernelToTag("0x0")); 103 assertEquals(0, kernelToTag("0xf00d")); 104 assertEquals(1, kernelToTag("0x100000000")); 105 assertEquals(14438007, kernelToTag("0xdc4e7700000000")); 106 assertEquals(TrafficStats.TAG_SYSTEM_DOWNLOAD, kernelToTag("0xffffff0100000000")); 107 } 108 109 @Test 110 public void testNetworkStatsWithSet() throws Exception { 111 final NetworkStats stats = parseDetailedStats(R.raw.xt_qtaguid_typical); 112 assertEquals(70, stats.size()); 113 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 114 676L); 115 assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L); 116 } 117 118 @Test 119 public void testNetworkStatsSummary() throws Exception { 120 stageFile(R.raw.net_dev_typical, file("net/dev")); 121 122 final NetworkStats stats = mFactory.readNetworkStatsIfaceDev(); 123 assertEquals(6, stats.size()); 124 assertStatsEntry(stats, "lo", UID_ALL, SET_ALL, TAG_NONE, 8308L, 8308L); 125 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 1507570L, 489339L); 126 assertStatsEntry(stats, "ifb0", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L); 127 assertStatsEntry(stats, "ifb1", UID_ALL, SET_ALL, TAG_NONE, 52454L, 0L); 128 assertStatsEntry(stats, "sit0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L); 129 assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_ALL, TAG_NONE, 0L, 0L); 130 } 131 132 @Test 133 public void testNetworkStatsSingle() throws Exception { 134 stageFile(R.raw.xt_qtaguid_iface_typical, file("net/xt_qtaguid/iface_stat_all")); 135 136 final NetworkStats stats = mFactory.readNetworkStatsSummaryDev(); 137 assertEquals(6, stats.size()); 138 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L); 139 assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L); 140 assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L); 141 } 142 143 @Test 144 public void testNetworkStatsXt() throws Exception { 145 stageFile(R.raw.xt_qtaguid_iface_fmt_typical, file("net/xt_qtaguid/iface_stat_fmt")); 146 147 final NetworkStats stats = mFactory.readNetworkStatsSummaryXt(); 148 assertEquals(3, stats.size()); 149 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 6824L, 16L, 5692L, 10L); 150 assertStatsEntry(stats, "rmnet1", UID_ALL, SET_ALL, TAG_NONE, 11153922L, 8051L, 190226L, 151 2468L); 152 assertStatsEntry(stats, "rmnet2", UID_ALL, SET_ALL, TAG_NONE, 4968L, 35L, 3081L, 39L); 153 } 154 155 @Test 156 public void testDoubleClatAccounting() throws Exception { 157 NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); 158 159 // xt_qtaguid_with_clat_simple is a synthetic file that simulates 160 // - 213 received 464xlat packets of size 200 bytes 161 // - 41 sent 464xlat packets of size 100 bytes 162 // - no other traffic on base interface for root uid. 163 NetworkStats stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_simple); 164 assertEquals(4, stats.size()); 165 166 assertStatsEntry(stats, "v4-wlan0", 10060, SET_DEFAULT, 0x0, 46860L, 4920L); 167 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 0L, 0L); 168 169 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat); 170 assertEquals(42, stats.size()); 171 172 assertStatsEntry(stats, "v4-wlan0", 0, SET_DEFAULT, 0x0, 356L, 276L); 173 assertStatsEntry(stats, "v4-wlan0", 1000, SET_DEFAULT, 0x0, 30812L, 2310L); 174 assertStatsEntry(stats, "v4-wlan0", 10102, SET_DEFAULT, 0x0, 10022L, 3330L); 175 assertStatsEntry(stats, "v4-wlan0", 10060, SET_DEFAULT, 0x0, 9532772L, 254112L); 176 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 15229L, 5766L); 177 assertStatsEntry(stats, "wlan0", 1000, SET_DEFAULT, 0x0, 6126L, 2013L); 178 assertStatsEntry(stats, "wlan0", 10013, SET_DEFAULT, 0x0, 0L, 144L); 179 assertStatsEntry(stats, "wlan0", 10018, SET_DEFAULT, 0x0, 5980263L, 167667L); 180 assertStatsEntry(stats, "wlan0", 10060, SET_DEFAULT, 0x0, 134356L, 8705L); 181 assertStatsEntry(stats, "wlan0", 10079, SET_DEFAULT, 0x0, 10926L, 1507L); 182 assertStatsEntry(stats, "wlan0", 10102, SET_DEFAULT, 0x0, 25038L, 8245L); 183 assertStatsEntry(stats, "wlan0", 10103, SET_DEFAULT, 0x0, 0L, 192L); 184 assertStatsEntry(stats, "dummy0", 0, SET_DEFAULT, 0x0, 0L, 168L); 185 assertStatsEntry(stats, "lo", 0, SET_DEFAULT, 0x0, 1288L, 1288L); 186 187 NetworkStatsFactory.clearStackedIfaces(); 188 } 189 190 @Test 191 public void testDoubleClatAccounting100MBDownload() throws Exception { 192 // Downloading 100mb from an ipv4 only destination in a foreground activity 193 194 long appRxBytesBefore = 328684029L; 195 long appRxBytesAfter = 439237478L; 196 assertEquals("App traffic should be ~100MB", 110553449, appRxBytesAfter - appRxBytesBefore); 197 198 long rootRxBytesBefore = 1394011L; 199 long rootRxBytesAfter = 1398634L; 200 assertEquals("UID 0 traffic should be ~0", 4623, rootRxBytesAfter - rootRxBytesBefore); 201 202 NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); 203 NetworkStats stats; 204 205 // Stats snapshot before the download 206 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_before); 207 assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesBefore, 5199872L); 208 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesBefore, 647888L); 209 210 // Stats snapshot after the download 211 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_after); 212 assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesAfter, 7867488L); 213 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesAfter, 647587L); 214 215 NetworkStatsFactory.clearStackedIfaces(); 216 } 217 218 /** 219 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 220 * testing purposes. 221 */ 222 private void stageFile(int rawId, File file) throws Exception { 223 new File(file.getParent()).mkdirs(); 224 InputStream in = null; 225 OutputStream out = null; 226 try { 227 in = InstrumentationRegistry.getContext().getResources().openRawResource(rawId); 228 out = new FileOutputStream(file); 229 Streams.copy(in, out); 230 } finally { 231 IoUtils.closeQuietly(in); 232 IoUtils.closeQuietly(out); 233 } 234 } 235 236 private void stageLong(long value, File file) throws Exception { 237 new File(file.getParent()).mkdirs(); 238 FileWriter out = null; 239 try { 240 out = new FileWriter(file); 241 out.write(Long.toString(value)); 242 } finally { 243 IoUtils.closeQuietly(out); 244 } 245 } 246 247 private File file(String path) throws Exception { 248 return new File(mTestProc, path); 249 } 250 251 private NetworkStats parseDetailedStats(int resourceId) throws Exception { 252 stageFile(resourceId, file("net/xt_qtaguid/stats")); 253 return mFactory.readNetworkStatsDetail(); 254 } 255 256 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 257 int tag, long rxBytes, long txBytes) { 258 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO, 259 DEFAULT_NETWORK_NO); 260 if (i < 0) { 261 fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", 262 iface, uid, set, tag)); 263 } 264 final NetworkStats.Entry entry = stats.getValues(i, null); 265 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 266 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 267 } 268 269 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 270 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { 271 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO, 272 DEFAULT_NETWORK_NO); 273 if (i < 0) { 274 fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", 275 iface, uid, set, tag)); 276 } 277 final NetworkStats.Entry entry = stats.getValues(i, null); 278 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 279 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 280 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 281 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 282 } 283} 284