summaryrefslogtreecommitdiff
path: root/tests/unit/java/com/android/server/net/NetworkStatsBaseTest.java
blob: a058a466a4ff056edc9644b880baa939777ab1ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.net;

import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
import static android.net.NetworkStats.METERED_ALL;
import static android.net.NetworkStats.METERED_NO;
import static android.net.NetworkStats.METERED_YES;
import static android.net.NetworkStats.ROAMING_ALL;
import static android.net.NetworkStats.ROAMING_NO;
import static android.net.NetworkStats.ROAMING_YES;
import static android.net.NetworkStats.SET_ALL;
import static android.net.NetworkStats.SET_DEFAULT;
import static android.net.NetworkStats.SET_FOREGROUND;
import static android.net.NetworkStats.TAG_NONE;

import static org.junit.Assert.assertEquals;

import android.net.NetworkStats;
import android.net.UnderlyingNetworkInfo;

import java.util.Arrays;

/** Superclass with utilities for NetworkStats(Service|Factory)Test */
abstract class NetworkStatsBaseTest {
    static final String TEST_IFACE = "test0";
    static final String TEST_IFACE2 = "test1";
    static final String TUN_IFACE = "test_nss_tun0";
    static final String TUN_IFACE2 = "test_nss_tun1";

    static final int UID_RED = 1001;
    static final int UID_BLUE = 1002;
    static final int UID_GREEN = 1003;
    static final int UID_VPN = 1004;

    void assertValues(NetworkStats stats, String iface, int uid, long rxBytes,
            long rxPackets, long txBytes, long txPackets) {
        assertValues(
                stats, iface, uid, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
                rxBytes, rxPackets, txBytes, txPackets, 0);
    }

    void assertValues(NetworkStats stats, String iface, int uid, int set, int tag,
            int metered, int roaming, int defaultNetwork, long rxBytes, long rxPackets,
            long txBytes, long txPackets, long operations) {
        final NetworkStats.Entry entry = new NetworkStats.Entry();
        final int[] sets;
        if (set == SET_ALL) {
            sets = new int[] {SET_ALL, SET_DEFAULT, SET_FOREGROUND};
        } else {
            sets = new int[] {set};
        }

        final int[] roamings;
        if (roaming == ROAMING_ALL) {
            roamings = new int[] {ROAMING_ALL, ROAMING_YES, ROAMING_NO};
        } else {
            roamings = new int[] {roaming};
        }

        final int[] meterings;
        if (metered == METERED_ALL) {
            meterings = new int[] {METERED_ALL, METERED_YES, METERED_NO};
        } else {
            meterings = new int[] {metered};
        }

        final int[] defaultNetworks;
        if (defaultNetwork == DEFAULT_NETWORK_ALL) {
            defaultNetworks =
                    new int[] {DEFAULT_NETWORK_ALL, DEFAULT_NETWORK_YES, DEFAULT_NETWORK_NO};
        } else {
            defaultNetworks = new int[] {defaultNetwork};
        }

        for (int s : sets) {
            for (int r : roamings) {
                for (int m : meterings) {
                    for (int d : defaultNetworks) {
                        final int i = stats.findIndex(iface, uid, s, tag, m, r, d);
                        if (i != -1) {
                            entry.add(stats.getValues(i, null));
                        }
                    }
                }
            }
        }

        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
        assertEquals("unexpected operations", operations, entry.operations);
    }

    static UnderlyingNetworkInfo createVpnInfo(String[] underlyingIfaces) {
        return createVpnInfo(TUN_IFACE, underlyingIfaces);
    }

    static UnderlyingNetworkInfo createVpnInfo(String vpnIface, String[] underlyingIfaces) {
        return new UnderlyingNetworkInfo(UID_VPN, vpnIface, Arrays.asList(underlyingIfaces));
    }
}