NetworkManagementServiceTest.java revision 8568db534118fc14cc28100306d51626464ff319
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 com.android.server.NetworkManagementSocketTagger.kernelToTag;
20import static com.android.server.NetworkManagementSocketTagger.tagToKernel;
21
22import android.content.res.Resources;
23import android.net.NetworkStats;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.LargeTest;
26
27import com.android.frameworks.servicestests.R;
28
29import java.io.File;
30import java.io.FileOutputStream;
31import java.io.InputStream;
32import java.io.OutputStream;
33
34import libcore.io.IoUtils;
35import libcore.io.Streams;
36
37/**
38 * Tests for {@link NetworkManagementService}.
39 */
40@LargeTest
41public class NetworkManagementServiceTest extends AndroidTestCase {
42    private File mTestProc;
43    private NetworkManagementService mService;
44
45    @Override
46    public void setUp() throws Exception {
47        super.setUp();
48
49        mTestProc = getContext().getFilesDir();
50        mService = NetworkManagementService.createForTest(mContext, mTestProc);
51    }
52
53    @Override
54    public void tearDown() throws Exception {
55        mService = null;
56
57        super.tearDown();
58    }
59
60    public void testNetworkStatsDetail() throws Exception {
61        stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
62
63        final NetworkStats stats = mService.getNetworkStatsDetail();
64        assertEquals(31, stats.size);
65        assertStatsEntry(stats, "wlan0", 0, 0, 14615L, 4270L);
66        assertStatsEntry(stats, "wlan0", 10004, 0, 333821L, 53558L);
67        assertStatsEntry(stats, "wlan0", 10004, 1947740890, 18725L, 1066L);
68        assertStatsEntry(stats, "rmnet0", 10037, 0, 31184994L, 684122L);
69        assertStatsEntry(stats, "rmnet0", 10037, 1947740890, 28507378L, 437004L);
70    }
71
72    public void testNetworkStatsDetailExtended() throws Exception {
73        stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats"));
74
75        final NetworkStats stats = mService.getNetworkStatsDetail();
76        assertEquals(2, stats.size);
77        assertStatsEntry(stats, "test0", 1000, 0, 1024L, 2048L);
78        assertStatsEntry(stats, "test0", 1000, 0xF00D, 512L, 512L);
79    }
80
81    public void testKernelTags() throws Exception {
82        assertEquals("0", tagToKernel(0x0));
83        assertEquals("214748364800", tagToKernel(0x32));
84        assertEquals("9223372032559808512", tagToKernel(Integer.MAX_VALUE));
85        assertEquals("0", tagToKernel(Integer.MIN_VALUE));
86        assertEquals("9223369837831520256", tagToKernel(Integer.MIN_VALUE - 512));
87
88        assertEquals(0, kernelToTag("0x0000000000000000"));
89        assertEquals(0x32, kernelToTag("0x0000003200000000"));
90        assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
91        assertEquals(0, kernelToTag("0x0000000000000000"));
92        assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
93
94    }
95
96    /**
97     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
98     * testing purposes.
99     */
100    private void stageFile(int rawId, File file) throws Exception {
101        new File(file.getParent()).mkdirs();
102        InputStream in = null;
103        OutputStream out = null;
104        try {
105            in = getContext().getResources().openRawResource(rawId);
106            out = new FileOutputStream(file);
107            Streams.copy(in, out);
108        } finally {
109            IoUtils.closeQuietly(in);
110            IoUtils.closeQuietly(out);
111        }
112    }
113
114    private static void assertStatsEntry(
115            NetworkStats stats, String iface, int uid, int tag, long rx, long tx) {
116        final int i = stats.findIndex(iface, uid, tag);
117        assertEquals(rx, stats.rx[i]);
118        assertEquals(tx, stats.tx[i]);
119    }
120
121}
122