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.internal.net.ipsec.ike; 18 19 import static com.android.internal.net.ipsec.ike.IkeLocalRequestScheduler.LOCAL_REQUEST_WAKE_LOCK_TAG; 20 import static com.android.internal.net.ipsec.ike.IkeSessionStateMachine.BUSY_WAKE_LOCK_TAG; 21 22 import static org.mockito.ArgumentMatchers.argThat; 23 import static org.mockito.Matchers.any; 24 import static org.mockito.Mockito.any; 25 import static org.mockito.Mockito.anyInt; 26 import static org.mockito.Mockito.doNothing; 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.eq; 29 import static org.mockito.Mockito.mock; 30 import static org.mockito.Mockito.spy; 31 32 import android.content.Context; 33 import android.content.IntentFilter; 34 import android.net.ConnectivityManager; 35 import android.net.InetAddresses; 36 import android.net.IpSecManager; 37 import android.net.IpSecManager.UdpEncapsulationSocket; 38 import android.net.Network; 39 import android.net.NetworkCapabilities; 40 import android.net.SocketKeepalive; 41 import android.os.Handler; 42 import android.os.PowerManager; 43 44 import com.android.internal.net.ipsec.ike.testutils.MockIpSecTestUtils; 45 import com.android.internal.net.ipsec.ike.utils.IkeAlarmReceiver; 46 import com.android.internal.net.ipsec.ike.utils.RandomnessFactory; 47 48 import org.junit.Before; 49 50 import java.net.Inet4Address; 51 import java.util.concurrent.Executor; 52 53 public abstract class IkeSessionTestBase { 54 protected static final Inet4Address LOCAL_ADDRESS = 55 (Inet4Address) (InetAddresses.parseNumericAddress("192.0.2.200")); 56 protected static final Inet4Address REMOTE_ADDRESS = 57 (Inet4Address) (InetAddresses.parseNumericAddress("127.0.0.1")); 58 protected static final String REMOTE_HOSTNAME = "ike.test.android.com"; 59 60 protected PowerManager.WakeLock mMockBusyWakelock; 61 protected PowerManager.WakeLock mMockLocalRequestWakelock; 62 63 protected MockIpSecTestUtils mMockIpSecTestUtils; 64 protected Context mSpyContext; 65 protected IpSecManager mIpSecManager; 66 protected PowerManager mPowerManager; 67 68 protected ConnectivityManager mMockConnectManager; 69 protected Network mMockDefaultNetwork; 70 protected SocketKeepalive mMockSocketKeepalive; 71 protected NetworkCapabilities mMockNetworkCapabilities; 72 73 @Before setUp()74 public void setUp() throws Exception { 75 mMockIpSecTestUtils = MockIpSecTestUtils.setUpMockIpSec(); 76 mIpSecManager = mMockIpSecTestUtils.getIpSecManager(); 77 78 mSpyContext = spy(mMockIpSecTestUtils.getContext()); 79 doReturn(null) 80 .when(mSpyContext) 81 .registerReceiver( 82 any(IkeAlarmReceiver.class), 83 any(IntentFilter.class), 84 any(), 85 any(Handler.class)); 86 doNothing().when(mSpyContext).unregisterReceiver(any(IkeAlarmReceiver.class)); 87 88 mPowerManager = mock(PowerManager.class); 89 mMockBusyWakelock = mock(PowerManager.WakeLock.class); 90 mMockLocalRequestWakelock = mock(PowerManager.WakeLock.class); 91 doReturn(mPowerManager).when(mSpyContext).getSystemService(eq(PowerManager.class)); 92 doReturn(mMockBusyWakelock) 93 .when(mPowerManager) 94 .newWakeLock(anyInt(), argThat(tag -> tag.contains(BUSY_WAKE_LOCK_TAG))); 95 // Only in test that all local requests will get the same WakeLock instance but in function 96 // code each local request will have a separate WakeLock. 97 doReturn(mMockLocalRequestWakelock) 98 .when(mPowerManager) 99 .newWakeLock(anyInt(), argThat(tag -> tag.contains(LOCAL_REQUEST_WAKE_LOCK_TAG))); 100 101 mMockConnectManager = mock(ConnectivityManager.class); 102 mMockDefaultNetwork = mock(Network.class); 103 doReturn(mMockDefaultNetwork).when(mMockConnectManager).getActiveNetwork(); 104 doReturn(REMOTE_ADDRESS).when(mMockDefaultNetwork).getByName(REMOTE_HOSTNAME); 105 doReturn(REMOTE_ADDRESS) 106 .when(mMockDefaultNetwork) 107 .getByName(REMOTE_ADDRESS.getHostAddress()); 108 109 mMockSocketKeepalive = mock(SocketKeepalive.class); 110 doReturn(mMockSocketKeepalive) 111 .when(mMockConnectManager) 112 .createSocketKeepalive( 113 any(Network.class), 114 any(UdpEncapsulationSocket.class), 115 any(Inet4Address.class), 116 any(Inet4Address.class), 117 any(Executor.class), 118 any(SocketKeepalive.Callback.class)); 119 doReturn(mMockConnectManager) 120 .when(mSpyContext) 121 .getSystemService(Context.CONNECTIVITY_SERVICE); 122 123 mMockNetworkCapabilities = mock(NetworkCapabilities.class); 124 doReturn(mMockNetworkCapabilities) 125 .when(mMockConnectManager) 126 .getNetworkCapabilities(any(Network.class)); 127 doReturn(false) 128 .when(mMockNetworkCapabilities) 129 .hasTransport(RandomnessFactory.TRANSPORT_TEST); 130 } 131 } 132