NetworkManagementServiceTest.java revision 0fea823673e9cab24bd16a1c80a9bd689fc7a6f9
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; 24import static com.android.server.NetworkManagementSocketTagger.tagToKernel; 25 26import android.content.res.Resources; 27import android.net.NetworkStats; 28import android.test.AndroidTestCase; 29import android.test.suitebuilder.annotation.LargeTest; 30 31import com.android.frameworks.servicestests.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 NetworkManagementService}. 44 */ 45@LargeTest 46public class NetworkManagementServiceTest extends AndroidTestCase { 47 private File mTestProc; 48 private NetworkManagementService mService; 49 50 @Override 51 public void setUp() throws Exception { 52 super.setUp(); 53 54 mTestProc = new File(getContext().getFilesDir(), "proc"); 55 if (mTestProc.exists()) { 56 IoUtils.deleteContents(mTestProc); 57 } 58 59 mService = NetworkManagementService.createForTest(mContext, mTestProc, true); 60 } 61 62 @Override 63 public void tearDown() throws Exception { 64 mService = null; 65 66 if (mTestProc.exists()) { 67 IoUtils.deleteContents(mTestProc); 68 } 69 70 super.tearDown(); 71 } 72 73 public void testNetworkStatsDetail() throws Exception { 74 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); 75 76 final NetworkStats stats = mService.getNetworkStatsDetail(); 77 assertEquals(31, stats.size()); 78 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0, 14615L, 4270L); 79 assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 0, 333821L, 53558L); 80 assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 1947740890, 18725L, 1066L); 81 assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 0, 31184994L, 684122L); 82 assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 1947740890, 28507378L, 437004L); 83 } 84 85 public void testNetworkStatsDetailExtended() throws Exception { 86 stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats")); 87 88 final NetworkStats stats = mService.getNetworkStatsDetail(); 89 assertEquals(2, stats.size()); 90 assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0, 1024L, 2048L); 91 assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0xF00D, 512L, 512L); 92 } 93 94 public void testNetworkStatsSummary() throws Exception { 95 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 96 97 final NetworkStats stats = mService.getNetworkStatsSummary(); 98 assertEquals(6, stats.size()); 99 assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L); 100 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); 101 assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); 102 assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L); 103 assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); 104 assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L); 105 } 106 107 public void testNetworkStatsSummaryDown() throws Exception { 108 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 109 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active")); 110 stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes")); 111 stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets")); 112 stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes")); 113 stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets")); 114 115 final NetworkStats stats = mService.getNetworkStatsSummary(); 116 assertEquals(7, stats.size()); 117 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L); 118 assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L); 119 } 120 121 public void testNetworkStatsCombined() throws Exception { 122 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 123 stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 124 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 125 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 126 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 127 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 128 129 final NetworkStats stats = mService.getNetworkStatsSummary(); 130 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L, 131 2205L + 20L, 489339L + 30L, 2237L + 40L); 132 } 133 134 public void testNetworkStatsCombinedInactive() throws Exception { 135 stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev")); 136 stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active")); 137 stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes")); 138 stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets")); 139 stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes")); 140 stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets")); 141 142 final NetworkStats stats = mService.getNetworkStatsSummary(); 143 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L); 144 } 145 146 public void testKernelTags() throws Exception { 147 assertEquals("0", tagToKernel(0x0)); 148 assertEquals("214748364800", tagToKernel(0x32)); 149 assertEquals("9223372032559808512", tagToKernel(Integer.MAX_VALUE)); 150 assertEquals("0", tagToKernel(Integer.MIN_VALUE)); 151 assertEquals("9223369837831520256", tagToKernel(Integer.MIN_VALUE - 512)); 152 153 assertEquals(0, kernelToTag("0x0000000000000000")); 154 assertEquals(0x32, kernelToTag("0x0000003200000000")); 155 assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); 156 assertEquals(0, kernelToTag("0x0000000000000000")); 157 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); 158 } 159 160 public void testNetworkStatsWithSet() throws Exception { 161 stageFile(R.raw.xt_qtaguid_typical_with_set, new File(mTestProc, "net/xt_qtaguid/stats")); 162 163 final NetworkStats stats = mService.getNetworkStatsDetail(); 164 assertEquals(12, stats.size()); 165 assertStatsEntry(stats, "rmnet0", 1000, SET_DEFAULT, 0, 278102L, 253L, 10487L, 182L); 166 assertStatsEntry(stats, "rmnet0", 1000, SET_FOREGROUND, 0, 26033L, 30L, 1401L, 26L); 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