NetworkStatsCollectionTest.java revision 63abc37356728c0575d6a62a203102ae6d97953b
1/* 2 * Copyright (C) 2012 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 com.android.server.net; 18 19import static android.net.NetworkTemplate.buildTemplateMobileAll; 20import static android.text.format.DateUtils.MINUTE_IN_MILLIS; 21 22import android.content.res.Resources; 23import android.net.ConnectivityManager; 24import android.net.NetworkIdentity; 25import android.net.NetworkStats; 26import android.net.NetworkTemplate; 27import android.test.AndroidTestCase; 28import android.test.suitebuilder.annotation.MediumTest; 29 30import com.android.frameworks.servicestests.R; 31 32import java.io.ByteArrayInputStream; 33import java.io.ByteArrayOutputStream; 34import java.io.DataOutputStream; 35import java.io.File; 36import java.io.FileOutputStream; 37import java.io.InputStream; 38import java.io.OutputStream; 39 40import libcore.io.IoUtils; 41import libcore.io.Streams; 42 43/** 44 * Tests for {@link NetworkStatsCollection}. 45 */ 46@MediumTest 47public class NetworkStatsCollectionTest extends AndroidTestCase { 48 49 private static final String TEST_FILE = "test.bin"; 50 private static final String TEST_IMSI = "310260000000000"; 51 52 public void testReadLegacyNetwork() throws Exception { 53 final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 54 stageFile(R.raw.netstats_v1, testFile); 55 56 final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 57 collection.readLegacyNetwork(testFile); 58 59 // verify that history read correctly 60 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 61 636014522L, 709291L, 88037144L, 518820L); 62 63 // now export into a unified format 64 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 65 collection.write(new DataOutputStream(bos)); 66 67 // clear structure completely 68 collection.reset(); 69 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 70 0L, 0L, 0L, 0L); 71 72 // and read back into structure, verifying that totals are same 73 collection.read(new ByteArrayInputStream(bos.toByteArray())); 74 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 75 636014522L, 709291L, 88037144L, 518820L); 76 } 77 78 public void testReadLegacyUid() throws Exception { 79 final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 80 stageFile(R.raw.netstats_uid_v4, testFile); 81 82 final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 83 collection.readLegacyUid(testFile, false); 84 85 // verify that history read correctly 86 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 87 637073904L, 711398L, 88342093L, 521006L); 88 89 // now export into a unified format 90 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 91 collection.write(new DataOutputStream(bos)); 92 93 // clear structure completely 94 collection.reset(); 95 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 96 0L, 0L, 0L, 0L); 97 98 // and read back into structure, verifying that totals are same 99 collection.read(new ByteArrayInputStream(bos.toByteArray())); 100 assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 101 637073904L, 711398L, 88342093L, 521006L); 102 } 103 104 public void testReadLegacyUidTags() throws Exception { 105 final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 106 stageFile(R.raw.netstats_uid_v4, testFile); 107 108 final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 109 collection.readLegacyUid(testFile, true); 110 111 // verify that history read correctly 112 assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 113 77017831L, 100995L, 35436758L, 92344L); 114 115 // now export into a unified format 116 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 117 collection.write(new DataOutputStream(bos)); 118 119 // clear structure completely 120 collection.reset(); 121 assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 122 0L, 0L, 0L, 0L); 123 124 // and read back into structure, verifying that totals are same 125 collection.read(new ByteArrayInputStream(bos.toByteArray())); 126 assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 127 77017831L, 100995L, 35436758L, 92344L); 128 } 129 130 /** 131 * Copy a {@link Resources#openRawResource(int)} into {@link File} for 132 * testing purposes. 133 */ 134 private void stageFile(int rawId, File file) throws Exception { 135 new File(file.getParent()).mkdirs(); 136 InputStream in = null; 137 OutputStream out = null; 138 try { 139 in = getContext().getResources().openRawResource(rawId); 140 out = new FileOutputStream(file); 141 Streams.copy(in, out); 142 } finally { 143 IoUtils.closeQuietly(in); 144 IoUtils.closeQuietly(out); 145 } 146 } 147 148 public static NetworkIdentitySet buildWifiIdent() { 149 final NetworkIdentitySet set = new NetworkIdentitySet(); 150 set.add(new NetworkIdentity(ConnectivityManager.TYPE_WIFI, 0, null, false)); 151 return set; 152 } 153 154 private static void assertSummaryTotal(NetworkStatsCollection collection, 155 NetworkTemplate template, long rxBytes, long rxPackets, long txBytes, long txPackets) { 156 final NetworkStats.Entry entry = collection.getSummary( 157 template, Long.MIN_VALUE, Long.MAX_VALUE).getTotal(null); 158 assertEntry(entry, rxBytes, rxPackets, txBytes, txPackets); 159 } 160 161 private static void assertSummaryTotalIncludingTags(NetworkStatsCollection collection, 162 NetworkTemplate template, long rxBytes, long rxPackets, long txBytes, long txPackets) { 163 final NetworkStats.Entry entry = collection.getSummary( 164 template, Long.MIN_VALUE, Long.MAX_VALUE).getTotalIncludingTags(null); 165 assertEntry(entry, rxBytes, rxPackets, txBytes, txPackets); 166 } 167 168 private static void assertEntry( 169 NetworkStats.Entry entry, long rxBytes, long rxPackets, long txBytes, long txPackets) { 170 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 171 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 172 assertEquals("unexpected txBytes", txBytes, entry.txBytes); 173 assertEquals("unexpected txPackets", txPackets, entry.txPackets); 174 } 175} 176