Merge remote branch 'korg/froyo' into manualmerge

Conflicts:
	tests/tests/permission/src/android/permission/cts/NoActivityRelatedPermissionTest.java
	tests/tests/telephony/src/android/telephony/cts/SmsManagerTest.java

Change-Id: Ie74c678a6ae142d9861b6965a8ca89ff58ebfa83
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index 354428b..17a3ac1 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -97,67 +97,6 @@
         assertFalse(ConnectivityManager.isNetworkTypeValid(-1));
     }
 
-    @TestTargets({
-        @TestTargetNew(
-            level = TestLevel.COMPLETE,
-            method = "getNetworkPreference",
-            args = {}
-        ),
-        @TestTargetNew(
-            level = TestLevel.SUFFICIENT,
-            method = "setNetworkPreference",
-            args = {int.class}
-        )
-    })
-    public void testAccessNetworkPreference() {
-        int initialSetting = mCm.getNetworkPreference();
-
-        // Changing the network preference requires android.permission.WRITE_SECURE_SETTINGS,
-        // which is only available to signed or system applications.
-
-        // Setting the same preference that is already set is a no-op and does not throw
-        // a SecurityException.
-        mCm.setNetworkPreference(initialSetting);
-        assertEquals(initialSetting, mCm.getNetworkPreference());
-
-        // find a valid setting that is different from the initial setting
-        int validSetting = -1;
-        NetworkInfo[] ni = mCm.getAllNetworkInfo();
-        for (NetworkInfo n : ni) {
-            int type = n.getType();
-            if (type != initialSetting) {
-                validSetting = type;
-                break;
-            }
-        }
-        if (validSetting >= 0) {
-            try {
-                mCm.setNetworkPreference(validSetting);
-                fail("Trying to change the network preference should throw SecurityException");
-            } catch (SecurityException expected) {
-                // expected
-            }
-        }
-
-        // find an invalid setting
-        int invalidSetting = -1;
-        for (int i = 0; i < 10; i++) {
-            if (!ConnectivityManager.isNetworkTypeValid(i)) {
-                invalidSetting = i;
-                break;
-            }
-        }
-        if (invalidSetting >= 0) {
-            // illegal setting should be ignored
-            mCm.setNetworkPreference(invalidSetting);
-            assertEquals(initialSetting, mCm.getNetworkPreference());
-        }
-
-        // illegal setting should be ignored
-        mCm.setNetworkPreference(-1);
-        assertEquals(initialSetting, mCm.getNetworkPreference());
-    }
-
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         notes = "Test getAllNetworkInfo().",
diff --git a/tests/cts/net/src/android/net/cts/ListeningPortsTest.java b/tests/cts/net/src/android/net/cts/ListeningPortsTest.java
index ff6b4e9..b6e6efb 100644
--- a/tests/cts/net/src/android/net/cts/ListeningPortsTest.java
+++ b/tests/cts/net/src/android/net/cts/ListeningPortsTest.java
@@ -23,6 +23,7 @@
 import java.util.Scanner;
 import java.util.regex.Pattern;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 public class ListeningPortsTest extends TestCase {
@@ -50,12 +51,40 @@
         EXCEPTION_PATTERNS.add("[0]{16}[F]{4}[0]{4}[0-9A-F]{6}7F:[0-9A-F]{4}"); // IPv4-6 Conversion
     }
 
-    public static void testNoListeningPorts() {
-        final boolean isTcp = true;
-        assertNoListeningPorts("/proc/net/tcp", isTcp);
-        assertNoListeningPorts("/proc/net/tcp6", isTcp);
-        assertNoListeningPorts("/proc/net/udp", !isTcp);
-        assertNoListeningPorts("/proc/net/udp6", !isTcp);
+    public void testNoListeningTcpPorts() {
+        assertNoListeningPorts("/proc/net/tcp", true);
+    }
+
+    public void testNoListeningTcp6Ports() {
+        assertNoListeningPorts("/proc/net/tcp6", true);
+    }
+
+    public void testNoListeningUdpPorts() throws Exception {
+        assertNoListeningUdpPorts("/proc/net/udp");
+    }
+
+    public void testNoListeningUdp6Ports() throws Exception {
+        assertNoListeningUdpPorts("/proc/net/udp6");
+    }
+
+    private static final int RETRIES_MAX = 6;
+
+    /**
+     * UDP tests can be flaky due to DNS lookups.  Compensate.
+     */
+    private static void assertNoListeningUdpPorts(String procFilePath) throws Exception {
+        for (int i = 0; i < RETRIES_MAX; i++) {
+            try {
+                assertNoListeningPorts(procFilePath, false);
+                return;
+            } catch (ListeningPortsAssertionError e) {
+                if (i == RETRIES_MAX - 1) {
+                    throw e;
+                }
+                Thread.sleep(2 * 1000 * i);
+            }
+        }
+        throw new IllegalStateException("unreachable");
     }
 
     private static void assertNoListeningPorts(String procFilePath, boolean isTcp) {
@@ -94,7 +123,8 @@
                         isAddress(localAddress));
 
                 if (!isException(localAddress) && isPortListening(state, isTcp)) {
-                    fail("Found port listening on " + localAddress + " in " + procFilePath);
+                    throw new ListeningPortsAssertionError(
+                            "Found port listening on " + localAddress + " in " + procFilePath);
                 }
             }
         } catch (FileNotFoundException notFound) {
@@ -128,4 +158,10 @@
         String listeningState = isTcp ? "0A" : "07";
         return listeningState.equals(state);
     }
