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 com.android.server.vcn;
18 
19 import static org.mockito.Mockito.doReturn;
20 
21 import android.content.Context;
22 import android.net.IpSecManager;
23 
24 import com.android.server.IpSecService;
25 
26 public class VcnTestUtils {
27     /** Mock system services by directly mocking the *Manager interface. */
setupSystemService( Context mockContext, Object service, String name, Class<?> serviceClass)28     public static void setupSystemService(
29             Context mockContext, Object service, String name, Class<?> serviceClass) {
30         doReturn(name).when(mockContext).getSystemServiceName(serviceClass);
31         doReturn(service).when(mockContext).getSystemService(name);
32     }
33 
34     /** Mock IpSecService by mocking the underlying service binder. */
setupIpSecManager(Context mockContext, IpSecService service)35     public static IpSecManager setupIpSecManager(Context mockContext, IpSecService service) {
36         doReturn(Context.IPSEC_SERVICE).when(mockContext).getSystemServiceName(IpSecManager.class);
37 
38         final IpSecManager ipSecMgr = new IpSecManager(mockContext, service);
39         doReturn(ipSecMgr).when(mockContext).getSystemService(Context.IPSEC_SERVICE);
40 
41         // Return to ensure this doesn't get reaped.
42         return ipSecMgr;
43     }
44 }
45