1 /* 2 * Copyright (C) 2020 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.networkstack 18 19 import android.content.ComponentName 20 import android.content.Context 21 import android.content.Intent 22 import android.content.ServiceConnection 23 import android.content.pm.PackageManager.MATCH_SYSTEM_ONLY 24 import android.net.INetworkStackConnector 25 import android.os.IBinder 26 import android.util.Log 27 import androidx.test.platform.app.InstrumentationRegistry 28 import kotlin.test.fail 29 30 /** 31 * A [NetworkStackClientBase] that binds to [com.android.server.TestNetworkStackService] 32 */ 33 class TestNetworkStackServiceClient private constructor() : NetworkStackClientBase() { 34 companion object { 35 private val TAG = "TestNetworkStackServiceClient" 36 private val testNetworkStackServiceAction = "android.net.INetworkStackConnector.Test" <lambda>null37 private val context by lazy { InstrumentationRegistry.getInstrumentation().context } 38 private val component = getNetworkStackComponent(testNetworkStackServiceAction) <lambda>null39 private val networkStackVersion by lazy { 40 val info = context.packageManager.getPackageInfo(component.packageName, 0 /* flags */) 41 info.longVersionCode 42 } 43 44 /** 45 * Create a [TestNetworkStackServiceClient] and connect it to the NetworkStack. 46 */ 47 @JvmStatic connectnull48 fun connect(): TestNetworkStackServiceClient { 49 return TestNetworkStackServiceClient().apply { init() } 50 } 51 52 @JvmStatic isSupportednull53 fun isSupported(): Boolean { 54 Log.d(TAG, "Running network stack module version is " + networkStackVersion) 55 // TestNetworkStackService was introduced in NetworkStack version 301100000. 56 // It is also available at HEAD in development branches, where the version code is 57 // 300000000. 58 return networkStackVersion == 300000000L || networkStackVersion >= 301100000L 59 } 60 getNetworkStackComponentnull61 private fun getNetworkStackComponent(connectorAction: String): ComponentName { 62 val connectorIntent = Intent(connectorAction) 63 return connectorIntent.resolveSystemService(context.packageManager, MATCH_SYSTEM_ONLY) 64 ?: fail("TestNetworkStackService not found") 65 } 66 } 67 68 // Inner class defined in subclass cannot access the protected methods of parent class directly, 69 // so have a method outside of the inner class: serviceConnection and call this method instead. onNetworkStackConnectednull70 private fun onNetworkStackConnected(service: IBinder) { 71 onNetworkStackConnected(INetworkStackConnector.Stub.asInterface(service)) 72 } 73 74 private val serviceConnection = object : ServiceConnection { onServiceConnectednull75 override fun onServiceConnected(name: ComponentName, service: IBinder) { 76 onNetworkStackConnected(service) 77 } 78 onServiceDisconnectednull79 override fun onServiceDisconnected(name: ComponentName) = Unit 80 } 81 82 private fun init() { 83 val bindIntent = Intent(testNetworkStackServiceAction) 84 bindIntent.component = getNetworkStackComponent(bindIntent.action) 85 context.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE) 86 } 87 disconnectnull88 fun disconnect() { 89 InstrumentationRegistry.getInstrumentation().context.unbindService(serviceConnection) 90 } 91 getNetworkStackPackageNamenull92 fun getNetworkStackPackageName(): String { 93 return component.packageName 94 } 95 } 96