diff --git a/services/tests/servicestests/res/raw/xt_qtaguid_typical b/services/tests/servicestests/res/raw/xt_qtaguid_typical new file mode 100644 index 0000000000..7c4f04e1fb --- /dev/null +++ b/services/tests/servicestests/res/raw/xt_qtaguid_typical @@ -0,0 +1,32 @@ +idx iface acct_tag_hex uid_tag_int rx_bytes tx_bytes +1 wlan0 0x0 0 14615 4270 +2 wlan0 0x0 1000 5175 915 +3 wlan0 0x0 1021 3381 903 +4 wlan0 0x0 10004 333821 53558 +5 wlan0 0x0 10010 4888 37363 +6 wlan0 0x0 10013 52 104 +7 wlan0 0x74182ada00000000 10004 18725 1066 +8 rmnet0 0x0 0 301274 30244 +9 rmnet0 0x0 1000 304 441 +10 rmnet0 0x0 1013 2880 2272 +11 rmnet0 0x0 1021 31407 8430 +12 rmnet0 0x0 10003 32665 3814 +13 rmnet0 0x0 10004 2373141 420112 +14 rmnet0 0x0 10010 870370 1111727 +15 rmnet0 0x0 10013 240 240 +16 rmnet0 0x0 10016 16703 13512 +17 rmnet0 0x0 10017 3990 3269 +18 rmnet0 0x0 10018 474504 14516062 +19 rmnet0 0x0 10019 782804 71077 +20 rmnet0 0x0 10022 70671 49684 +21 rmnet0 0x0 10029 5785354 397159 +22 rmnet0 0x0 10033 2102 1686 +23 rmnet0 0x0 10034 15495464 227694 +24 rmnet0 0x0 10037 31184994 684122 +25 rmnet0 0x0 10051 298687 113485 +26 rmnet0 0x0 10056 29504 20669 +27 rmnet0 0x0 10069 683 596 +28 rmnet0 0x0 10072 34051 12453 +29 rmnet0 0x0 10077 7025393 213866 +30 rmnet0 0x0 10081 354 1178 +31 rmnet0 0x74182ada00000000 10037 28507378 437004 diff --git a/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java new file mode 100644 index 0000000000..8b752ee474 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/NetworkManagementServiceTest.java @@ -0,0 +1,121 @@ +/* + * 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; + +import static dalvik.system.BlockGuard.kernelToTag; +import static dalvik.system.BlockGuard.tagToKernel; + +import android.content.res.Resources; +import android.net.NetworkStats; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.LargeTest; + +import com.android.frameworks.servicestests.R; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; + +import libcore.io.IoUtils; +import libcore.io.Streams; + +/** + * Tests for {@link NetworkManagementService}. + */ +@LargeTest +public class NetworkManagementServiceTest extends AndroidTestCase { + private File mTestProc; + private NetworkManagementService mService; + + @Override + public void setUp() throws Exception { + super.setUp(); + + mTestProc = getContext().getFilesDir(); + mService = NetworkManagementService.createForTest(mContext, mTestProc); + } + + @Override + public void tearDown() throws Exception { + mService = null; + + super.tearDown(); + } + + public void testNetworkStatsDetail() throws Exception { + stageFile(R.raw.xt_qtaguid_typical, new File(mTestProc, "net/xt_qtaguid/stats")); + + final NetworkStats stats = mService.getNetworkStatsDetail(); + assertEquals(31, stats.size); + assertStatsEntry(stats, "wlan0", 0, 0, 14615L, 4270L); + assertStatsEntry(stats, "wlan0", 10004, 0, 333821L, 53558L); + assertStatsEntry(stats, "wlan0", 10004, 1947740890, 18725L, 1066L); + assertStatsEntry(stats, "rmnet0", 10037, 0, 31184994L, 684122L); + assertStatsEntry(stats, "rmnet0", 10037, 1947740890, 28507378L, 437004L); + } + + public void testNetworkStatsDetailExtended() throws Exception { + stageFile(R.raw.xt_qtaguid_extended, new File(mTestProc, "net/xt_qtaguid/stats")); + + final NetworkStats stats = mService.getNetworkStatsDetail(); + assertEquals(2, stats.size); + assertStatsEntry(stats, "test0", 1000, 0, 1024L, 2048L); + assertStatsEntry(stats, "test0", 1000, 0xF00D, 512L, 512L); + } + + public void testKernelTags() throws Exception { + assertEquals("0", tagToKernel(0x0)); + assertEquals("214748364800", tagToKernel(0x32)); + assertEquals("9223372032559808512", tagToKernel(Integer.MAX_VALUE)); + assertEquals("0", tagToKernel(Integer.MIN_VALUE)); + assertEquals("9223369837831520256", tagToKernel(Integer.MIN_VALUE - 512)); + + assertEquals(0, kernelToTag("0x0000000000000000")); + assertEquals(0x32, kernelToTag("0x0000003200000000")); + assertEquals(2147483647, kernelToTag("0x7fffffff00000000")); + assertEquals(0, kernelToTag("0x0000000000000000")); + assertEquals(2147483136, kernelToTag("0x7FFFFE0000000000")); + + } + + /** + * Copy a {@link Resources#openRawResource(int)} into {@link File} for + * testing purposes. + */ + private void stageFile(int rawId, File file) throws Exception { + new File(file.getParent()).mkdirs(); + InputStream in = null; + OutputStream out = null; + try { + in = getContext().getResources().openRawResource(rawId); + out = new FileOutputStream(file); + Streams.copy(in, out); + } finally { + IoUtils.closeQuietly(in); + IoUtils.closeQuietly(out); + } + } + + private static void assertStatsEntry( + NetworkStats stats, String iface, int uid, int tag, long rx, long tx) { + final int i = stats.findIndex(iface, uid, tag); + assertEquals(rx, stats.rx[i]); + assertEquals(tx, stats.tx[i]); + } + +} diff --git a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java index 636d0593e9..903f2b0b6f 100644 --- a/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/NetworkStatsServiceTest.java @@ -59,7 +59,6 @@ import android.os.INetworkManagementService; import android.telephony.TelephonyManager; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.LargeTest; -import android.util.Log; import android.util.TrustedTime; import com.android.server.net.NetworkStatsService; @@ -611,6 +610,9 @@ public class NetworkStatsServiceTest extends AndroidTestCase { mAlarmManager.setInexactRepeating( eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class)); expectLastCall().atLeastOnce(); + + mNetManager.setBandwidthControlEnabled(true); + expectLastCall().atLeastOnce(); } private void expectNetworkState(NetworkState... state) throws Exception { @@ -631,6 +633,7 @@ public class NetworkStatsServiceTest extends AndroidTestCase { private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory) throws Exception { + expect(mSettings.getEnabled()).andReturn(true).anyTimes(); expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes(); expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes(); expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();