NetworkStatsFactoryTest.java revision 0fea823673e9cab24bd16a1c80a9bd689fc7a6f9
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
174414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeypackage com.android.server;
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;
248568db534118fc14cc28100306d51626464ff319Jesse Wilsonimport static com.android.server.NetworkManagementSocketTagger.tagToKernel;
254414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
264414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.content.res.Resources;
274414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.net.NetworkStats;
284414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.test.AndroidTestCase;
294414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport android.test.suitebuilder.annotation.LargeTest;
304414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
314414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport com.android.frameworks.servicestests.R;
324414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
334414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.File;
344414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.FileOutputStream;
35fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkeyimport java.io.FileWriter;
364414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.InputStream;
374414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport java.io.OutputStream;
384414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
394414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport libcore.io.IoUtils;
404414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeyimport libcore.io.Streams;
414414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
424414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey/**
434414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey * Tests for {@link NetworkManagementService}.
444414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey */
454414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey@LargeTest
464414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkeypublic class NetworkManagementServiceTest extends AndroidTestCase {
474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    private File mTestProc;
484414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    private NetworkManagementService mService;
494414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
504414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    @Override
514414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void setUp() throws Exception {
524414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        super.setUp();
534414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
54a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey        mTestProc = new File(getContext().getFilesDir(), "proc");
55fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        if (mTestProc.exists()) {
56a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            IoUtils.deleteContents(mTestProc);
57fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
58fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
59fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        mService = NetworkManagementService.createForTest(mContext, mTestProc, true);
604414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
614414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
624414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    @Override
634414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void tearDown() throws Exception {
644414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        mService = null;
654414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
66fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        if (mTestProc.exists()) {
67a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey            IoUtils.deleteContents(mTestProc);
68fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
69fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
704414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        super.tearDown();
714414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
724414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
734414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void testNetworkStatsDetail() throws Exception {
744414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats"));
754414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
764414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        final NetworkStats stats = mService.getNetworkStatsDetail();
77fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(31, stats.size());
78b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "wlan0", 0, SET_DEFAULT, 0, 14615L, 4270L);
79b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 0, 333821L, 53558L);
80b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "wlan0", 10004, SET_DEFAULT, 1947740890, 18725L, 1066L);
81b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 0, 31184994L, 684122L);
82b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", 10037, SET_DEFAULT, 1947740890, 28507378L, 437004L);
834414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
844414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
854414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void testNetworkStatsDetailExtended() throws Exception {
864414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats"));
874414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
884414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        final NetworkStats stats = mService.getNetworkStatsDetail();
89fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(2, stats.size());
90b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0, 1024L, 2048L);
91b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "test0", 1000, SET_DEFAULT, 0xF00D, 512L, 512L);
924414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
934414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
94fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    public void testNetworkStatsSummary() throws Exception {
95fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
96fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
97fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        final NetworkStats stats = mService.getNetworkStatsSummary();
98fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(6, stats.size());
99b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "lo", UID_ALL, SET_DEFAULT, TAG_NONE, 8308L, 8308L);
100b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
101b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ifb0", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
102b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ifb1", UID_ALL, SET_DEFAULT, TAG_NONE, 52454L, 0L);
103b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "sit0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
104b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "ip6tnl0", UID_ALL, SET_DEFAULT, TAG_NONE, 0L, 0L);
105fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
106fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
107fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    public void testNetworkStatsSummaryDown() throws Exception {
108fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
1090fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/active"));
110fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(1024L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_bytes"));
111fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(128L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/rx_packets"));
112fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(2048L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_bytes"));
113fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        stageLong(256L, new File(mTestProc, "net/xt_qtaguid/iface_stat/wlan0/tx_packets"));
114fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
115fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        final NetworkStats stats = mService.getNetworkStatsSummary();
116fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        assertEquals(7, stats.size());
117b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L, 489339L);
118b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "wlan0", UID_ALL, SET_DEFAULT, TAG_NONE, 1024L, 2048L);
119fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
12047eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey
12147eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey    public void testNetworkStatsCombined() throws Exception {
12247eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
1230fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(1L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
12447eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
12547eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
12647eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
12747eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
12847eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey
12947eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        final NetworkStats stats = mService.getNetworkStatsSummary();
13047eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 1507570L + 10L,
13147eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey                2205L + 20L, 489339L + 30L, 2237L + 40L);
13247eb102b40cd1324d89816a7fb0fecd14fd7a408Jeff Sharkey    }
1330fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey
1340fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey    public void testNetworkStatsCombinedInactive() throws Exception {
1350fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageFile(R.raw.net_dev_typical, new File(mTestProc, "net/dev"));
1360fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(0L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/active"));
1370fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(10L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_bytes"));
1380fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(20L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/rx_packets"));
1390fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(30L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_bytes"));
1400fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        stageLong(40L, new File(mTestProc, "net/xt_qtaguid/iface_stat/rmnet0/tx_packets"));
1410fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey
1420fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        final NetworkStats stats = mService.getNetworkStatsSummary();
1430fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", UID_ALL, SET_DEFAULT, TAG_NONE, 10L, 20L, 30L, 40L);
1440fea823673e9cab24bd16a1c80a9bd689fc7a6f9Jeff Sharkey    }
145fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
1464414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    public void testKernelTags() throws Exception {
1474414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals("0", tagToKernel(0x0));
1484414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals("214748364800", tagToKernel(0x32));
1494414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals("9223372032559808512", tagToKernel(Integer.MAX_VALUE));
1504414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals("0", tagToKernel(Integer.MIN_VALUE));
1514414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals("9223369837831520256", tagToKernel(Integer.MIN_VALUE - 512));
1524414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
1534414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0, kernelToTag("0x0000000000000000"));
1544414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0x32, kernelToTag("0x0000003200000000"));
1554414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(2147483647, kernelToTag("0x7fffffff00000000"));
1564414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(0, kernelToTag("0x0000000000000000"));
1574414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000"));
1584414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
1594414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
160b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    public void testNetworkStatsWithSet() throws Exception {
161b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        stageFile(R.raw.xt_qtaguid_typical_with_set, new File(mTestProc, "net/xt_qtaguid/stats"));
162b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
163b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final NetworkStats stats = mService.getNetworkStatsDetail();
164b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals(12, stats.size());
165b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", 1000, SET_DEFAULT, 0, 278102L, 253L, 10487L, 182L);
166b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertStatsEntry(stats, "rmnet0", 1000, SET_FOREGROUND, 0, 26033L, 30L, 1401L, 26L);
167b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    }
168b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
1694414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    /**
1704414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
1714414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     * testing purposes.
1724414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey     */
1734414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    private void stageFile(int rawId, File file) throws Exception {
1744414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        new File(file.getParent()).mkdirs();
1754414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        InputStream in = null;
1764414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        OutputStream out = null;
1774414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        try {
1784414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            in = getContext().getResources().openRawResource(rawId);
1794414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            out = new FileOutputStream(file);
1804414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            Streams.copy(in, out);
1814414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        } finally {
1824414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            IoUtils.closeQuietly(in);
1834414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey            IoUtils.closeQuietly(out);
1844414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey        }
1854414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
1864414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
187fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    private void stageLong(long value, File file) throws Exception {
188fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        new File(file.getParent()).mkdirs();
189fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        FileWriter out = null;
190fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        try {
191fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            out = new FileWriter(file);
192fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            out.write(Long.toString(value));
193fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        } finally {
194fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey            IoUtils.closeQuietly(out);
195fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        }
196fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey    }
197fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey
198b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
199b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey            int tag, long rxBytes, long txBytes) {
200b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final int i = stats.findIndex(iface, uid, set, tag);
201b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final NetworkStats.Entry entry = stats.getValues(i, null);
202b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
203b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
204b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    }
205b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey
206b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey    private static void assertStatsEntry(NetworkStats stats, String iface, int uid, int set,
207b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey            int tag, long rxBytes, long rxPackets, long txBytes, long txPackets) {
208b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        final int i = stats.findIndex(iface, uid, set, tag);
209fd8be3e5e7420f3cca591daeec8a44487f5f65aaJeff Sharkey        final NetworkStats.Entry entry = stats.getValues(i, null);
210b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
211b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
212b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txBytes", txBytes, entry.txBytes);
213b5d55e302d2253e4bfb233ea705caf258cdc4cb9Jeff Sharkey        assertEquals("unexpected txPackets", txPackets, entry.txPackets);
2144414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey    }
2154414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey
2164414cea13908b8230640f84ef39603d68ff9c377Jeff Sharkey}
217