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