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 package com.android.networkstack.tethering; 17 18 import static android.Manifest.permission.WRITE_SETTINGS; 19 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 20 21 import static org.mockito.Mockito.mock; 22 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.ITetheringConnector; 26 import android.os.Binder; 27 import android.os.IBinder; 28 29 import androidx.annotation.NonNull; 30 31 public class MockTetheringService extends TetheringService { 32 private final Tethering mTethering = mock(Tethering.class); 33 34 @Override onBind(Intent intent)35 public IBinder onBind(Intent intent) { 36 return new MockTetheringConnector(super.onBind(intent)); 37 } 38 39 @Override makeTethering(TetheringDependencies deps)40 public Tethering makeTethering(TetheringDependencies deps) { 41 return mTethering; 42 } 43 44 @Override checkAndNoteWriteSettingsOperation(@onNull Context context, int uid, @NonNull String callingPackage, boolean throwException)45 boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid, 46 @NonNull String callingPackage, boolean throwException) { 47 // Test this does not verify the calling package / UID, as calling package could be shell 48 // and not match the UID. 49 return context.checkCallingOrSelfPermission(WRITE_SETTINGS) == PERMISSION_GRANTED; 50 } 51 getTethering()52 public Tethering getTethering() { 53 return mTethering; 54 } 55 56 public class MockTetheringConnector extends Binder { 57 final IBinder mBase; MockTetheringConnector(IBinder base)58 MockTetheringConnector(IBinder base) { 59 mBase = base; 60 } 61 getTetheringConnector()62 public ITetheringConnector getTetheringConnector() { 63 return ITetheringConnector.Stub.asInterface(mBase); 64 } 65 getService()66 public MockTetheringService getService() { 67 return MockTetheringService.this; 68 } 69 } 70 } 71