NetworkStatsFactoryTest.java revision c86013be936f36c61fa0cdfad95141f6de3c6ef7
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.METERED_NO; 20import static android.net.NetworkStats.ROAMING_NO; 21import static android.net.NetworkStats.SET_ALL; 22import static android.net.NetworkStats.SET_DEFAULT; 23import static android.net.NetworkStats.SET_FOREGROUND; 24import static android.net.NetworkStats.TAG_NONE; 25import static android.net.NetworkStats.UID_ALL; 26import static com.android.server.NetworkManagementSocketTagger.kernelToTag; 27 28import android.content.res.Resources; 29import android.net.NetworkStats; 30import android.net.TrafficStats; 31import android.test.AndroidTestCase; 32 33import com.android.frameworks.tests.net.R; 34 35import java.io.File; 36import java.io.FileOutputStream; 37import java.io.FileWriter; 38import java.io.InputStream; 39import java.io.OutputStream; 40 41import libcore.io.IoUtils; 42import libcore.io.Streams; 43 44/** 45 * Tests for {@link NetworkStatsFactory}. 46 */ 47public class NetworkStatsFactoryTest extends AndroidTestCase { 48 private File mTestProc; 49 private NetworkStatsFactory mFactory; 50 51 @Override 52 public void setUp() throws Exception { 53 super.setUp(); 54 55 mTestProc = new File(getContext().getFilesDir(), "proc"); 56 if (mTestProc.exists()) { 57 IoUtils.deleteContents(mTestProc); 58 } 59 60 mFactory = new NetworkStatsFactory(mTestProc); 61 } 62 63 @Override 64 public void tearDown() throws Exception { 65 mFactory = null; 66 67 if (mTestProc.exists()) { 68 IoUtils.deleteContents(mTestProc); 69 } 70 71 super.tearDown(); 72 } 73 74 public void testNetworkStatsDetail() throws Exception { 75 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); 76 77 final NetworkStats stats = mFactory.readNetworkStatsDetail(); 78 assertEquals(70, stats.size()); 79 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L); 80 assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L); 81 assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L); 82 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L); 83 assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L); 84 } 85 86 public void testKernelTags() throws Exception { 87 assertEquals(0, kernelToTag("0x0000000000000000")); 88 assertEquals(0x32, kernelToTag("0x0000003200000000")); 89 assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); 90 assertEquals(0, kernelToTag("0x0000000000000000")); 91 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); 92 93 assertEquals(0, kernelToTag("0x0")); 94 assertEquals(0, kernelToTag("0xf00d")); 95 assertEquals(1, kernelToTag("0x100000000")); 96 assertEquals(14438007, kernelToTag("0xdc4e7700000000")); 97 assertEquals(TrafficStats.TAG_SYSTEM_DOWNLOAD, kernelToTag("0xffffff0100000000")); 98 } 99 100 public void testNetworkStatsWithSet() throws Exception { 101 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); 102 103 final NetworkStats stats = mFactory.readNetworkStatsDetail(); 104 assertEquals(70, stats.size()); 105 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 106 676L); 107 assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L); 108 } 109 110 public void testNetworkStatsSingle() throws Exception { 111 stageFile(R.raw.xt_qtaguid_iface_typical, 112 new File(mTestProc, "net/xt_qtaguid/iface_stat_all")); 113 114 final NetworkStats stats = mFactory.readNetworkStatsSummaryDev(); 115 assertEquals(6, stats.size()); 116 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L); 117 assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L); 118 assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L); 119 } 120 121 public void testNetworkStatsXt() throws Exception { 122 stageFile(R.raw.xt_qtaguid_iface_fmt_typical, 123 new File(mTestProc, "net/xt_qtaguid/iface_stat_fmt")); 124 125 final NetworkStats stats = mFactory.readNetworkStatsSummaryXt(); 126 assertEquals(3, stats.size()); 127 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 6824L, 16L, 5692L, 10L); 128 assertStatsEntry(stats, "rmnet1", UID_ALL, SET_ALL, TAG_NONE, 11153922L, 8051L, 190226L, 129 2468L); 130 assertStatsEntry(stats, "rmnet2", UID_ALL, SET_ALL, TAG_NONE, 4968L, 35L, 3081L, 39L); 131 } 132 133 /** 134 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 135 * testing purposes. 136 */ 137 private void stageFile(int rawId, File file) throws Exception { 138 new File(file.getParent()).mkdirs(); 139 InputStream in = null; 140 OutputStream out = null; 141 try { 142 in = getContext().getResources().openRawResource(rawId); 143 out = new FileOutputStream(file); 144 Streams.copy(in, out); 145 } finally { 146 IoUtils.closeQuietly(in); 147 IoUtils.closeQuietly(out); 148 } 149 } 150 151 private void stageLong(long value, File file) throws Exception { 152 new File(file.getParent()).mkdirs(); 153 FileWriter out = null; 154 try { 155 out = new FileWriter(file); 156 out.write(Long.toString(value)); 157 } finally { 158 IoUtils.closeQuietly(out); 159 } 160 } 161 162 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 163 int tag, long rxBytes, long txBytes) { 164 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO); 165 final NetworkStats.Entry entry = stats.getValues(i, null); 166 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 167 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 168 } 169 170 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set, 171 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) { 172 final int i = stats.findIndex(iface, uid, set, tag, METERED_NO, ROAMING_NO); 173 final NetworkStats.Entry entry = stats.getValues(i, null); 174 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 175 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 176 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 177 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 178 } 179 180} 181