blob: d3dd01a2401fe833ff7b532085f12d0415a65c6c [file] [log] [blame]
Jeff Sharkey4414cea2011-06-24 17:05:24 -07001/*
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 Sharkey1059c3c2011-10-04 16:54:49 -070017package com.android.internal.net;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070018
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070019import static android.net.NetworkStats.SET_ALL;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070020import static android.net.NetworkStats.SET_DEFAULT;
21import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070022import static android.net.NetworkStats.TAG_NONE;
23import static android.net.NetworkStats.UID_ALL;
Jesse Wilson8568db52011-06-28 19:06:31 -070024import static com.android.server.NetworkManagementSocketTagger.kernelToTag;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070025
26import android.content.res.Resources;
27import android.net.NetworkStats;
Jeff Sharkey2d6c5802012-05-02 16:00:52 -070028import android.net.TrafficStats;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070029import android.test.AndroidTestCase;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070030
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070031import com.android.frameworks.coretests.R;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070032
33import java.io.File;
34import java.io.FileOutputStream;
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070035import java.io.FileWriter;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070036import java.io.InputStream;
37import java.io.OutputStream;
38
39import libcore.io.IoUtils;
40import libcore.io.Streams;
41
42/**
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070043 * Tests for {@link NetworkStatsFactory}.
Jeff Sharkey4414cea2011-06-24 17:05:24 -070044 */
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070045public class NetworkStatsFactoryTest extends AndroidTestCase {
Jeff Sharkey4414cea2011-06-24 17:05:24 -070046 private File mTestProc;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070047 private NetworkStatsFactory mFactory;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070048
49 @Override
50 public void setUp() throws Exception {
51 super.setUp();
52
Jeff Sharkeya63ba592011-07-19 23:47:12 -070053 mTestProc = new File(getContext().getFilesDir(), "proc");
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070054 if (mTestProc.exists()) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -070055 IoUtils.deleteContents(mTestProc);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070056 }
57
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070058 mFactory = new NetworkStatsFactory(mTestProc);
Jeff Sharkey4414cea2011-06-24 17:05:24 -070059 }
60
61 @Override
62 public void tearDown() throws Exception {
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070063 mFactory = null;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070064
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070065 if (mTestProc.exists()) {
Jeff Sharkeya63ba592011-07-19 23:47:12 -070066 IoUtils.deleteContents(mTestProc);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -070067 }
68
Jeff Sharkey4414cea2011-06-24 17:05:24 -070069 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
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075 final NetworkStats stats = mFactory.readNetworkStatsDetail();
Jeff Sharkey163e6442011-10-31 16:37:52 -070076 assertEquals(70, stats.size());
77 assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L);
78 assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L);
79 assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L);
80 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L);
81 assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L);
Jeff Sharkey4414cea2011-06-24 17:05:24 -070082 }
83
84 public void testKernelTags() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -070085 assertEquals(0, kernelToTag("0x0000000000000000"));
86 assertEquals(0x32, kernelToTag("0x0000003200000000"));
87 assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
88 assertEquals(0, kernelToTag("0x0000000000000000"));
89 assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
Jeff Sharkey2d6c5802012-05-02 16:00:52 -070090
91 assertEquals(0, kernelToTag("0x0"));
92 assertEquals(0, kernelToTag("0xf00d"));
93 assertEquals(1, kernelToTag("0x100000000"));
94 assertEquals(14438007, kernelToTag("0xdc4e7700000000"));
95 assertEquals(TrafficStats.TAG_SYSTEM_DOWNLOAD, kernelToTag("0xffffff0100000000"));
Jeff Sharkey4414cea2011-06-24 17:05:24 -070096 }
97
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070098 public void testNetworkStatsWithSet() throws Exception {
Jeff Sharkey163e6442011-10-31 16:37:52 -070099 stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700100
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700101 final NetworkStats stats = mFactory.readNetworkStatsDetail();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700102 assertEquals(70, stats.size());
103 assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 676L);
104 assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700105 }
106
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700107 public void testNetworkStatsSingle() throws Exception {
108 stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all"));
109
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700110 final NetworkStats stats = mFactory.readNetworkStatsSummaryDev();
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700111 assertEquals(6, stats.size());
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700112 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 2112L, 24L, 700L, 10L);
113 assertStatsEntry(stats, "test1", UID_ALL, SET_ALL, TAG_NONE, 6L, 8L, 10L, 12L);
114 assertStatsEntry(stats, "test2", UID_ALL, SET_ALL, TAG_NONE, 1L, 2L, 3L, 4L);
Jeff Sharkeyae2c1812011-10-04 13:11:40 -0700115 }
116
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700117 public void testNetworkStatsXt() throws Exception {
118 stageFile(R.raw.xt_qtaguid_iface_fmt_typical,
119 new File(mTestProc, "net/xt_qtaguid/iface_stat_fmt"));
120
121 final NetworkStats stats = mFactory.readNetworkStatsSummaryXt();
122 assertEquals(3, stats.size());
123 assertStatsEntry(stats, "rmnet0", UID_ALL, SET_ALL, TAG_NONE, 6824L, 16L, 5692L, 10L);
124 assertStatsEntry(stats, "rmnet1", UID_ALL, SET_ALL, TAG_NONE, 11153922L, 8051L, 190226L, 2468L);
125 assertStatsEntry(stats, "rmnet2", UID_ALL, SET_ALL, TAG_NONE, 4968L, 35L, 3081L, 39L);
126 }
127
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700128 /**
129 * Copy a {@link Resources#openRawResource(int)} into {@link File} for
130 * testing purposes.
131 */
132 private void stageFile(int rawId, File file) throws Exception {
133 new File(file.getParent()).mkdirs();
134 InputStream in = null;
135 OutputStream out = null;
136 try {
137 in = getContext().getResources().openRawResource(rawId);
138 out = new FileOutputStream(file);
139 Streams.copy(in, out);
140 } finally {
141 IoUtils.closeQuietly(in);
142 IoUtils.closeQuietly(out);
143 }
144 }
145
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700146 private void stageLong(long value, File file) throws Exception {
147 new File(file.getParent()).mkdirs();
148 FileWriter out = null;
149 try {
150 out = new FileWriter(file);
151 out.write(Long.toString(value));
152 } finally {
153 IoUtils.closeQuietly(out);
154 }
155 }
156
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700157 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
158 int tag, long rxBytes, long txBytes) {
159 final int i = stats.findIndex(iface, uid, set, tag);
Jeff Sharkeyfd8be3e2011-07-11 14:36:15 -0700160 final NetworkStats.Entry entry = stats.getValues(i, null);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700161 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
162 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
163 }
164
165 private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
166 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
167 final int i = stats.findIndex(iface, uid, set, tag);
168 final NetworkStats.Entry entry = stats.getValues(i, null);
169 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
170 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
171 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
172 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700173 }
174
175}