FileDocCategorySizeDatePackage
BandwidthEnforcementTestService.javaAPI DocAndroid 5.1 API7179Thu Mar 12 22:22:42 GMT 2015com.android.tests.bandwidthenforcement

BandwidthEnforcementTestService

public 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

param
query the dns strings of the server
return
the byte array of the dns query to send
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();
    
public static voiddumpResult(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 voidonHandleIntent(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 booleantestDns()
Tests dns query.

return
true if it was able to connect, false otherwise.

        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 booleantestSntp()
Tests to connect via sntp.

return
true if it was able to connect, false otherwise.

        final SntpClient client = new SntpClient();
        if (client.requestTime("0.pool.ntp.org", 10000)) {
            return true;
        }
        return false;
    
public static booleantestUrlConnection()
Tests a normal http url connection.

return
true if it was able to connect, false otherwise.

        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 booleantestUrlConnectionv6()
Tests a ipv6 http url connection.

return
true if it was able to connect, false otherwise.

        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;