CtsTestServer: Don't always use one port

Don't always use port 4444 / 4445 for running the CTS
test server. This has the following advantages:

1) Tests can now be run in parallel.
2) Tests have different port numbers, helping eliminate interaction
between different tests.

Bug: 6392702
Change-Id: Ic7b2fa38117e0ffac8006e3516821bacaf01ace5
diff --git a/libs/testserver/src/android/webkit/cts/CtsTestServer.java b/libs/testserver/src/android/webkit/cts/CtsTestServer.java
index 131c03c..16d57ea 100755
--- a/libs/testserver/src/android/webkit/cts/CtsTestServer.java
+++ b/libs/testserver/src/android/webkit/cts/CtsTestServer.java
@@ -88,8 +88,6 @@
  */
 public class CtsTestServer {
     private static final String TAG = "CtsTestServer";
-    private static final int SERVER_PORT = 4444;
-    private static final int SSL_SERVER_PORT = 4445;
 
     public static final String FAVICON_PATH = "/favicon.ico";
     public static final String USERAGENT_PATH = "/useragent.html";
@@ -122,7 +120,6 @@
     public static final String MESSAGE_403 = "403 forbidden";
     public static final String MESSAGE_404 = "404 not found";
 
-    private static CtsTestServer sInstance;
     private static Hashtable<Integer, String> sReasons;
 
     private ServerThread mServerThread;
@@ -165,24 +162,18 @@
      * @throws Exception
      */
     public CtsTestServer(Context context, boolean ssl) throws Exception {
-        if (sInstance != null) {
-            // attempt to start a new instance while one is still running
-            // shut down the old instance first
-            sInstance.shutdown();
-        }
-        sInstance = this;
         mContext = context;
         mAssets = mContext.getAssets();
         mResources = mContext.getResources();
         mSsl = ssl;
-        if (mSsl) {
-            mServerUri = "https://localhost:" + SSL_SERVER_PORT;
-        } else {
-            mServerUri = "http://localhost:" + SERVER_PORT;
-        }
         mRequestEntities = new ArrayList<HttpEntity>();
         mMap = MimeTypeMap.getSingleton();
         mServerThread = new ServerThread(this, mSsl);
+        if (mSsl) {
+            mServerUri = "https://localhost:" + mServerThread.mSocket.getLocalPort();
+        } else {
+            mServerUri = "http://localhost:" + mServerThread.mSocket.getLocalPort();
+        }
         mServerThread.start();
     }
 
@@ -217,8 +208,6 @@
         } catch (KeyManagementException e) {
             throw new IllegalStateException(e);
         }
-
-        sInstance = null;
     }
 
     private URLConnection openConnection(URL url)
@@ -794,10 +783,9 @@
                     if (mIsSsl) {
                         mSslContext = SSLContext.getInstance("TLS");
                         mSslContext.init(getKeyManagers(), null, null);
-                        mSocket = mSslContext.getServerSocketFactory().createServerSocket(
-                                SSL_SERVER_PORT);
+                        mSocket = mSslContext.getServerSocketFactory().createServerSocket(0);
                     } else {
-                        mSocket = new ServerSocket(SERVER_PORT);
+                        mSocket = new ServerSocket(0);
                     }
                     return;
                 } catch (IOException e) {