163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey/* 263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * Copyright (C) 2012 The Android Open Source Project 363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * 463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * you may not use this file except in compliance with the License. 663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * You may obtain a copy of the License at 763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * 863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * 1063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * Unless required by applicable law or agreed to in writing, software 1163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 1263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * See the License for the specific language governing permissions and 1463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * limitations under the License. 1563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey */ 1663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 1763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeypackage com.android.server.net; 1863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 1970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkeyimport static android.net.NetworkStats.SET_DEFAULT; 2070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkeyimport static android.net.NetworkStats.TAG_NONE; 2170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkeyimport static android.net.NetworkStats.UID_ALL; 2263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport static android.net.NetworkTemplate.buildTemplateMobileAll; 2370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkeyimport static android.text.format.DateUtils.HOUR_IN_MILLIS; 2463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport static android.text.format.DateUtils.MINUTE_IN_MILLIS; 2563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 2663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport android.content.res.Resources; 2763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport android.net.NetworkStats; 2863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport android.net.NetworkTemplate; 2963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport android.test.AndroidTestCase; 3063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport android.test.suitebuilder.annotation.MediumTest; 3163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 3263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport com.android.frameworks.servicestests.R; 3363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 3463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.ByteArrayInputStream; 3563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.ByteArrayOutputStream; 3663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.DataOutputStream; 3763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.File; 3863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.FileOutputStream; 3963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.InputStream; 4063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport java.io.OutputStream; 4163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 4263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport libcore.io.IoUtils; 4363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeyimport libcore.io.Streams; 4463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 4563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey/** 4663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * Tests for {@link NetworkStatsCollection}. 4763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey */ 4863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey@MediumTest 4963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkeypublic class NetworkStatsCollectionTest extends AndroidTestCase { 500cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 5163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private static final String TEST_FILE = "test.bin"; 5263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private static final String TEST_IMSI = "310260000000000"; 5363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 5470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey @Override 5570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey public void setUp() throws Exception { 5670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey super.setUp(); 5770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 5870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey // ignore any device overlay while testing 5970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey NetworkTemplate.forceAllNetworkTypes(); 6070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey } 6170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 6263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey public void testReadLegacyNetwork() throws Exception { 6363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 6463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey stageFile(R.raw.netstats_v1, testFile); 6563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 6663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 6763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.readLegacyNetwork(testFile); 680cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 6963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // verify that history read correctly 7063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 710cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 636016770L, 709306L, 88038768L, 518836L); 7263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 7363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // now export into a unified format 7463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 7563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.write(new DataOutputStream(bos)); 7663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 7763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // clear structure completely 7863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.reset(); 7963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 8063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 0L, 0L, 0L, 0L); 8163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 8263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // and read back into structure, verifying that totals are same 8363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.read(new ByteArrayInputStream(bos.toByteArray())); 8463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 850cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 636016770L, 709306L, 88038768L, 518836L); 8663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 8763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 8863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey public void testReadLegacyUid() throws Exception { 8963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 9063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey stageFile(R.raw.netstats_uid_v4, testFile); 9163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 9263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 9363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.readLegacyUid(testFile, false); 9463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 9563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // verify that history read correctly 9663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 970cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 637076152L, 711413L, 88343717L, 521022L); 9863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 9963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // now export into a unified format 10063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 10163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.write(new DataOutputStream(bos)); 10263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 10363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // clear structure completely 10463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.reset(); 10563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 10663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 0L, 0L, 0L, 0L); 10763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 10863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // and read back into structure, verifying that totals are same 10963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.read(new ByteArrayInputStream(bos.toByteArray())); 11063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), 1110cf6de0c930c9082b7bffdb9824e4a7c7832b6dfJeff Sharkey 637076152L, 711413L, 88343717L, 521022L); 11263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 11363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 11463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey public void testReadLegacyUidTags() throws Exception { 11563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final File testFile = new File(getContext().getFilesDir(), TEST_FILE); 11663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey stageFile(R.raw.netstats_uid_v4, testFile); 11763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 11863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final NetworkStatsCollection collection = new NetworkStatsCollection(30 * MINUTE_IN_MILLIS); 11963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.readLegacyUid(testFile, true); 12063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 12163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // verify that history read correctly 12263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 12363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 77017831L, 100995L, 35436758L, 92344L); 12463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 12563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // now export into a unified format 12663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 12763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.write(new DataOutputStream(bos)); 12863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 12963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // clear structure completely 13063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.reset(); 13163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 13263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 0L, 0L, 0L, 0L); 13363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 13463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey // and read back into structure, verifying that totals are same 13563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey collection.read(new ByteArrayInputStream(bos.toByteArray())); 13663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertSummaryTotalIncludingTags(collection, buildTemplateMobileAll(TEST_IMSI), 13763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 77017831L, 100995L, 35436758L, 92344L); 13863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 13963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 14070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey public void testStartEndAtomicBuckets() throws Exception { 14170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey final NetworkStatsCollection collection = new NetworkStatsCollection(HOUR_IN_MILLIS); 14270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 14370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey // record empty data straddling between buckets 14470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey final NetworkStats.Entry entry = new NetworkStats.Entry(); 14570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey entry.rxBytes = 32; 14670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey collection.recordData(null, UID_ALL, SET_DEFAULT, TAG_NONE, 30 * MINUTE_IN_MILLIS, 14770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 90 * MINUTE_IN_MILLIS, entry); 14870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 14970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey // assert that we report boundary in atomic buckets 15070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey assertEquals(0, collection.getStartMillis()); 15170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey assertEquals(2 * HOUR_IN_MILLIS, collection.getEndMillis()); 15270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey } 15370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 15463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey /** 15563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * Copy a {@link Resources#openRawResource(int)} into {@link File} for 15663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey * testing purposes. 15763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey */ 15863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private void stageFile(int rawId, File file) throws Exception { 15963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey new File(file.getParent()).mkdirs(); 16063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey InputStream in = null; 16163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey OutputStream out = null; 16263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey try { 16363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey in = getContext().getResources().openRawResource(rawId); 16463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey out = new FileOutputStream(file); 16563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey Streams.copy(in, out); 16663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } finally { 16763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey IoUtils.closeQuietly(in); 16863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey IoUtils.closeQuietly(out); 16963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 17063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 17163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 17263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private static void assertSummaryTotal(NetworkStatsCollection collection, 17363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey NetworkTemplate template, long rxBytes, long rxPackets, long txBytes, long txPackets) { 17463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final NetworkStats.Entry entry = collection.getSummary( 17563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey template, Long.MIN_VALUE, Long.MAX_VALUE).getTotal(null); 17663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEntry(entry, rxBytes, rxPackets, txBytes, txPackets); 17763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 17863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 17963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private static void assertSummaryTotalIncludingTags(NetworkStatsCollection collection, 18063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey NetworkTemplate template, long rxBytes, long rxPackets, long txBytes, long txPackets) { 18163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey final NetworkStats.Entry entry = collection.getSummary( 18263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey template, Long.MIN_VALUE, Long.MAX_VALUE).getTotalIncludingTags(null); 18363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEntry(entry, rxBytes, rxPackets, txBytes, txPackets); 18463abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 18563abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey 18663abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey private static void assertEntry( 18763abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey NetworkStats.Entry entry, long rxBytes, long rxPackets, long txBytes, long txPackets) { 18863abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes); 18963abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets); 19063abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEquals("unexpected txBytes", txBytes, entry.txBytes); 19163abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey assertEquals("unexpected txPackets", txPackets, entry.txPackets); 19263abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey } 19363abc37356728c0575d6a62a203102ae6d97953bJeff Sharkey} 194