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