NetworkManagementServiceTest.java revision ae2c1810839430c84e4a1172580d9c4b18f568ca
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.server; 18 19import static android.net.NetworkStats.SET_DEFAULT; 20import static android.net.NetworkStats.SET_FOREGROUND; 21import static android.net.NetworkStats.TAG_NONE; 22import static android.net.NetworkStats.UID_ALL; 23import static com.android.server.NetworkManagementSocketTagger.kernelToTag; 24 25import android.content.res.Resources; 26import android.net.NetworkStats; 27import android.test.AndroidTestCase; 28import android.test.suitebuilder.annotation.LargeTest; 29 30import com.android.frameworks.servicestests.R; 31 32import java.io.File; 33import java.io.FileOutputStream; 34import java.io.FileWriter; 35import java.io.InputStream; 36import java.io.OutputStream; 37 38import libcore.io.IoUtils; 39import libcore.io.Streams; 40 41/** 42 * Tests for {@link NetworkManagementService}. 43 */ 44@LargeTest 45public class NetworkManagementServiceTest extends AndroidTestCase { 46 private File mTestProc; 47 private NetworkManagementService mService; 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 mService = NetworkManagementService.createForTest(mContext, mTestProc, true); 59 } 60 61 @Override 62 public void tearDown() throws Exception { 63 mService = 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 = mService.getNetworkStatsDetail(); 76 assertEquals(31, stats.size()); 77 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0, 14615L, 4270L); 78 assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 0, 333821L, 53558L); 79 assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 1947740890, 18725L, 1066L); 80 assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 0, 31184994L, 684122L); 81 assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 1947740890, 28507378L, 437004L); 82 } 83 84 public void testNetworkStatsDetailExtended() throws Exception { 85 stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats")); 86 87 final NetworkStats stats = mService.getNetworkStatsDetail(); 88 assertEquals(2, stats.size()); 89 assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0, 1024L, 2048L); 90 assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0xF00D, 512L, 512L); 91 } 92 93 public void testNetworkStatsSummary() throws Exception { 94 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 95 96 final NetworkStats stats = mService.getNetworkStatsSummary(); 97 assertEquals(6, stats.size()); 98 assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L); 99 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); 100 assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); 101 assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); 102 assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); 103 assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); 104 } 105 106 public void testNetworkStatsSummaryDown() throws Exception { 107 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 108 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active")); 109 stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes")); 110 stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets")); 111 stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes")); 112 stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets")); 113 114 final NetworkStats stats = mService.getNetworkStatsSummary(); 115 assertEquals(7, stats.size()); 116 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); 117 assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L); 118 } 119 120 public void testNetworkStatsCombined() throws Exception { 121 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 122 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 123 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 124 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 125 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 126 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 127 128 final NetworkStats stats = mService.getNetworkStatsSummary(); 129 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L, 130 2205L + 20L, 489339L + 30L, 2237L + 40L); 131 } 132 133 public void testNetworkStatsCombinedInactive() throws Exception { 134 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 135 stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 136 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 137 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 138 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 139 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 140 141 final NetworkStats stats = mService.getNetworkStatsSummary(); 142 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L); 143 } 144 145 public void testKernelTags() throws Exception { 146 assertEquals(0, kernelToTag("0x0000000000000000")); 147 assertEquals(0x32, kernelToTag("0x0000003200000000")); 148 assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); 149 assertEquals(0, kernelToTag("0x0000000000000000")); 150 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); 151 } 152 153 public void testNetworkStatsWithSet() throws Exception { 154 stageFile(R.raw.xt_qtaguid_typical_with_set, new File(mTestProc, "net/xt_qtaguid/stats")); 155 156 final NetworkStats stats = mService.getNetworkStatsDetail(); 157 assertEquals(12, stats.size()); 158 assertStatsEntry(stats, "rmnet0", 1000, SET_DEFAULT, 0, 278102L, 253L, 10487L, 182L); 159 assertStatsEntry(stats, "rmnet0", 1000, SET_FOREGROUND, 0, 26033L, 30L, 1401L, 26L); 160 } 161 162 public void testNetworkStatsSingle() throws Exception { 163 stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all")); 164 165 final NetworkStats stats = mService.getNetworkStatsSummary(); 166 assertEquals(6, stats.size()); 167 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 2112L, 24L, 700L, 10L); 168 assertStatsEntry(stats, "test1", UID_ALL, SET_DEFAULT, TAG_NONE, 6L, 8L, 10L, 12L); 169 assertStatsEntry(stats, "test2", UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L); 170 } 171 172 /** 173 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 174 * testing purposes. 175 */ 176 private void stageFile(int rawId, File file) throws Exception { 177 new File(file.getParent()).mkdirs(); 178 InputStream in = null; 179 OutputStream out = null; 180 try { 181 in = getContext().getResources().openRawResource(rawId); 182 out = new FileOutputStream(file); 183 Streams.copy(in, out); 184 } finally { 185 IoUtils.closeQuietly(in); 186 IoUtils.closeQuietly(out); 187 } 188 } 189 190 private void stageLong(long value, File file) throws Exception { 191 new File(file.getParent()).mkdirs(); 192 FileWriter out = null; 193 try { 194 out = new FileWriter(file); 195 out.write(Long.toString(value)); 196 } finally { 197 IoUtils.closeQuietly(out); 198 } 199 } 200 201 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 202 int tag, long rxBytes, long txBytes) { 203 final int i = stats.findIndex(iface, uid, set, tag); 204 final NetworkStats.Entry entry = stats.getValues(i, null); 205 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 206 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 207 } 208 209 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 210 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { 211 final int i = stats.findIndex(iface, uid, set, tag); 212 final NetworkStats.Entry entry = stats.getValues(i, null); 213 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 214 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 215 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 216 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 217 } 218 219} 220