aboutsummaryrefslogtreecommitdiff
path: root/tests/iketests/src/java/com/android
diff options
context:
space:
mode:
authorevitayan <evitayan@google.com>2018-08-01 10:29:37 -0700
committerevitayan <evitayan@google.com>2018-08-13 15:45:33 -0700
commit3c41feaa0e7b6d1e9174d8eb3cc11368a8afa30b (patch)
tree99ca166d02d6dc7b007423f8be317dbd1824f17e /tests/iketests/src/java/com/android
parentc8177b4017e1ccebfef9e14b1fee8b969a22740d (diff)
downloadike-3c41feaa0e7b6d1e9174d8eb3cc11368a8afa30b.tar.gz
Decode KE payload
This commits: - Create IkeKePayload class and add a constructor for decoding and validating. - Add IkeKePayloadTest - Add MessageTestUtil containing commonly used method for test files. Bug: 112044006 Test: FrameworksIkeTests IkeKePayloadTest Change-Id: Id82cdf8005a4cb5cc3e89e74db7449b22c153e17
Diffstat (limited to 'tests/iketests/src/java/com/android')
-rw-r--r--tests/iketests/src/java/com/android/ike/ikev2/message/IkeKePayloadTest.java75
-rw-r--r--tests/iketests/src/java/com/android/ike/ikev2/message/IkeMessageTest.java24
-rw-r--r--tests/iketests/src/java/com/android/ike/ikev2/message/MessageTestUtil.java31
3 files changed, 112 insertions, 18 deletions
diff --git a/tests/iketests/src/java/com/android/ike/ikev2/message/IkeKePayloadTest.java b/tests/iketests/src/java/com/android/ike/ikev2/message/IkeKePayloadTest.java
new file mode 100644
index 00000000..cc912218
--- /dev/null
+++ b/tests/iketests/src/java/com/android/ike/ikev2/message/IkeKePayloadTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ike.ikev2.message;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import com.android.ike.ikev2.exceptions.InvalidSyntaxException;
+
+import org.junit.Test;
+
+public final class IkeKePayloadTest {
+ private static final String KE_PAYLOAD_RAW_PACKET =
+ "00020000b4a2faf4bb54878ae21d638512ece55d9236fc50"
+ + "46ab6cef82220f421f3ce6361faf36564ecb6d28798a94aa"
+ + "d7b2b4b603ddeaaa5630adb9ece8ac37534036040610ebdd"
+ + "92f46bef84f0be7db860351843858f8acf87056e272377f7"
+ + "0c9f2d81e29c7b0ce4f291a3a72476bb0b278fd4b7b0a4c2"
+ + "6bbeb08214c7071376079587";
+
+ private static final boolean CRITICAL_BIT = false;
+ @IkeKePayload.DhGroup
+ private static final int EXPECTED_DH_GROUP = IkeKePayload.DH_GROUP_1024_BIT_MODP;
+
+ private static final String KEY_EXCHANGE_DATA_RAW_PACKET =
+ "b4a2faf4bb54878ae21d638512ece55d9236fc5046ab6cef"
+ + "82220f421f3ce6361faf36564ecb6d28798a94aad7b2b4b6"
+ + "03ddeaaa5630adb9ece8ac37534036040610ebdd92f46bef"
+ + "84f0be7db860351843858f8acf87056e272377f70c9f2d81"
+ + "e29c7b0ce4f291a3a72476bb0b278fd4b7b0a4c26bbeb082"
+ + "14c7071376079587";
+
+ @Test
+ public void testDecodeIkeKePayload() throws Exception {
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(KE_PAYLOAD_RAW_PACKET);
+
+ IkeKePayload payload = new IkeKePayload(CRITICAL_BIT, inputPacket);
+
+ assertEquals(EXPECTED_DH_GROUP, payload.dhGroup);
+
+ byte[] keyExchangeData = MessageTestUtil.hexStringToByteArray(KEY_EXCHANGE_DATA_RAW_PACKET);
+ assertEquals(keyExchangeData.length, payload.keyExchangeData.length);
+ for (int i = 0; i < keyExchangeData.length; i++) {
+ assertEquals(keyExchangeData[i], payload.keyExchangeData[i]);
+ }
+ }
+
+ @Test
+ public void testDecodeIkeKePayloadWithInvalidKeData() throws Exception {
+ // Cut bytes of KE data from original KE payload
+ String badKeyPayloadPacket =
+ KE_PAYLOAD_RAW_PACKET.substring(0, KE_PAYLOAD_RAW_PACKET.length() - 2);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(badKeyPayloadPacket);
+
+ try {
+ IkeKePayload payload = new IkeKePayload(CRITICAL_BIT, inputPacket);
+ fail("Expected InvalidSyntaxException: KE data length doesn't match its DH group type");
+ } catch (InvalidSyntaxException expected) {
+ }
+ }
+}
diff --git a/tests/iketests/src/java/com/android/ike/ikev2/message/IkeMessageTest.java b/tests/iketests/src/java/com/android/ike/ikev2/message/IkeMessageTest.java
index 6ca8fdc3..1177d964 100644
--- a/tests/iketests/src/java/com/android/ike/ikev2/message/IkeMessageTest.java
+++ b/tests/iketests/src/java/com/android/ike/ikev2/message/IkeMessageTest.java
@@ -100,7 +100,7 @@ public final class IkeMessageTest {
@Test
public void testDecodeIkeHeader() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
IkeHeader header = new IkeHeader(inputPacket);
assertEquals(IKE_MSG_LENGTH, inputPacket.length);
@@ -122,7 +122,7 @@ public final class IkeMessageTest {
@Test
public void testDecodeIkeMessage() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
IkeHeader header = new IkeHeader(inputPacket);
IkeMessage message = IkeMessage.decode(header, inputPacket);
assertEquals(SUPPORTED_PAYLOAD_LIST.length, message.ikePayloadList.size());
@@ -133,7 +133,7 @@ public final class IkeMessageTest {
@Test
public void testDecodeMessageWithUnsupportedUncriticalPayload() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
// Set first payload unsupported uncritical
inputPacket[FIRST_PAYLOAD_TYPE_POSITION] = (byte) 0xff;
IkeHeader header = new IkeHeader(inputPacket);
@@ -146,7 +146,7 @@ public final class IkeMessageTest {
@Test
public void testThrowInvalidMajorVersionException() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
// Set major version 3.
inputPacket[VERSION_POSITION] = (byte) 0x30;
// Set Exchange type 0
@@ -164,7 +164,7 @@ public final class IkeMessageTest {
@Test
public void testThrowInvalidSyntaxException() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
// Set Exchange type 0
inputPacket[EXCHANGE_TYPE_POSITION] = (byte) 0x00;
IkeHeader header = new IkeHeader(inputPacket);
@@ -177,7 +177,7 @@ public final class IkeMessageTest {
@Test
public void testThrowUnsupportedCriticalPayloadException() throws Exception {
- byte[] inputPacket = hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
+ byte[] inputPacket = MessageTestUtil.hexStringToByteArray(IKE_SA_INIT_RAW_PACKET);
// Set first payload unsupported critical
inputPacket[FIRST_PAYLOAD_TYPE_POSITION] = (byte) 0xff;
inputPacket[IkeHeader.IKE_HEADER_LENGTH + PAYLOAD_CRITICAL_BIT_POSITION] = (byte) 0x80;
@@ -193,18 +193,6 @@ public final class IkeMessageTest {
}
}
- private byte[] hexStringToByteArray(String s) {
- int len = s.length();
- byte[] data = new byte[len / 2];
- for (int i = 0; i < len; i += 2) {
- data[i / 2] =
- (byte)
- ((Character.digit(s.charAt(i), 16) << 4)
- + Character.digit(s.charAt(i + 1), 16));
- }
- return data;
- }
-
private boolean support(int payloadType) {
return (payloadType == IkePayload.PAYLOAD_TYPE_SA
|| payloadType == IkePayload.PAYLOAD_TYPE_KE
diff --git a/tests/iketests/src/java/com/android/ike/ikev2/message/MessageTestUtil.java b/tests/iketests/src/java/com/android/ike/ikev2/message/MessageTestUtil.java
new file mode 100644
index 00000000..b9544a87
--- /dev/null
+++ b/tests/iketests/src/java/com/android/ike/ikev2/message/MessageTestUtil.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ike.ikev2.message;
+
+public final class MessageTestUtil {
+ static byte[] hexStringToByteArray(String s) {
+ int len = s.length();
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] =
+ (byte)
+ ((Character.digit(s.charAt(i), 16) << 4)
+ + Character.digit(s.charAt(i + 1), 16));
+ }
+ return data;
+ }
+}