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(70, stats.size());
75        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L);
76        assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L);
77        assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L);
78        assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L);
79        assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L);
80    }
81
82    public void testNetworkStatsSummary() throws Exception {
83        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
84
85        final NetworkStats stats = mFactory.readNetworkStatsSummary();
86        assertEquals(6, stats.size());
87        assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L);
88        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
89        assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
90        assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
91        assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
92        assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
93    }
94
95    public void testNetworkStatsSummaryDown() throws Exception {
96        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
97        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active"));
98        stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes"));
99        stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets"));
100        stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes"));
101        stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets"));
102
103        final NetworkStats stats = mFactory.readNetworkStatsSummary();
104        assertEquals(7, stats.size());
105        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
106        assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L);
107    }
108
109    public void testNetworkStatsCombined() throws Exception {
110        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
111        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
112        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
113        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
114        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
115        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
116
117        final NetworkStats stats = mFactory.readNetworkStatsSummary();
118        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L,
119                2205L + 20L, 489339L + 30L, 2237L + 40L);
120    }
121
122    public void testNetworkStatsCombinedInactive() throws Exception {
123        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
124        stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
125        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
126        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
127        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
128        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
129
130        final NetworkStats stats = mFactory.readNetworkStatsSummary();
131        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L);
132    }
133
134    public void testKernelTags() throws Exception {
135        assertEquals(0, kernelToTag("0x0000000000000000"));
136        assertEquals(0x32, kernelToTag("0x0000003200000000"));
137        assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
138        assertEquals(0, kernelToTag("0x0000000000000000"));
139        assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
140    }
141
142    public void testNetworkStatsWithSet() throws Exception {
143        stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
144
145        final NetworkStats stats = mFactory.readNetworkStatsDetail();
146        assertEquals(70, stats.size());
147        assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 676L);
148        assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L);
149    }
150
151    public void testNetworkStatsSingle() throws Exception {
152        stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all"));
153
154        final NetworkStats stats = mFactory.readNetworkStatsSummary();
155        assertEquals(6, stats.size());
156        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 2112L, 24L, 700L, 10L);
157        assertStatsEntry(stats, "test1", UID_ALL, SET_DEFAULT, TAG_NONE, 6L, 8L, 10L, 12L);
158        assertStatsEntry(stats, "test2", UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L);
159    }
160
161    /**
162     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
163     * testing purposes.
164     */
165    private void stageFile(int rawId, File file) throws Exception {
166        new File(file.getParent()).mkdirs();
167        InputStream in = null;
168        OutputStream out = null;
169        try {
170            in = getContext().getResources().openRawResource(rawId);
171            out = new FileOutputStream(file);
172            Streams.copy(in, out);
173        } finally {
174            IoUtils.closeQuietly(in);
175            IoUtils.closeQuietly(out);
176        }
177    }
178
179    private void stageLong(long value, File file) throws Exception {
180        new File(file.getParent()).mkdirs();
181        FileWriter out = null;
182        try {
183            out = new FileWriter(file);
184            out.write(Long.toString(value));
185        } finally {
186            IoUtils.closeQuietly(out);
187        }
188    }
189
190    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
191            int tag, long rxBytes, long txBytes) {
192        final int i = stats.findIndex(iface, uid, set, tag);
193        final NetworkStats.Entry entry = stats.getValues(i, null);
194        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
195        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
196    }
197
198    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
199            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
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 rxPackets", rxPackets, entry.rxPackets);
204        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
205        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
206    }
207
208}
209