+
+    private static class ListeningPortsAssertionError extends AssertionFailedError {
+        private ListeningPortsAssertionError(String msg) {
+            super(msg);
+        }
+    }
 }
diff --git a/tests/cts/net/src/android/net/cts/SSLCertificateSocketFactoryTest.java b/tests/cts/net/src/android/net/cts/SSLCertificateSocketFactoryTest.java
index 6cd5d6f..258ac4d 100644
--- a/tests/cts/net/src/android/net/cts/SSLCertificateSocketFactoryTest.java
+++ b/tests/cts/net/src/android/net/cts/SSLCertificateSocketFactoryTest.java
@@ -21,6 +21,7 @@
 import java.net.Socket;
 
 import javax.net.SocketFactory;
+import javax.net.ssl.SSLPeerUnverifiedException;
 
 import android.net.SSLCertificateSocketFactory;
 import android.test.AndroidTestCase;
@@ -141,4 +142,73 @@
         // The socket level is invalid.
     }
 
+    // a host and port that are expected to be available but have
+    // a cert with a different CN, in this case CN=mtalk.google.com
+    private static String TEST_CREATE_SOCKET_HOST = "mobile-gtalk.l.google.com";
+    private static int TEST_CREATE_SOCKET_PORT = 5228;
+
+    /**
+     * b/2807618 Make sure that hostname verifcation in cases were it
+     * is documented to be included by various
+     * SSLCertificateSocketFactory.createSocket messages.
+     *
+     * NOTE: Test will fail if external server is not available.
+     */
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "createSocket",
+        args = {String.class, int.class}
+    )
+    public void test_createSocket_simple() throws Exception {
+        try {
+            mFactory.createSocket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT);
+            fail();
+        } catch (SSLPeerUnverifiedException expected) {
+            // expected
+        }
+    }
+
+    /**
+     * b/2807618 Make sure that hostname verifcation in cases were it
+     * is documented to be included by various
+     * SSLCertificateSocketFactory.createSocket messages.
+     *
+     * NOTE: Test will fail if external server is not available.
+     */
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "createSocket",
+        args = {Socket.class, String.class, int.class, boolean.class}
+    )
+    public void test_createSocket_wrapping() throws Exception {
+        try {
+            Socket underlying = new Socket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT);
+            mFactory.createSocket(
+                    underlying, TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT, true);
+            fail();
+        } catch (SSLPeerUnverifiedException expected) {
+            // expected
+        }
+    }
+
+    /**
+     * b/2807618 Make sure that hostname verifcation in cases were it
+     * is documented to be included by various
+     * SSLCertificateSocketFactory.createSocket messages.
+     *
+     * NOTE: Test will fail if external server is not available.
+     */
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "createSocket",
+        args = {String.class, int.class, InetAddress.class, int.class}
+    )
+    public void test_createSocket_bind() throws Exception {
+        try {
+            mFactory.createSocket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT, null, 0);
+            fail();
+        } catch (SSLPeerUnverifiedException expected) {
+            // expected
+        }
+    }
 }
diff --git a/tests/cts/net/src/android/net/cts/TrafficStatsTest.java b/tests/cts/net/src/android/net/cts/TrafficStatsTest.java
index 9d23a87..183f891 100644
--- a/tests/cts/net/src/android/net/cts/TrafficStatsTest.java
+++ b/tests/cts/net/src/android/net/cts/TrafficStatsTest.java
@@ -16,24 +16,20 @@
 
 package android.net.cts;
 
-import android.os.Process;
-import android.net.TrafficStats;
-import android.test.AndroidTestCase;
-
-import dalvik.annotation.TestTargets;
 import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetNew;
 import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
