diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2016-12-22 19:57:07 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-12-22 19:57:08 +0000 |
commit | b49041a96954a5724ac162f0e4bd4842073fffec (patch) | |
tree | 3771a7dc34d836a8ff2f8fcbeb1de82a53c9bedb /tests | |
parent | 94203bb70f2afd3b2b3ac50bda8aa5900b78663b (diff) | |
parent | d8dddd9671750e6bfbcfa218db16ad096b9904ee (diff) |
Merge "hotspot2: ANQP elements cleanup Part 4"
Diffstat (limited to 'tests')
9 files changed, 914 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTest.java new file mode 100644 index 000000000..e600e3bf8 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.NAIRealmData}. + */ +@SmallTest +public class NAIRealmDataTest { + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + NAIRealmData.parse(ByteBuffer.wrap(new byte[0])); + } + + /** + * Verify that ProtocolException will be thrown when parsing a truncated buffer + * (missing a byte at the end). + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseTruncatedBuffer() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES); + buffer.limit(buffer.remaining() - 1); + NAIRealmData.parse(buffer); + } + + /** + * Verify that an expected NAIRealmData will be returned when parsing a buffer contained + * the test data with realm string encoded using UTF8. + * + * @throws Exception + */ + @Test + public void parseBufferWithUTF8EncodedNAIRealmData() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES); + assertEquals(NAIRealmDataTestUtil.TEST_REALM_DATA, NAIRealmData.parse(buffer)); + } + + /** + * Verify that the expected NAIRealmData will be returned when parsing a buffer contained + * the test data with realm string encoded using non-UTF8. + * + * @throws Exception + */ + @Test + public void parseBufferWithNonUTF8EncodedNAIRealmData() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap( + NAIRealmDataTestUtil.TEST_REAML_WITH_NON_UTF8_DATA_BYTES); + assertEquals(NAIRealmDataTestUtil.TEST_REALM_DATA, NAIRealmData.parse(buffer)); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTestUtil.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTestUtil.java new file mode 100644 index 000000000..1ce304498 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmDataTestUtil.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp; + +import android.net.wifi.EAPConstants; + +import com.android.server.wifi.hotspot2.anqp.eap.AuthParam; +import com.android.server.wifi.hotspot2.anqp.eap.CredentialType; +import com.android.server.wifi.hotspot2.anqp.eap.EAPMethod; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Utility class containing test data for NAI Realm Data. + */ +public class NAIRealmDataTestUtil { + /** + * Raw bytes for EAP Method. + */ + private static final byte[] TEST_EAP_METHOD_BYTES = + new byte[] {0x05 /* length */, 0x0D /* EAP_TLS */, 0x01 /* Auth Param Count */, + 0x05 /* CredentialType */, 0x01, 0x02 /* USIM */}; + + /** + * NAI Realm strings. + */ + private static final String[] TEST_REALMS = new String[] {"test1", "test2"}; + + /** + * Setup expected EAPMethod list. + */ + private static final Map<Integer, Set<AuthParam>> TEST_EAP_METHOD_AUTH_PARAMS = + new HashMap<>(); + private static final Set<AuthParam> TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS = new HashSet<>(); + private static final List<EAPMethod> TEST_EAP_METHOD_LIST = new ArrayList<>(); + static { + TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_USIM)); + TEST_EAP_METHOD_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, + TEST_EAP_METHOD_CREDENTIAL_TYPE_PARAMS); + + TEST_EAP_METHOD_LIST.add(new EAPMethod(EAPConstants.EAP_TLS, TEST_EAP_METHOD_AUTH_PARAMS)); + } + + /** + * Setup expected NAIRealmData. + */ + public static final NAIRealmData TEST_REALM_DATA = + new NAIRealmData(Arrays.asList(TEST_REALMS), TEST_EAP_METHOD_LIST); + + public static byte[] TEST_REAML_WITH_UTF8_DATA_BYTES = formatNAIRealmData(true); + public static byte[] TEST_REAML_WITH_NON_UTF8_DATA_BYTES = formatNAIRealmData(false); + + /** + * Helper function for returning raw bytes of NAI Realm Data (including the length field) for + * testing. + * + * @param utfEncoding Flag indicating the UTF encoding of the realm string + * @return byte[] + */ + private static byte[] formatNAIRealmData(boolean utfEncoding) { + try { + byte[] realmData = getNAIRealmData(utfEncoding); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + // Realm Data length in Little-Endian. + stream.write((byte) realmData.length); + stream.write((byte) realmData.length >> 8); + stream.write(realmData); + return stream.toByteArray(); + } catch (IOException e) { + return null; + } + } + + /** + * Helper function for returning raw bytes of NAI Realm Data payload. + * + * @param utfEncoding Flag indicating the encoding of NAI Realm string + * @return byte[] + * @throws IOException + */ + private static byte[] getNAIRealmData(boolean utfEncoding) throws IOException { + String realmsStr = String.join(NAIRealmData.NAI_REALM_STRING_SEPARATOR, TEST_REALMS); + byte[] realmStrData = realmsStr.getBytes( + utfEncoding ? StandardCharsets.UTF_8 : StandardCharsets.US_ASCII); + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + // NAI Realm Encoding byte. + stream.write((byte) (utfEncoding ? NAIRealmData.NAI_ENCODING_UTF8_MASK : 0)); + stream.write((byte) realmStrData.length); + stream.write(realmStrData); + stream.write((byte) 1); // EAP Method count + stream.write(TEST_EAP_METHOD_BYTES); + return stream.toByteArray(); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmElementTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmElementTest.java new file mode 100644 index 000000000..6b7aa84c6 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/NAIRealmElementTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.List; + + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.NAIRealmElement}. + */ +@SmallTest +public class NAIRealmElementTest { + /** + * Helper function for returning a ByteBuffer containing raw bytes for NAI Realm Element + * with specified number of NAI Realm Data. + * + * @param dataCount The number of NAI Realm Data to be added to the buffer + * @return {@link ByteBuffer} + */ + private static ByteBuffer getTestBufferWithNAIRealmData(int dataCount) { + int dataLength = NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES.length * dataCount; + // 2-bytes for the NAI Realm Data count header. + ByteBuffer buffer = ByteBuffer.allocate(dataLength + 2).order(ByteOrder.LITTLE_ENDIAN); + buffer.putShort((short) dataCount); + for (int i = 0; i < dataCount; i++) { + buffer.put(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES); + } + buffer.position(0); + return buffer; + } + + /** + * Verify that a NAIRealmElement with an empty NAIRealmData list will be returned when parsing + * from an empty buffer. + * + * @throws Exception + */ + @Test + public void parseEmptyBuffer() throws Exception { + assertTrue(NAIRealmElement.parse( + ByteBuffer.wrap(new byte[0])).getRealmDataList().isEmpty()); + } + + /** + * Verify that an expected NAIRealmElement will be returned when parsing a buffer containing + * a NAI Realm Element with single NAI Realm Data. + * + * @throws Exception + */ + @Test + public void parseBufferWithSingleNAIRealmData() throws Exception { + // Setup expected NAIRealmElement. + List<NAIRealmData> realmDataList = new ArrayList<>(); + realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); + NAIRealmElement expected = new NAIRealmElement(realmDataList); + + assertEquals(expected, NAIRealmElement.parse(getTestBufferWithNAIRealmData(1))); + } + + /** + * Verify that an expected NAIRealmElement will be returned when parsing a buffer containing + * a NAI Realm Element with multiple NAI Realm Data. + * + * @throws Exception + */ + @Test + public void parseBufferWithMultipleNAIRealmData() throws Exception { + // Setup expected NAIRealmElement. + List<NAIRealmData> realmDataList = new ArrayList<>(); + realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); + realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); + NAIRealmElement expected = new NAIRealmElement(realmDataList); + + assertEquals(expected, NAIRealmElement.parse(getTestBufferWithNAIRealmData(2))); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/CredentialTypeTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/CredentialTypeTest.java new file mode 100644 index 000000000..14be73d5f --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/CredentialTypeTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.CredentialType}. + */ +@SmallTest +public class CredentialTypeTest { + private static final int TEST_TYPE = CredentialType.CREDENTIAL_TYPE_USIM; + + /** + * Helper function for generating the test buffer. + * + * @return {@link ByteBuffer} + */ + private ByteBuffer getTestBuffer() { + return ByteBuffer.wrap(new byte[] {(byte) TEST_TYPE}); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + CredentialType.parse( + ByteBuffer.wrap(new byte[0]), CredentialType.EXPECTED_LENGTH_VALUE, false); + } + + /** + * Verify that ProtocolException will be thrown when the data length value is not the same + * as the expected + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithInvalidLength() throws Exception { + CredentialType.parse(getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE - 1, false); + } + + /** + * Verify that an expected CredentialType is returned when parsing the buffer for a + * non-tunneled EAP method. + * + * @throws Exception + */ + @Test + public void parseBufferForNonTunneledEAPMethod() throws Exception { + CredentialType expected = + new CredentialType(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, TEST_TYPE); + CredentialType actual = CredentialType.parse( + getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE, false); + assertEquals(expected, actual); + } + + /** + * Verify that an expected CredentialType is returned when parsing the buffer for a + * tunneled EAP method. + * + * @throws Exception + */ + @Test + public void parseBufferForTunneledEAPMethod() throws Exception { + CredentialType expected = new CredentialType( + AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, TEST_TYPE); + CredentialType actual = CredentialType.parse( + getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE, true); + assertEquals(expected, actual); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/EAPMethodTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/EAPMethodTest.java new file mode 100644 index 000000000..51434c212 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/EAPMethodTest.java @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.net.wifi.EAPConstants; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.EAPMethod}. + */ +@SmallTest +public class EAPMethodTest { + /** + * Setup basic test data - contained multiple parameters of the same type. + */ + private static final byte[] TEST_DATA1_BYTES = + new byte[] {0x0B /* length */, 0x0D /* EAP_TLS */, 0x03 /* Auth Param Count */, + 0x05 /* CredentialType */, 0x01, 0x02 /* USIM */, + 0x05 /* CredentialType */, 0x01, 0x06 /* Certificate */, + 0x06 /* Tunneled CredentialType */, 0x01, 0x03 /* NFC */}; + private static final Map<Integer, Set<AuthParam>> TEST_DATA1_AUTH_PARAMS = + new HashMap<>(); + private static final Set<AuthParam> TEST_DATA1_CREDENTIAL_TYPE_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA1_TUNNELED_CREDENTIAL_TYPE_PARAMS = + new HashSet<>(); + static { + TEST_DATA1_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_USIM)); + TEST_DATA1_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_CERTIFICATE)); + + TEST_DATA1_TUNNELED_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, + CredentialType.CREDENTIAL_TYPE_NFC)); + + TEST_DATA1_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, + TEST_DATA1_CREDENTIAL_TYPE_PARAMS); + TEST_DATA1_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, + TEST_DATA1_TUNNELED_CREDENTIAL_TYPE_PARAMS); + } + private static final EAPMethod TEST_DATA1_EAP_METHOD = new EAPMethod( + EAPConstants.EAP_TLS, TEST_DATA1_AUTH_PARAMS); + + /** + * Setup test data for testing an EAP Method containing all types of authentication parameters. + */ + private static final byte[] TEST_DATA2_BYTES = + new byte[] {0x26 /* length */, 0x0D /* EAP_TLS */, 0x07, + // Expanded EAP Method + 0x01, 0x07, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, + // Non-EAP Inner Auth Type + 0x02, 0x01, 0x03 /* AUTH_TYPE_MSCHAP */, + // Inner Auth EAP Method + 0x03, 0x01, 0x1D /* EAP_PEAP */, + // Expanded Inner EAP Method + 0x04, 0x07, 0x01, 0x23, 0x45, 0x56, 0x78, 0x56, 0x12, + // Credential Type + 0x05, 0x01, 0x02 /* USIM */, + // Tunneled Credential Type + 0x06, 0x01, 0x03 /* NFC */, + // Vendor Specific + (byte) 0xDD, 0x04, 0x12, 0x23, 0x45, 0x56}; + private static final Map<Integer, Set<AuthParam>> TEST_DATA2_AUTH_PARAMS = + new HashMap<>(); + private static final Set<AuthParam> TEST_DATA2_EXPANDED_EAP_METHOD_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_NON_EAP_INNER_AUTH_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_INNER_AUTH_EAP_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_EXPANDED_INNER_EAP_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_CREDENTIAL_TYPE_PARAMS = new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_TUNNELED_CREDENTIAL_TYPE_PARAMS = + new HashSet<>(); + private static final Set<AuthParam> TEST_DATA2_VENDOR_SPECIFIC_PARAMS = new HashSet<>(); + static { + TEST_DATA2_EXPANDED_EAP_METHOD_PARAMS.add(new ExpandedEAPMethod( + AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD, 0x122334, 0x45566778L)); + TEST_DATA2_NON_EAP_INNER_AUTH_PARAMS.add(new NonEAPInnerAuth( + NonEAPInnerAuth.AUTH_TYPE_MSCHAP)); + TEST_DATA2_INNER_AUTH_EAP_PARAMS.add(new InnerAuthEAP(EAPConstants.EAP_PEAP)); + TEST_DATA2_EXPANDED_INNER_EAP_PARAMS.add(new ExpandedEAPMethod( + AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD, 0x012345, 0x56785612L)); + TEST_DATA2_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, CredentialType.CREDENTIAL_TYPE_USIM)); + TEST_DATA2_TUNNELED_CREDENTIAL_TYPE_PARAMS.add(new CredentialType( + AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, + CredentialType.CREDENTIAL_TYPE_NFC)); + TEST_DATA2_VENDOR_SPECIFIC_PARAMS.add(new VendorSpecificAuth( + new byte[] {0x12, 0x23, 0x45, 0x56})); + + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD, + TEST_DATA2_EXPANDED_EAP_METHOD_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_NON_EAP_INNER_AUTH_TYPE, + TEST_DATA2_NON_EAP_INNER_AUTH_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_INNER_AUTH_EAP_METHOD_TYPE, + TEST_DATA2_INNER_AUTH_EAP_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD, + TEST_DATA2_EXPANDED_INNER_EAP_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, + TEST_DATA2_CREDENTIAL_TYPE_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, + TEST_DATA2_TUNNELED_CREDENTIAL_TYPE_PARAMS); + TEST_DATA2_AUTH_PARAMS.put(AuthParam.PARAM_TYPE_VENDOR_SPECIFIC, + TEST_DATA2_VENDOR_SPECIFIC_PARAMS); + } + private static final EAPMethod TEST_DATA2_EAP_METHOD = new EAPMethod( + EAPConstants.EAP_TLS, TEST_DATA2_AUTH_PARAMS); + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + EAPMethod.parse(ByteBuffer.wrap(new byte[0])); + } + + /** + * Verify that ProtocolException will be thrown when parsing a truncated buffer + * (missing a byte at the end). + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseTruncatedBuffer() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(TEST_DATA1_BYTES, 0, TEST_DATA1_BYTES.length - 1); + EAPMethod.parse(buffer); + } + + /** + * Verify that the expected EAPMethod is return when parsing a buffer contained + * {@link #TEST_DATA1_BYTES}. + * + * @throws Exception + */ + @Test + public void parseBufferWithTestData1() throws Exception { + assertEquals(TEST_DATA1_EAP_METHOD, EAPMethod.parse(ByteBuffer.wrap(TEST_DATA1_BYTES))); + } + + /** + * Verify that the expected EAPMethod is return when parsing a buffer contained + * {@link #TEST_DATA2_BYTES}. + * + * @throws Exception + */ + @Test + public void parseBufferWithTestData2() throws Exception { + assertEquals(TEST_DATA2_EAP_METHOD, EAPMethod.parse(ByteBuffer.wrap(TEST_DATA2_BYTES))); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/ExpandedEAPMethodTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/ExpandedEAPMethodTest.java new file mode 100644 index 000000000..8f114b2b2 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/ExpandedEAPMethodTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.ExpandedEAPMethod}. + */ +@SmallTest +public class ExpandedEAPMethodTest { + private static final int TEST_VENDOR_ID = 0x123456; + private static final long TEST_VENDOR_TYPE = 0x23456523; + private static final byte[] TEST_DATA_BYTES = + new byte[] {0x12, 0x34, 0x56, 0x23, 0x45, 0x65, 0x23}; + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + ExpandedEAPMethod.parse( + ByteBuffer.wrap(new byte[0]), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false); + } + + /** + * Verify that ProtocolException will be thrown when the data length is not the expected + * length. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithInvalidLength() throws Exception { + ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES), + ExpandedEAPMethod.EXPECTED_LENGTH_VALUE - 1, false); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseBufferWithTruncatedBuffer() throws Exception { + ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES, 0, TEST_DATA_BYTES.length - 1), + ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false); + } + + /** + * Verify that an expected ExpandedEAPMethod is returned when parsing the buffer for a + * non-inner EAP method. + * + * @throws Exception + */ + @Test + public void parseBufferForNonInnerEAPMethod() throws Exception { + ExpandedEAPMethod expected = new ExpandedEAPMethod( + AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE); + ExpandedEAPMethod actual = ExpandedEAPMethod.parse( + ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false); + assertEquals(expected, actual); + } + + /** + * Verify that an expected CredentialType is returned when parsing the buffer for a + * inner EAP method. + * + * @throws Exception + */ + @Test + public void parseBufferForTunneledEAPMethod() throws Exception { + ExpandedEAPMethod expected = new ExpandedEAPMethod( + AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE); + ExpandedEAPMethod actual = ExpandedEAPMethod.parse( + ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, true); + assertEquals(expected, actual); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/InnerAuthEAPTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/InnerAuthEAPTest.java new file mode 100644 index 000000000..a4a813b1e --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/InnerAuthEAPTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.net.wifi.EAPConstants; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.InnerAuthEAP}. + */ +@SmallTest +public class InnerAuthEAPTest { + private static final int TEST_EAP_METHOD_ID = EAPConstants.EAP_TTLS; + + /** + * Helper function for generating the test buffer. + * + * @return {@link ByteBuffer} + */ + private ByteBuffer getTestBuffer() { + return ByteBuffer.wrap(new byte[] {(byte) TEST_EAP_METHOD_ID}); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + InnerAuthEAP.parse(ByteBuffer.wrap(new byte[0]), InnerAuthEAP.EXPECTED_LENGTH_VALUE); + } + + /** + * Verify that ProtocolException will be thrown when the data length is not the expected + * length. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithInvalidLength() throws Exception { + InnerAuthEAP.parse(getTestBuffer(), InnerAuthEAP.EXPECTED_LENGTH_VALUE - 1); + } + + /** + * Verify that an expected InnerAuthEAP is returned when parsing a buffer contained + * the expected EAP method ID. + * + * @throws Exception + */ + @Test + public void parseBuffer() throws Exception { + InnerAuthEAP expected = new InnerAuthEAP(TEST_EAP_METHOD_ID); + InnerAuthEAP actual = + InnerAuthEAP.parse(getTestBuffer(), InnerAuthEAP.EXPECTED_LENGTH_VALUE); + assertEquals(expected, actual); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/NonEAPInnerAuthTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/NonEAPInnerAuthTest.java new file mode 100644 index 000000000..977032117 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/NonEAPInnerAuthTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.NonEAPInnerAuth}. + */ +@SmallTest +public class NonEAPInnerAuthTest { + private static final int TEST_AUTH_TYPE = NonEAPInnerAuth.AUTH_TYPE_MSCHAP; + + /** + * Helper function for generating the test buffer. + * + * @return {@link ByteBuffer} + */ + private ByteBuffer getTestBuffer() { + return ByteBuffer.wrap(new byte[] {(byte) TEST_AUTH_TYPE}); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + NonEAPInnerAuth.parse(ByteBuffer.wrap(new byte[0]), NonEAPInnerAuth.EXPECTED_LENGTH_VALUE); + } + + /** + * Verify that ProtocolException will be thrown when the data length is not the expected + * length. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithInvalidLength() throws Exception { + NonEAPInnerAuth.parse(getTestBuffer(), NonEAPInnerAuth.EXPECTED_LENGTH_VALUE - 1); + } + + /** + * Verify that an expected NonEAPInnerAuth is returned when parsing a buffer contained + * the expected auth type. + * + * @throws Exception + */ + @Test + public void parseBuffer() throws Exception { + NonEAPInnerAuth expected = new NonEAPInnerAuth(TEST_AUTH_TYPE); + NonEAPInnerAuth actual = + NonEAPInnerAuth.parse(getTestBuffer(), NonEAPInnerAuth.EXPECTED_LENGTH_VALUE); + assertEquals(expected, actual); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/VendorSpecificAuthTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/VendorSpecificAuthTest.java new file mode 100644 index 000000000..dbb2d8f7d --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/VendorSpecificAuthTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.VendorSpecificAuth}. + */ +@SmallTest +public class VendorSpecificAuthTest { + private static final byte[] TEST_DATA = new byte[] {0x12, 0x34, 0x45, 0x56}; + + /** + * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + VendorSpecificAuth.parse(ByteBuffer.wrap(new byte[0]), 1); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing from a truncated buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseTruncatedBuffer() throws Exception { + VendorSpecificAuth.parse( + ByteBuffer.wrap(TEST_DATA, 0, TEST_DATA.length - 1), TEST_DATA.length); + } + + /** + * Verify that a VendorSpecificAuth with a empty data array is returned when parsing + * a zero byte from a buffer. + * + * @throws Exception + */ + @Test + public void parseBufferWithZeroLength() throws Exception { + VendorSpecificAuth expected = new VendorSpecificAuth(new byte[0]); + assertEquals(expected, VendorSpecificAuth.parse(ByteBuffer.wrap(TEST_DATA), 0)); + } + + /** + * Verify that an expected VendorSpecificAuth is returned when parsing a buffer contained + * the expected data. + * + * @throws Exception + */ + @Test + public void parseBuffer() throws Exception { + VendorSpecificAuth expected = new VendorSpecificAuth(TEST_DATA); + VendorSpecificAuth actual = + VendorSpecificAuth.parse(ByteBuffer.wrap(TEST_DATA), TEST_DATA.length); + assertEquals(expected, actual); + } +} |