• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.test.ike;
18 
19 import static com.android.internal.net.ipsec.test.ike.IkeLocalRequestScheduler.LOCAL_REQUEST_WAKE_LOCK_TAG;
20 import static com.android.internal.net.ipsec.test.ike.IkeSessionStateMachine.BUSY_WAKE_LOCK_TAG;
21 import static com.android.internal.net.ipsec.test.ike.IkeSocket.SERVER_PORT_NON_UDP_ENCAPSULATED;
22 import static com.android.internal.net.ipsec.test.ike.IkeSocket.SERVER_PORT_UDP_ENCAPSULATED;
23 
24 import static org.mockito.ArgumentMatchers.argThat;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyInt;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.any;
29 import static org.mockito.Mockito.anyInt;
30 import static org.mockito.Mockito.doAnswer;
31 import static org.mockito.Mockito.doNothing;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.eq;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.reset;
36 import static org.mockito.Mockito.spy;
37 import static org.mockito.Mockito.when;
38 
39 import android.content.Context;
40 import android.content.IntentFilter;
41 import android.net.ConnectivityManager;
42 import android.net.InetAddresses;
43 import android.net.IpSecManager;
44 import android.net.LinkAddress;
45 import android.net.LinkProperties;
46 import android.net.Network;
47 import android.net.NetworkCapabilities;
48 import android.net.SocketKeepalive;
49 import android.os.Handler;
50 import android.os.PowerManager;
51 
52 import com.android.internal.net.ipsec.test.ike.net.IkeLocalAddressGenerator;
53 import com.android.internal.net.ipsec.test.ike.testutils.MockIpSecTestUtils;
54 import com.android.internal.net.ipsec.test.ike.utils.IkeAlarmReceiver;
55 import com.android.internal.net.ipsec.test.ike.utils.RandomnessFactory;
56 
57 import org.junit.Before;
58 import org.mockito.invocation.InvocationOnMock;
59 import org.mockito.stubbing.Answer;
60 
61 import java.io.IOException;
62 import java.net.Inet4Address;
63 import java.net.Inet6Address;
64 import java.net.InetAddress;
65 import java.util.concurrent.Executor;
66 
67 public abstract class IkeSessionTestBase {
68     protected static final Inet4Address LOCAL_ADDRESS =
69             (Inet4Address) InetAddresses.parseNumericAddress("192.0.2.200");
70     protected static final Inet4Address UPDATED_LOCAL_ADDRESS =
71             (Inet4Address) InetAddresses.parseNumericAddress("192.0.2.201");
72     protected static final Inet4Address REMOTE_ADDRESS =
73             (Inet4Address) InetAddresses.parseNumericAddress("127.0.0.1");
74     protected static final Inet6Address LOCAL_ADDRESS_V6 =
75             (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::200");
76     protected static final Inet6Address UPDATED_LOCAL_ADDRESS_V6 =
77             (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::201");
78     protected static final Inet6Address REMOTE_ADDRESS_V6 =
79             (Inet6Address) InetAddresses.parseNumericAddress("::1");
80     protected static final String REMOTE_HOSTNAME = "ike.test.android.com";
81 
82     protected PowerManager.WakeLock mMockBusyWakelock;
83     protected PowerManager.WakeLock mMockLocalRequestWakelock;
84 
85     protected MockIpSecTestUtils mMockIpSecTestUtils;
86     protected Context mSpyContext;
87     protected IpSecManager mIpSecManager;
88     protected PowerManager mPowerManager;
89 
90     protected ConnectivityManager mMockConnectManager;
91     protected Network mMockDefaultNetwork;
92     protected SocketKeepalive mMockSocketKeepalive;
93     protected NetworkCapabilities mMockNetworkCapabilities;
94     protected IkeLocalAddressGenerator mMockIkeLocalAddressGenerator;
95 
96     @Before
setUp()97     public void setUp() throws Exception {
98         mMockIpSecTestUtils = MockIpSecTestUtils.setUpMockIpSec();
99         mIpSecManager = mMockIpSecTestUtils.getIpSecManager();
100 
101         mSpyContext = spy(mMockIpSecTestUtils.getContext());
102         doReturn(null)
103                 .when(mSpyContext)
104                 .registerReceiver(
105                         any(IkeAlarmReceiver.class),
106                         any(IntentFilter.class),
107                         any(),
108                         any(Handler.class),
109                         anyInt());
110         doNothing().when(mSpyContext).unregisterReceiver(any(IkeAlarmReceiver.class));
111 
112         mPowerManager = mock(PowerManager.class);
113         mMockBusyWakelock = mock(PowerManager.WakeLock.class);
114         mMockLocalRequestWakelock = mock(PowerManager.WakeLock.class);
115         doReturn(mPowerManager).when(mSpyContext).getSystemService(eq(PowerManager.class));
116         doReturn(mMockBusyWakelock)
117                 .when(mPowerManager)
118                 .newWakeLock(anyInt(), argThat(tag -> tag.contains(BUSY_WAKE_LOCK_TAG)));
119         // Only in test that all local requests will get the same WakeLock instance but in function
120         // code each local request will have a separate WakeLock.
121         doReturn(mMockLocalRequestWakelock)
122                 .when(mPowerManager)
123                 .newWakeLock(anyInt(), argThat(tag -> tag.contains(LOCAL_REQUEST_WAKE_LOCK_TAG)));
124 
125         mMockDefaultNetwork = mock(Network.class);
126         resetDefaultNetwork();
127 
128         mMockSocketKeepalive = mock(SocketKeepalive.class);
129 
130         mMockNetworkCapabilities = mock(NetworkCapabilities.class);
131         doReturn(false)
132                 .when(mMockNetworkCapabilities)
133                 .hasTransport(RandomnessFactory.TRANSPORT_TEST);
134 
135         mMockConnectManager = mock(ConnectivityManager.class);
136         doReturn(mMockConnectManager)
137                 .when(mSpyContext)
138                 .getSystemService(Context.CONNECTIVITY_SERVICE);
139 
140         mMockIkeLocalAddressGenerator = mock(IkeLocalAddressGenerator.class);
141         resetMockConnectManager();
142     }
143 
resetMockConnectManager()144     protected void resetMockConnectManager() throws Exception {
145         reset(mMockConnectManager);
146         doReturn(mMockDefaultNetwork).when(mMockConnectManager).getActiveNetwork();
147         doReturn(mMockSocketKeepalive)
148                 .when(mMockConnectManager)
149                 .createSocketKeepalive(
150                         any(Network.class),
151                         any(),
152                         any(Inet4Address.class),
153                         any(Inet4Address.class),
154                         any(Executor.class),
155                         any(SocketKeepalive.Callback.class));
156         doReturn(mMockNetworkCapabilities)
157                 .when(mMockConnectManager)
158                 .getNetworkCapabilities(any(Network.class));
159         setupLocalAddressForNetwork(mMockDefaultNetwork, LOCAL_ADDRESS);
160         setupRemoteAddressForNetwork(mMockDefaultNetwork, REMOTE_ADDRESS);
161     }
162 
resetDefaultNetwork()163     protected void resetDefaultNetwork() throws Exception {
164         reset(mMockDefaultNetwork);
165         doReturn(new InetAddress[] {REMOTE_ADDRESS})
166                 .when(mMockDefaultNetwork)
167                 .getAllByName(REMOTE_HOSTNAME);
168         doReturn(new InetAddress[] {REMOTE_ADDRESS})
169                 .when(mMockDefaultNetwork)
170                 .getAllByName(REMOTE_ADDRESS.getHostAddress());
171     }
172 
resetMockIkeSocket(IkeSocket mockSocket)173     protected void resetMockIkeSocket(IkeSocket mockSocket) {
174         reset(mockSocket);
175         if (mockSocket instanceof IkeUdp4Socket || mockSocket instanceof IkeUdp6Socket) {
176             when(mockSocket.getIkeServerPort()).thenReturn(SERVER_PORT_NON_UDP_ENCAPSULATED);
177         } else {
178             when(mockSocket.getIkeServerPort()).thenReturn(SERVER_PORT_UDP_ENCAPSULATED);
179         }
180     }
181 
newMockIkeSocket(Class<T> socketClass)182     protected <T extends IkeSocket> T newMockIkeSocket(Class<T> socketClass) {
183         T mockSocket = mock(socketClass);
184         resetMockIkeSocket(mockSocket);
185 
186         return mockSocket;
187     }
188 
setupLocalAddressAndGetLinkAddress(Network network, InetAddress address)189     private LinkAddress setupLocalAddressAndGetLinkAddress(Network network, InetAddress address)
190             throws Exception {
191         boolean isIpv4 = address instanceof Inet4Address;
192         when(mMockIkeLocalAddressGenerator.generateLocalAddress(
193                         eq(network), eq(isIpv4), any(), anyInt()))
194                 .thenReturn(address);
195 
196         LinkAddress mockLinkAddress = mock(LinkAddress.class);
197         when(mockLinkAddress.getAddress()).thenReturn(address);
198         if (!isIpv4) {
199             when(mockLinkAddress.isGlobalPreferred()).thenReturn(true);
200         }
201 
202         return mockLinkAddress;
203     }
204 
setupLocalAddressForNetwork( Network network, Inet4Address addressV4, Inet6Address addressV6)205     protected void setupLocalAddressForNetwork(
206             Network network, Inet4Address addressV4, Inet6Address addressV6) throws Exception {
207         LinkProperties linkProperties = new LinkProperties();
208         if (addressV4 != null) {
209             linkProperties.addLinkAddress(setupLocalAddressAndGetLinkAddress(network, addressV4));
210         }
211         if (addressV6 != null) {
212             linkProperties.addLinkAddress(setupLocalAddressAndGetLinkAddress(network, addressV6));
213         }
214         when(mMockConnectManager.getLinkProperties(eq(network))).thenReturn(linkProperties);
215     }
216 
setupLocalAddressForNetwork(Network network, InetAddress address)217     protected void setupLocalAddressForNetwork(Network network, InetAddress address)
218             throws Exception {
219         if (address instanceof Inet4Address) {
220             setupLocalAddressForNetwork(network, (Inet4Address) address, null);
221         } else {
222             setupLocalAddressForNetwork(network, null, (Inet6Address) address);
223         }
224     }
225 
setupRemoteAddressForNetwork(Network network, InetAddress... addresses)226     protected void setupRemoteAddressForNetwork(Network network, InetAddress... addresses)
227             throws Exception {
228         doAnswer(
229                 new Answer() {
230                         public Object answer(InvocationOnMock invocation) throws IOException {
231                             return addresses;
232                         }
233                 })
234                 .when(network)
235                 .getAllByName(REMOTE_HOSTNAME);
236     }
237 }
238