diff options
author | Cody Kesting <ckesting@google.com> | 2019-10-01 12:52:15 -0700 |
---|---|---|
committer | Cody Kesting <ckesting@google.com> | 2019-10-07 16:40:58 -0700 |
commit | 2631057a20063d3b47dc198f13c835fada879d36 (patch) | |
tree | df22248efca52395eba7fa64e74dd277f8f6e6cc | |
parent | 57d6c7f79efbbdd9ca2abf3e28b7299cced0894b (diff) | |
download | ike-2631057a20063d3b47dc198f13c835fada879d36.tar.gz |
Validate parameters for EAP Type Data constructors.
EAP-SIM and EAP-AKA both use Type Data constructors for constructing
their responses that take the int eapSubtype and a List<Attribute>.
These values need to be validated, as this will help catch
implementation errors if they are used incorrectly within the EAP
library implementation.
Bug: 139805493
Test: added test cases to EapAkaTypeDataTest and EapSimTypeDataTest.
Test: atest FrameworksIkeTests
Change-Id: I346cd756f9afab75450ef9228733bedac21f121e
4 files changed, 64 insertions, 2 deletions
diff --git a/src/java/com/android/ike/eap/message/simaka/EapAkaTypeData.java b/src/java/com/android/ike/eap/message/simaka/EapAkaTypeData.java index c486485e..f8e0a3a3 100644 --- a/src/java/com/android/ike/eap/message/simaka/EapAkaTypeData.java +++ b/src/java/com/android/ike/eap/message/simaka/EapAkaTypeData.java @@ -32,6 +32,8 @@ import java.util.Set; * EapAkaTypeData represents the Type Data for an {@link EapMessage} during an EAP-AKA session. */ public class EapAkaTypeData extends EapSimAkaTypeData { + private static final String TAG = EapAkaTypeData.class.getSimpleName(); + // EAP-AKA Subtype values defined by IANA // https://www.iana.org/assignments/eapsimaka-numbers/eapsimaka-numbers.xhtml public static final int EAP_AKA_CHALLENGE = 1; @@ -80,8 +82,15 @@ public class EapAkaTypeData extends EapSimAkaTypeData { public EapAkaTypeData(int eapSubtype, List<EapSimAkaAttribute> attributes) { super(eapSubtype, new LinkedHashMap<>()); + if (!SUPPORTED_SUBTYPES.contains(eapSubtype)) { + throw new IllegalArgumentException("Invalid subtype for EAP-AKA: " + eapSubtype); + } + for (EapSimAkaAttribute attribute : attributes) { - // TODO(b/139805493): check for duplicate attributes + if (attributeMap.containsKey(attribute.attributeType)) { + throw new IllegalArgumentException( + "Duplicate attribute in attributes: " + attribute.attributeType); + } attributeMap.put(attribute.attributeType, attribute); } } diff --git a/src/java/com/android/ike/eap/message/simaka/EapSimTypeData.java b/src/java/com/android/ike/eap/message/simaka/EapSimTypeData.java index de8b3879..df699326 100644 --- a/src/java/com/android/ike/eap/message/simaka/EapSimTypeData.java +++ b/src/java/com/android/ike/eap/message/simaka/EapSimTypeData.java @@ -32,6 +32,8 @@ import java.util.Set; * EapSimTypeData represents the Type Data for an {@link EapMessage} during an EAP-SIM session. */ public class EapSimTypeData extends EapSimAkaTypeData { + private static final String TAG = EapSimTypeData.class.getSimpleName(); + // EAP-SIM Subtype values defined by IANA // https://www.iana.org/assignments/eapsimaka-numbers/eapsimaka-numbers.xhtml public static final int EAP_SIM_START = 10; @@ -68,8 +70,15 @@ public class EapSimTypeData extends EapSimAkaTypeData { public EapSimTypeData(int eapSubtype, List<EapSimAkaAttribute> attributes) { super(eapSubtype, new LinkedHashMap<>()); + if (!SUPPORTED_SUBTYPES.contains(eapSubtype)) { + throw new IllegalArgumentException("Invalid subtype for EAP-SIM: " + eapSubtype); + } + for (EapSimAkaAttribute attribute : attributes) { - // TODO(b/135637161): check for duplicate attributes + if (attributeMap.containsKey(attribute.attributeType)) { + throw new IllegalArgumentException( + "Duplicate attribute in attributes: " + attribute.attributeType); + } attributeMap.put(attribute.attributeType, attribute); } } diff --git a/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapAkaTypeDataTest.java b/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapAkaTypeDataTest.java index f476b742..65f0df1d 100644 --- a/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapAkaTypeDataTest.java +++ b/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapAkaTypeDataTest.java @@ -27,6 +27,8 @@ import static com.android.ike.eap.message.simaka.EapSimAkaAttribute.EAP_AT_MAC; import static com.android.ike.eap.message.simaka.EapSimAkaAttribute.EAP_AT_RES; import static com.android.ike.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; @@ -41,12 +43,14 @@ import com.android.ike.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 EapAkaTypeDataDecoder mEapAkaTypeDataDecoder; @@ -94,4 +98,22 @@ public class EapAkaTypeDataTest { 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/ike/eap/message/simaka/EapSimTypeDataTest.java b/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapSimTypeDataTest.java index 23437d75..40b55c19 100644 --- a/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapSimTypeDataTest.java +++ b/tests/iketests/src/java/com/android/ike/eap/message/simaka/EapSimTypeDataTest.java @@ -25,6 +25,8 @@ import static com.android.ike.eap.message.EapTestMessageDefinitions.TYPE_DATA_IN import static com.android.ike.eap.message.simaka.EapSimAkaAttribute.EAP_AT_PERMANENT_ID_REQ; import static com.android.ike.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; @@ -48,6 +50,7 @@ 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; @@ -151,4 +154,23 @@ public class EapSimTypeDataTest { 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) { + } + } } |