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 import androidx.annotation.Nullable;
31 
32 public class MockTetheringService extends TetheringService {
33     private final Tethering mTethering = mock(Tethering.class);
34 
35     @Override
onBind(Intent intent)36     public IBinder onBind(Intent intent) {
37         return new MockTetheringConnector(super.onBind(intent));
38     }
39 
40     @Override
makeTethering(TetheringDependencies deps)41     public Tethering makeTethering(TetheringDependencies deps) {
42         return mTethering;
43     }
44 
45     @Override
checkAndNoteWriteSettingsOperation(@onNull Context context, int uid, @NonNull String callingPackage, @Nullable String callingAttributionTag, boolean throwException)46     boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid,
47             @NonNull String callingPackage, @Nullable String callingAttributionTag,
48             boolean throwException) {
49         // Test this does not verify the calling package / UID, as calling package could be shell
50         // and not match the UID.
51         return context.checkCallingOrSelfPermission(WRITE_SETTINGS) == PERMISSION_GRANTED;
52     }
53 
getTethering()54     public Tethering getTethering() {
55         return mTethering;
56     }
57 
58     public class MockTetheringConnector extends Binder {
59         final IBinder mBase;
MockTetheringConnector(IBinder base)60         MockTetheringConnector(IBinder base) {
61             mBase = base;
62         }
63 
getTetheringConnector()64         public ITetheringConnector getTetheringConnector() {
65             return ITetheringConnector.Stub.asInterface(mBase);
66         }
67 
getService()68         public MockTetheringService getService() {
69             return MockTetheringService.this;
70         }
71     }
72 }
73