NetworkStatsTest.java revision 1b5a2a96f793211bfbd39aa29cc41031dfa23950
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 android.net;
18
19import static android.net.NetworkStats.TAG_NONE;
20
21import android.test.suitebuilder.annotation.SmallTest;
22
23import junit.framework.TestCase;
24
25@SmallTest
26public class NetworkStatsTest extends TestCase {
27
28    private static final String TEST_IFACE = "test0";
29    private static final int TEST_UID = 1001;
30    private static final long TEST_START = 1194220800000L;
31
32    public void testFindIndex() throws Exception {
33        final NetworkStats stats = new NetworkStats(TEST_START, 3)
34                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
35                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L)
36                .addEntry(TEST_IFACE, 102, TAG_NONE, 1024L, 1024L);
37
38        assertEquals(2, stats.findIndex(TEST_IFACE, 102, TAG_NONE));
39        assertEquals(2, stats.findIndex(TEST_IFACE, 102, TAG_NONE));
40        assertEquals(0, stats.findIndex(TEST_IFACE, 100, TAG_NONE));
41        assertEquals(-1, stats.findIndex(TEST_IFACE, 6, TAG_NONE));
42    }
43
44    public void testAddEntryGrow() throws Exception {
45        final NetworkStats stats = new NetworkStats(TEST_START, 2);
46
47        assertEquals(0, stats.size);
48        assertEquals(2, stats.iface.length);
49
50        stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 1L, 2L);
51        stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L);
52
53        assertEquals(2, stats.size);
54        assertEquals(2, stats.iface.length);
55
56        stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 3L, 4L);
57        stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 4L, 4L);
58        stats.addEntry(TEST_IFACE, TEST_UID, TAG_NONE, 5L, 5L);
59
60        assertEquals(5, stats.size);
61        assertTrue(stats.iface.length >= 5);
62
63        assertEquals(1L, stats.rx[0]);
64        assertEquals(2L, stats.rx[1]);
65        assertEquals(3L, stats.rx[2]);
66        assertEquals(4L, stats.rx[3]);
67        assertEquals(5L, stats.rx[4]);
68    }
69
70    public void testCombineExisting() throws Exception {
71        final NetworkStats stats = new NetworkStats(TEST_START, 10);
72
73        stats.addEntry(TEST_IFACE, 1001, TAG_NONE, 512L, 256L);
74        stats.addEntry(TEST_IFACE, 1001, 0xff, 128L, 128L);
75        stats.combineEntry(TEST_IFACE, 1001, TAG_NONE, -128L, -128L);
76
77        assertStatsEntry(stats, 0, TEST_IFACE, 1001, TAG_NONE, 384L, 128L);
78        assertStatsEntry(stats, 1, TEST_IFACE, 1001, 0xff, 128L, 128L);
79
80        // now try combining that should create row
81        stats.combineEntry(TEST_IFACE, 5005, TAG_NONE, 128L, 128L);
82        assertStatsEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 128L, 128L);
83        stats.combineEntry(TEST_IFACE, 5005, TAG_NONE, 128L, 128L);
84        assertStatsEntry(stats, 2, TEST_IFACE, 5005, TAG_NONE, 256L, 256L);
85    }
86
87    public void testSubtractIdenticalData() throws Exception {
88        final NetworkStats before = new NetworkStats(TEST_START, 2)
89                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
90                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L);
91
92        final NetworkStats after = new NetworkStats(TEST_START, 2)
93                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
94                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L);
95
96        final NetworkStats result = after.subtract(before);
97
98        // identical data should result in zero delta
99        assertEquals(0, result.rx[0]);
100        assertEquals(0, result.tx[0]);
101        assertEquals(0, result.rx[1]);
102        assertEquals(0, result.tx[1]);
103    }
104
105    public void testSubtractIdenticalRows() throws Exception {
106        final NetworkStats before = new NetworkStats(TEST_START, 2)
107                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
108                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L);
109
110        final NetworkStats after = new NetworkStats(TEST_START, 2)
111                .addEntry(TEST_IFACE, 100, TAG_NONE, 1025L, 2L)
112                .addEntry(TEST_IFACE, 101, TAG_NONE, 3L, 1028L);
113
114        final NetworkStats result = after.subtract(before);
115
116        // expect delta between measurements
117        assertEquals(1, result.rx[0]);
118        assertEquals(2, result.tx[0]);
119        assertEquals(3, result.rx[1]);
120        assertEquals(4, result.tx[1]);
121    }
122
123    public void testSubtractNewRows() throws Exception {
124        final NetworkStats before = new NetworkStats(TEST_START, 2)
125                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
126                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L);
127
128        final NetworkStats after = new NetworkStats(TEST_START, 3)
129                .addEntry(TEST_IFACE, 100, TAG_NONE, 1024L, 0L)
130                .addEntry(TEST_IFACE, 101, TAG_NONE, 0L, 1024L)
131                .addEntry(TEST_IFACE, 102, TAG_NONE, 1024L, 1024L);
132
133        final NetworkStats result = after.subtract(before);
134
135        // its okay to have new rows
136        assertEquals(0, result.rx[0]);
137        assertEquals(0, result.tx[0]);
138        assertEquals(0, result.rx[1]);
139        assertEquals(0, result.tx[1]);
140        assertEquals(1024, result.rx[2]);
141        assertEquals(1024, result.tx[2]);
142    }
143
144    private static void assertStatsEntry(
145            NetworkStats stats, int i, String iface, int uid, int tag, long rx, long tx) {
146        assertEquals(iface, stats.iface[i]);
147        assertEquals(uid, stats.uid[i]);
148        assertEquals(tag, stats.tag[i]);
149        assertEquals(rx, stats.rx[i]);
150        assertEquals(tx, stats.tx[i]);
151    }
152
153}
154