diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2019-11-11 21:24:43 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2019-11-11 21:24:43 +0000 |
commit | be2e569365fb597b26939bb475e88d99b3da2d63 (patch) | |
tree | b39c3e794945072d38b97c963c5a6f10753514e1 /tests/iketests/src/java/com/android/internal/net/eap/message | |
parent | 78052775925ce7e49f6013e2fbea4130779f13cf (diff) | |
parent | eb4c77d7228f956f928c7d3500a220339ee78388 (diff) | |
download | ike-be2e569365fb597b26939bb475e88d99b3da2d63.tar.gz |
Snap for 6001391 from eb4c77d7228f956f928c7d3500a220339ee78388 to qt-aml-networking-releaseandroid-mainline-10.0.0_r6android10-mainline-networking-release
Change-Id: Ifede049eeae909912e9792f87a89bf4ede7e34d9
Diffstat (limited to 'tests/iketests/src/java/com/android/internal/net/eap/message')
36 files changed, 4271 insertions, 0 deletions
diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/EapDataTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/EapDataTest.java new file mode 100644 index 00000000..9d4cc7ca --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/EapDataTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message; + +import static com.android.internal.net.eap.message.EapData.EAP_TYPE_SIM; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class EapDataTest { + private static final byte[] EXPECTED_EAP_DATA_BYTES = new byte[] { + EAP_TYPE_SIM, + (byte) 1, + (byte) 2, + (byte) 3 + }; + private static final byte[] EAP_TYPE_DATA = new byte[] {(byte) 1, (byte) 2, (byte) 3}; + private static final int UNSUPPORTED_EAP_TYPE = -1; + + @Test + public void testEapDataConstructor() { + new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + } + + @Test + public void testEapDataConstructorNullEapData() { + try { + new EapData(EAP_TYPE_SIM, null); + fail("IllegalArgumentException expected for null eapTypeData"); + } catch (IllegalArgumentException expected) { + } + } + + @Test + public void testEapDataConstructorUnsupportedType() { + try { + new EapData(UNSUPPORTED_EAP_TYPE, EAP_TYPE_DATA); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + } + + @Test + public void testGetLength() { + EapData eapData = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + assertEquals(EAP_TYPE_DATA.length + 1, eapData.getLength()); + } + + @Test + public void testEquals() throws Exception { + EapData eapData = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + EapData eapDataCopy = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + assertEquals(eapData, eapDataCopy); + + EapData eapDataDifferent = new EapData(EAP_TYPE_SIM, new byte[0]); + assertNotEquals(eapData, eapDataDifferent); + } + + @Test + public void testHashCode() throws Exception { + EapData eapData = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + EapData eapDataCopy = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + assertNotEquals(0, eapData.hashCode()); + assertEquals(eapData.hashCode(), eapDataCopy.hashCode()); + } + + @Test + public void testEncodeToByteBuffer() { + EapData eapData = new EapData(EAP_TYPE_SIM, EAP_TYPE_DATA); + + ByteBuffer b = ByteBuffer.allocate(eapData.getLength()); + eapData.encodeToByteBuffer(b); + assertArrayEquals(EXPECTED_EAP_DATA_BYTES, b.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/EapMessageTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/EapMessageTest.java new file mode 100644 index 00000000..813a30f7 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/EapMessageTest.java @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.EapData.EAP_NAK; +import static com.android.internal.net.eap.message.EapData.EAP_TYPE_SIM; +import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_REQUEST; +import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_RESPONSE; +import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_SUCCESS; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_REQUEST_SIM_START_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_REQUEST_SIM_TYPE_DATA; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_RESPONSE_NAK_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_RESPONSE_NOTIFICATION_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_SUCCESS_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.ID_INT; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INCOMPLETE_HEADER_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INVALID_CODE_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.LONG_SUCCESS_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.REQUEST_MISSING_TYPE_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.REQUEST_UNSUPPORTED_TYPE_PACKET; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SHORT_PACKET; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.EapResult; +import com.android.internal.net.eap.EapResult.EapResponse; +import com.android.internal.net.eap.exceptions.EapInvalidPacketLengthException; +import com.android.internal.net.eap.exceptions.InvalidEapCodeException; +import com.android.internal.net.eap.exceptions.UnsupportedEapTypeException; + +import org.junit.Test; + +import java.util.Arrays; + +public class EapMessageTest { + @Test + public void testConstructorRequestWithoutType() throws Exception { + try { + new EapMessage(EAP_CODE_REQUEST, ID_INT, null); + fail("Expected EapInvalidPacketLengthException for an EAP-Request without Type value"); + } catch (EapInvalidPacketLengthException expected) { + } + } + + @Test + public void testDecode() throws Exception { + EapMessage result = EapMessage.decode(EAP_SUCCESS_PACKET); + assertEquals(EAP_CODE_SUCCESS, result.eapCode); + assertEquals(ID_INT, result.eapIdentifier); + assertEquals(EAP_SUCCESS_PACKET.length, result.eapLength); + assertNull(result.eapData); + + EapData expectedEapData = new EapData(EAP_TYPE_SIM, + hexStringToByteArray(EAP_REQUEST_SIM_TYPE_DATA)); + result = EapMessage.decode(EAP_REQUEST_SIM_START_PACKET); + assertEquals(EAP_CODE_REQUEST, result.eapCode); + assertEquals(ID_INT, result.eapIdentifier); + assertEquals(EAP_REQUEST_SIM_START_PACKET.length, result.eapLength); + assertEquals(expectedEapData, result.eapData); + } + + @Test + public void testDecodeInvalidCode() throws Exception { + try { + EapMessage.decode(INVALID_CODE_PACKET); + fail("Expected InvalidEapCodeException"); + } catch (InvalidEapCodeException expected) { + } + } + + @Test + public void testDecodeIncompleteHeader() throws Exception { + try { + EapMessage.decode(INCOMPLETE_HEADER_PACKET); + fail("Expected EapInvalidPacketLengthException"); + } catch (EapInvalidPacketLengthException expected) { + } + } + + @Test + public void testDecodeShortPacket() throws Exception { + try { + EapMessage.decode(SHORT_PACKET); + fail("Expected EapInvalidPacketLengthException"); + } catch (EapInvalidPacketLengthException expected) { + } + } + + @Test + public void testDecodeSuccessIncorrectLength() throws Exception { + try { + EapMessage.decode(LONG_SUCCESS_PACKET); + fail("Expected EapInvalidPacketLengthException"); + } catch (EapInvalidPacketLengthException expected) { + } + } + + @Test + public void testDecodeMissingTypeData() throws Exception { + try { + EapMessage.decode(REQUEST_MISSING_TYPE_PACKET); + fail("Expected EapInvalidPacketLengthException"); + } catch (EapInvalidPacketLengthException expected) { + } + } + + @Test + public void testDecodeUnsupportedEapType() throws Exception { + try { + EapMessage.decode(REQUEST_UNSUPPORTED_TYPE_PACKET); + fail("Expected UnsupportedEapDataTypeException"); + } catch (UnsupportedEapTypeException expected) { + assertEquals(ID_INT, expected.eapIdentifier); + } + } + + @Test + public void testEncode() throws Exception { + EapMessage eapMessage = new EapMessage(EAP_CODE_SUCCESS, ID_INT, null); + byte[] actualPacket = eapMessage.encode(); + assertArrayEquals(EAP_SUCCESS_PACKET, actualPacket); + + EapData nakData = new EapData(EAP_NAK, new byte[] {EAP_TYPE_SIM}); + eapMessage = new EapMessage(EAP_CODE_RESPONSE, ID_INT, nakData); + actualPacket = eapMessage.encode(); + assertArrayEquals(EAP_RESPONSE_NAK_PACKET, actualPacket); + } + + @Test + public void testEncodeDecode() throws Exception { + EapMessage eapMessage = new EapMessage(EAP_CODE_SUCCESS, ID_INT, null); + EapMessage result = EapMessage.decode(eapMessage.encode()); + + assertEquals(eapMessage.eapCode, result.eapCode); + assertEquals(eapMessage.eapIdentifier, result.eapIdentifier); + assertEquals(eapMessage.eapLength, result.eapLength); + assertEquals(eapMessage.eapData, result.eapData); + } + + @Test + public void testDecodeEncode() throws Exception { + byte[] result = EapMessage.decode(EAP_REQUEST_SIM_START_PACKET).encode(); + assertArrayEquals(EAP_REQUEST_SIM_START_PACKET, result); + } + + @Test + public void testGetNakResponse() { + EapResult nakResponse = EapMessage.getNakResponse(ID_INT, Arrays.asList(EAP_TYPE_SIM)); + + assertTrue(nakResponse instanceof EapResponse); + EapResponse eapResponse = (EapResponse) nakResponse; + assertArrayEquals(EAP_RESPONSE_NAK_PACKET, eapResponse.packet); + } + + @Test + public void testGetNotificationResponse() { + EapResult notificationResponse = EapMessage.getNotificationResponse(ID_INT); + + assertTrue(notificationResponse instanceof EapResponse); + EapResponse eapResponse = (EapResponse) notificationResponse; + assertArrayEquals(EAP_RESPONSE_NOTIFICATION_PACKET, eapResponse.packet); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/EapTestMessageDefinitions.java b/tests/iketests/src/java/com/android/internal/net/eap/message/EapTestMessageDefinitions.java new file mode 100644 index 00000000..20517583 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/EapTestMessageDefinitions.java @@ -0,0 +1,327 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_VERSION_LIST_DATA; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.IDENTITY_STRING; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NONCE_MT_STRING; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RAND_1; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RAND_2; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RES; + +/** + * EapTestMessageDefinitions provides byte[] encodings of commonly used EAP Messages. + * + * @see <a href="https://tools.ietf.org/html/rfc3748#section-4">RFC 3748, Extensible Authentication + * Protocol (EAP)</a> + */ +public class EapTestMessageDefinitions { + public static final String ID = "10"; + public static final int ID_INT = Integer.parseInt(ID, 16 /* radix */); + + // EAP-AKA Identity request + public static final String EAP_REQUEST_TYPE_DATA = "0500000D010000"; + public static final byte[] EAP_AKA_IDENTITY_REQUEST = + hexStringToByteArray(EAP_REQUEST_TYPE_DATA); + + // EAP-AKA/Identity request with no attributes + public static final byte[] EAP_REQUEST_AKA = hexStringToByteArray("01" + ID + "000817050000"); + public static final byte[] EAP_REQUEST_AKA_IDENTITY_PACKET = + hexStringToByteArray("01" + ID + "000A17" + EAP_REQUEST_TYPE_DATA); + public static final byte[] EAP_REQUEST_IDENTITY_PACKET = + hexStringToByteArray("01" + ID + "000501"); + + // EAP-Identity: hex for ASCII in "test@android.net" + public static final String EAP_IDENTITY_STRING = "7465737440616E64726F69642E6E6574"; + public static final byte[] EAP_IDENTITY = hexStringToByteArray(EAP_IDENTITY_STRING); + public static final byte[] EAP_RESPONSE_IDENTITY_PACKET = + hexStringToByteArray("02" + ID + "001501" + EAP_IDENTITY_STRING); + public static final byte[] EAP_RESPONSE_IDENTITY_DEFAULT_PACKET = + hexStringToByteArray("02" + ID + "000501"); + public static final byte[] EAP_REQUEST_NOTIFICATION_PACKET = + hexStringToByteArray("01" + ID + "000802AABBCC"); + public static final byte[] EAP_SUCCESS_PACKET = hexStringToByteArray("03" + ID + "0004"); + public static final byte[] EAP_FAILURE_PACKET = hexStringToByteArray("04" + ID + "0004"); + public static final byte[] EAP_SIM_CLIENT_ERROR_RESPONSE = + hexStringToByteArray("02" + ID + "000C120E000016010001"); + public static final byte[] EAP_SIM_CLIENT_ERROR_INSUFFICIENT_CHALLENGES = + hexStringToByteArray("02" + ID + "000C120E000016010002"); + public static final byte[] EAP_SIM_CLIENT_ERROR_UNABLE_TO_PROCESS = + hexStringToByteArray("02" + ID + "000C120E000016010000"); + public static final byte[] EAP_AKA_CLIENT_ERROR_UNABLE_TO_PROCESS = + hexStringToByteArray("02" + ID + "000C170E000016010000"); + + // EAP-SIM response containing SELECTED_VERSION (1) and IDENTITY attributes + public static final byte[] EAP_SIM_RESPONSE_PACKET = hexStringToByteArray( + "02" + ID + "0024120A0000100100010E060011" + IDENTITY_STRING + "000000"); + public static final byte[] EAP_SIM_RESPONSE_WITHOUT_IDENTITY = + hexStringToByteArray("02" + ID + "0020120A000007050000" + NONCE_MT_STRING + "10010001"); + public static final byte[] EAP_SIM_NOTIFICATION_RESPONSE = hexStringToByteArray( + "02" + ID + "0008120C0000"); + public static final byte[] EAP_AKA_NOTIFICATION_RESPONSE = + hexStringToByteArray("02" + ID + "0008170C0000"); + + // Body of EapData is the list of supported methods + public static final byte[] EAP_RESPONSE_NAK_PACKET = + hexStringToByteArray("02" + ID + "00060312"); + public static final byte[] EAP_RESPONSE_NOTIFICATION_PACKET = + hexStringToByteArray("02" + ID + "000502"); + public static final byte[] EAP_REQUEST_MD5_CHALLENGE = + hexStringToByteArray("01" + ID + "000504"); + public static final byte[] EAP_REQUEST_NAK_PACKET = + hexStringToByteArray("01" + ID + "000503"); + public static final String EAP_REQUEST_SIM_TYPE_DATA = "0A00000F02000200010000"; + public static final byte[] EAP_REQUEST_SIM_START_PACKET = + hexStringToByteArray("01" + ID + "001012" + EAP_REQUEST_SIM_TYPE_DATA); + + public static final byte[] REQUEST_UNSUPPORTED_TYPE_PACKET = + hexStringToByteArray("01" + ID + "0005FF"); + public static final byte[] REQUEST_MISSING_TYPE_PACKET = + hexStringToByteArray("01" + ID + "0004"); + public static final byte[] LONG_SUCCESS_PACKET = hexStringToByteArray("03" + ID + "000500"); + public static final byte[] SHORT_PACKET = hexStringToByteArray("01" + ID + "0005"); + public static final byte[] INCOMPLETE_HEADER_PACKET = hexStringToByteArray("03" + ID); + public static final byte[] INVALID_CODE_PACKET = hexStringToByteArray("F0" + ID + "0004"); + + // Attributes + public static final String SKIPPABLE_DATA = "112233445566"; + public static final byte[] SKIPPABLE_DATA_BYTES = hexStringToByteArray(SKIPPABLE_DATA); + public static final byte[] SKIPPABLE_INVALID_ATTRIBUTE = + hexStringToByteArray("FF02" + SKIPPABLE_DATA); + public static final byte[] NON_SKIPPABLE_INVALID_ATTRIBUTE = + hexStringToByteArray("7F010000"); + + // Type-Data + public static final byte[] EAP_SIM_START_SUBTYPE = + hexStringToByteArray("0A00000F02" + AT_VERSION_LIST_DATA + "0A010000"); + public static final byte[] INVALID_SUBTYPE = hexStringToByteArray("FF"); + public static final byte[] TYPE_DATA_INVALID_AT_RAND = + hexStringToByteArray("0A000001050000" + RAND_1); + public static final byte[] SHORT_TYPE_DATA = hexStringToByteArray("0A"); + public static final byte[] TYPE_DATA_INVALID_ATTRIBUTE = + hexStringToByteArray("0A00007F01"); + public static final byte[] EAP_SIM_START_DUPLICATE_ATTRIBUTES = + hexStringToByteArray("0A00000F02" + "0A010000" + "0A010000"); + + // RAND Challenge Results + public static final String SRES_1 = "11223344"; + public static final byte[] SRES_1_BYTES = hexStringToByteArray(SRES_1); + public static final String SRES_2 = "44332211"; + public static final byte[] SRES_2_BYTES = hexStringToByteArray(SRES_2); + public static final byte[] SRES_BYTES = hexStringToByteArray(SRES_1 + SRES_2); + public static final String KC_1 = "0102030405060708"; + public static final byte[] KC_1_BYTES = hexStringToByteArray(KC_1); + public static final String KC_2 = "0807060504030201"; + public static final byte[] KC_2_BYTES = hexStringToByteArray(KC_2); + public static final byte[] VALID_CHALLENGE_RESPONSE = + hexStringToByteArray("04" + SRES_1 + "08" + KC_1); + public static final byte[] CHALLENGE_RESPONSE_INVALID_SRES = hexStringToByteArray("03"); + public static final byte[] CHALLENGE_RESPONSE_INVALID_KC = + hexStringToByteArray("04" + SRES_1 + "04"); + + public static final String IMSI = "123456789012345"; + public static final String EAP_SIM_IDENTITY = "1" + IMSI; + public static final byte[] EAP_SIM_IDENTITY_BYTES = hexStringToByteArray(EAP_SIM_IDENTITY); + + // ASCII hex for "0" + IMSI (EAP-AKA identity format) + public static final String EAP_AKA_IDENTITY_BYTES = "30313233343536373839303132333435"; + + // Master Key generation + public static final String MK_STRING = "0123456789ABCDEF0123456789ABCDEF01234567"; + public static final byte[] MK = hexStringToByteArray(MK_STRING); + public static final String K_ENCR_STRING = "000102030405060708090A0B0C0D0E0F"; + public static final byte[] K_ENCR = hexStringToByteArray(K_ENCR_STRING); + public static final String K_AUT_STRING = "0F0E0D0C0B0A09080706050403020100"; + public static final byte[] K_AUT = hexStringToByteArray(K_AUT_STRING); + public static final String MSK_STRING = + "00112233445566778899AABBCCDDEEFF" + + "00112233445566778899AABBCCDDEEFF" + + "00112233445566778899AABBCCDDEEFF" + + "00112233445566778899AABBCCDDEEFF"; + public static final byte[] MSK = hexStringToByteArray(MSK_STRING); + public static final String EMSK_STRING = + "FFEEDDCCBBAA99887766554433221100" + + "FFEEDDCCBBAA99887766554433221100" + + "FFEEDDCCBBAA99887766554433221100" + + "FFEEDDCCBBAA99887766554433221100"; + public static final byte[] EMSK = hexStringToByteArray(EMSK_STRING); + + // MAC computation + public static final String ORIGINAL_MAC_STRING = "112233445566778899AABBCCDDEEFF11"; + public static final byte[] ORIGINAL_MAC = hexStringToByteArray(ORIGINAL_MAC_STRING); + public static final String COMPUTED_MAC_STRING = "FFEEDDCCBBAA998877665544332211FF"; + public static final byte[] COMPUTED_MAC = hexStringToByteArray(COMPUTED_MAC_STRING); + public static final String EAP_SIM_CHALLENGE_REQUEST_STRING = + "01" + ID + "0040" // EAP-Request | ID | length in bytes + + "120b0000" // EAP-SIM | Challenge | 2B padding + + "01090000" + RAND_1 + RAND_2 // EAP-SIM AT_RAND attribute + + "0B05000000000000000000000000000000000000"; // AT_MAC attribute with no MAC + public static final byte[] MAC_INPUT = + hexStringToByteArray(EAP_SIM_CHALLENGE_REQUEST_STRING + NONCE_MT_STRING); + + // Response Message with MAC + public static final String EAP_SIM_CHALLENGE_RESPONSE_EMPTY_MAC = + "02" + ID + "001C" // EAP-Response | ID | length in bytes + + "120b0000" // EAP-SIM | Challenge | 2B padding + + "0B05000000000000000000000000000000000000"; // AT_MAC attribute with no MAC + public static final byte[] EAP_SIM_CHALLENGE_RESPONSE_MAC_INPUT = + hexStringToByteArray(EAP_SIM_CHALLENGE_RESPONSE_EMPTY_MAC + SRES_1 + SRES_2); + public static final byte[] EAP_SIM_CHALLENGE_RESPONSE_WITH_MAC = hexStringToByteArray( + "02" + ID + "001C" // EAP-Response | ID | length in bytes + + "120b0000" // EAP-SIM | Challenge | 2B padding + + "0B050000" + COMPUTED_MAC_STRING); // AT_MAC attribute + public static final byte[] EAP_SIM_NOTIFICATION_REQUEST_WITH_EMPTY_MAC = hexStringToByteArray( + "01" + ID + "0020" // EAP-Request | ID | length in bytes + + "120C0000" // EAP-SIM | Notification | 2B padding + + "0C010000" // AT_NOTIFICATION attribute + + "0B05000000000000000000000000000000000000"); // empty AT_MAC attribute + public static final byte[] EAP_SIM_NOTIFICATION_RESPONSE_WITH_EMPTY_MAC = hexStringToByteArray( + "02" + ID + "001C" // EAP-Response | ID | length in bytes + + "120C0000" // EAP-SIM | Notification | 2B padding + + "0B05000000000000000000000000000000000000"); // empty AT_MAC attribute + public static final byte[] EAP_SIM_NOTIFICATION_RESPONSE_WITH_MAC = hexStringToByteArray( + "02" + ID + "001C" // EAP-Response | ID | length in bytes + + "120C0000" // EAP-SIM | Notification | 2B padding + + "0B050000" + COMPUTED_MAC_STRING); // AT_MAC attribute + + public static final byte[] EAP_AKA_IDENTITY_RESPONSE = + hexStringToByteArray("02" + ID + "001C" // EAP-Response | ID | length in bytes + + "17050000" // EAP-AKA | Identity | 2B padding + + "0E050010" + EAP_AKA_IDENTITY_BYTES); // AT_IDENTITY ("0" + IMSI) + + // Base64 of: FF0111 + public static final String EAP_AKA_UICC_RESP_INVALID_TAG = "/wER"; + + // Base64 of: DC0E112233445566778899AABBCCDDEE + public static final String EAP_AKA_UICC_RESP_SYNCHRONIZE_BASE_64 = "3A4RIjNEVWZ3iJmqu8zd7g=="; + + public static final byte[] EAP_AKA_SYNCHRONIZATION_FAILURE = + hexStringToByteArray("02" + ID + "0018" // EAP-Response | ID | length in bytes + + "17040000" // EAP-SIM | Synchronization-Failure | 2B padding + + "0404112233445566778899AABBCCDDEE"); // AT_AUTS attribute + + public static final String IK = "00112233445566778899AABBCCDDEEFF"; + public static final byte[] IK_BYTES = hexStringToByteArray(IK); + public static final String CK = "FFEEDDCCBBAA99887766554433221100"; + public static final byte[] CK_BYTES = hexStringToByteArray(CK); + + // Base-64 of: 'DB05' + RES_BYTES + '10' + IK + '10' + CK + // 'DB0511223344551000112233445566778899AABBCCDDEEFF10FFEEDDCCBBAA99887766554433221100' + public static final String EAP_AKA_UICC_RESP_SUCCESS_BASE_64 = + "2wURIjNEVRAAESIzRFVmd4iZqrvM3e7/EP/u3cy7qpmId2ZVRDMiEQA="; + + public static final byte[] EAP_AKA_AUTHENTICATION_REJECT = + hexStringToByteArray("02" + ID + "000817020000"); + public static final String EAP_AKA_CHALLENGE_RESPONSE_MAC = "C70366512D9C5EBA8E3484509A25DCE4"; + public static final byte[] EAP_AKA_CHALLENGE_RESPONSE_MAC_BYTES = + hexStringToByteArray(EAP_AKA_CHALLENGE_RESPONSE_MAC); + public static final byte[] EAP_AKA_CHALLENGE_RESPONSE_TYPE_DATA = + hexStringToByteArray( + "01000003030028" + RES + "0000000B050000" + EAP_AKA_CHALLENGE_RESPONSE_MAC); + public static final byte[] EAP_AKA_CHALLENGE_RESPONSE = + hexStringToByteArray( + "02100028" // EAP-Response | ID | length in bytes + + "17010000" // EAP-AKA | Challenge | 2B padding + + "03030028" + RES + "000000" // AT_RES attribute + + "0B050000" + EAP_AKA_CHALLENGE_RESPONSE_MAC); // AT_MAC attribute + + public static final byte[] EAP_SUCCESS = hexStringToByteArray("03860004"); + + public static final byte[] EAP_REQUEST_MSCHAP_V2 = + hexStringToByteArray("01" + ID + "00061A01"); + + // MSCHAPv2 Test vectors taken from RFC 2759#9.2 and RFC 3079#3.5.3 + public static final String MSCHAP_V2_USERNAME = "User"; + public static final String MSCHAP_V2_USERNAME_HEX = "55736572"; + public static final byte[] MSCHAP_V2_USERNAME_ASCII_BYTES = + hexStringToByteArray(MSCHAP_V2_USERNAME_HEX); + public static final String MSCHAP_V2_PASSWORD = "clientPass"; + public static final byte[] MSCHAP_V2_PASSWORD_UTF_BYTES = + hexStringToByteArray("63006C00690065006E0074005000610073007300"); + public static final String MSCHAP_V2_AUTHENTICATOR_CHALLENGE_STRING = + "5B5D7C7D7B3F2F3E3C2C602132262628"; + public static final byte[] MSCHAP_V2_AUTHENTICATOR_CHALLENGE = + hexStringToByteArray(MSCHAP_V2_AUTHENTICATOR_CHALLENGE_STRING); + public static final String MSCHAP_V2_PEER_CHALLENGE_STRING = "21402324255E262A28295F2B3A337C7E"; + public static final byte[] MSCHAP_V2_PEER_CHALLENGE = + hexStringToByteArray(MSCHAP_V2_PEER_CHALLENGE_STRING); + public static final byte[] MSCHAP_V2_CHALLENGE = hexStringToByteArray("D02E4386BCE91226"); + public static final byte[] MSCHAP_V2_PASSWORD_HASH = + hexStringToByteArray("44EBBA8D5312B8D611474411F56989AE"); + public static final byte[] MSCHAP_V2_PASSWORD_HASH_HASH = + hexStringToByteArray("41C00C584BD2D91C4017A2A12FA59F3F"); + public static final String MSCHAP_V2_NT_RESPONSE_STRING = + "82309ECD8D708B5EA08FAA3981CD83544233114A3D85D6DF"; + public static final byte[] MSCHAP_V2_NT_RESPONSE = + hexStringToByteArray(MSCHAP_V2_NT_RESPONSE_STRING); + public static final byte[] MSCHAP_V2_AUTHENTICATOR_RESPONSE = + hexStringToByteArray("407A5589115FD0D6209F510FE9C04566932CDA56"); + public static final byte[] MSCHAP_V2_MASTER_KEY = + hexStringToByteArray("FDECE3717A8C838CB388E527AE3CDD31"); + + // generated based on RFC 3079#3.5.3 params + public static final String SEND_KEY = "D5F0E9521E3EA9589645E86051C82226"; + public static final byte[] MSCHAP_V2_SEND_START_KEY = hexStringToByteArray(SEND_KEY); + + // This value is labeled 'send key' in RFC 3079#3.5.3. However, it's used as 'receive key' here, + // because send and receive keys are swapped for peers relative to authenticators. + public static final String RECEIVE_KEY = "8B7CDC149B993A1BA118CB153F56DCCB"; + public static final byte[] MSCHAP_V2_RECEIVE_START_KEY = hexStringToByteArray(RECEIVE_KEY); + + // MSK: MSCHAP_V2_SEND_START_KEY + MSCHAP_V2_RECEIVE_START_KEY + public static final byte[] MSCHAP_V2_MSK = hexStringToByteArray(SEND_KEY + RECEIVE_KEY); + + public static final String MSCHAP_V2_ID = "42"; + public static final int MSCHAP_V2_ID_INT = Integer.parseInt(MSCHAP_V2_ID, 16 /* radix */); + public static final byte[] EAP_MSCHAP_V2_CHALLENGE_RESPONSE = + hexStringToByteArray("02" + ID + "003F" // EAP-Response | ID | length in bytes + + "1A02" + MSCHAP_V2_ID // EAP-MSCHAPv2 | Response | MSCHAPv2 ID + + "003A31" // MS length | Value Size (0x31) + + MSCHAP_V2_PEER_CHALLENGE_STRING + + "0000000000000000" // 8B (reserved) + + MSCHAP_V2_NT_RESPONSE_STRING + + "00" // Flags (always 0) + + MSCHAP_V2_USERNAME_HEX); + + public static final byte[] EAP_MSCHAP_V2_SUCCESS_RESPONSE = + hexStringToByteArray("02" + ID + "0006" // EAP-Response | ID | length in bytes + + "1A03"); // EAP-MSCHAPv2 | Success + + public static final byte[] INVALID_AUTHENTICATOR_RESPONSE = new byte[20]; + + public static final byte[] EAP_MSCHAP_V2_FAILURE_RESPONSE = + hexStringToByteArray("02" + ID + "0006" // EAP-Response | ID | length in bytes + + "1A04"); // EAP-MSCHAPv2 | Failure + + public static final byte[] EAP_AKA_PRIME_REQUEST = + hexStringToByteArray("01" + ID + "000832050000"); + public static final byte[] EAP_AKA_PRIME_CLIENT_ERROR_UNABLE_TO_PROCESS = + hexStringToByteArray("02" + ID + "000C320E000016010000"); + public static final String EAP_AKA_PRIME_IDENTITY = "36313233343536373839303132333435"; + public static final byte[] EAP_AKA_PRIME_IDENTITY_BYTES = + hexStringToByteArray(EAP_AKA_PRIME_IDENTITY); + public static final byte[] EAP_AKA_PRIME_IDENTITY_RESPONSE = + hexStringToByteArray( + "02" + ID + "001C" // EAP-Response | ID | length in bytes + + "32050000" // EAP-AKA' | Identity | 2B padding + + "0E050010" + EAP_AKA_PRIME_IDENTITY); // AT_IDENTITY ("6" + IMSI) + public static final byte[] EAP_AKA_PRIME_AUTHENTICATION_REJECT = + hexStringToByteArray( + "02" + ID + "0008" // EAP-Response | ID | length in bytes + + "32020000"); // EAP-AKA' | Authentication Reject | 2B padding +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeRequestTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeRequestTest.java new file mode 100644 index 00000000..45a86735 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeRequestTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_REQUEST_LONG_MS_LENGTH; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_REQUEST_SHORT_CHALLENGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_REQUEST_SHORT_MS_LENGTH; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_REQUEST_WRONG_OP_CODE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_CHALLENGE_REQUEST; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ID_INT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SERVER_NAME_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_CHALLENGE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.EapResult.EapError; +import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2ChallengeRequest; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder.DecodeResult; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.BufferUnderflowException; + +public class EapMsChapV2ChallengeRequestTest { + private static final String TAG = EapMsChapV2ChallengeRequestTest.class.getSimpleName(); + + private EapMsChapV2TypeDataDecoder mTypeDataDecoder; + + @Before + public void setUp() { + mTypeDataDecoder = new EapMsChapV2TypeDataDecoder(); + } + + @Test + public void testDecodeChallengeRequest() { + DecodeResult<EapMsChapV2ChallengeRequest> result = + mTypeDataDecoder.decodeChallengeRequest(TAG, EAP_MSCHAP_V2_CHALLENGE_REQUEST); + assertTrue(result.isSuccessfulDecode()); + EapMsChapV2ChallengeRequest challengeRequest = result.eapTypeData; + + assertEquals(EAP_MSCHAP_V2_CHALLENGE, challengeRequest.opCode); + assertEquals(ID_INT, challengeRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_CHALLENGE_REQUEST.length, challengeRequest.msLength); + assertArrayEquals(CHALLENGE_BYTES, challengeRequest.challenge); + assertArrayEquals(SERVER_NAME_BYTES, challengeRequest.name); + } + + @Test + public void testDecodeChallengeRequestWrongOpCode() { + DecodeResult<EapMsChapV2ChallengeRequest> result = + mTypeDataDecoder.decodeChallengeRequest(TAG, CHALLENGE_REQUEST_WRONG_OP_CODE); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeChallengeRequestShortChallenge() { + DecodeResult<EapMsChapV2ChallengeRequest> result = + mTypeDataDecoder.decodeChallengeRequest(TAG, CHALLENGE_REQUEST_SHORT_CHALLENGE); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeChallengeRequestShortMsLength() { + DecodeResult<EapMsChapV2ChallengeRequest> result = + mTypeDataDecoder.decodeChallengeRequest(TAG, CHALLENGE_REQUEST_SHORT_MS_LENGTH); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeChallengeRequestLongMsLength() { + DecodeResult<EapMsChapV2ChallengeRequest> result = + mTypeDataDecoder.decodeChallengeRequest(TAG, CHALLENGE_REQUEST_LONG_MS_LENGTH); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof BufferUnderflowException); + } + + @Test + public void testEncodeChallengeRequestFails() throws Exception { + EapMsChapV2ChallengeRequest challengeRequest = + new EapMsChapV2ChallengeRequest( + ID_INT, + EAP_MSCHAP_V2_CHALLENGE_REQUEST.length, + CHALLENGE_BYTES, + SERVER_NAME_BYTES); + try { + challengeRequest.encode(); + fail("Expected UnsupportedOperationException for encoding a Challenge Request"); + } catch (UnsupportedOperationException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeResponseTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeResponseTest.java new file mode 100644 index 00000000..3e243a98 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeResponseTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_CHALLENGE_RESPONSE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ID_INT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.NT_RESPONSE_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.PEER_CHALLENGE_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.PEER_NAME_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SHORT_CHALLENGE_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SHORT_NT_RESPONSE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_RESPONSE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2ChallengeResponse; + +import org.junit.Test; + +public class EapMsChapV2ChallengeResponseTest { + private static final int FLAGS = 0; + private static final int INVALID_FLAGS = 0xFF; + + @Test + public void testConstructor() throws Exception { + EapMsChapV2ChallengeResponse challengeResponse = + new EapMsChapV2ChallengeResponse( + ID_INT, PEER_CHALLENGE_BYTES, NT_RESPONSE_BYTES, FLAGS, PEER_NAME_BYTES); + assertEquals(EAP_MSCHAP_V2_RESPONSE, challengeResponse.opCode); + assertEquals(ID_INT, challengeResponse.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_CHALLENGE_RESPONSE.length, challengeResponse.msLength); + assertArrayEquals(PEER_CHALLENGE_BYTES, challengeResponse.peerChallenge); + assertArrayEquals(NT_RESPONSE_BYTES, challengeResponse.ntResponse); + assertEquals(FLAGS, challengeResponse.flags); + assertArrayEquals(PEER_NAME_BYTES, challengeResponse.name); + } + + @Test + public void testConstructorInvalidChallenge() { + try { + EapMsChapV2ChallengeResponse challengeResponse = + new EapMsChapV2ChallengeResponse( + ID_INT, + SHORT_CHALLENGE_BYTES, + NT_RESPONSE_BYTES, + FLAGS, + PEER_NAME_BYTES); + fail("Expected EapMsChapV2ParsingException for invalid Peer Challenge length"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testConstructorInvalidNtResponse() { + try { + EapMsChapV2ChallengeResponse challengeResponse = + new EapMsChapV2ChallengeResponse( + ID_INT, + PEER_CHALLENGE_BYTES, + SHORT_NT_RESPONSE, + FLAGS, + PEER_NAME_BYTES); + fail("Expected EapMsChapV2ParsingException for invalid NT-Response length"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testConstructorInvalidFlags() { + try { + EapMsChapV2ChallengeResponse challengeResponse = + new EapMsChapV2ChallengeResponse( + ID_INT, + PEER_CHALLENGE_BYTES, + NT_RESPONSE_BYTES, + INVALID_FLAGS, + PEER_NAME_BYTES); + fail("Expected EapMsChapV2ParsingException for non-zero Flags value"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testEncode() throws Exception { + EapMsChapV2ChallengeResponse challengeResponse = + new EapMsChapV2ChallengeResponse( + ID_INT, PEER_CHALLENGE_BYTES, NT_RESPONSE_BYTES, FLAGS, PEER_NAME_BYTES); + byte[] encodedChallengeResponse = challengeResponse.encode(); + + assertArrayEquals(EAP_MSCHAP_V2_CHALLENGE_RESPONSE, encodedChallengeResponse); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureRequestTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureRequestTest.java new file mode 100644 index 00000000..ead8022a --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureRequestTest.java @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.CHALLENGE_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_FAILURE_REQUEST; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE_WITH_SPACE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ERROR_CODE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.FAILURE_REQUEST_EXTRA_ATTRIBUTE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.FAILURE_REQUEST_INVALID_CHALLENGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.FAILURE_REQUEST_INVALID_ERROR_CODE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.FAILURE_REQUEST_INVALID_PASSWORD_CHANGE_PROTOCOL; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.FAILURE_REQUEST_SHORT_CHALLENGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ID_INT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE_MISSING_TEXT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.PASSWORD_CHANGE_PROTOCOL; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.RETRY_BIT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_FAILURE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2FailureRequest; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder.DecodeResult; + +import org.junit.Before; +import org.junit.Test; + +public class EapMsChapV2FailureRequestTest { + private static final String TAG = EapMsChapV2FailureRequestTest.class.getSimpleName(); + + private EapMsChapV2TypeDataDecoder mTypeDataDecoder; + + @Before + public void setUp() { + mTypeDataDecoder = new EapMsChapV2TypeDataDecoder(); + } + + @Test + public void testDecodeFailureRequest() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest(TAG, EAP_MSCHAP_V2_FAILURE_REQUEST); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2FailureRequest failureRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_FAILURE, failureRequest.opCode); + assertEquals(ID_INT, failureRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_FAILURE_REQUEST.length, failureRequest.msLength); + assertEquals(ERROR_CODE, failureRequest.errorCode); + assertEquals(RETRY_BIT, failureRequest.isRetryable); + assertArrayEquals(CHALLENGE_BYTES, failureRequest.challenge); + assertEquals(PASSWORD_CHANGE_PROTOCOL, failureRequest.passwordChangeProtocol); + assertEquals(MESSAGE, failureRequest.message); + } + + @Test + public void testDecodeFailureRequestMissingMessage() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest( + TAG, EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2FailureRequest failureRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_FAILURE, failureRequest.opCode); + assertEquals(ID_INT, failureRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE.length, failureRequest.msLength); + assertEquals(ERROR_CODE, failureRequest.errorCode); + assertEquals(RETRY_BIT, failureRequest.isRetryable); + assertArrayEquals(CHALLENGE_BYTES, failureRequest.challenge); + assertEquals(PASSWORD_CHANGE_PROTOCOL, failureRequest.passwordChangeProtocol); + assertEquals(MESSAGE_MISSING_TEXT, failureRequest.message); + } + + @Test + public void testDecodeFailureRequestMissingMessageWithSpace() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest( + TAG, EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE_WITH_SPACE); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2FailureRequest failureRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_FAILURE, failureRequest.opCode); + assertEquals(ID_INT, failureRequest.msChapV2Id); + assertEquals( + EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE_WITH_SPACE.length, + failureRequest.msLength); + assertEquals(ERROR_CODE, failureRequest.errorCode); + assertEquals(RETRY_BIT, failureRequest.isRetryable); + assertArrayEquals(CHALLENGE_BYTES, failureRequest.challenge); + assertEquals(PASSWORD_CHANGE_PROTOCOL, failureRequest.passwordChangeProtocol); + assertEquals(MESSAGE_MISSING_TEXT, failureRequest.message); + } + + @Test + public void testDecodeFailureRequestInvalidErrorCode() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest(TAG, FAILURE_REQUEST_INVALID_ERROR_CODE); + assertTrue(!result.isSuccessfulDecode()); + assertTrue(result.eapError.cause instanceof NumberFormatException); + } + + @Test + public void testDecodeFailureRequestInvalidChallenge() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest(TAG, FAILURE_REQUEST_INVALID_CHALLENGE); + assertTrue(!result.isSuccessfulDecode()); + assertTrue(result.eapError.cause instanceof NumberFormatException); + } + + @Test + public void testDecodeFailureRequestShortChallenge() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest(TAG, FAILURE_REQUEST_SHORT_CHALLENGE); + assertTrue(!result.isSuccessfulDecode()); + assertTrue(result.eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeFailureRequestInvalidPasswordChangeProtocol() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest( + TAG, FAILURE_REQUEST_INVALID_PASSWORD_CHANGE_PROTOCOL); + assertTrue(!result.isSuccessfulDecode()); + assertTrue(result.eapError.cause instanceof NumberFormatException); + } + + @Test + public void testDecodeFailureExtraAttribute() { + DecodeResult<EapMsChapV2FailureRequest> result = + mTypeDataDecoder.decodeFailureRequest(TAG, FAILURE_REQUEST_EXTRA_ATTRIBUTE); + assertTrue(!result.isSuccessfulDecode()); + assertTrue(result.eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testEncodeFails() throws Exception { + EapMsChapV2FailureRequest failureRequest = + new EapMsChapV2FailureRequest( + ID_INT, + EAP_MSCHAP_V2_FAILURE_REQUEST.length, + ERROR_CODE, + RETRY_BIT, + CHALLENGE_BYTES, + PASSWORD_CHANGE_PROTOCOL, + MESSAGE); + try { + failureRequest.encode(); + fail("Expected UnsupportedOperationException for encoding a request"); + } catch (UnsupportedOperationException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureResponseTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureResponseTest.java new file mode 100644 index 00000000..261ddb28 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2FailureResponseTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_FAILURE_RESPONSE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_FAILURE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2FailureResponse; + +import org.junit.Test; + +public class EapMsChapV2FailureResponseTest { + @Test + public void testGetEapMsChapV2FailureResponse() { + EapMsChapV2FailureResponse failureResponse = + EapMsChapV2FailureResponse.getEapMsChapV2FailureResponse(); + assertEquals(EAP_MSCHAP_V2_FAILURE, failureResponse.opCode); + } + + @Test + public void testEncode() { + EapMsChapV2FailureResponse failureResponse = + EapMsChapV2FailureResponse.getEapMsChapV2FailureResponse(); + assertArrayEquals(EAP_MSCHAP_V2_FAILURE_RESPONSE, failureResponse.encode()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2PacketDefinitions.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2PacketDefinitions.java new file mode 100644 index 00000000..e70c2a2f --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2PacketDefinitions.java @@ -0,0 +1,284 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; + +public class EapMsChapV2PacketDefinitions { + public static final String ID = "1F"; + public static final int ID_INT = Integer.parseInt(ID, 16 /* radix */); + + public static final String CHALLENGE = "000102030405060708090A0B0C0D0E0F"; + public static final byte[] CHALLENGE_BYTES = hexStringToByteArray(CHALLENGE); + + // server name is the ASCII hex for "authenticator@android.net" + public static final String SERVER_NAME = "61757468656E74696361746F7240616E64726F69642E6E6574"; + public static final byte[] SERVER_NAME_BYTES = hexStringToByteArray(SERVER_NAME); + public static final byte[] EAP_MSCHAP_V2_CHALLENGE_REQUEST = + hexStringToByteArray("01" + ID + "002E10" + CHALLENGE + SERVER_NAME); + + public static final byte[] CHALLENGE_REQUEST_WRONG_OP_CODE = hexStringToByteArray("02"); + public static final String SHORT_CHALLENGE = "001122334455"; + public static final byte[] SHORT_CHALLENGE_BYTES = hexStringToByteArray(SHORT_CHALLENGE); + public static final byte[] CHALLENGE_REQUEST_SHORT_CHALLENGE = + hexStringToByteArray("01" + ID + "002406" + SHORT_CHALLENGE + SERVER_NAME); + public static final byte[] CHALLENGE_REQUEST_SHORT_MS_LENGTH = + hexStringToByteArray("01" + ID + "000110" + CHALLENGE + SERVER_NAME); + public static final byte[] CHALLENGE_REQUEST_LONG_MS_LENGTH = + hexStringToByteArray("01" + ID + "00FF10" + CHALLENGE + SERVER_NAME); + + public static final String PEER_CHALLENGE = "00112233445566778899AABBCCDDEEFF"; + public static final byte[] PEER_CHALLENGE_BYTES = hexStringToByteArray(PEER_CHALLENGE); + public static final String NT_RESPONSE = "FFEEDDCCBBAA998877665544332211000011223344556677"; + public static final byte[] NT_RESPONSE_BYTES = hexStringToByteArray(NT_RESPONSE); + + // peer name is the ASCII hex for "peer@android.net" + public static final String PEER_NAME = "7065657240616E64726F69642E6E6574"; + public static final byte[] PEER_NAME_BYTES = hexStringToByteArray(PEER_NAME); + public static final byte[] EAP_MSCHAP_V2_CHALLENGE_RESPONSE = + hexStringToByteArray( + "02" + + ID + + "004631" + + PEER_CHALLENGE + + "0000000000000000" + + NT_RESPONSE + + "00" + + PEER_NAME); + + public static final byte[] SHORT_NT_RESPONSE = hexStringToByteArray("0011223344"); + + public static final String AUTH_STRING = "00112233445566778899AABBCCDDEEFF00112233"; + + // ASCII hex for AUTH_STRING + public static final String AUTH_STRING_HEX = + "30303131323233333434353536363737383839394141424243434444454546463030313132323333"; + public static final byte[] AUTH_BYTES = hexStringToByteArray(AUTH_STRING); + + // hex("S=") + AUTH_STRING_HEX + public static final String FORMATTED_AUTH_STRING = "533D" + AUTH_STRING_HEX; + + public static final String SPACE_HEX = "20"; + + // ASCII hex for: "test Android 1234" + public static final String MESSAGE = "test Android 1234"; + public static final String MESSAGE_HEX = "7465737420416E64726F69642031323334"; + + // hex("M=") + MESSAGE_HEX + public static final String FORMATTED_MESSAGE = "4D3D" + MESSAGE_HEX; + + public static final byte[] EAP_MSCHAP_V2_SUCCESS_REQUEST = + hexStringToByteArray( + "03" + ID + "0042" + FORMATTED_AUTH_STRING + SPACE_HEX + FORMATTED_MESSAGE); + public static final byte[] EAP_MSCHAP_V2_SUCCESS_REQUEST_EMPTY_MESSAGE = + hexStringToByteArray("03" + ID + "0031" + FORMATTED_AUTH_STRING + SPACE_HEX + "4D3D"); + public static final byte[] EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE = + hexStringToByteArray("03" + ID + "002E" + FORMATTED_AUTH_STRING); + public static final byte[] EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE_WITH_SPACE = + hexStringToByteArray("03" + ID + "002F" + FORMATTED_AUTH_STRING + SPACE_HEX); + public static final String MESSAGE_MISSING_TEXT = "<omitted by authenticator>"; + + public static final String SHORT_AUTH_STRING = "001122334455"; + + public static final byte[] SUCCESS_REQUEST_WRONG_OP_CODE = hexStringToByteArray("02"); + + // message format: hex("M=") + AUTH_STRING_HEX + hex("M=") + MESSAGE_HEX + public static final byte[] SUCCESS_REQUEST_WRONG_PREFIX = + hexStringToByteArray("03" + ID + "00314D3D" + AUTH_STRING_HEX + SPACE_HEX + "4D3D"); + + // message format: hex("S=") + SHORT_AUTH_STRING + hex("M=") + MESSAGE_HEX + public static final byte[] SUCCESS_REQUEST_SHORT_AUTH_STRING = + hexStringToByteArray("03" + ID + "0031533D" + SHORT_AUTH_STRING + SPACE_HEX + "4D3D"); + + public static final String INVALID_AUTH_HEX = + "3030313132323333343435353636373738383939414142424343444445454646303031317A7A7979"; + public static final byte[] SUCCESS_REQUEST_INVALID_AUTH_STRING = + hexStringToByteArray("03" + ID + "0031533D" + INVALID_AUTH_HEX + SPACE_HEX + "4D3D"); + + // extra key-value: hex("N=12") + public static final String EXTRA_KEY = "4E3D3132"; + public static final byte[] SUCCESS_REQUEST_EXTRA_ATTRIBUTE = + hexStringToByteArray( + "03" + + ID + + "0042" + + FORMATTED_AUTH_STRING + + SPACE_HEX + + EXTRA_KEY + + SPACE_HEX + + FORMATTED_MESSAGE); + + public static final String SUCCESS_REQUEST = "S=" + AUTH_STRING + " M=" + MESSAGE; + public static final String EXTRA_M_MESSAGE = "M=" + MESSAGE; + public static final String SUCCESS_REQUEST_EXTRA_M = + "S=" + AUTH_STRING + " M=" + EXTRA_M_MESSAGE; + public static final String SUCCESS_REQUEST_MISSING_M = "S=" + AUTH_STRING; + public static final String SUCCESS_REQUEST_INVALID_FORMAT = + "S==" + AUTH_STRING + "M=" + MESSAGE; + public static final String SUCCESS_REQUEST_DUPLICATE_KEY = + "S=" + AUTH_STRING + " S=" + AUTH_STRING + " M=" + MESSAGE; + + public static final byte[] EAP_MSCHAP_V2_SUCCESS_RESPONSE = hexStringToByteArray("03"); + + public static final int ERROR_CODE = 647; // account disabled + + // formatted error code: hex("E=" + ERROR_CODE) + public static final String FORMATTED_ERROR_CODE = "453D363437"; + public static final boolean RETRY_BIT = true; + + // formatted retry bit: hex("R=1") + public static final String FORMATTED_RETRY_BIT = "523D31"; + + // challenge hex: hex(CHALLENGE) + public static final String CHALLENGE_HEX = + "3030303130323033303430353036303730383039304130423043304430453046"; + + // formatted challenge: hex("C=") + CHALLENGE_HEX + public static final String FORMATTED_CHALLENGE = "433D" + CHALLENGE_HEX; + + public static final int PASSWORD_CHANGE_PROTOCOL = 3; + + // formatted password change protocol: hex("V=3") + public static final String FORMATTED_PASSWORD_CHANGE_PROTOCOL = "563D33"; + + public static final byte[] EAP_MSCHAP_V2_FAILURE_REQUEST = + hexStringToByteArray( + "04" + + ID + + "0048" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + FORMATTED_MESSAGE); + public static final byte[] EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE = + hexStringToByteArray( + "04" + + ID + + "0034" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL); + public static final byte[] EAP_MSCHAP_V2_FAILURE_REQUEST_MISSING_MESSAGE_WITH_SPACE = + hexStringToByteArray( + "04" + + ID + + "0035" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX); + + // invalid error code: hex("E=abc") + public static final String INVALID_ERROR_CODE = "453D616263"; + public static final byte[] FAILURE_REQUEST_INVALID_ERROR_CODE = + hexStringToByteArray( + "04" + + ID + + "0048" + + INVALID_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + FORMATTED_MESSAGE); + + // invalid challenge: hex("C=zyxd") + public static final String INVALID_CHALLENGE = "433D7A797864"; + public static final byte[] FAILURE_REQUEST_INVALID_CHALLENGE = + hexStringToByteArray( + "04" + + ID + + "0032" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + INVALID_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + FORMATTED_MESSAGE); + + // short challenge: hex("C=" + SHORT_CHALLENGE) + public static final String FORMATTED_SHORT_CHALLENGE = "433D303031313232333334343535"; + public static final byte[] FAILURE_REQUEST_SHORT_CHALLENGE = + hexStringToByteArray( + "04" + + ID + + "0034" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_SHORT_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + FORMATTED_MESSAGE); + + // invalid password change protocol: hex("V=d") + public static final String INVALID_PASSWORD_CHANGE_PROTOCOL = "563D64"; + public static final byte[] FAILURE_REQUEST_INVALID_PASSWORD_CHANGE_PROTOCOL = + hexStringToByteArray( + "04" + + ID + + "0048" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + INVALID_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + FORMATTED_MESSAGE); + + public static final byte[] FAILURE_REQUEST_EXTRA_ATTRIBUTE = + hexStringToByteArray( + "04" + + ID + + "0048" + + FORMATTED_ERROR_CODE + + SPACE_HEX + + FORMATTED_RETRY_BIT + + SPACE_HEX + + FORMATTED_CHALLENGE + + SPACE_HEX + + FORMATTED_PASSWORD_CHANGE_PROTOCOL + + SPACE_HEX + + EXTRA_KEY + + SPACE_HEX + + FORMATTED_MESSAGE); + + public static final byte[] EAP_MSCHAP_V2_FAILURE_RESPONSE = hexStringToByteArray("04"); +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessRequestTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessRequestTest.java new file mode 100644 index 00000000..1e0909ed --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessRequestTest.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.AUTH_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_SUCCESS_REQUEST; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_SUCCESS_REQUEST_EMPTY_MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE_WITH_SPACE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ID_INT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE_MISSING_TEXT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_EXTRA_ATTRIBUTE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_INVALID_AUTH_STRING; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_SHORT_AUTH_STRING; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_WRONG_OP_CODE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_WRONG_PREFIX; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_SUCCESS; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.EapResult.EapError; +import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2SuccessRequest; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder.DecodeResult; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.BufferUnderflowException; + +public class EapMsChapV2SuccessRequestTest { + private static final String TAG = EapMsChapV2SuccessRequestTest.class.getSimpleName(); + + private EapMsChapV2TypeDataDecoder mTypeDataDecoder; + + @Before + public void setUp() { + mTypeDataDecoder = new EapMsChapV2TypeDataDecoder(); + } + + @Test + public void testDecodeSuccessRequest() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, EAP_MSCHAP_V2_SUCCESS_REQUEST); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2SuccessRequest successRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_SUCCESS, successRequest.opCode); + assertEquals(ID_INT, successRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_SUCCESS_REQUEST.length, successRequest.msLength); + assertArrayEquals(AUTH_BYTES, successRequest.authBytes); + assertEquals(MESSAGE, successRequest.message); + } + + @Test + public void testDecodeSuccessRequestEmptyMessage() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest( + TAG, EAP_MSCHAP_V2_SUCCESS_REQUEST_EMPTY_MESSAGE); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2SuccessRequest successRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_SUCCESS, successRequest.opCode); + assertEquals(ID_INT, successRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_SUCCESS_REQUEST_EMPTY_MESSAGE.length, successRequest.msLength); + assertArrayEquals(AUTH_BYTES, successRequest.authBytes); + assertTrue(successRequest.message.isEmpty()); + } + + @Test + public void testDecodeSuccessRequestMissingMessage() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest( + TAG, EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2SuccessRequest successRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_SUCCESS, successRequest.opCode); + assertEquals(ID_INT, successRequest.msChapV2Id); + assertEquals(EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE.length, successRequest.msLength); + assertArrayEquals(AUTH_BYTES, successRequest.authBytes); + assertEquals(MESSAGE_MISSING_TEXT, successRequest.message); + } + + @Test + public void testDecodeSuccessRequestMissingMessageWithSpace() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest( + TAG, EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE_WITH_SPACE); + assertTrue(result.isSuccessfulDecode()); + + EapMsChapV2SuccessRequest successRequest = result.eapTypeData; + assertEquals(EAP_MSCHAP_V2_SUCCESS, successRequest.opCode); + assertEquals(ID_INT, successRequest.msChapV2Id); + assertEquals( + EAP_MSCHAP_V2_SUCCESS_REQUEST_MISSING_MESSAGE_WITH_SPACE.length, + successRequest.msLength); + assertArrayEquals(AUTH_BYTES, successRequest.authBytes); + assertEquals(MESSAGE_MISSING_TEXT, successRequest.message); + } + + @Test + public void testDecodeSuccessRequestWrongOpCode() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, SUCCESS_REQUEST_WRONG_OP_CODE); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeSuccessRequestShortMessage() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, new byte[0]); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof BufferUnderflowException); + } + + @Test + public void testDecodeSuccessRequestInvalidPrefix() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, SUCCESS_REQUEST_WRONG_PREFIX); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeSuccessRequestShortAuthString() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, SUCCESS_REQUEST_SHORT_AUTH_STRING); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testDecodeSuccessRequestInvalidAuthString() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, SUCCESS_REQUEST_INVALID_AUTH_STRING); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof NumberFormatException); + } + + @Test + public void testDecodeSuccessRequestExtraAttribute() { + DecodeResult<EapMsChapV2SuccessRequest> result = + mTypeDataDecoder.decodeSuccessRequest(TAG, SUCCESS_REQUEST_EXTRA_ATTRIBUTE); + assertFalse(result.isSuccessfulDecode()); + EapError eapError = result.eapError; + assertTrue(eapError.cause instanceof EapMsChapV2ParsingException); + } + + @Test + public void testEncodeFails() throws Exception { + EapMsChapV2SuccessRequest successRequest = + new EapMsChapV2SuccessRequest( + ID_INT, EAP_MSCHAP_V2_SUCCESS_REQUEST.length, AUTH_BYTES, MESSAGE); + try { + successRequest.encode(); + fail("Expected UnsupportedOperationException for encoding a request"); + } catch (UnsupportedOperationException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessResponseTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessResponseTest.java new file mode 100644 index 00000000..524b3ecf --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2SuccessResponseTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_SUCCESS_RESPONSE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_SUCCESS; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2SuccessResponse; + +import org.junit.Test; + +public class EapMsChapV2SuccessResponseTest { + @Test + public void testGetEapMsChapV2SuccessResponse() { + EapMsChapV2SuccessResponse successResponse = + EapMsChapV2SuccessResponse.getEapMsChapV2SuccessResponse(); + assertEquals(EAP_MSCHAP_V2_SUCCESS, successResponse.opCode); + } + + @Test + public void testEncode() { + EapMsChapV2SuccessResponse successResponse = + EapMsChapV2SuccessResponse.getEapMsChapV2SuccessResponse(); + assertArrayEquals(EAP_MSCHAP_V2_SUCCESS_RESPONSE, successResponse.encode()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2TypeDataTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2TypeDataTest.java new file mode 100644 index 00000000..45f1c641 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2TypeDataTest.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.mschapv2; + +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.AUTH_BYTES; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.AUTH_STRING; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EXTRA_M_MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.MESSAGE_MISSING_TEXT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_DUPLICATE_KEY; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_EXTRA_M; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_INVALID_FORMAT; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SUCCESS_REQUEST_MISSING_M; +import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_CHALLENGE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.EapResult.EapError; +import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2TypeDataDecoder.DecodeResult; +import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2VariableTypeData; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class EapMsChapV2TypeDataTest { + private static final int INVALID_OPCODE = -1; + private static final int MSCHAP_V2_ID = 1; + private static final int MS_LENGTH = 32; + private static final String HEX_STRING_INVALID_LENGTH = "00112"; + private static final String HEX_STRING_INVALID_CHARS = "001122z-+x"; + + @Test + public void testEapMsChapV2TypeDataConstructor() throws Exception { + EapMsChapV2TypeData typeData = new EapMsChapV2TypeData(EAP_MSCHAP_V2_CHALLENGE) {}; + assertEquals(EAP_MSCHAP_V2_CHALLENGE, typeData.opCode); + + try { + new EapMsChapV2TypeData(INVALID_OPCODE) {}; + fail("ExpectedEapMsChapV2ParsingException for invalid OpCode"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testEapMsChapV2VariableTypeDataConstructor() throws Exception { + EapMsChapV2VariableTypeData typeData = + new EapMsChapV2VariableTypeData( + EAP_MSCHAP_V2_CHALLENGE, MSCHAP_V2_ID, MS_LENGTH) {}; + assertEquals(EAP_MSCHAP_V2_CHALLENGE, typeData.opCode); + assertEquals(MSCHAP_V2_ID, typeData.msChapV2Id); + assertEquals(MS_LENGTH, typeData.msLength); + + try { + new EapMsChapV2VariableTypeData(INVALID_OPCODE, MSCHAP_V2_ID, MS_LENGTH) {}; + fail("ExpectedEapMsChapV2ParsingException for invalid OpCode"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testDecodeResultIsSuccessfulDecode() throws Exception { + DecodeResult<EapMsChapV2TypeData> result = + new DecodeResult(new EapMsChapV2TypeData(EAP_MSCHAP_V2_CHALLENGE) {}); + assertTrue(result.isSuccessfulDecode()); + + result = new DecodeResult(new EapError(new Exception())); + assertFalse(result.isSuccessfulDecode()); + } + + @Test + public void testGetMessageMappings() throws Exception { + Map<String, String> expectedMappings = new HashMap<>(); + expectedMappings.put("S", AUTH_STRING); + expectedMappings.put("M", MESSAGE); + assertEquals(expectedMappings, EapMsChapV2TypeData.getMessageMappings(SUCCESS_REQUEST)); + + expectedMappings = new HashMap<>(); + expectedMappings.put("S", AUTH_STRING); + expectedMappings.put("M", EXTRA_M_MESSAGE); + assertEquals( + expectedMappings, EapMsChapV2TypeData.getMessageMappings(SUCCESS_REQUEST_EXTRA_M)); + + expectedMappings = new HashMap<>(); + expectedMappings.put("S", AUTH_STRING); + expectedMappings.put("M", MESSAGE_MISSING_TEXT); + assertEquals( + expectedMappings, + EapMsChapV2TypeData.getMessageMappings(SUCCESS_REQUEST_MISSING_M)); + } + + @Test + public void testGetMessageMappingsInvalidFormat() { + try { + EapMsChapV2TypeData.getMessageMappings(SUCCESS_REQUEST_INVALID_FORMAT); + fail("Expected EapMsChapV2ParsingException for extra '='s in message"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testGetMessageMappingDuplicateKey() { + try { + EapMsChapV2TypeData.getMessageMappings(SUCCESS_REQUEST_DUPLICATE_KEY); + fail("Expected EapMsChapV2ParsingException for duplicate key in message"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testHexStringToByteArray() throws Exception { + byte[] result = EapMsChapV2TypeData.hexStringToByteArray(AUTH_STRING); + assertArrayEquals(AUTH_BYTES, result); + } + + @Test + public void testHexStringToByteArrayInvalidLength() { + try { + EapMsChapV2TypeData.hexStringToByteArray(HEX_STRING_INVALID_LENGTH); + fail("Expected EapMsChapV2ParsingException for invalid hex string length"); + } catch (EapMsChapV2ParsingException expected) { + } + } + + @Test + public void testHexStringToByteArrayInvalidChars() throws Exception { + try { + EapMsChapV2TypeData.hexStringToByteArray(HEX_STRING_INVALID_CHARS); + fail("Expected NumberFormatException for invalid hex chars"); + } catch (NumberFormatException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaPrimeTypeDataTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaPrimeTypeDataTest.java new file mode 100644 index 00000000..6a9e517f --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaPrimeTypeDataTest.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.simaka.EapAkaTypeData.EAP_AKA_CHALLENGE; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_AUTN; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_KDF; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_KDF_INPUT; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_MAC; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RAND; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_KDF_INPUT; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.KDF_VERSION; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NETWORK_NAME_BYTES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NETWORK_NAME_HEX; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.android.internal.net.eap.message.simaka.EapAkaPrimeTypeData.EapAkaPrimeTypeDataDecoder; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAutn; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtClientErrorCode; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtKdf; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtKdfInput; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtMac; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRandAka; +import com.android.internal.net.eap.message.simaka.EapSimAkaTypeData.DecodeResult; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map.Entry; + +public class EapAkaPrimeTypeDataTest { + private static final String RAND = "7A1FCDC0034BA1227E7B9FCEAFD47D53"; + private static final byte[] RAND_BYTES = hexStringToByteArray(RAND); + private static final String AUTN = "000102030405060708090A0B0C0D0E0F"; + private static final byte[] AUTN_BYTES = hexStringToByteArray(AUTN); + private static final String MAC = "95FEB9E70427F34B4FAC8F2C7A65A302"; + private static final byte[] MAC_BYTES = hexStringToByteArray(MAC); + private static final byte[] EAP_AKA_PRIME_CHALLENGE_REQUEST = + hexStringToByteArray( + "010000" // Challenge | 2B padding + + "01050000" + RAND // AT_RAND attribute + + "02050000" + AUTN // AT_AUTN attribute + + "1704000B" + NETWORK_NAME_HEX + "00" // AT_KDF_INPUT + + "18010001" // AT_KDF + + "0B050000" + MAC); // AT_MAC attribute + private static final byte[] EAP_AKA_PRIME_MULTIPLE_AT_KDF = + hexStringToByteArray( + "010000" // Challenge | 2B padding + + "01050000" + RAND // AT_RAND attribute + + "02050000" + AUTN // AT_AUTN attribute + + "1704000B" + NETWORK_NAME_HEX + "00" // AT_KDF_INPUT + + "18010001" // AT_KDF + + "18010002" // AT_KDF + + "0B050000" + MAC); // AT_MAC attribute + + private EapAkaPrimeTypeDataDecoder mTypeDataDecoder; + + @Before + public void setUp() { + mTypeDataDecoder = EapAkaPrimeTypeData.getEapAkaPrimeTypeDataDecoder(); + } + + @Test + public void testDecode() { + DecodeResult<EapAkaTypeData> result = + mTypeDataDecoder.decode(EAP_AKA_PRIME_CHALLENGE_REQUEST); + + assertTrue(result.isSuccessfulDecode()); + EapAkaPrimeTypeData eapAkaPrimeTypeData = (EapAkaPrimeTypeData) result.eapTypeData; + assertEquals(EAP_AKA_CHALLENGE, eapAkaPrimeTypeData.eapSubtype); + + // also check Map entries (needs to match input order) + Iterator<Entry<Integer, EapSimAkaAttribute>> itr = + eapAkaPrimeTypeData.attributeMap.entrySet().iterator(); + Entry<Integer, EapSimAkaAttribute> entry = itr.next(); + assertEquals(EAP_AT_RAND, (int) entry.getKey()); + assertArrayEquals(RAND_BYTES, ((AtRandAka) entry.getValue()).rand); + + entry = itr.next(); + assertEquals(EAP_AT_AUTN, (int) entry.getKey()); + assertArrayEquals(AUTN_BYTES, ((AtAutn) entry.getValue()).autn); + + entry = itr.next(); + assertEquals(EAP_AT_KDF_INPUT, (int) entry.getKey()); + assertArrayEquals(NETWORK_NAME_BYTES, ((AtKdfInput) entry.getValue()).networkName); + + entry = itr.next(); + assertEquals(EAP_AT_KDF, (int) entry.getKey()); + assertEquals(KDF_VERSION, ((AtKdf) entry.getValue()).kdf); + + entry = itr.next(); + assertEquals(EAP_AT_MAC, (int) entry.getKey()); + assertArrayEquals(MAC_BYTES, ((AtMac) entry.getValue()).mac); + + assertFalse(itr.hasNext()); + } + + @Test + public void testDecodeMultipleAtKdfAttributes() { + DecodeResult<EapAkaTypeData> result = + mTypeDataDecoder.decode(EAP_AKA_PRIME_MULTIPLE_AT_KDF); + + assertFalse(result.isSuccessfulDecode()); + assertEquals(AtClientErrorCode.UNABLE_TO_PROCESS, result.atClientErrorCode); + } + + @Test + public void testEncode() throws Exception { + LinkedHashMap<Integer, EapSimAkaAttribute> attributes = new LinkedHashMap<>(); + attributes.put(EAP_AT_RAND, new AtRandAka(RAND_BYTES)); + attributes.put(EAP_AT_AUTN, new AtAutn(AUTN_BYTES)); + attributes.put(EAP_AT_KDF_INPUT, new AtKdfInput(AT_KDF_INPUT.length, NETWORK_NAME_BYTES)); + attributes.put(EAP_AT_KDF, new AtKdf(KDF_VERSION)); + attributes.put(EAP_AT_MAC, new AtMac(MAC_BYTES)); + EapAkaPrimeTypeData eapAkaPrimeTypeData = + new EapAkaPrimeTypeData(EAP_AKA_CHALLENGE, attributes); + + byte[] result = eapAkaPrimeTypeData.encode(); + assertArrayEquals(EAP_AKA_PRIME_CHALLENGE_REQUEST, result); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaTypeDataTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaTypeDataTest.java new file mode 100644 index 00000000..b2d89d16 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapAkaTypeDataTest.java @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_AKA_CHALLENGE_RESPONSE_MAC_BYTES; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_AKA_CHALLENGE_RESPONSE_TYPE_DATA; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_AKA_IDENTITY_REQUEST; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INVALID_SUBTYPE; +import static com.android.internal.net.eap.message.simaka.EapAkaTypeData.EAP_AKA_CHALLENGE; +import static com.android.internal.net.eap.message.simaka.EapAkaTypeData.EAP_AKA_IDENTITY; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_ANY_ID_REQ; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_AUTN; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_CHECKCODE; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_MAC; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RAND; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RES_BYTES; + +import static junit.framework.TestCase.fail; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.android.internal.net.eap.message.simaka.EapAkaTypeData.EapAkaTypeDataDecoder; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAnyIdReq; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAutn; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtMac; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRandAka; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRes; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EapSimAkaUnsupportedAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaTypeData.DecodeResult; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map.Entry; + +public class EapAkaTypeDataTest { + private static final int UNABLE_TO_PROCESS_CODE = 0; + private static final int INVALID_SUBTYPE_INT = -1; + + private static final int EAP_AT_TRUST_IND = 139; + private static final String RAND = "7A1FCDC0034BA1227E7B9FCEAFD47D53"; + private static final byte[] RAND_BYTES = hexStringToByteArray(RAND); + private static final String AUTN = "000102030405060708090A0B0C0D0E0F"; + private static final byte[] AUTN_BYTES = hexStringToByteArray(AUTN); + private static final String MAC = "95FEB9E70427F34B4FAC8F2C7A65A302"; + private static final byte[] MAC_BYTES = hexStringToByteArray(MAC); + private static final byte[] EAP_AKA_REQUEST = + hexStringToByteArray( + "010000" // Challenge | 2B padding + + "01050000" + RAND // AT_RAND attribute + + "02050000" + AUTN // AT_AUTN attribute + + "8B010002" // AT_RESULT_IND attribute (TS 124 302#8.2.3.1) + + "0B050000" + MAC // AT_MAC attribute + + "86010000"); // AT_CHECKCODE attribute + + private EapAkaTypeDataDecoder mEapAkaTypeDataDecoder; + + @Before + public void setUp() { + mEapAkaTypeDataDecoder = EapAkaTypeData.getEapAkaTypeDataDecoder(); + } + + @Test + public void testDecode() { + DecodeResult<EapAkaTypeData> result = + mEapAkaTypeDataDecoder.decode(EAP_AKA_CHALLENGE_RESPONSE_TYPE_DATA); + + assertTrue(result.isSuccessfulDecode()); + EapAkaTypeData eapAkaTypeData = result.eapTypeData; + assertEquals(EAP_AKA_CHALLENGE, eapAkaTypeData.eapSubtype); + + // also check Map entries (needs to match input order) + Iterator<Entry<Integer, EapSimAkaAttribute>> itr = + eapAkaTypeData.attributeMap.entrySet().iterator(); + Entry<Integer, EapSimAkaAttribute> entry = itr.next(); + assertEquals(EAP_AT_RES, (int) entry.getKey()); + assertArrayEquals(RES_BYTES, ((AtRes) entry.getValue()).res); + + entry = itr.next(); + assertEquals(EAP_AT_MAC, (int) entry.getKey()); + assertArrayEquals(EAP_AKA_CHALLENGE_RESPONSE_MAC_BYTES, ((AtMac) entry.getValue()).mac); + + assertFalse(itr.hasNext()); + } + + @Test + public void testDecodeWithOptionalAttributes() { + DecodeResult<EapAkaTypeData> result = mEapAkaTypeDataDecoder.decode(EAP_AKA_REQUEST); + + assertTrue(result.isSuccessfulDecode()); + EapAkaTypeData eapAkaTypeData = result.eapTypeData; + assertEquals(EAP_AKA_CHALLENGE, eapAkaTypeData.eapSubtype); + + // also check Map entries (needs to match input order) + Iterator<Entry<Integer, EapSimAkaAttribute>> itr = + eapAkaTypeData.attributeMap.entrySet().iterator(); + Entry<Integer, EapSimAkaAttribute> entry = itr.next(); + assertEquals(EAP_AT_RAND, (int) entry.getKey()); + assertArrayEquals(RAND_BYTES, ((AtRandAka) entry.getValue()).rand); + + entry = itr.next(); + assertEquals(EAP_AT_AUTN, (int) entry.getKey()); + assertArrayEquals(AUTN_BYTES, ((AtAutn) entry.getValue()).autn); + + entry = itr.next(); + assertEquals(EAP_AT_TRUST_IND, (int) entry.getKey()); + assertTrue(entry.getValue() instanceof EapSimAkaUnsupportedAttribute); + + entry = itr.next(); + assertEquals(EAP_AT_MAC, (int) entry.getKey()); + assertArrayEquals(MAC_BYTES, ((AtMac) entry.getValue()).mac); + + entry = itr.next(); + assertEquals(EAP_AT_CHECKCODE, (int) entry.getKey()); + assertTrue(entry.getValue() instanceof EapSimAkaUnsupportedAttribute); + + assertFalse(itr.hasNext()); + } + + @Test + public void testDecodeInvalidSubtype() { + DecodeResult<EapAkaTypeData> result = mEapAkaTypeDataDecoder.decode(INVALID_SUBTYPE); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testEncode() throws Exception { + LinkedHashMap<Integer, EapSimAkaAttribute> attributes = new LinkedHashMap<>(); + attributes.put(EAP_AT_ANY_ID_REQ, new AtAnyIdReq()); + EapAkaTypeData eapAkaTypeData = new EapAkaTypeData(EAP_AKA_IDENTITY, attributes); + + byte[] result = eapAkaTypeData.encode(); + assertArrayEquals(EAP_AKA_IDENTITY_REQUEST, result); + } + + @Test + public void testConstructorInvalidSubtype() throws Exception { + try { + new EapAkaTypeData(INVALID_SUBTYPE_INT, Arrays.asList(new AtAnyIdReq())); + fail("Expected IllegalArgumentException for invalid subtype"); + } catch (IllegalArgumentException expected) { + } + } + + @Test + public void testConstructorDuplicateAttributes() throws Exception { + try { + new EapAkaTypeData(EAP_AKA_IDENTITY, Arrays.asList(new AtAnyIdReq(), new AtAnyIdReq())); + fail("Expected IllegalArgumentException for duplicate attributes"); + } catch (IllegalArgumentException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimAkaAttributeFactoryTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimAkaAttributeFactoryTest.java new file mode 100644 index 00000000..5b6f5d60 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimAkaAttributeFactoryTest.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SKIPPABLE_DATA; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SKIPPABLE_DATA_BYTES; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SKIPPABLE_INVALID_ATTRIBUTE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaUnsupportedAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EapSimAkaUnsupportedAttribute; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class EapSimAkaAttributeFactoryTest { + private static final int SKIPPABLE_ATTRIBUTE_TYPE = 0xFF; + private static final int SKIPPABLE_EXPECTED_LENGTH = 8; + + private static final int NON_SKIPPABLE_ATTRIBUTE_TYPE = 0x7F; + private static final int NON_SKIPPABLE_ATTRIBUTE_LENGTH = 4; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecodeInvalidSkippable() throws Exception { + ByteBuffer byteBuffer = ByteBuffer.wrap(SKIPPABLE_DATA_BYTES); + + EapSimAkaAttribute result = mAttributeFactory.getAttribute( + SKIPPABLE_ATTRIBUTE_TYPE, + SKIPPABLE_EXPECTED_LENGTH, + byteBuffer); + assertTrue(result instanceof EapSimAkaUnsupportedAttribute); + EapSimAkaUnsupportedAttribute unsupportedAttribute = (EapSimAkaUnsupportedAttribute) result; + assertEquals(SKIPPABLE_ATTRIBUTE_TYPE, unsupportedAttribute.attributeType); + assertEquals(SKIPPABLE_EXPECTED_LENGTH, unsupportedAttribute.lengthInBytes); + assertArrayEquals(hexStringToByteArray(SKIPPABLE_DATA), unsupportedAttribute.data); + } + + @Test + public void testEncodeInvalidSkippable() throws Exception { + EapSimAkaUnsupportedAttribute unsupportedAttribute = new EapSimAkaUnsupportedAttribute( + SKIPPABLE_ATTRIBUTE_TYPE, + SKIPPABLE_EXPECTED_LENGTH, + hexStringToByteArray(SKIPPABLE_DATA)); + + ByteBuffer result = ByteBuffer.allocate(SKIPPABLE_EXPECTED_LENGTH); + unsupportedAttribute.encode(result); + assertArrayEquals(SKIPPABLE_INVALID_ATTRIBUTE, result.array()); + } + + @Test + public void testDecodeInvalidNonSkippable() throws Exception { + // Unskippable type + length + byte[] represent shortest legitimate attribute: "7F040000" + ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[2]); + + try { + mAttributeFactory.getAttribute( + NON_SKIPPABLE_ATTRIBUTE_TYPE, + NON_SKIPPABLE_ATTRIBUTE_LENGTH, + byteBuffer); + fail("Expected EapSimAkaUnsupportedAttributeException for decoding invalid" + + " non-skippable Attribute"); + } catch (EapSimAkaUnsupportedAttributeException expected) { + } + } + +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimTypeDataTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimTypeDataTest.java new file mode 100644 index 00000000..678a812b --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/EapSimTypeDataTest.java @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka; + +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_SIM_START_DUPLICATE_ATTRIBUTES; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_SIM_START_SUBTYPE; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INVALID_SUBTYPE; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SHORT_TYPE_DATA; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.TYPE_DATA_INVALID_ATTRIBUTE; +import static com.android.internal.net.eap.message.EapTestMessageDefinitions.TYPE_DATA_INVALID_AT_RAND; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_PERMANENT_ID_REQ; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_VERSION_LIST; + +import static junit.framework.TestCase.fail; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtPermanentIdReq; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtVersionList; +import com.android.internal.net.eap.message.simaka.EapSimAkaTypeData.DecodeResult; +import com.android.internal.net.eap.message.simaka.EapSimTypeData.EapSimTypeDataDecoder; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; + +public class EapSimTypeDataTest { + private static final int UNABLE_TO_PROCESS_CODE = 0; + private static final int INSUFFICIENT_CHALLENGES_CODE = 2; + private static final int EAP_SIM_START = 10; + private static final int INVALID_SUBTYPE_INT = -1; + + private EapSimTypeDataDecoder mEapSimTypeDataDecoder; + + @Before + public void setUp() { + mEapSimTypeDataDecoder = EapSimTypeData.getEapSimTypeDataDecoder(); + } + + @Test + public void testConstructor() throws Exception { + List<EapSimAkaAttribute> attributes = Arrays.asList( + new AtVersionList(8, 1), new AtPermanentIdReq()); + + EapSimTypeData eapSimTypeData = new EapSimTypeData(EAP_SIM_START, attributes); + assertEquals(EAP_SIM_START, eapSimTypeData.eapSubtype); + + // check order of entries in EapSimTypeData.attributeMap + Iterator<Entry<Integer, EapSimAkaAttribute>> itr = + eapSimTypeData.attributeMap.entrySet().iterator(); + Entry<Integer, EapSimAkaAttribute> pair = itr.next(); + assertEquals(EAP_AT_VERSION_LIST, (int) pair.getKey()); + assertEquals(Arrays.asList(1), ((AtVersionList) pair.getValue()).versions); + + pair = itr.next(); + assertEquals(EAP_AT_PERMANENT_ID_REQ, (int) pair.getKey()); + assertTrue(pair.getValue() instanceof AtPermanentIdReq); + } + + @Test + public void testDecode() { + DecodeResult<EapSimTypeData> result = mEapSimTypeDataDecoder.decode(EAP_SIM_START_SUBTYPE); + + assertTrue(result.isSuccessfulDecode()); + EapSimTypeData eapSimTypeData = result.eapTypeData; + assertEquals(EAP_SIM_START, eapSimTypeData.eapSubtype); + assertTrue(eapSimTypeData.attributeMap.containsKey(EAP_AT_VERSION_LIST)); + AtVersionList atVersionList = (AtVersionList) + eapSimTypeData.attributeMap.get(EAP_AT_VERSION_LIST); + assertEquals(Arrays.asList(1), atVersionList.versions); + assertTrue(eapSimTypeData.attributeMap.containsKey(EAP_AT_PERMANENT_ID_REQ)); + + // also check order of Map entries (needs to match input order) + Iterator<Integer> itr = eapSimTypeData.attributeMap.keySet().iterator(); + assertEquals(EAP_AT_VERSION_LIST, (int) itr.next()); + assertEquals(EAP_AT_PERMANENT_ID_REQ, (int) itr.next()); + assertFalse(itr.hasNext()); + } + + @Test + public void testDecodeNullTypeData() { + DecodeResult<EapSimTypeData> result = mEapSimTypeDataDecoder.decode(null); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testDecodeInvalidSubtype() { + DecodeResult<EapSimTypeData> result = mEapSimTypeDataDecoder.decode(INVALID_SUBTYPE); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testDecodeInvalidAtRand() { + DecodeResult<EapSimTypeData> result = + mEapSimTypeDataDecoder.decode(TYPE_DATA_INVALID_AT_RAND); + assertFalse(result.isSuccessfulDecode()); + assertEquals(INSUFFICIENT_CHALLENGES_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testDecodeShortPacket() { + DecodeResult<EapSimTypeData> result = mEapSimTypeDataDecoder.decode(SHORT_TYPE_DATA); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testDecodeInvalidEapAttribute() { + DecodeResult<EapSimTypeData> result = + mEapSimTypeDataDecoder.decode(TYPE_DATA_INVALID_ATTRIBUTE); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testEncode() throws Exception { + LinkedHashMap<Integer, EapSimAkaAttribute> attributes = new LinkedHashMap<>(); + attributes.put(EAP_AT_VERSION_LIST, new AtVersionList(8, 1)); + attributes.put(EAP_AT_PERMANENT_ID_REQ, new AtPermanentIdReq()); + EapSimTypeData eapSimTypeData = new EapSimTypeData(EAP_SIM_START, attributes); + + byte[] result = eapSimTypeData.encode(); + assertArrayEquals(EAP_SIM_START_SUBTYPE, result); + } + + @Test + public void testDecodeDuplicateAttributes() { + DecodeResult<EapSimTypeData> result = + mEapSimTypeDataDecoder.decode(EAP_SIM_START_DUPLICATE_ATTRIBUTES); + assertFalse(result.isSuccessfulDecode()); + assertEquals(UNABLE_TO_PROCESS_CODE, result.atClientErrorCode.errorCode); + } + + @Test + public void testConstructorInvalidSubtype() throws Exception { + try { + new EapSimTypeData(INVALID_SUBTYPE_INT, Arrays.asList(new AtPermanentIdReq())); + fail("Expected IllegalArgumentException for invalid subtype"); + } catch (IllegalArgumentException expected) { + } + } + + @Test + public void testConstructorDuplicateAttributes() throws Exception { + try { + new EapSimTypeData( + EAP_SIM_START, Arrays.asList(new AtPermanentIdReq(), new AtPermanentIdReq())); + fail("Expected IllegalArgumentException for duplicate attributes"); + } catch (IllegalArgumentException expected) { + } + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutnTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutnTest.java new file mode 100644 index 00000000..ebf22e4f --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutnTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_AUTN; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_AUTN; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_AUTN_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AUTN_BYTES; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAutn; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtAutnTest { + private EapAkaAttributeFactory mEapAkaAttributeFactory; + + @Before + public void setUp() { + mEapAkaAttributeFactory = EapAkaAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_AUTN); + EapSimAkaAttribute result = mEapAkaAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtAutn atAutn = (AtAutn) result; + assertEquals(EAP_AT_AUTN, atAutn.attributeType); + assertEquals(AT_AUTN.length, atAutn.lengthInBytes); + assertArrayEquals(AUTN_BYTES, atAutn.autn); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_AUTN_INVALID_LENGTH); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtAutn atAutn = new AtAutn(AUTN_BYTES); + + ByteBuffer result = ByteBuffer.allocate(AT_AUTN.length); + atAutn.encode(result); + assertArrayEquals(AT_AUTN, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutsTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutsTest.java new file mode 100644 index 00000000..d65a735f --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtAutsTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_AUTS; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_AUTS; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_AUTS_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AUTS_BYTES; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAuts; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtAutsTest { + private EapAkaAttributeFactory mEapAkaAttributeFactory; + + @Before + public void setUp() { + mEapAkaAttributeFactory = EapAkaAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_AUTS); + EapSimAkaAttribute result = mEapAkaAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtAuts atAuts = (AtAuts) result; + assertEquals(EAP_AT_AUTS, atAuts.attributeType); + assertEquals(AT_AUTS.length, atAuts.lengthInBytes); + assertArrayEquals(AUTS_BYTES, atAuts.auts); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_AUTS_INVALID_LENGTH); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtAuts atAuts = new AtAuts(AUTS_BYTES); + + ByteBuffer result = ByteBuffer.allocate(AT_AUTS.length); + atAuts.encode(result); + assertArrayEquals(AT_AUTS, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtBiddingTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtBiddingTest.java new file mode 100644 index 00000000..efdfcc2e --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtBiddingTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_BIDDING; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_BIDDING_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_BIDDING_SUPPORTS_AKA_PRIME; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtBidding; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtBiddingTest { + private EapAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = EapAkaAttributeFactory.getInstance(); + } + + @Test + public void testDecodeServerSupportsAkaPrime() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_BIDDING_SUPPORTS_AKA_PRIME); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtBidding atBidding = (AtBidding) result; + assertEquals(EAP_AT_BIDDING, atBidding.attributeType); + assertEquals(AT_BIDDING_SUPPORTS_AKA_PRIME.length, atBidding.lengthInBytes); + assertTrue(atBidding.doesServerSupportEapAkaPrime); + } + + @Test + public void testDecodeDoesNotSupportAkaPrime() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtBidding atBidding = (AtBidding) result; + assertEquals(EAP_AT_BIDDING, atBidding.attributeType); + assertEquals(AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME.length, atBidding.lengthInBytes); + assertFalse(atBidding.doesServerSupportEapAkaPrime); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_BIDDING_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeServerSupportsAkaPrime() throws Exception { + AtBidding atBidding = new AtBidding(true); + + ByteBuffer result = ByteBuffer.allocate(AT_BIDDING_SUPPORTS_AKA_PRIME.length); + atBidding.encode(result); + assertArrayEquals(AT_BIDDING_SUPPORTS_AKA_PRIME, result.array()); + } + + @Test + public void testEncodeDoesNotSupportAkaPrime() throws Exception { + AtBidding atBidding = new AtBidding(false); + + ByteBuffer result = ByteBuffer.allocate(AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME.length); + atBidding.encode(result); + assertArrayEquals(AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtClientErrorCodeTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtClientErrorCodeTest.java new file mode 100644 index 00000000..2051414c --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtClientErrorCodeTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToInt; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_CLIENT_ERROR_CODE; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_CLIENT_ERROR_CODE; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_CLIENT_ERROR_CODE_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.ERROR_CODE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtClientErrorCode; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +public class AtClientErrorCodeTest { + private static final int EXPECTED_LENGTH = 4; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_CLIENT_ERROR_CODE); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtClientErrorCode); + AtClientErrorCode atClientErrorCode = (AtClientErrorCode) result; + assertEquals(EAP_AT_CLIENT_ERROR_CODE, atClientErrorCode.attributeType); + assertEquals(EXPECTED_LENGTH, atClientErrorCode.lengthInBytes); + assertEquals(hexStringToInt(ERROR_CODE), atClientErrorCode.errorCode); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_CLIENT_ERROR_CODE_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected BufferUnderflowException for invalid attribute length"); + } catch (BufferUnderflowException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtClientErrorCode atNotification = new AtClientErrorCode( + EXPECTED_LENGTH, hexStringToInt(ERROR_CODE)); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atNotification.encode(result); + assertArrayEquals(AT_CLIENT_ERROR_CODE, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtCounterTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtCounterTest.java new file mode 100644 index 00000000..eb1086d5 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtCounterTest.java @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_COUNTER; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_COUNTER_TOO_SMALL; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_COUNTER; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_COUNTER_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_COUNTER_TOO_SMALL; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_COUNTER_TOO_SMALL_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.COUNTER_INT; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtCounter; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtCounterTooSmall; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtCounterTest { + private static final int EXPECTED_LENGTH = 4; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecodeAtCounter() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_COUNTER); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtCounter); + AtCounter atCounter = (AtCounter) result; + assertEquals(EAP_AT_COUNTER, atCounter.attributeType); + assertEquals(EXPECTED_LENGTH, atCounter.lengthInBytes); + assertEquals(COUNTER_INT, atCounter.counter); + } + + @Test + public void testDecodeAtCounterInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_COUNTER_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeAtCounter() throws Exception { + AtCounter atCounter = new AtCounter(COUNTER_INT); + + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + atCounter.encode(result); + assertArrayEquals(AT_COUNTER, result.array()); + } + + @Test + public void testAtCounterTooSmallConstructor() throws Exception { + AtCounterTooSmall atCounterTooSmall = new AtCounterTooSmall(); + assertEquals(EAP_AT_COUNTER_TOO_SMALL, atCounterTooSmall.attributeType); + assertEquals(EXPECTED_LENGTH, atCounterTooSmall.lengthInBytes); + } + + @Test + public void testDecodeAtCounterTooSmall() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_COUNTER_TOO_SMALL); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtCounterTooSmall); + AtCounterTooSmall atCounterTooSmall = (AtCounterTooSmall) result; + assertEquals(EAP_AT_COUNTER_TOO_SMALL, atCounterTooSmall.attributeType); + assertEquals(EXPECTED_LENGTH, atCounterTooSmall.lengthInBytes); + } + + @Test + public void testDecodeAtCounterTooSmallInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_COUNTER_TOO_SMALL_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeAtCounterTooSmall() throws Exception { + AtCounterTooSmall atCounterTooSmall = new AtCounterTooSmall(); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + atCounterTooSmall.encode(result); + assertArrayEquals(AT_COUNTER_TOO_SMALL, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdReqTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdReqTest.java new file mode 100644 index 00000000..d006053e --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdReqTest.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_ANY_ID_REQ; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_FULLAUTH_ID_REQ; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_PERMANENT_ID_REQ; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.ANY_ID_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_ANY_ID_REQ; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_FULL_AUTH_ID_REQ; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_PERMANENT_ID_REQ; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.FULL_AUTH_ID_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.PERMANENT_ID_INVALID_LENGTH; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtAnyIdReq; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtFullauthIdReq; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtPermanentIdReq; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtIdReqTest { + private static final int EXPECTED_LENGTH = 4; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecodeAtPermanentIdReq() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_PERMANENT_ID_REQ); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtPermanentIdReq); + AtPermanentIdReq atPermanentIdReq = (AtPermanentIdReq) result; + assertEquals(EAP_AT_PERMANENT_ID_REQ, atPermanentIdReq.attributeType); + assertEquals(EXPECTED_LENGTH, atPermanentIdReq.lengthInBytes); + } + + @Test + public void testDecodeAtPermanentIdReqInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(PERMANENT_ID_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid attribute length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeAtPermanentIdReq() throws Exception { + AtPermanentIdReq atPermanentIdReq = new AtPermanentIdReq(); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atPermanentIdReq.encode(result); + assertArrayEquals(AT_PERMANENT_ID_REQ, result.array()); + } + + @Test + public void testDecodeAtAnyIdReq() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_ANY_ID_REQ); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtAnyIdReq); + AtAnyIdReq atAnyIdReq = (AtAnyIdReq) result; + assertEquals(EAP_AT_ANY_ID_REQ, atAnyIdReq.attributeType); + assertEquals(EXPECTED_LENGTH, atAnyIdReq.lengthInBytes); + } + + @Test + public void testDecodeAtAnyIdReqInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(ANY_ID_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid attribute length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeAtAnyIdReq() throws Exception { + AtAnyIdReq atPermanentIdReq = new AtAnyIdReq(); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atPermanentIdReq.encode(result); + assertArrayEquals(AT_ANY_ID_REQ, result.array()); + } + + @Test + public void testDecodeAtFullauthIdReq() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_FULL_AUTH_ID_REQ); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtFullauthIdReq); + AtFullauthIdReq atFullauthIdReq = (AtFullauthIdReq) result; + assertEquals(EAP_AT_FULLAUTH_ID_REQ, atFullauthIdReq.attributeType); + assertEquals(EXPECTED_LENGTH, atFullauthIdReq.lengthInBytes); + } + + @Test + public void testDecodeAtFullauthIdReqInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(FULL_AUTH_ID_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid attribute length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncodeAtFullauthIdReq() throws Exception { + AtFullauthIdReq atPermanentIdReq = new AtFullauthIdReq(); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atPermanentIdReq.encode(result); + assertArrayEquals(AT_FULL_AUTH_ID_REQ, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdentityTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdentityTest.java new file mode 100644 index 00000000..cf8e8803 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtIdentityTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_IDENTITY; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_IDENTITY; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.IDENTITY; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtIdentity; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtIdentityTest { + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_IDENTITY); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtIdentity); + AtIdentity atIdentity = (AtIdentity) result; + assertEquals(EAP_AT_IDENTITY, atIdentity.attributeType); + assertEquals(AT_IDENTITY.length, atIdentity.lengthInBytes); + assertArrayEquals(IDENTITY, atIdentity.identity); + } + + @Test + public void testEncode() throws Exception { + AtIdentity atIdentity = new AtIdentity(AT_IDENTITY.length, IDENTITY); + ByteBuffer result = ByteBuffer.allocate(AT_IDENTITY.length); + atIdentity.encode(result); + + assertArrayEquals(AT_IDENTITY, result.array()); + } + + @Test + public void testGetAtIdentity() throws Exception { + AtIdentity atIdentity = AtIdentity.getAtIdentity(IDENTITY); + + assertArrayEquals(IDENTITY, atIdentity.identity); + + ByteBuffer buffer = ByteBuffer.allocate(atIdentity.lengthInBytes); + atIdentity.encode(buffer); + buffer.rewind(); + + EapSimAkaAttribute eapSimAkaAttribute = + EapSimAttributeFactory.getInstance().getAttribute(buffer); + assertTrue(eapSimAkaAttribute instanceof AtIdentity); + AtIdentity newAtIdentity = (AtIdentity) eapSimAkaAttribute; + assertEquals(atIdentity.lengthInBytes, newAtIdentity.lengthInBytes); + assertArrayEquals(atIdentity.identity, newAtIdentity.identity); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfInputTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfInputTest.java new file mode 100644 index 00000000..51ea3f14 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfInputTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_KDF_INPUT; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_KDF_INPUT; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_KDF_INPUT_EMPTY_NETWORK_NAME; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NETWORK_NAME_BYTES; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import com.android.internal.net.eap.message.simaka.EapAkaPrimeAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtKdfInput; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtKdfInputTest { + private EapAkaPrimeAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = EapAkaPrimeAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_KDF_INPUT); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtKdfInput atKdfInput = (AtKdfInput) result; + assertEquals(EAP_AT_KDF_INPUT, atKdfInput.attributeType); + assertEquals(AT_KDF_INPUT.length, atKdfInput.lengthInBytes); + assertArrayEquals(NETWORK_NAME_BYTES, atKdfInput.networkName); + } + + @Test + public void testDecodeEmptyNetworkName() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_KDF_INPUT_EMPTY_NETWORK_NAME); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtKdfInput atKdfInput = (AtKdfInput) result; + assertEquals(EAP_AT_KDF_INPUT, atKdfInput.attributeType); + assertEquals(AT_KDF_INPUT_EMPTY_NETWORK_NAME.length, atKdfInput.lengthInBytes); + assertArrayEquals(new byte[0], atKdfInput.networkName); + } + + @Test + public void testEncode() throws Exception { + AtKdfInput atKdfInput = new AtKdfInput(AT_KDF_INPUT.length, NETWORK_NAME_BYTES); + ByteBuffer result = ByteBuffer.allocate(AT_KDF_INPUT.length); + + atKdfInput.encode(result); + assertArrayEquals(AT_KDF_INPUT, result.array()); + assertFalse(result.hasRemaining()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfTest.java new file mode 100644 index 00000000..0bb07326 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtKdfTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_KDF; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_KDF; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_KDF_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.KDF_VERSION; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaPrimeAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtKdf; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtKdfTest { + private EapAkaPrimeAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = EapAkaPrimeAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_KDF); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtKdf atKdf = (AtKdf) result; + assertEquals(EAP_AT_KDF, atKdf.attributeType); + assertEquals(AT_KDF.length, atKdf.lengthInBytes); + assertEquals(KDF_VERSION, atKdf.kdf); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_KDF_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtKdf atKdf = new AtKdf(KDF_VERSION); + ByteBuffer result = ByteBuffer.allocate(AT_KDF.length); + + atKdf.encode(result); + assertArrayEquals(AT_KDF, result.array()); + assertFalse(result.hasRemaining()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtMacTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtMacTest.java new file mode 100644 index 00000000..82b066d5 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtMacTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_MAC; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_MAC; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_MAC_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.MAC; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtMac; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtMacTest { + private static final int EXPECTED_LENGTH = 20; + private static final int MAC_LENGTH = 16; + private static final byte[] MAC_BYTES = hexStringToByteArray(MAC); + private static final byte[] INVALID_MAC = {(byte) 1, (byte) 2, (byte) 3}; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testConstructor() throws Exception { + AtMac atMac = new AtMac(); + assertEquals(EAP_AT_MAC, atMac.attributeType); + assertEquals(EXPECTED_LENGTH, atMac.lengthInBytes); + assertArrayEquals(new byte[MAC_LENGTH], atMac.mac); + } + + @Test + public void testParameterizedConstructor() throws Exception { + AtMac atMac = new AtMac(MAC_BYTES); + assertEquals(EAP_AT_MAC, atMac.attributeType); + assertEquals(EXPECTED_LENGTH, atMac.lengthInBytes); + assertArrayEquals(MAC_BYTES, atMac.mac); + } + + @Test + public void testParameterizedConstructorInvalidMac() { + try { + AtMac atMac = new AtMac(INVALID_MAC); + fail("Expected EapSimAkaInvalidAttributeException for invalid MAC length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_MAC); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtMac); + AtMac atMac = (AtMac) result; + assertEquals(EAP_AT_MAC, atMac.attributeType); + assertEquals(EXPECTED_LENGTH, atMac.lengthInBytes); + assertArrayEquals(MAC_BYTES, atMac.mac); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_MAC_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtMac atMac = new AtMac(MAC_BYTES); + + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + atMac.encode(result); + assertArrayEquals(AT_MAC, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceMtTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceMtTest.java new file mode 100644 index 00000000..751908a2 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceMtTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_NONCE_MT; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NONCE_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NONCE_MT; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NONCE_MT; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtNonceMt; +import com.android.internal.net.eap.message.simaka.EapSimAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtNonceMtTest { + private static final byte[] INVALID_NONCE = new byte[10]; + private static final int EXPECTED_LENGTH = 20; + + private EapSimAttributeFactory mEapSimAttributeFactory; + + @Before + public void setUp() { + mEapSimAttributeFactory = EapSimAttributeFactory.getInstance(); + } + + @Test + public void testConstructorInvalidNonceLength() { + try { + new AtNonceMt(INVALID_NONCE); + fail("Expected EapSimAkaInvalidAttributeException for invalid NonceMt length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NONCE_MT); + EapSimAkaAttribute result = mEapSimAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtNonceMt); + AtNonceMt atNonceMt = (AtNonceMt) result; + assertEquals(EAP_AT_NONCE_MT, atNonceMt.attributeType); + assertEquals(EXPECTED_LENGTH, atNonceMt.lengthInBytes); + assertArrayEquals(NONCE_MT, atNonceMt.nonceMt); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NONCE_INVALID_LENGTH); + try { + mEapSimAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid attribute length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws EapSimAkaInvalidAttributeException { + AtNonceMt atNonceMt = new AtNonceMt(NONCE_MT); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atNonceMt.encode(result); + assertArrayEquals(AT_NONCE_MT, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceSTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceSTest.java new file mode 100644 index 00000000..1ad64669 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNonceSTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_NONCE_S; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NONCE_S; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NONCE_S_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NONCE_S; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtNonceS; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtNonceSTest { + private static final byte[] INVALID_NONCE = new byte[10]; + private static final int EXPECTED_LENGTH = 20; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testConstructorInvalidNonceLength() { + try { + new AtNonceS(INVALID_NONCE); + fail("Expected EapSimAkaInvalidAttributeException for invalid NonceMt length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NONCE_S); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtNonceS); + AtNonceS atNonceS = (AtNonceS) result; + assertEquals(EAP_AT_NONCE_S, atNonceS.attributeType); + assertEquals(EXPECTED_LENGTH, atNonceS.lengthInBytes); + assertArrayEquals(hexStringToByteArray(NONCE_S), atNonceS.nonceS); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NONCE_S_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtNonceS atNonceS = new AtNonceS(hexStringToByteArray(NONCE_S)); + + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + atNonceS.encode(result); + assertArrayEquals(AT_NONCE_S, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNotificationTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNotificationTest.java new file mode 100644 index 00000000..2db3cbb2 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtNotificationTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToInt; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtNotification.GENERAL_FAILURE_POST_CHALLENGE; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_NOTIFICATION; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NOTIFICATION; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NOTIFICATION_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_NOTIFICATION_INVALID_STATE; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.NOTIFICATION_CODE; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtNotification; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtNotificationTest { + private static final int EXPECTED_LENGTH = 4; + private static final int UNKNOWN_CODE = 0xA0FF; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NOTIFICATION); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtNotification); + AtNotification atNotification = (AtNotification) result; + assertEquals(EAP_AT_NOTIFICATION, atNotification.attributeType); + assertEquals(EXPECTED_LENGTH, atNotification.lengthInBytes); + assertTrue(atNotification.isSuccessCode); + assertFalse(atNotification.isPreSuccessfulChallenge); + assertEquals(hexStringToInt(NOTIFICATION_CODE), atNotification.notificationCode); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NOTIFICATION_INVALID_LENGTH); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid attribute length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecodeInvalidState() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_NOTIFICATION_INVALID_STATE); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid state"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtNotification atNotification = new AtNotification(hexStringToInt(NOTIFICATION_CODE)); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atNotification.encode(result); + assertArrayEquals(AT_NOTIFICATION, result.array()); + } + + @Test + public void testToString() throws Exception { + AtNotification knownCode = new AtNotification(GENERAL_FAILURE_POST_CHALLENGE); + AtNotification unknownCode = new AtNotification(UNKNOWN_CODE); + + assertNotNull(knownCode.toString()); + assertNotNull(unknownCode.toString()); + assertNotEquals(knownCode.toString(), unknownCode.toString()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtPaddingTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtPaddingTest.java new file mode 100644 index 00000000..d310d504 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtPaddingTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_PADDING; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_PADDING; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_PADDING_INVALID_PADDING; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAtPaddingException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtPadding; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtPaddingTest { + private static final int EXPECTED_LENGTH = 8; + + private EapSimAkaAttributeFactory mAttributeFactory; + + @Before + public void setUp() { + mAttributeFactory = new EapSimAkaAttributeFactory() {}; + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_PADDING); + EapSimAkaAttribute result = mAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtPadding); + AtPadding atPadding = (AtPadding) result; + assertEquals(EAP_AT_PADDING, atPadding.attributeType); + assertEquals(EXPECTED_LENGTH, atPadding.lengthInBytes); + } + + @Test + public void testDecodeInvalidPadding() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_PADDING_INVALID_PADDING); + try { + mAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAtPaddingException for nonzero padding bytes"); + } catch (EapSimAkaInvalidAtPaddingException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtPadding atPadding = new AtPadding(EXPECTED_LENGTH); + + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + atPadding.encode(result); + + assertFalse(result.hasRemaining()); + assertArrayEquals(AT_PADDING, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandAkaTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandAkaTest.java new file mode 100644 index 00000000..bdffdda9 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandAkaTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RAND; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RAND_AKA; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RAND_AKA_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RAND_1_BYTES; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRandAka; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtRandAkaTest { + private EapAkaAttributeFactory mEapAkaAttributeFactory; + + @Before + public void setUp() { + mEapAkaAttributeFactory = EapAkaAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RAND_AKA); + EapSimAkaAttribute result = mEapAkaAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtRandAka atRandAka = (AtRandAka) result; + assertEquals(EAP_AT_RAND, atRandAka.attributeType); + assertEquals(AT_RAND_AKA.length, atRandAka.lengthInBytes); + assertArrayEquals(RAND_1_BYTES, atRandAka.rand); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RAND_AKA_INVALID_LENGTH); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtRandAka atRandAka = new AtRandAka(RAND_1_BYTES); + + ByteBuffer result = ByteBuffer.allocate(AT_RAND_AKA.length); + atRandAka.encode(result); + assertArrayEquals(AT_RAND_AKA, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandSimTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandSimTest.java new file mode 100644 index 00000000..7456be6c --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtRandSimTest.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RAND; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RAND_SIM; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RAND_SIM_DUPLICATE_RANDS; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RAND_SIM_INVALID_NUM_RANDS; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RAND_1; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RAND_2; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.exceptions.simaka.EapSimInvalidAtRandException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRandSim; +import com.android.internal.net.eap.message.simaka.EapSimAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtRandSimTest { + private static final int EXPECTED_NUM_RANDS = 2; + + private EapSimAttributeFactory mEapSimAttributeFactory; + + @Before + public void setUp() { + mEapSimAttributeFactory = EapSimAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RAND_SIM); + EapSimAkaAttribute result = mEapSimAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtRandSim); + AtRandSim atRandSim = (AtRandSim) result; + assertEquals(EAP_AT_RAND, atRandSim.attributeType); + assertEquals(AT_RAND_SIM.length, atRandSim.lengthInBytes); + assertEquals(EXPECTED_NUM_RANDS, atRandSim.rands.size()); + assertArrayEquals(hexStringToByteArray(RAND_1), atRandSim.rands.get(0)); + assertArrayEquals(hexStringToByteArray(RAND_2), atRandSim.rands.get(1)); + } + + @Test + public void testDecodeInvalidNumRands() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RAND_SIM_INVALID_NUM_RANDS); + try { + mEapSimAttributeFactory.getAttribute(input); + fail("Expected EapSimInvalidAtRandException for invalid number of RANDs"); + } catch (EapSimInvalidAtRandException expected) { + } + } + + @Test + public void testDecodeDuplicateRands() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RAND_SIM_DUPLICATE_RANDS); + try { + mEapSimAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for duplicate RANDs"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + byte[][] expectedRands = new byte[][] { + hexStringToByteArray(RAND_1), + hexStringToByteArray(RAND_2) + }; + AtRandSim atRandSim = new AtRandSim(AT_RAND_SIM.length, expectedRands); + + ByteBuffer result = ByteBuffer.allocate(AT_RAND_SIM.length); + atRandSim.encode(result); + assertArrayEquals(AT_RAND_SIM, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtResTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtResTest.java new file mode 100644 index 00000000..34c2ff39 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtResTest.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RES_INVALID_RES_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RES_LONG_RES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_RES_SHORT_RES; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.RES_BYTES; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapAkaAttributeFactory; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtRes; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtResTest { + private EapAkaAttributeFactory mEapAkaAttributeFactory; + + @Before + public void setUp() { + mEapAkaAttributeFactory = EapAkaAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RES); + EapSimAkaAttribute result = mEapAkaAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + AtRes atRes = (AtRes) result; + assertEquals(EAP_AT_RES, atRes.attributeType); + assertEquals(AT_RES.length, atRes.lengthInBytes); + assertArrayEquals(RES_BYTES, atRes.res); + } + + @Test + public void testDecodeInvalidResLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RES_INVALID_RES_LENGTH); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid RES length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecodeShortResLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RES_SHORT_RES); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for too short RES"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testDecodeLongResLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_RES_LONG_RES); + try { + mEapAkaAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for too long RES"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtRes atRes = new AtRes(AT_RES.length, RES_BYTES); + + ByteBuffer result = ByteBuffer.allocate(AT_RES.length); + atRes.encode(result); + assertArrayEquals(AT_RES, result.array()); + } + + @Test + public void testGetAtRes() throws Exception { + AtRes atRes = AtRes.getAtRes(RES_BYTES); + + ByteBuffer result = ByteBuffer.allocate(AT_RES.length); + atRes.encode(result); + assertArrayEquals(AT_RES, result.array()); + } + + @Test + public void testIsValidResLen() { + // valid RES length: 4 <= RES length <= 16 + assertTrue(AtRes.isValidResLen(5)); + assertFalse(AtRes.isValidResLen(0)); + assertFalse(AtRes.isValidResLen(20)); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtSelectedVersionTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtSelectedVersionTest.java new file mode 100644 index 00000000..659fe9a8 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtSelectedVersionTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_SELECTED_VERSION; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_SELECTED_VERSION; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_SELECTED_VERSION_INVALID_LENGTH; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtSelectedVersion; +import com.android.internal.net.eap.message.simaka.EapSimAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class AtSelectedVersionTest { + private static final int EXPECTED_LENGTH = 4; + private static final int EXPECTED_VERSION = 1; + + private EapSimAttributeFactory mEapSimAttributeFactory; + + @Before + public void setUp() { + mEapSimAttributeFactory = EapSimAttributeFactory.getInstance(); + } + + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_SELECTED_VERSION); + EapSimAkaAttribute result = mEapSimAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtSelectedVersion); + AtSelectedVersion atSelectedVersion = (AtSelectedVersion) result; + assertEquals(EAP_AT_SELECTED_VERSION, atSelectedVersion.attributeType); + assertEquals(EXPECTED_LENGTH, atSelectedVersion.lengthInBytes); + assertEquals(EXPECTED_VERSION, atSelectedVersion.selectedVersion); + } + + @Test + public void testDecodeInvalidLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_SELECTED_VERSION_INVALID_LENGTH); + try { + mEapSimAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid actual list length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtSelectedVersion atSelectedVersion = new AtSelectedVersion( + EXPECTED_LENGTH, EXPECTED_VERSION); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atSelectedVersion.encode(result); + assertArrayEquals(AT_SELECTED_VERSION, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtVersionListTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtVersionListTest.java new file mode 100644 index 00000000..96bb7ca3 --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/AtVersionListTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToInt; +import static com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.EAP_AT_VERSION_LIST; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_VERSION_LIST; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.AT_VERSION_LIST_INVALID_LENGTH; +import static com.android.internal.net.eap.message.simaka.attributes.EapTestAttributeDefinitions.VERSION; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.android.internal.net.eap.exceptions.simaka.EapSimAkaInvalidAttributeException; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute.AtVersionList; +import com.android.internal.net.eap.message.simaka.EapSimAttributeFactory; + +import org.junit.Before; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; + +public class AtVersionListTest { + private static final int EXPECTED_LENGTH = 8; + private static final List<Integer> EXPECTED_VERSIONS = Arrays.asList(1); + + private EapSimAttributeFactory mEapSimAttributeFactory; + + @Before + public void setUp() { + mEapSimAttributeFactory = EapSimAttributeFactory.getInstance(); + } + + @Test + public void testDecode() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_VERSION_LIST); + EapSimAkaAttribute result = mEapSimAttributeFactory.getAttribute(input); + + assertFalse(input.hasRemaining()); + assertTrue(result instanceof AtVersionList); + AtVersionList atVersionList = (AtVersionList) result; + assertEquals(EAP_AT_VERSION_LIST, atVersionList.attributeType); + assertEquals(EXPECTED_LENGTH, atVersionList.lengthInBytes); + assertEquals(EXPECTED_VERSIONS, atVersionList.versions); + } + + @Test + public void testDecodeInvalidActualLength() throws Exception { + ByteBuffer input = ByteBuffer.wrap(AT_VERSION_LIST_INVALID_LENGTH); + try { + mEapSimAttributeFactory.getAttribute(input); + fail("Expected EapSimAkaInvalidAttributeException for invalid actual list length"); + } catch (EapSimAkaInvalidAttributeException expected) { + } + } + + @Test + public void testEncode() throws Exception { + AtVersionList atVersionList = new AtVersionList(EXPECTED_LENGTH, hexStringToInt(VERSION)); + ByteBuffer result = ByteBuffer.allocate(EXPECTED_LENGTH); + + atVersionList.encode(result); + assertArrayEquals(AT_VERSION_LIST, result.array()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapSimAkaAttributeTest.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapSimAkaAttributeTest.java new file mode 100644 index 00000000..98ea222c --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapSimAkaAttributeTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; + +import com.android.internal.net.eap.message.simaka.EapSimAkaAttribute; + +import org.junit.Test; + +import java.nio.ByteBuffer; + +public class EapSimAkaAttributeTest { + private static final int EXPECTED_ATTRIBUTE_TYPE = 1; + private static final int EXPECTED_LENGTH_IN_BYTES = 4; + private static final int BUFFER_LENGTH = 2; + private static final int EXPECTED_LENGTH_ENCODED = 1; + private static final byte[] EXPECTED_ENCODING = { + (byte) EXPECTED_ATTRIBUTE_TYPE, + (byte) EXPECTED_LENGTH_ENCODED + }; + + @Test + public void testEncode() throws Exception { + EapSimAkaAttribute eapSimAkaAttribute = new EapSimAkaAttribute( + EXPECTED_ATTRIBUTE_TYPE, + EXPECTED_LENGTH_IN_BYTES) { + public void encode(ByteBuffer byteBuffer) { + encodeAttributeHeader(byteBuffer); + } + }; + + ByteBuffer result = ByteBuffer.allocate(BUFFER_LENGTH); + eapSimAkaAttribute.encode(result); + assertArrayEquals(EXPECTED_ENCODING, result.array()); + assertFalse(result.hasRemaining()); + } +} diff --git a/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapTestAttributeDefinitions.java b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapTestAttributeDefinitions.java new file mode 100644 index 00000000..60397e1f --- /dev/null +++ b/tests/iketests/src/java/com/android/internal/net/eap/message/simaka/attributes/EapTestAttributeDefinitions.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2019 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.internal.net.eap.message.simaka.attributes; + +import static com.android.internal.net.TestUtils.hexStringToByteArray; + +/** + * EapTestAttributeDefinitions provides byte[] encodings of commonly used EAP Messages. + * + * @ee <a href="https://tools.ietf.org/html/rfc4186#section-10">RFC 4186, EAP-SIM Authentication, + * Section 10</a> + * @see <a href="https://tools.ietf.org/html/rfc4187#section-10">RFC 4187, EAP-AKA Authentication, + * Section 10</a> + */ +public class EapTestAttributeDefinitions { + public static final String VERSION = "0001"; + public static final String AT_VERSION_LIST_DATA = "0002" + VERSION + "0000"; + public static final byte[] AT_VERSION_LIST = + hexStringToByteArray("0F02" + AT_VERSION_LIST_DATA); + public static final byte[] AT_SELECTED_VERSION = hexStringToByteArray("10010001"); + public static final String NONCE_MT_STRING = "0123456789ABCDEFFEDCBA9876543210"; + public static final byte[] NONCE_MT = hexStringToByteArray(NONCE_MT_STRING); + public static final byte[] AT_NONCE_MT = hexStringToByteArray("07050000" + NONCE_MT_STRING); + public static final byte[] AT_PERMANENT_ID_REQ = hexStringToByteArray("0A010000"); + public static final byte[] AT_ANY_ID_REQ = hexStringToByteArray("0D010000"); + public static final byte[] AT_FULL_AUTH_ID_REQ = hexStringToByteArray("11010000"); + + // Identity = "test1@android.net" + public static final String IDENTITY_STRING = "746573743140616E64726F69642E6E6574"; + public static final byte[] IDENTITY = hexStringToByteArray(IDENTITY_STRING); + public static final byte[] AT_IDENTITY = + hexStringToByteArray("0E060011" + IDENTITY_STRING + "000000"); + public static final String RAND_1 = "00112233445566778899AABBCCDDEEFF"; + public static final byte[] RAND_1_BYTES = hexStringToByteArray(RAND_1); + public static final String RAND_2 = "FFEEDDCCBBAA99887766554433221100"; + public static final byte[] RAND_2_BYTES = hexStringToByteArray(RAND_2); + public static final byte[] AT_RAND_SIM = hexStringToByteArray("01090000" + RAND_1 + RAND_2); + public static final byte[] AT_RAND_AKA = hexStringToByteArray("01050000" + RAND_1); + public static final byte[] AT_PADDING = hexStringToByteArray("0602000000000000"); + public static final String MAC = "112233445566778899AABBCCDDEEFF11"; + public static final byte[] MAC_BYTES = hexStringToByteArray(MAC); + public static final byte[] AT_MAC = hexStringToByteArray("0B050000" + MAC); + public static final String COUNTER = "000A"; + public static final int COUNTER_INT = Integer.parseInt(COUNTER, 16 /* radix */); + public static final byte[] AT_COUNTER = hexStringToByteArray("1301" + COUNTER); + public static final byte[] AT_COUNTER_TOO_SMALL = hexStringToByteArray("14010000"); + public static final String NONCE_S = "0123456789ABCDEFFEDCBA9876543210"; + public static final byte[] AT_NONCE_S = hexStringToByteArray("15050000" + NONCE_S); + public static final String NOTIFICATION_CODE = "8000"; + public static final byte[] AT_NOTIFICATION = hexStringToByteArray("0C01" + NOTIFICATION_CODE); + public static final String ERROR_CODE = "0001"; + public static final byte[] AT_CLIENT_ERROR_CODE = hexStringToByteArray("1601" + ERROR_CODE); + public static final String AUTN = "0123456789ABCDEFFEDCBA9876543210"; + public static final byte[] AUTN_BYTES = hexStringToByteArray(AUTN); + public static final byte[] AT_AUTN = hexStringToByteArray("02050000" + AUTN); + public static final String RES = "1122334455"; + public static final byte[] RES_BYTES = hexStringToByteArray(RES); + public static final byte[] AT_RES = hexStringToByteArray("03030028" + RES + "000000"); + public static final String AUTS = "112233445566778899AABBCCDDEE"; + public static final byte[] AUTS_BYTES = hexStringToByteArray(AUTS); + public static final byte[] AT_AUTS = hexStringToByteArray("0404" + AUTS); + public static final byte[] AT_BIDDING_SUPPORTS_AKA_PRIME = hexStringToByteArray("88018000"); + public static final byte[] AT_BIDDING_DOES_NOT_SUPPORT_AKA_PRIME = + hexStringToByteArray("88010000"); + + // Network Name = "android.net" + public static final String NETWORK_NAME_HEX = "616E64726F69642E6E6574"; + public static final byte[] NETWORK_NAME_BYTES = hexStringToByteArray(NETWORK_NAME_HEX); + public static final byte[] AT_KDF_INPUT = + hexStringToByteArray("1704000B" + NETWORK_NAME_HEX + "00"); + public static final byte[] AT_KDF_INPUT_EMPTY_NETWORK_NAME = hexStringToByteArray("17010000"); + public static final int KDF_VERSION = 1; + public static final byte[] AT_KDF = hexStringToByteArray("18010001"); + + public static final byte[] AT_VERSION_LIST_INVALID_LENGTH = hexStringToByteArray("0F020003"); + public static final byte[] AT_SELECTED_VERSION_INVALID_LENGTH = + hexStringToByteArray("10020001"); + public static final byte[] AT_NONCE_INVALID_LENGTH = + hexStringToByteArray("07060000" + NONCE_MT_STRING); + public static final byte[] PERMANENT_ID_INVALID_LENGTH = hexStringToByteArray("0A020000"); + public static final byte[] ANY_ID_INVALID_LENGTH = hexStringToByteArray("0D020000"); + public static final byte[] FULL_AUTH_ID_INVALID_LENGTH = hexStringToByteArray("11020000"); + public static final byte[] AT_RAND_SIM_INVALID_NUM_RANDS = + hexStringToByteArray("01050000" + RAND_1); + public static final byte[] AT_RAND_SIM_DUPLICATE_RANDS = + hexStringToByteArray("01090000" + RAND_1 + RAND_1); + public static final byte[] AT_RAND_AKA_INVALID_LENGTH = hexStringToByteArray("01010000"); + public static final byte[] AT_PADDING_INVALID_PADDING = hexStringToByteArray("0601FFFF"); + public static final byte[] AT_MAC_INVALID_LENGTH = hexStringToByteArray("0B06"); + public static final byte[] AT_COUNTER_INVALID_LENGTH = hexStringToByteArray("1302"); + public static final byte[] AT_COUNTER_TOO_SMALL_INVALID_LENGTH = hexStringToByteArray("1402"); + public static final byte[] AT_NONCE_S_INVALID_LENGTH = hexStringToByteArray("1506"); + public static final byte[] AT_NOTIFICATION_INVALID_LENGTH = hexStringToByteArray("0C02"); + public static final byte[] AT_NOTIFICATION_INVALID_STATE = hexStringToByteArray("0C01C000"); + public static final byte[] AT_CLIENT_ERROR_CODE_INVALID_LENGTH = hexStringToByteArray("1602"); + public static final byte[] AT_AUTN_INVALID_LENGTH = hexStringToByteArray("02010000"); + public static final byte[] AT_RES_INVALID_RES_LENGTH = + hexStringToByteArray("030300241122334450000000"); + public static final byte[] AT_RES_SHORT_RES = + hexStringToByteArray("0302000811000000"); + public static final byte[] AT_RES_LONG_RES = + hexStringToByteArray("0306008800112233445566778899AABBCCDDEEFF11000000"); + public static final byte[] AT_AUTS_INVALID_LENGTH = hexStringToByteArray("03010000"); + public static final byte[] AT_KDF_INVALID_LENGTH = hexStringToByteArray("18020001"); + public static final byte[] AT_BIDDING_INVALID_LENGTH = hexStringToByteArray("88020000"); +} |