Cronet: add test server

This will be an extension of webview's ctstestserver
and only supports http1

Test: atest CronetApiTest

Change-Id: I754bce6f84ff59bc7d61326853c9bd6b2bd0eec9
diff --git a/Cronet/tests/cts/Android.bp b/Cronet/tests/cts/Android.bp
index e71c707..f6d1e03 100644
--- a/Cronet/tests/cts/Android.bp
+++ b/Cronet/tests/cts/Android.bp
@@ -20,10 +20,14 @@
 
 java_library {
     name: "CronetApiCommonTests",
-    srcs: ["src/**/*.java"],
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt"
+    ],
     static_libs: [
         "androidx.test.rules",
         "androidx.core_core",
+        "ctstestserver",
         "junit",
     ],
     libs: [
@@ -32,6 +36,7 @@
         "android.test.mock",
         "androidx.annotation_annotation",
         "framework-cronet",
+        "org.apache.http.legacy",
     ],
 }
 
diff --git a/Cronet/tests/cts/AndroidManifest.xml b/Cronet/tests/cts/AndroidManifest.xml
index db0f0b3..5bbd597 100644
--- a/Cronet/tests/cts/AndroidManifest.xml
+++ b/Cronet/tests/cts/AndroidManifest.xml
@@ -18,18 +18,18 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-        package="org.chromium.net.test">
+          package="org.chromium.net.test">
 
     <uses-permission android:name="android.permission.INTERNET"/>
-    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 
-    <application>
-        <uses-library android:name="android.test.runner" />
-        <uses-library android:name="framework-cronet" />
+    <application android:networkSecurityConfig="@xml/network_security_config">
+        <uses-library android:name="android.test.runner"/>
+        <uses-library android:name="framework-cronet"/>
     </application>
 
     <instrumentation
         android:name="androidx.test.runner.AndroidJUnitRunner"
         android:targetPackage="org.chromium.net.test"
-        android:label="Cronet API Networking Tests" />
-</manifest>
\ No newline at end of file
+        android:label="Cronet API Networking Tests"/>
+</manifest>
diff --git a/Cronet/tests/cts/assets/html/hello_world.html b/Cronet/tests/cts/assets/html/hello_world.html
new file mode 100644
index 0000000..ea62ce2
--- /dev/null
+++ b/Cronet/tests/cts/assets/html/hello_world.html
@@ -0,0 +1,24 @@
+<!--
+  ~ Copyright (C) 2022 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.
+  -->
+
+<html>
+<head>
+    <title>hello world</title>
+</head>
+<body>
+<h3>hello world</h3><br>
+</body>
+</html>
\ No newline at end of file
diff --git a/Cronet/tests/cts/res/xml/network_security_config.xml b/Cronet/tests/cts/res/xml/network_security_config.xml
new file mode 100644
index 0000000..7d7530b
--- /dev/null
+++ b/Cronet/tests/cts/res/xml/network_security_config.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  ~ Copyright (C) 2022 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.
+  -->
+
+<network-security-config>
+    <domain-config cleartextTrafficPermitted="true">
+        <domain includeSubdomains="true">localhost</domain>
+    </domain-config>
+</network-security-config>
\ No newline at end of file
diff --git a/Cronet/tests/cts/src/org/chromium/net/test/CronetUrlRequestTest.java b/Cronet/tests/cts/src/org/chromium/net/test/CronetUrlRequestTest.java
index 7dd9a9a..59f95cd 100644
--- a/Cronet/tests/cts/src/org/chromium/net/test/CronetUrlRequestTest.java
+++ b/Cronet/tests/cts/src/org/chromium/net/test/CronetUrlRequestTest.java
@@ -29,6 +29,7 @@
 import org.chromium.net.CronetEngine;
 import org.chromium.net.UrlRequest;
 import org.chromium.net.UrlResponseInfo;
+import org.chromium.net.test.util.CronetCtsTestServer;
 import org.chromium.net.test.util.TestUrlRequestCallback;
 import org.chromium.net.test.util.TestUrlRequestCallback.ResponseStep;
 import org.junit.Before;
@@ -45,6 +46,7 @@
     private final String[] mTestDomains = {"www.google.com", "www.android.com"};
     @NonNull private CronetEngine mCronetEngine;
     @NonNull private ConnectivityManager mCm;
+    @NonNull private CronetCtsTestServer mTestServer;
 
     @Before
     public void setUp() throws Exception {
@@ -56,6 +58,7 @@
                 // .enableBrotli(true)
                 .enableQuic(true);
         mCronetEngine = builder.build();
+        mTestServer = new CronetCtsTestServer(context);
     }
 
     private static void assertGreaterThan(String msg, int first, int second) {
@@ -74,7 +77,7 @@
     @Test
     public void testUrlRequestGet_CompletesSuccessfully() throws Exception {
         assertHasTestableNetworks();
-        String url = HTTPS_PREFIX + getRandomDomain();
+        String url = mTestServer.getSuccessUrl();
         TestUrlRequestCallback callback = new TestUrlRequestCallback();
         UrlRequest.Builder builder = mCronetEngine.newUrlRequestBuilder(url, callback,
                 callback.getExecutor());
diff --git a/Cronet/tests/cts/src/org/chromium/net/test/util/CronetCtsTestServer.kt b/Cronet/tests/cts/src/org/chromium/net/test/util/CronetCtsTestServer.kt
new file mode 100644
index 0000000..cc9ad5c
--- /dev/null
+++ b/Cronet/tests/cts/src/org/chromium/net/test/util/CronetCtsTestServer.kt
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 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 org.chromium.net.test.util
+
+import android.content.Context
+import android.webkit.cts.CtsTestServer
+
+/** Extends CtsTestServer to handle POST requests and other cronet specific test requests */
+class CronetCtsTestServer(context: Context) : CtsTestServer(context) {
+
+    val successUrl: String = getAssetUrl("html/hello_world.html")
+}