NetworkManagementServiceTest.java revision fd8be3e5e7420f3cca591daeec8a44487f5f65aa
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.TAG_NONE;
20import static android.net.NetworkStats.UID_ALL;
21import static com.android.server.NetworkManagementSocketTagger.kernelToTag;
22import static com.android.server.NetworkManagementSocketTagger.tagToKernel;
23
24import android.content.res.Resources;
25import android.net.NetworkStats;
26import android.test.AndroidTestCase;
27import android.test.suitebuilder.annotation.LargeTest;
28
29import com.android.frameworks.servicestests.R;
30import com.google.common.io.Files;
31
32import java.io.File;
33import java.io.FileOutputStream;
34import java.io.FileWriter;
35import java.io.InputStream;
36import java.io.OutputStream;
37
38import libcore.io.IoUtils;
39import libcore.io.Streams;
40
41/**
42 * Tests for {@link NetworkManagementService}.
43 */
44@LargeTest
45public class NetworkManagementServiceTest extends AndroidTestCase {
46    private File mTestProc;
47    private NetworkManagementService mService;
48
49    @Override
50    public void setUp() throws Exception {
51        super.setUp();
52
53        final File canonicalFilesDir = getContext().getFilesDir().getCanonicalFile();
54        mTestProc = new File(canonicalFilesDir, "proc");
55        if (mTestProc.exists()) {
56            Files.deleteRecursively(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            Files.deleteRecursively(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, 0, 14615L, 4270L);
79        assertStatsEntry(stats, "wlan0", 10004, 0, 333821L, 53558L);
80        assertStatsEntry(stats, "wlan0", 10004, 1947740890, 18725L, 1066L);
81        assertStatsEntry(stats, "rmnet0", 10037, 0, 31184994L, 684122L);
82        assertStatsEntry(stats, "rmnet0", 10037, 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, 0, 1024L, 2048L);
91        assertStatsEntry(stats, "test0", 1000, 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, TAG_NONE, 8308L, 8308L);
100        assertStatsEntry(stats, "rmnet0", UID_ALL, TAG_NONE, 1507570L, 489339L);
101        assertStatsEntry(stats, "ifb0", UID_ALL, TAG_NONE, 52454L, 0L);
102        assertStatsEntry(stats, "ifb1", UID_ALL, TAG_NONE, 52454L, 0L);
103        assertStatsEntry(stats, "sit0", UID_ALL, TAG_NONE, 0L, 0L);
104        assertStatsEntry(stats, "ip6tnl0", UID_ALL, 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, TAG_NONE, 1507570L, 489339L);
117        assertStatsEntry(stats, "wlan0", UID_ALL, 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    /**
135     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
136     * testing purposes.
137     */
138    private void stageFile(int rawId, File file) throws Exception {
139        new File(file.getParent()).mkdirs();
140        InputStream in = null;
141        OutputStream out = null;
142        try {
143            in = getContext().getResources().openRawResource(rawId);
144            out = new FileOutputStream(file);
145            Streams.copy(in, out);
146        } finally {
147            IoUtils.closeQuietly(in);
148            IoUtils.closeQuietly(out);
149        }
150    }
151
152    private void stageLong(long value, File file) throws Exception {
153        new File(file.getParent()).mkdirs();
154        FileWriter out = null;
155        try {
156            out = new FileWriter(file);
157            out.write(Long.toString(value));
158        } finally {
159            IoUtils.closeQuietly(out);
160        }
161    }
162
163    private static void assertStatsEntry(
164            NetworkStats stats, String iface, int uid, int tag, long rxBytes, long txBytes) {
165        final int i = stats.findIndex(iface, uid, tag);
166        final NetworkStats.Entry entry = stats.getValues(i, null);
167        assertEquals(rxBytes, entry.rxBytes);
168        assertEquals(txBytes, entry.txBytes);
169    }
170
171}
172