1 /* 2 * Copyright (C) 2019 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.testutils; 18 19 import static android.system.OsConstants.AF_INET; 20 import static android.system.OsConstants.IPPROTO_UDP; 21 import static android.system.OsConstants.SOCK_DGRAM; 22 23 import static org.mockito.Matchers.anyInt; 24 import static org.mockito.Matchers.anyObject; 25 import static org.mockito.Matchers.anyString; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.net.IpSecManager; 31 import android.net.IpSecSpiResponse; 32 import android.net.IpSecUdpEncapResponse; 33 import android.system.Os; 34 35 import androidx.test.InstrumentationRegistry; 36 37 import com.android.server.IpSecService; 38 39 /** This class provides utility methods for mocking IPsec surface. */ 40 public final class MockIpSecTestUtils { 41 private static final int DUMMY_CHILD_SPI = 0x2ad4c0a2; 42 private static final int DUMMY_CHILD_SPI_RESOURCE_ID = 0x1234; 43 44 private static final int DUMMY_UDP_ENCAP_PORT = 34567; 45 private static final int DUMMY_UDP_ENCAP_RESOURCE_ID = 0x3234; 46 47 private Context mContext; 48 private IpSecService mMockIpSecService; 49 private IpSecManager mIpSecManager; 50 MockIpSecTestUtils()51 private MockIpSecTestUtils() throws Exception { 52 mMockIpSecService = mock(IpSecService.class); 53 mContext = InstrumentationRegistry.getContext(); 54 mIpSecManager = new IpSecManager(mContext, mMockIpSecService); 55 56 when(mMockIpSecService.allocateSecurityParameterIndex(anyString(), anyInt(), anyObject())) 57 .thenReturn( 58 new IpSecSpiResponse( 59 IpSecManager.Status.OK, 60 DUMMY_CHILD_SPI_RESOURCE_ID, 61 DUMMY_CHILD_SPI)); 62 63 when(mMockIpSecService.openUdpEncapsulationSocket(anyInt(), anyObject())) 64 .thenReturn( 65 new IpSecUdpEncapResponse( 66 IpSecManager.Status.OK, 67 DUMMY_UDP_ENCAP_RESOURCE_ID, 68 DUMMY_UDP_ENCAP_PORT, 69 Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))); 70 } 71 setUpMockIpSec()72 public static MockIpSecTestUtils setUpMockIpSec() throws Exception { 73 return new MockIpSecTestUtils(); 74 } 75 buildDummyIpSecSpiResponse(int spi)76 public static IpSecSpiResponse buildDummyIpSecSpiResponse(int spi) throws Exception { 77 return new IpSecSpiResponse(IpSecManager.Status.OK, DUMMY_CHILD_SPI_RESOURCE_ID, spi); 78 } 79 buildDummyIpSecUdpEncapResponse(int port)80 public static IpSecUdpEncapResponse buildDummyIpSecUdpEncapResponse(int port) throws Exception { 81 return new IpSecUdpEncapResponse( 82 IpSecManager.Status.OK, 83 DUMMY_UDP_ENCAP_RESOURCE_ID, 84 port, 85 Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)); 86 } 87 getContext()88 public Context getContext() { 89 return mContext; 90 } 91 getIpSecManager()92 public IpSecManager getIpSecManager() { 93 return mIpSecManager; 94 } 95 getIpSecService()96 public IpSecService getIpSecService() { 97 return mMockIpSecService; 98 } 99 } 100