• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.server.net.integrationtests
18 
19 import android.app.Service
20 import android.content.Intent
21 import java.net.URL
22 import java.util.Collections
23 import java.util.concurrent.ConcurrentHashMap
24 import java.util.concurrent.ConcurrentLinkedQueue
25 import kotlin.collections.ArrayList
26 import kotlin.test.fail
27 
28 /**
29  * An instrumentation interface for the NetworkStack that allows controlling behavior to
30  * facilitate integration tests.
31  */
32 class NetworkStackInstrumentationService : Service() {
33     override fun onBind(intent: Intent) = InstrumentationConnector.asBinder()
34 
35     object InstrumentationConnector : INetworkStackInstrumentation.Stub() {
36         private val httpResponses = ConcurrentHashMap<String, ConcurrentLinkedQueue<HttpResponse>>()
37                 .run {
38                     withDefault { key -> getOrPut(key) { ConcurrentLinkedQueue() } }
39                 }
40         private val httpRequestUrls = Collections.synchronizedList(ArrayList<String>())
41 
42         /**
43          * Called when an HTTP request is being processed by NetworkMonitor. Returns the response
44          * that should be simulated.
45          */
46         fun processRequest(url: URL): HttpResponse {
47             val strUrl = url.toString()
48             httpRequestUrls.add(strUrl)
49             return httpResponses[strUrl]?.poll()
50                     ?: fail("No mocked response for request: $strUrl. " +
51                             "Mocked URL keys are: ${httpResponses.keys}")
52         }
53 
54         /**
55          * Clear all state of this connector. This is intended for use between two tests, so all
56          * state should be reset as if the connector was just created.
57          */
58         override fun clearAllState() {
59             httpResponses.clear()
60             httpRequestUrls.clear()
61         }
62 
63         /**
64          * Add a response to a future HTTP request.
65          *
66          * <p>For any subsequent HTTP/HTTPS query, the first response with a matching URL will be
67          * used to mock the query response.
68          *
69          * <p>All requests that are expected to be sent must have a mock response: if an unexpected
70          * request is seen, the test will fail.
71          */
72         override fun addHttpResponse(response: HttpResponse) {
73             httpResponses.getValue(response.requestUrl).add(response)
74         }
75 
76         /**
77          * Get the ordered list of request URLs that have been sent by NetworkMonitor, and were
78          * answered based on mock responses.
79          */
80         override fun getRequestUrls(): List<String> {
81             return ArrayList(httpRequestUrls)
82         }
83     }
84 }