diff options
author | Tsu Chiang Chuang <tsu@google.com> | 2011-08-26 15:19:45 -0700 |
---|---|---|
committer | Tsu Chiang Chuang <tsu@google.com> | 2011-08-29 15:30:22 -0700 |
commit | 89355b4cfd87e4ed6facf588bfca20fe2503dd71 (patch) | |
tree | 6df49c80d0ab514d8b0b969257774addb1e45ec2 /tests/BandwidthTests | |
parent | 6f7588850b2b1dd4a96584bad889bd4b51112c98 (diff) | |
download | frameworks_base-89355b4cfd87e4ed6facf588bfca20fe2503dd71.zip frameworks_base-89355b4cfd87e4ed6facf588bfca20fe2503dd71.tar.gz frameworks_base-89355b4cfd87e4ed6facf588bfca20fe2503dd71.tar.bz2 |
Adding bandwidth enforcement test app.
Change-Id: I6e3509aff056ad0008960b205b7489ee5f066521
Diffstat (limited to 'tests/BandwidthTests')
6 files changed, 340 insertions, 0 deletions
diff --git a/tests/BandwidthTests/Android.mk b/tests/BandwidthTests/Android.mk new file mode 100644 index 0000000..2cc2009 --- /dev/null +++ b/tests/BandwidthTests/Android.mk @@ -0,0 +1,22 @@ +# +# 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. +# +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_PACKAGE_NAME := BandwidthEnforcementTest +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +include $(BUILD_PACKAGE)
\ No newline at end of file diff --git a/tests/BandwidthTests/AndroidManifest.xml b/tests/BandwidthTests/AndroidManifest.xml new file mode 100644 index 0000000..19f38ca --- /dev/null +++ b/tests/BandwidthTests/AndroidManifest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.tests.bandwidthenforcement"> + <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <application> + <activity android:name=".BandwidthEnforcementTestActivity"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + <!-- adb shell am startservice -n com.android.tests.bandwidthenforcement/.BandwidthEnforcementTestService --> + <service android:name=".BandwidthEnforcementTestService" android:exported="true" /> + </application> +</manifest> diff --git a/tests/BandwidthTests/res/layout/main.xml b/tests/BandwidthTests/res/layout/main.xml new file mode 100644 index 0000000..3392b21 --- /dev/null +++ b/tests/BandwidthTests/res/layout/main.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal"> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/app_name" /> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/app_desc" /> +</LinearLayout> diff --git a/tests/BandwidthTests/res/values/strings.xml b/tests/BandwidthTests/res/values/strings.xml new file mode 100644 index 0000000..a4a78c2 --- /dev/null +++ b/tests/BandwidthTests/res/values/strings.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + <string name="app_name">BandwidthEnforcementTest</string> + <string name="app_desc">Tries several tricks to get Internet access.</string> + <string name="start">Start</string> + <string name="stop">Stop</string> +</resources> diff --git a/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestActivity.java b/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestActivity.java new file mode 100644 index 0000000..f0e43ac --- /dev/null +++ b/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestActivity.java @@ -0,0 +1,27 @@ +/* + * 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.tests.bandwidthenforcement; + +import android.app.Activity; +import android.os.Bundle; + +public class BandwidthEnforcementTestActivity extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + } +} diff --git a/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestService.java b/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestService.java new file mode 100644 index 0000000..a2427f5 --- /dev/null +++ b/tests/BandwidthTests/src/com/android/tests/bandwidthenforcement/BandwidthEnforcementTestService.java @@ -0,0 +1,210 @@ +/* + * 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.tests.bandwidthenforcement; + +import android.app.IntentService; +import android.content.Intent; +import android.net.SntpClient; +import android.os.Environment; +import android.util.Log; + +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.URL; +import java.util.Random; + +import libcore.io.Streams; + +/* + * Test Service that tries to connect to the web via different methods and outputs the results to + * the log and a output file. + */ +public class BandwidthEnforcementTestService extends IntentService { + private static final String TAG = "BandwidthEnforcementTestService"; + private static final String OUTPUT_FILE = "BandwidthEnforcementTestServiceOutputFile"; + + public BandwidthEnforcementTestService() { + super(TAG); + } + + @Override + protected void onHandleIntent(Intent intent) { + Log.d(TAG, "Trying to establish a connection."); + // Read output file path from intent. + String outputFile = intent.getStringExtra(OUTPUT_FILE); + dumpResult("testUrlConnection", testUrlConnection(), outputFile); + dumpResult("testUrlConnectionv6", testUrlConnectionv6(), outputFile); + dumpResult("testSntp", testSntp(), outputFile); + dumpResult("testDns", testDns(), outputFile); + } + + public static void dumpResult(String tag, boolean result, String outputFile) { + Log.d(TAG, "Test output file: " + outputFile); + try { + if (outputFile != null){ + File extStorage = Environment.getExternalStorageDirectory(); + File outFile = new File(extStorage, outputFile); + FileWriter writer = new FileWriter(outFile, true); + BufferedWriter out = new BufferedWriter(writer); + if (result) { + out.append(tag + ":fail\n"); + } else { + out.append(tag + ":pass\n"); + } + out.close(); + } + if (result) { + Log.e(TAG, tag + " FAILURE ===================="); + Log.e(TAG, tag + " FAILURE was able to use data"); + Log.e(TAG, tag + " FAILURE ===================="); + } else { + Log.d(TAG, tag + " success; unable to use data"); + } + } catch (IOException e) { + Log.e(TAG, "Could not write file " + e.getMessage()); + } + } + + /** + * Tests a normal http url connection. + * @return true if it was able to connect, false otherwise. + */ + public static boolean testUrlConnection() { + try { + final HttpURLConnection conn = (HttpURLConnection) new URL("http://www.google.com/") + .openConnection(); + try { + conn.connect(); + final String content = Streams.readFully( + new InputStreamReader(conn.getInputStream())); + if (content.contains("Google")) { + return true; + } + } finally { + conn.disconnect(); + } + } catch (IOException e) { + Log.d(TAG, "error: " + e); + } + return false; + } + + /** + * Tests a ipv6 http url connection. + * @return true if it was able to connect, false otherwise. + */ + public static boolean testUrlConnectionv6() { + try { + final HttpURLConnection conn = (HttpURLConnection) new URL("http://ipv6.google.com/") + .openConnection(); + try { + conn.connect(); + final String content = Streams.readFully( + new InputStreamReader(conn.getInputStream())); + if (content.contains("Google")) { + return true; + } + } finally { + conn.disconnect(); + } + } catch (IOException e) { + Log.d(TAG, "error: " + e); + } + return false; + } + + /** + * Tests to connect via sntp. + * @return true if it was able to connect, false otherwise. + */ + public static boolean testSntp() { + final SntpClient client = new SntpClient(); + if (client.requestTime("0.pool.ntp.org", 10000)) { + return true; + } + return false; + } + + /** + * Tests dns query. + * @return true if it was able to connect, false otherwise. + */ + public static boolean testDns() { + try { + final DatagramSocket socket = new DatagramSocket(); + try { + socket.setSoTimeout(10000); + + final byte[] query = buildDnsQuery("www", "android", "com"); + final DatagramPacket queryPacket = new DatagramPacket( + query, query.length, InetAddress.parseNumericAddress("8.8.8.8"), 53); + socket.send(queryPacket); + + final byte[] reply = new byte[query.length]; + final DatagramPacket replyPacket = new DatagramPacket(reply, reply.length); + socket.receive(replyPacket); + return true; + + } finally { + socket.close(); + } + } catch (IOException e) { + Log.d(TAG, "error: " + e); + } + return false; + } + + /** + * Helper method to build a dns query + * @param query the dns strings of the server + * @return the byte array of the dns query to send + * @throws IOException + */ + private static byte[] buildDnsQuery(String... query) throws IOException { + final Random random = new Random(); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + + final byte[] id = new byte[2]; + random.nextBytes(id); + + out.write(id); + out.write(new byte[] { 0x01, 0x00 }); + out.write(new byte[] { 0x00, 0x01 }); + out.write(new byte[] { 0x00, 0x00 }); + out.write(new byte[] { 0x00, 0x00 }); + out.write(new byte[] { 0x00, 0x00 }); + + for (String phrase : query) { + final byte[] bytes = phrase.getBytes("US-ASCII"); + out.write(bytes.length); + out.write(bytes); + } + out.write(0x00); + + out.write(new byte[] { 0x00, 0x01 }); + out.write(new byte[] { 0x00, 0x01 }); + + return out.toByteArray(); + } +} |