BandwidthEnforcementTestServicepublic class BandwidthEnforcementTestService extends android.app.IntentService
Fields Summary |
---|
private static final String | TAG | private static final String | OUTPUT_FILE |
Constructors Summary |
---|
public BandwidthEnforcementTestService()
super(TAG);
|
Methods Summary |
---|
private static byte[] | buildDnsQuery(java.lang.String query)Helper method to build a dns query
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();
| public static void | dumpResult(java.lang.String tag, boolean result, java.lang.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());
}
| protected void | onHandleIntent(android.content.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 boolean | testDns()Tests dns query.
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;
| public static boolean | testSntp()Tests to connect via sntp.
final SntpClient client = new SntpClient();
if (client.requestTime("0.pool.ntp.org", 10000)) {
return true;
}
return false;
| public static boolean | testUrlConnection()Tests a normal http url connection.
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;
| public static boolean | testUrlConnectionv6()Tests a ipv6 http url connection.
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;
|
|