NetworkManagementServiceTest.java revision b5d55e302d2253e4bfb233ea705caf258cdc4cb9
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.server;
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;
24import static com.android.server.NetworkManagementSocketTagger.tagToKernel;
25
26import android.content.res.Resources;
27import android.net.NetworkStats;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.LargeTest;
30
31import com.android.frameworks.servicestests.R;
32
33import java.io.File;
34import java.io.FileOutputStream;
35import java.io.FileWriter;
36import java.io.InputStream;
37import java.io.OutputStream;
38
39import libcore.io.IoUtils;
40import libcore.io.Streams;
41
42/**
43 * Tests for {@link NetworkManagementService}.
44 */
45@LargeTest
46public class NetworkManagementServiceTest extends AndroidTestCase {
47    private File mTestProc;
48    private NetworkManagementService mService;
49
50    @Override
51    public void setUp() throws Exception {
52        super.setUp();
53
54        mTestProc = new File(getContext().getFilesDir(), "proc");
55        if (mTestProc.exists()) {
56            IoUtils.deleteContents(mTestProc);
57        }
58
59        mService = NetworkManagementService.createForTest(mContext, mTestProc, true);
60    }
61
62    @Override
63    public void tearDown() throws Exception {
64        mService = null;
65
66        if (mTestProc.exists()) {
67            IoUtils.deleteContents(mTestProc);
68        }
69
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
76        final NetworkStats stats = mService.getNetworkStatsDetail();
77        assertEquals(31, stats.size());
78        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0, 14615L, 4270L);
79        assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 0, 333821L, 53558L);
80        assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 1947740890, 18725L, 1066L);
81        assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 0, 31184994L, 684122L);
82        assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 1947740890, 28507378L, 437004L);
83    }
84
85    public void testNetworkStatsDetailExtended() throws Exception {
86        stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats"));
87
88        final NetworkStats stats = mService.getNetworkStatsDetail();
89        assertEquals(2, stats.size());
90        assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0, 1024L, 2048L);
91        assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0xF00D, 512L, 512L);
92    }
93
94    public void testNetworkStatsSummary() throws Exception {
95        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
96
97        final NetworkStats stats = mService.getNetworkStatsSummary();
98        assertEquals(6, stats.size());
99        assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L);
100        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
101        assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
102        assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
103        assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
104        assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
105    }
106
107    public void testNetworkStatsSummaryDown() throws Exception {
108        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
109        stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes"));
110        stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets"));
111        stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes"));
112        stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets"));
113
114        final NetworkStats stats = mService.getNetworkStatsSummary();
115        assertEquals(7, stats.size());
116        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
117        assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L);
118    }
119
120    public void testKernelTags() throws Exception {
121        assertEquals("0", tagToKernel(0x0));
122        assertEquals("214748364800", tagToKernel(0x32));
123        assertEquals("9223372032559808512", tagToKernel(Integer.MAX_VALUE));
124        assertEquals("0", tagToKernel(Integer.MIN_VALUE));
125        assertEquals("9223369837831520256", tagToKernel(Integer.MIN_VALUE - 512));
126
127        assertEquals(0, kernelToTag("0x0000000000000000"));
128        assertEquals(0x32, kernelToTag("0x0000003200000000"));
129        assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
130        assertEquals(0, kernelToTag("0x0000000000000000"));
131        assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
132    }
133
134    public void testNetworkStatsWithSet() throws Exception {
135        stageFile(R.raw.xt_qtaguid_typical_with_set, new File(mTestProc, "net/xt_qtaguid/stats"));
136
137        final NetworkStats stats = mService.getNetworkStatsDetail();
138        assertEquals(12, stats.size());
139        assertStatsEntry(stats, "rmnet0", 1000, SET_DEFAULT, 0, 278102L, 253L, 10487L, 182L);
140        assertStatsEntry(stats, "rmnet0", 1000, SET_FOREGROUND, 0, 26033L, 30L, 1401L, 26L);
141    }
142
143    /**
144     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
145     * testing purposes.
146     */
147    private void stageFile(int rawId, File file) throws Exception {
148        new File(file.getParent()).mkdirs();
149        InputStream in = null;
150        OutputStream out = null;
151        try {
152            in = getContext().getResources().openRawResource(rawId);
153            out = new FileOutputStream(file);
154            Streams.copy(in, out);
155        } finally {
156            IoUtils.closeQuietly(in);
157            IoUtils.closeQuietly(out);
158        }
159    }
160
161    private void stageLong(long value, File file) throws Exception {
162        new File(file.getParent()).mkdirs();
163        FileWriter out = null;
164        try {
165            out = new FileWriter(file);
166            out.write(Long.toString(value));
167        } finally {
168            IoUtils.closeQuietly(out);
169        }
170    }
171
172    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
173            int tag, long rxBytes, long txBytes) {
174        final int i = stats.findIndex(iface, uid, set, tag);
175        final NetworkStats.Entry entry = stats.getValues(i, null);
176        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
177        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
178    }
179
180    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
181            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
182        final int i = stats.findIndex(iface, uid, set, tag);
183        final NetworkStats.Entry entry = stats.getValues(i, null);
184        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
185        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
186        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
187        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
188    }
189
190}
191