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 testNetworkStatsSingle() throws Exception { 120 stageFile(R.raw.xt_qtaguid_iface_typical, file("net/xt_qtaguid/iface_stat_all")); 121 122 final NetworkStats stats = mFactory.readNetworkStatsSummaryDev(); 123 assertEquals(6, stats.size()); 124 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L); 125 assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L); 126 assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L); 127 } 128 129 @Test 130 public void testNetworkStatsXt() throws Exception { 131 stageFile(R.raw.xt_qtaguid_iface_fmt_typical, file("net/xt_qtaguid/iface_stat_fmt")); 132 133 final NetworkStats stats = mFactory.readNetworkStatsSummaryXt(); 134 assertEquals(3, stats.size()); 135 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 6824L, 16L, 5692L, 10L); 136 assertStatsEntry(stats, "rmnet1", UID_ALL, SET_ALL, TAG_NONE, 11153922L, 8051L, 190226L, 137 2468L); 138 assertStatsEntry(stats, "rmnet2", UID_ALL, SET_ALL, TAG_NONE, 4968L, 35L, 3081L, 39L); 139 } 140 141 @Test 142 public void testDoubleClatAccounting() throws Exception { 143 NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); 144 145 // xt_qtaguid_with_clat_simple is a synthetic file that simulates 146 // - 213 received 464xlat packets of size 200 bytes 147 // - 41 sent 464xlat packets of size 100 bytes 148 // - no other traffic on base interface for root uid. 149 NetworkStats stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_simple); 150 assertEquals(4, stats.size()); 151 152 assertStatsEntry(stats, "v4-wlan0", 10060, SET_DEFAULT, 0x0, 46860L, 4920L); 153 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 0L, 0L); 154 155 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat); 156 assertEquals(42, stats.size()); 157 158 assertStatsEntry(stats, "v4-wlan0", 0, SET_DEFAULT, 0x0, 356L, 276L); 159 assertStatsEntry(stats, "v4-wlan0", 1000, SET_DEFAULT, 0x0, 30812L, 2310L); 160 assertStatsEntry(stats, "v4-wlan0", 10102, SET_DEFAULT, 0x0, 10022L, 3330L); 161 assertStatsEntry(stats, "v4-wlan0", 10060, SET_DEFAULT, 0x0, 9532772L, 254112L); 162 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 15229L, 5766L); 163 assertStatsEntry(stats, "wlan0", 1000, SET_DEFAULT, 0x0, 6126L, 2013L); 164 assertStatsEntry(stats, "wlan0", 10013, SET_DEFAULT, 0x0, 0L, 144L); 165 assertStatsEntry(stats, "wlan0", 10018, SET_DEFAULT, 0x0, 5980263L, 167667L); 166 assertStatsEntry(stats, "wlan0", 10060, SET_DEFAULT, 0x0, 134356L, 8705L); 167 assertStatsEntry(stats, "wlan0", 10079, SET_DEFAULT, 0x0, 10926L, 1507L); 168 assertStatsEntry(stats, "wlan0", 10102, SET_DEFAULT, 0x0, 25038L, 8245L); 169 assertStatsEntry(stats, "wlan0", 10103, SET_DEFAULT, 0x0, 0L, 192L); 170 assertStatsEntry(stats, "dummy0", 0, SET_DEFAULT, 0x0, 0L, 168L); 171 assertStatsEntry(stats, "lo", 0, SET_DEFAULT, 0x0, 1288L, 1288L); 172 173 NetworkStatsFactory.clearStackedIfaces(); 174 } 175 176 @Test 177 public void testDoubleClatAccounting100MBDownload() throws Exception { 178 // Downloading 100mb from an ipv4 only destination in a foreground activity 179 180 long appRxBytesBefore = 328684029L; 181 long appRxBytesAfter = 439237478L; 182 assertEquals("App traffic should be ~100MB", 110553449, appRxBytesAfter - appRxBytesBefore); 183 184 long rootRxBytesBefore = 1394011L; 185 long rootRxBytesAfter = 1398634L; 186 assertEquals("UID 0 traffic should be ~0", 4623, rootRxBytesAfter - rootRxBytesBefore); 187 188 NetworkStatsFactory.noteStackedIface("v4-wlan0", "wlan0"); 189 NetworkStats stats; 190 191 // Stats snapshot before the download 192 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_before); 193 assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesBefore, 5199872L); 194 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesBefore, 647888L); 195 196 // Stats snapshot after the download 197 stats = parseDetailedStats(R.raw.xt_qtaguid_with_clat_100mb_download_after); 198 assertStatsEntry(stats, "v4-wlan0", 10106, SET_FOREGROUND, 0x0, appRxBytesAfter, 7867488L); 199 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, rootRxBytesAfter, 647587L); 200 201 NetworkStatsFactory.clearStackedIfaces(); 202 } 203 204 /** 205 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 206 * testing purposes. 207 */ 208 private void stageFile(int rawId, File file) throws Exception { 209 new File(file.getParent()).mkdirs(); 210 InputStream in = null; 211 OutputStream out = null; 212 try { 213 in = InstrumentationRegistry.getContext().getResources().openRawResource(rawId); 214 out = new FileOutputStream(file); 215 Streams.copy(in, out); 216 } finally { 217 IoUtils.closeQuietly(in); 218 IoUtils.closeQuietly(out); 219 } 220 } 221 222 private void stageLong(long value, File file) throws Exception { 223 new File(file.getParent()).mkdirs(); 224 FileWriter out = null; 225 try { 226 out = new FileWriter(file); 227 out.write(Long.toString(value)); 228 } finally { 229 IoUtils.closeQuietly(out); 230 } 231 } 232 233 private File file(String path) throws Exception { 234 return new File(mTestProc, path); 235 } 236 237 private NetworkStats parseDetailedStats(int resourceId) throws Exception { 238 stageFile(resourceId, file("net/xt_qtaguid/stats")); 239 return mFactory.readNetworkStatsDetail(); 240 } 241 242 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 243 int tag, long rxBytes, long txBytes) { 244 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO, 245 DEFAULT_NETWORK_NO); 246 if (i < 0) { 247 fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", 248 iface, uid, set, tag)); 249 } 250 final NetworkStats.Entry entry = stats.getValues(i, null); 251 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 252 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 253 } 254 255 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 256 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { 257 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO, 258 DEFAULT_NETWORK_NO); 259 if (i < 0) { 260 fail(String.format("no NetworkStats for (iface: %s, uid: %d, set: %d, tag: %d)", 261 iface, uid, set, tag)); 262 } 263 final NetworkStats.Entry entry = stats.getValues(i, null); 264 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 265 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 266 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 267 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 268 } 269} 270