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 android.net
18 
19 import android.content.ComponentName
20 import android.content.Context
21 import android.content.Intent
22 import android.net.networkstack.NetworkStackClientBase
23 import android.os.IBinder
24 import com.android.server.net.integrationtests.TestNetworkStackService
25 import org.mockito.Mockito.any
26 import org.mockito.Mockito.spy
27 import org.mockito.Mockito.timeout
28 import org.mockito.Mockito.verify
29 import kotlin.test.fail
30 
31 const val TEST_ACTION_SUFFIX = ".Test"
32 
33 class TestNetworkStackClient(private val context: Context) : NetworkStackClientBase() {
34     // TODO: consider switching to TrackRecord for more expressive checks
35     private val lastCallbacks = HashMap<Network, INetworkMonitorCallbacks>()
36     private val moduleConnector = ConnectivityModuleConnector { _, action, _, _ ->
37         val intent = Intent(action)
38         val serviceName = TestNetworkStackService::class.qualifiedName
39                 ?: fail("TestNetworkStackService name not found")
40         intent.component = ComponentName(context.packageName, serviceName)
41         return@ConnectivityModuleConnector intent
42     }.also { it.init(context) }
43 
44     fun start() {
45         moduleConnector.startModuleService(
46                 INetworkStackConnector::class.qualifiedName + TEST_ACTION_SUFFIX,
47                 NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) { connector ->
48             onNetworkStackConnected(INetworkStackConnector.Stub.asInterface(connector))
49         }
50     }
51 
52     // base may be an instance of an inaccessible subclass, so non-spyable.
53     // Use a known open class that delegates to the original instance for all methods except
54     // asBinder. asBinder needs to use its own non-delegated implementation as otherwise it would
55     // return a binder token to a class that is not spied on.
56     open class NetworkMonitorCallbacksWrapper(private val base: INetworkMonitorCallbacks) :
57             INetworkMonitorCallbacks.Stub(), INetworkMonitorCallbacks by base {
58         // asBinder is implemented by both base class and delegate: specify explicitly
59         override fun asBinder(): IBinder {
60             return super.asBinder()
61         }
62     }
63 
64     override fun makeNetworkMonitor(network: Network, name: String?, cb: INetworkMonitorCallbacks) {
65         val cbSpy = spy(NetworkMonitorCallbacksWrapper(cb))
66         lastCallbacks[network] = cbSpy
67         super.makeNetworkMonitor(network, name, cbSpy)
68     }
69 
70     fun verifyNetworkMonitorCreated(network: Network, timeoutMs: Long) {
71         val cb = lastCallbacks[network]
72                 ?: fail("NetworkMonitor for network $network not requested")
73         verify(cb, timeout(timeoutMs)).onNetworkMonitorCreated(any())
74     }
75 }