NetworkStatsTest.java revision a63ba59260cd1bb3f5c16e395ace45a61f1d4461
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                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 10)
35                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 11)
36                .addValues(TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L, 12);
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.internalSize());
49
50        stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 1L, 1L, 2L, 2L, 3);
51        stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L, 2L, 2L, 4);
52
53        assertEquals(2, stats.size());
54        assertEquals(2, stats.internalSize());
55
56        stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 3L, 30L, 4L, 40L, 7);
57        stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 4L, 40L, 4L, 40L, 8);
58        stats.addValues(TEST_IFACE, TEST_UID, TAG_NONE, 5L, 50L, 5L, 50L, 10);
59
60        assertEquals(5, stats.size());
61        assertTrue(stats.internalSize() >= 5);
62
63        assertValues(stats, 0, TEST_IFACE, TEST_UID, TAG_NONE, 1L, 1L, 2L, 2L, 3);
64        assertValues(stats, 1, TEST_IFACE, TEST_UID, TAG_NONE, 2L, 2L, 2L, 2L, 4);
65        assertValues(stats, 2, TEST_IFACE, TEST_UID, TAG_NONE, 3L, 30L, 4L, 40L, 7);
66        assertValues(stats, 3, TEST_IFACE, TEST_UID, TAG_NONE, 4L, 40L, 4L, 40L, 8);
67        assertValues(stats, 4, TEST_IFACE, TEST_UID, TAG_NONE, 5L, 50L, 5L, 50L, 10);
68    }
69
70    public void testCombineExisting() throws Exception {
71        final NetworkStats stats = new NetworkStats(TEST_START, 10);
72
73        stats.addValues(TEST_IFACE, 1001, TAG_NONE, 512L, 4L, 256L, 2L, 10);
74        stats.addValues(TEST_IFACE, 1001, 0xff, 128L, 1L, 128L, 1L, 2);
75        stats.combineValues(TEST_IFACE, 1001, TAG_NONE, -128L, -1L, -128L, -1L, -1);
76
77        assertValues(stats, 0, TEST_IFACE, 1001, TAG_NONE, 384L, 3L, 128L, 1L, 9);
78        assertValues(stats, 1, TEST_IFACE, 1001, 0xff, 128L, 1L, 128L, 1L, 2);
79
80        // now try combining that should create row
81        stats.combineValues(TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L, 3);
82        assertValues(stats, 2, TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L, 3);
83        stats.combineValues(TEST_IFACE, 5005, TAG_NONE, 128L, 1L, 128L, 1L, 3);
84        assertValues(stats, 2, TEST_IFACE, 5005, TAG_NONE, 256L, 2L, 256L, 2L, 6);
85    }
86
87    public void testSubtractIdenticalData() throws Exception {
88        final NetworkStats before = new NetworkStats(TEST_START, 2)
89                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
90                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
91
92        final NetworkStats after = new NetworkStats(TEST_START, 2)
93                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
94                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
95
96        final NetworkStats result = after.subtract(before);
97
98        // identical data should result in zero delta
99        assertValues(result, 0, TEST_IFACE, 100, TAG_NONE, 0L, 0L, 0L, 0L, 0);
100        assertValues(result, 1, TEST_IFACE, 101, TAG_NONE, 0L, 0L, 0L, 0L, 0);
101    }
102
103    public void testSubtractIdenticalRows() throws Exception {
104        final NetworkStats before = new NetworkStats(TEST_START, 2)
105                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
106                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
107
108        final NetworkStats after = new NetworkStats(TEST_START, 2)
109                .addValues(TEST_IFACE, 100, TAG_NONE, 1025L, 9L, 2L, 1L, 15)
110                .addValues(TEST_IFACE, 101, TAG_NONE, 3L, 1L, 1028L, 9L, 20);
111
112        final NetworkStats result = after.subtract(before);
113
114        // expect delta between measurements
115        assertValues(result, 0, TEST_IFACE, 100, TAG_NONE, 1L, 1L, 2L, 1L, 4);
116        assertValues(result, 1, TEST_IFACE, 101, TAG_NONE, 3L, 1L, 4L, 1L, 8);
117    }
118
119    public void testSubtractNewRows() throws Exception {
120        final NetworkStats before = new NetworkStats(TEST_START, 2)
121                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
122                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 12);
123
124        final NetworkStats after = new NetworkStats(TEST_START, 3)
125                .addValues(TEST_IFACE, 100, TAG_NONE, 1024L, 8L, 0L, 0L, 11)
126                .addValues(TEST_IFACE, 101, TAG_NONE, 0L, 0L, 1024L, 8L, 12)
127                .addValues(TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L, 20);
128
129        final NetworkStats result = after.subtract(before);
130
131        // its okay to have new rows
132        assertValues(result, 0, TEST_IFACE, 100, TAG_NONE, 0L, 0L, 0L, 0L, 0);
133        assertValues(result, 1, TEST_IFACE, 101, TAG_NONE, 0L, 0L, 0L, 0L, 0);
134        assertValues(result, 2, TEST_IFACE, 102, TAG_NONE, 1024L, 8L, 1024L, 8L, 20);
135    }
136
137    private static void assertValues(NetworkStats stats, int index, String iface, int uid, int tag,
138            long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
139        final NetworkStats.Entry entry = stats.getValues(index, null);
140        assertEquals(iface, entry.iface);
141        assertEquals(uid, entry.uid);
142        assertEquals(tag, entry.tag);
143        assertEquals(rxBytes, entry.rxBytes);
144        assertEquals(rxPackets, entry.rxPackets);
145        assertEquals(txBytes, entry.txBytes);
146        assertEquals(txPackets, entry.txPackets);
147        assertEquals(operations, entry.operations);
148    }
149
150}
151