1 /* 2 * Copyright (C) 2023 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; 18 19 import static com.android.server.IpSecXfrmControllerTestHex.XFRM_ESRCH_HEX; 20 import static com.android.server.IpSecXfrmControllerTestHex.XFRM_NEW_SA_HEX; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNotNull; 24 import static org.junit.Assert.assertNull; 25 import static org.junit.Assert.fail; 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.ArgumentMatchers.eq; 28 import static org.mockito.Mockito.any; 29 import static org.mockito.Mockito.eq; 30 import static org.mockito.Mockito.verify; 31 import static org.mockito.Mockito.when; 32 33 import android.net.InetAddresses; 34 import android.system.ErrnoException; 35 36 import androidx.test.filters.SmallTest; 37 import androidx.test.runner.AndroidJUnit4; 38 39 import com.android.net.module.util.netlink.NetlinkMessage; 40 import com.android.net.module.util.netlink.xfrm.XfrmNetlinkNewSaMessage; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.ArgumentCaptor; 46 import org.mockito.Captor; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 50 import java.io.FileDescriptor; 51 import java.net.InetAddress; 52 import java.nio.ByteBuffer; 53 import java.nio.ByteOrder; 54 55 @RunWith(AndroidJUnit4.class) 56 @SmallTest 57 public class IpSecXfrmControllerTest { 58 private static final InetAddress DEST_ADDRESS = 59 InetAddresses.parseNumericAddress("2001:db8::111"); 60 private static final long SPI = 0xaabbccddL; 61 private static final int ESRCH = -3; 62 63 private IpSecXfrmController mXfrmController; 64 private FileDescriptor mDummyNetlinkSocket; 65 66 @Mock private IpSecXfrmController.Dependencies mMockDeps; 67 68 @Captor private ArgumentCaptor<byte[]> mRequestByteArrayCaptor; 69 70 @Before setUp()71 public void setUp() throws Exception { 72 MockitoAnnotations.initMocks(this); 73 mDummyNetlinkSocket = new FileDescriptor(); 74 75 when(mMockDeps.newNetlinkSocket()).thenReturn(mDummyNetlinkSocket); 76 mXfrmController = new IpSecXfrmController(mMockDeps); 77 } 78 79 @Test testStartStop()80 public void testStartStop() throws Exception { 81 mXfrmController.openNetlinkSocketIfNeeded(); 82 83 verify(mMockDeps).newNetlinkSocket(); 84 assertNotNull(mXfrmController.getNetlinkSocket()); 85 86 mXfrmController.closeNetlinkSocketIfNeeded(); 87 verify(mMockDeps).releaseNetlinkSocket(eq(mDummyNetlinkSocket)); 88 assertNull(mXfrmController.getNetlinkSocket()); 89 } 90 injectRxMessage(IpSecXfrmController.Dependencies mockDeps, byte[] bytes)91 private static void injectRxMessage(IpSecXfrmController.Dependencies mockDeps, byte[] bytes) 92 throws Exception { 93 final ByteBuffer buff = ByteBuffer.wrap(bytes); 94 buff.order(ByteOrder.nativeOrder()); 95 96 when(mockDeps.recvMessage(any(FileDescriptor.class))).thenReturn(buff); 97 } 98 99 @Test testIpSecGetSa()100 public void testIpSecGetSa() throws Exception { 101 final int expectedReqLen = 40; 102 injectRxMessage(mMockDeps, XFRM_NEW_SA_HEX); 103 104 final NetlinkMessage netlinkMessage = mXfrmController.ipSecGetSa(DEST_ADDRESS, SPI); 105 final XfrmNetlinkNewSaMessage message = (XfrmNetlinkNewSaMessage) netlinkMessage; 106 107 // Verifications 108 assertEquals(SPI, message.getXfrmUsersaInfo().getSpi()); 109 assertEquals(DEST_ADDRESS, message.getXfrmUsersaInfo().getDestAddress()); 110 111 verify(mMockDeps).sendMessage(eq(mDummyNetlinkSocket), mRequestByteArrayCaptor.capture()); 112 final byte[] request = mRequestByteArrayCaptor.getValue(); 113 assertEquals(expectedReqLen, request.length); 114 115 verify(mMockDeps).recvMessage(eq(mDummyNetlinkSocket)); 116 } 117 118 @Test testIpSecGetSa_NlErrorMsg()119 public void testIpSecGetSa_NlErrorMsg() throws Exception { 120 injectRxMessage(mMockDeps, XFRM_ESRCH_HEX); 121 122 try { 123 mXfrmController.ipSecGetSa(DEST_ADDRESS, SPI); 124 fail("Expected to fail with ESRCH "); 125 } catch (ErrnoException e) { 126 assertEquals(ESRCH, e.errno); 127 } 128 } 129 } 130