• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.test.ike.message;
18 
19 import static com.android.internal.net.ipsec.test.ike.message.IkeMessage.DECODE_STATUS_UNPROTECTED_ERROR;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 
27 import android.net.ipsec.test.ike.exceptions.IkeProtocolException;
28 import android.util.Pair;
29 
30 import com.android.internal.net.TestUtils;
31 import com.android.internal.net.ipsec.test.ike.message.IkeMessage.DecodeResult;
32 import com.android.internal.net.ipsec.test.ike.message.IkeMessage.DecodeResultError;
33 
34 import java.nio.ByteBuffer;
35 
36 /**
37  * IkeTestUtils provides utility methods for testing IKE library.
38  *
39  * <p>TODO: Consider moving it under ikev2/
40  */
41 public final class IkeTestUtils {
hexStringToIkePayload( @kePayload.PayloadType int payloadType, boolean isResp, String payloadHexString)42     public static IkePayload hexStringToIkePayload(
43             @IkePayload.PayloadType int payloadType, boolean isResp, String payloadHexString)
44             throws IkeProtocolException {
45         byte[] payloadBytes = TestUtils.hexStringToByteArray(payloadHexString);
46         // Returned Pair consists of the IkePayload and the following IkePayload's type.
47         Pair<IkePayload, Integer> pair =
48                 IkePayloadFactory.getIkePayload(payloadType, isResp, ByteBuffer.wrap(payloadBytes));
49         return pair.first;
50     }
51 
decodeAndVerifyUnprotectedErrorMsg( byte[] inputPacket, Class<T> expectedException)52     public static <T extends IkeProtocolException> T decodeAndVerifyUnprotectedErrorMsg(
53             byte[] inputPacket, Class<T> expectedException) throws Exception {
54         IkeHeader header = new IkeHeader(inputPacket);
55         DecodeResult decodeResult = IkeMessage.decode(0, header, inputPacket);
56 
57         assertEquals(DECODE_STATUS_UNPROTECTED_ERROR, decodeResult.status);
58         DecodeResultError resultError = (DecodeResultError) decodeResult;
59         assertNotNull(resultError.ikeException);
60         assertTrue(expectedException.isInstance(resultError.ikeException));
61 
62         return (T) resultError.ikeException;
63     }
64 
makeDummySkfPayload( byte[] unencryptedData, int fragNum, int totalFrags)65     public static IkeSkfPayload makeDummySkfPayload(
66             byte[] unencryptedData, int fragNum, int totalFrags) throws Exception {
67         IkeEncryptedPayloadBody mockEncryptedBody = mock(IkeEncryptedPayloadBody.class);
68         doReturn(unencryptedData).when(mockEncryptedBody).getUnencryptedData();
69         return new IkeSkfPayload(mockEncryptedBody, fragNum, totalFrags);
70     }
71 }
72