+import android.net.TrafficStats;
+import android.os.Process;
+import android.test.AndroidTestCase;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.UnknownHostException;
-import java.util.Random;
 
 @TestTargetClass(TrafficStats.class)
 public class TrafficStatsTest extends AndroidTestCase {
@@ -58,63 +54,6 @@
     }
 
     @TestTargets({
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getTotalTxPackets"),
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getTotalRxPackets"),
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getTotalTxBytes"),
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getTotalRxBytes"),
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getUidTxBytes"),
-        @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getUidRxBytes")
-    })
-    public void testTrafficStatsWithHostLookup() {
-        long txPacketsBefore = TrafficStats.getTotalTxPackets();
-        long rxPacketsBefore = TrafficStats.getTotalRxPackets();
-        long txBytesBefore = TrafficStats.getTotalTxBytes();
-        long rxBytesBefore = TrafficStats.getTotalRxBytes();
-        long uidTxBytesBefore = TrafficStats.getUidTxBytes(Process.myUid());
-        long uidRxBytesBefore = TrafficStats.getUidRxBytes(Process.myUid());
-
-        // Look up random hostnames in a wildcard domain owned by Google.
-        // This will require a DNS request, which should generate traffic.
-
-        int found = 0;
-        Random r = new Random();
-        for (int i = 0; i < 10; i++) {
-            try {
-                String host = "test" + r.nextInt(100000) + ".clients.google.com";
-                InetAddress[] addr = InetAddress.getAllByName(host);
-                if (addr.length > 0) found++;
-            } catch (UnknownHostException e) {
-                // Ignore -- our purpose is not to test network connectivity,
-                // and we'd rather have false positives than a flaky test.
-            }
-        }
-
-        long txPacketsAfter = TrafficStats.getTotalTxPackets();
-        long rxPacketsAfter = TrafficStats.getTotalRxPackets();
-        long txBytesAfter = TrafficStats.getTotalTxBytes();
-        long rxBytesAfter = TrafficStats.getTotalRxBytes();
-        long uidTxBytesAfter = TrafficStats.getUidTxBytes(Process.myUid());
-        long uidRxBytesAfter = TrafficStats.getUidRxBytes(Process.myUid());
-
-        // Make some conservative assertions about the data used:
-        // each successful resolution should exchange at least one packet,
-        // and at least 20 bytes in each direction.
-
-        assertTrue("txp: " + txPacketsBefore + " [" + found + "] " + txPacketsAfter,
-                   txPacketsAfter >= txPacketsBefore + found);
-        assertTrue("rxp: " + rxPacketsBefore + " [" + found + "] " + rxPacketsAfter,
-                   rxPacketsAfter >= rxPacketsBefore + found);
-        assertTrue("txb: " + txBytesBefore + " [" + found + "] " + txBytesAfter,
-                   txBytesAfter >= txBytesBefore + found * 20);
-        assertTrue("rxb: " + rxBytesBefore + " [" + found + "] " + rxBytesAfter,
-                   rxBytesAfter >= rxBytesBefore + found * 20);
-        assertTrue("uidtxb: " + uidTxBytesBefore + " [" + found + "] " + uidTxBytesAfter,
-                   uidTxBytesAfter >= uidTxBytesBefore + found * 20);
-        assertTrue("uidrxb: " + uidRxBytesBefore + " [" + found + "] " + uidRxBytesAfter,
-                   uidRxBytesAfter >= uidRxBytesBefore + found * 20);
-    }
-
-    @TestTargets({
         @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getMobileTxPackets"),
         @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getMobileRxPackets"),
         @TestTargetNew(level = TestLevel.PARTIAL_COMPLETE, method = "getMobileTxBytes"),
diff --git a/tests/cts/net/src/android/net/wifi/cts/WifiInfoTest.java b/tests/cts/net/src/android/net/wifi/cts/WifiInfoTest.java
index d6d7d8e..3b1a6c1 100644
--- a/tests/cts/net/src/android/net/wifi/cts/WifiInfoTest.java
+++ b/tests/cts/net/src/android/net/wifi/cts/WifiInfoTest.java
@@ -184,6 +184,8 @@
         wifiInfo.getMacAddress();
         setWifiEnabled(false);
         Thread.sleep(DURATION);
+        wifiInfo = mWifiManager.getConnectionInfo();
+        assertEquals(-1, wifiInfo.getNetworkId());
         assertEquals(WifiManager.WIFI_STATE_DISABLED, mWifiManager.getWifiState());
     }