14414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey/*
24414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project
34414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey *
44414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
54414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * you may not use this file except in compliance with the License.
64414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * You may obtain a copy of the License at
74414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey *
84414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
94414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey *
104414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
114414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
124414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * See the License for the specific language governing permissions and
144414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * limitations under the License.
154414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey */
164414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
171059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkeypackage com.android.internal.net;
184414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
19b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkeyimport static android.net.NetworkStats.SET_DEFAULT;
20b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkeyimport static android.net.NetworkStats.SET_FOREGROUND;
21fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkeyimport static android.net.NetworkStats.TAG_NONE;
22fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkeyimport static android.net.NetworkStats.UID_ALL;
238568db534118fc14cc28100306d51626464ff319Jesse Wilsonimport static com.android.server.NetworkManagementSocketTagger.kernelToTag;
244414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
254414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.content.res.Resources;
264414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.net.NetworkStats;
274414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.test.AndroidTestCase;
284414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
291059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkeyimport com.android.frameworks.coretests.R;
304414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
314414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.File;
324414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.FileOutputStream;
33fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkeyimport java.io.FileWriter;
344414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.InputStream;
354414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.OutputStream;
364414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
374414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport libcore.io.IoUtils;
384414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport libcore.io.Streams;
394414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
404414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey/**
411059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey * Tests for {@link NetworkStatsFactory}.
424414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey */
431059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkeypublic class NetworkStatsFactoryTest extends AndroidTestCase {
444414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    private File mTestProc;
451059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey    private NetworkStatsFactory mFactory;
464414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    @Override
484414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void setUp() throws Exception {
494414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        super.setUp();
504414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
51a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        mTestProc = new File(getContext().getFilesDir(), "proc");
52fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        if (mTestProc.exists()) {
53a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            IoUtils.deleteContents(mTestProc);
54fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
55fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
561059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        mFactory = new NetworkStatsFactory(mTestProc);
574414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
584414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
594414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    @Override
604414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void tearDown() throws Exception {
611059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        mFactory = null;
624414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
63fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        if (mTestProc.exists()) {
64a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            IoUtils.deleteContents(mTestProc);
65fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
66fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
674414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        super.tearDown();
684414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
694414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
704414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void testNetworkStatsDetail() throws Exception {
714414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
724414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
731059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsDetail();
74163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertEquals(70, stats.size());
75163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0x0, 18621L, 2898L);
76163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "wlan0", 10011, SET_DEFAULT, 0x0, 35777L, 5718L);
77163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "wlan0", 10021, SET_DEFAULT, 0x7fffff01, 562386L, 49228L);
78163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 227423L);
79163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "rmnet2", 10001, SET_DEFAULT, 0x0, 1125899906842624L, 984L);
804414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
814414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
82fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    public void testNetworkStatsSummary() throws Exception {
83fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
84fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
851059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsSummary();
86fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(6, stats.size());
87b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L);
88b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
89b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
90b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
91b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
92b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
93fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
94fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
95fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    public void testNetworkStatsSummaryDown() throws Exception {
96fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
970fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active"));
98fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes"));
99fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets"));
100fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes"));
101fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets"));
102fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
1031059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsSummary();
104fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(7, stats.size());
105b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
106b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L);
107fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
10847eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey
10947eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey    public void testNetworkStatsCombined() throws Exception {
11047eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
1110fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
11247eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
11347eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
11447eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
11547eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
11647eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey
1171059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsSummary();
11847eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L,
11947eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey                2205L + 20L, 489339L + 30L, 2237L + 40L);
12047eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey    }
1210fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey
1220fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey    public void testNetworkStatsCombinedInactive() throws Exception {
1230fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
1240fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
1250fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
1260fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
1270fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
1280fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
1290fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey
1301059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsSummary();
1310fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L);
1320fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey    }
133fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
1344414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void testKernelTags() throws Exception {
1354414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0, kernelToTag("0x0000000000000000"));
1364414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0x32, kernelToTag("0x0000003200000000"));
1374414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
1384414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0, kernelToTag("0x0000000000000000"));
1394414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
1404414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
1414414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
142b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    public void testNetworkStatsWithSet() throws Exception {
143163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
144b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
1451059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsDetail();
146163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertEquals(70, stats.size());
147163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "rmnet1", 10021, SET_DEFAULT, 0x30100000, 219110L, 578L, 227423L, 676L);
148163e6443f27884a9bfcb9a48ef606dc635852c23Jeff Sharkey        assertStatsEntry(stats, "rmnet1", 10021, SET_FOREGROUND, 0x30100000, 742L, 3L, 1265L, 3L);
149b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    }
150b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
151ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey    public void testNetworkStatsSingle() throws Exception {
152ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey        stageFile(R.raw.xt_qtaguid_iface_typical, new File(mTestProc, "net/xt_qtaguid/iface_stat_all"));
153ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey
1541059c3c30ad96a15695c1a92ae8896e078a6309fJeff Sharkey        final NetworkStats stats = mFactory.readNetworkStatsSummary();
155ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey        assertEquals(6, stats.size());
156ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 2112L, 24L, 700L, 10L);
157ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey        assertStatsEntry(stats, "test1", UID_ALL, SET_DEFAULT, TAG_NONE, 6L, 8L, 10L, 12L);
158ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey        assertStatsEntry(stats, "test2", UID_ALL, SET_DEFAULT, TAG_NONE, 1L, 2L, 3L, 4L);
159ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey    }
160ae2c1810839430c84e4a1172580d9c4b18f568caJeff Sharkey
1614414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    /**
1624414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
1634414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     * testing purposes.
1644414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     */
1654414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    private void stageFile(int rawId, File file) throws Exception {
1664414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        new File(file.getParent()).mkdirs();
1674414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        InputStream in = null;
1684414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        OutputStream out = null;
1694414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        try {
1704414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            in = getContext().getResources().openRawResource(rawId);
1714414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            out = new FileOutputStream(file);
1724414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            Streams.copy(in, out);
1734414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        } finally {
1744414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            IoUtils.closeQuietly(in);
1754414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            IoUtils.closeQuietly(out);
1764414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        }
1774414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
1784414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
179fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    private void stageLong(long value, File file) throws Exception {
180fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        new File(file.getParent()).mkdirs();
181fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        FileWriter out = null;
182fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        try {
183fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            out = new FileWriter(file);
184fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            out.write(Long.toString(value));
185fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        } finally {
186fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            IoUtils.closeQuietly(out);
187fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
188fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
189fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
190b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
191b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey            int tag, long rxBytes, long txBytes) {
192b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final int i = stats.findIndex(iface, uid, set, tag);
193b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final NetworkStats.Entry entry = stats.getValues(i, null);
194b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
195b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
196b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    }
197b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
198b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
199b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
200b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final int i = stats.findIndex(iface, uid, set, tag);
201fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        final NetworkStats.Entry entry = stats.getValues(i, null);
202b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
203b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
204b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
205b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
2064414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
2074414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
2084414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey}
209