diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2016-12-22 05:40:37 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-12-22 05:40:37 +0000 |
commit | 94203bb70f2afd3b2b3ac50bda8aa5900b78663b (patch) | |
tree | ee09ef5fded25b1b4095650eb53fb95a59c10e13 /tests | |
parent | 37151ab9156d77cc98aa4fc8c569de045e840c27 (diff) | |
parent | 54481f724e41249c4e036a9f59e8cb3e6fb821d8 (diff) |
Merge "hotspot2: ANQP elements cleanup Part 3"
Diffstat (limited to 'tests')
3 files changed, 345 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTest.java new file mode 100644 index 000000000..50ab18935 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTest.java @@ -0,0 +1,111 @@ +/* + * 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.assertNull; + +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.ArrayList; +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.CellularNetwork}. + */ +@SmallTest +public class CellularNetworkTest { + private static final byte[] TEST_PLMN_BYTES_1 = new byte[] {0x12, 0x34, 0x56}; + private static final String TEST_PLMN_STRING_1 = "214653"; + private static final byte[] TEST_PLMN_BYTES_2 = new byte[] {0x13, (byte) 0xF9, 0x32}; + private static final String TEST_PLMN_STRING_2 = "31923"; + + /** + * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseBufferWithEmptyBuffer() throws Exception { + CellularNetwork.parse(ByteBuffer.allocate(0)); + } + + /** + * Verify that a null will be returned when parsing a buffer contained an unsupported IEI type. + * + * @throws Exception + */ + @Test + public void parseBufferWithInvalidIEIType() throws Exception { + byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; + byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(1, plmnsData); + assertNull(CellularNetwork.parse(ByteBuffer.wrap(testData))); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer + * (missing a byte at the end). + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseBufferWithIncompleteData() throws Exception { + byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; + byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(plmnsData); + CellularNetwork.parse(ByteBuffer.wrap(testData, 0, testData.length - 1)); + } + + /** + * Verify that ProtocolException will be thrown when IEI size and the PLMN count doesn't + * match. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithMismatchIEISizeAndPLMNCount() throws Exception { + byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; + // Get test data with IEI size set to incorrect value. + byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI( + CellularNetwork.IEI_TYPE_PLMN_LIST, plmnsData, true); + CellularNetwork.parse(ByteBuffer.wrap(testData)); + } + + /** + * Verify that the expected ProtocolPortTuple is returned when parsing a buffer contained + * the test data. + * + * @throws Exception + */ + @Test + public void parseBufferWithTestData() throws Exception { + byte[][] plmnsData = new byte[][] {TEST_PLMN_BYTES_1, TEST_PLMN_BYTES_2}; + byte[] testData = CellularNetworkTestUtil.formatPLMNListIEI(plmnsData); + + // Setup the expected CellularNetwork. + List<String> plmnList = new ArrayList<>(); + plmnList.add(TEST_PLMN_STRING_1); + plmnList.add(TEST_PLMN_STRING_2); + CellularNetwork expected = new CellularNetwork(plmnList); + + assertEquals(expected, CellularNetwork.parse(ByteBuffer.wrap(testData))); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTestUtil.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTestUtil.java new file mode 100644 index 000000000..dad2919f0 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/CellularNetworkTestUtil.java @@ -0,0 +1,94 @@ +/* + * 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 java.io.ByteArrayOutputStream; +import java.io.IOException; + +/** + * Utility class for formatting IEI (Information Element Identity) data for testing. + */ +public class CellularNetworkTestUtil { + /** + * Format and return PLMN List IEI with the given PLMN list data. + * + * @param plmnList The array of PLMN data + * @return byte[] + * @throws IOException + */ + public static byte[] formatPLMNListIEI(byte[][] plmnList) throws IOException { + return formatPLMNListIEI(CellularNetwork.IEI_TYPE_PLMN_LIST, plmnList); + } + + /** + * Format and return PLMN List IEI with the given IEI type and PLMN list data. This + * allows the test to use an invalid IEI type for testing purpose. + * + * @param ieiType The IEI type + * @param plmnList The array of PLMN data + * @return byte[] + * @throws IOException + */ + public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList) throws IOException { + return formatPLMNListIEI(ieiType, plmnList, false); + } + + /** + * Format and return PLMN List IEI with the given IEI type and PLMN list data. This also + * allows the test to intentionally setting an incorrect size value. + * + * @param ieiType The IEI type + * @param plmnList The array of PLMN data + * @param setWrongSize Flag for setting incorrect IEI size + * @return byte[] + * @throws IOException + */ + public static byte[] formatPLMNListIEI(int ieiType, byte[][] plmnList, boolean setWrongSize) + throws IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + // Calculate the total bytes for all the PLMNs. + int plmnsSize = getDataSize(plmnList); + + // Use incorrect size intentionally. + if (setWrongSize) plmnsSize -= 1; + + stream.write((byte) ieiType); + // One extra byte for the PLMN count field. + stream.write((byte) ((plmnsSize + 1) & CellularNetwork.IEI_CONTENT_LENGTH_MASK)); + stream.write((byte) plmnList.length); + for (byte[] plmn : plmnList) { + stream.write(plmn); + } + + return stream.toByteArray(); + } + + /** + * Return the number of bytes in a 2D array. + * + * @param dataArray The 2D array + * @return The number of bytes in the 2D array + */ + public static int getDataSize(byte[][] dataArray) { + int size = 0; + for (byte[] data : dataArray) { + size += data.length; + } + return size; + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ThreeGPPNetworkElementTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ThreeGPPNetworkElementTest.java new file mode 100644 index 000000000..02d45ef46 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ThreeGPPNetworkElementTest.java @@ -0,0 +1,140 @@ +/* + * 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.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.ProtocolException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.ThreeGPPNetworkElement}. + */ +@SmallTest +public class ThreeGPPNetworkElementTest { + private static final byte[][] TEST_NETWORK1_PLMN_BYTES = + new byte[][] { new byte[] {0x21, 0x63, 0x54}, + new byte[] {0x43, (byte) 0x85, 0x76} }; + private static final List<String> TEST_NETWORK1_PLMN_LIST = new ArrayList<>(); + static { + TEST_NETWORK1_PLMN_LIST.add("123456"); + TEST_NETWORK1_PLMN_LIST.add("345678"); + } + private static final CellularNetwork TEST_NETWORK1 = + new CellularNetwork(TEST_NETWORK1_PLMN_LIST); + + private static final byte[][] TEST_NETWORK2_PLMN_BYTES = + new byte[][] { new byte[] {(byte) 0x87, 0x29, 0x10}, + new byte[] {0x62, (byte) 0xF5, 0x73} }; + private static final List<String> TEST_NETWORK2_PLMN_LIST = new ArrayList<>(); + static { + TEST_NETWORK2_PLMN_LIST.add("789012"); + TEST_NETWORK2_PLMN_LIST.add("26537"); + } + private static final CellularNetwork TEST_NETWORK2 = + new CellularNetwork(TEST_NETWORK2_PLMN_LIST); + + /** + * Helper function for generating test data. + * + * @param version The GUD version number + * @param ieiList The array containing IEI data + * @return byte[] + * @throws IOException + */ + private static byte[] getTestData(int version, byte[][] ieiList) + throws IOException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + int totalIeiSize = CellularNetworkTestUtil.getDataSize(ieiList); + stream.write((byte) version); + stream.write((byte) totalIeiSize); + for (byte[] iei : ieiList) { + stream.write(iei); + } + return stream.toByteArray(); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseBufferWithEmptyBuffer() throws Exception { + ThreeGPPNetworkElement.parse(ByteBuffer.allocate(0)); + } + + /** + * Verify that ProtocolException will be thrown when parsing an buffer contained + * an unsupported version number. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithUnsupportedVersionNumber() throws Exception { + byte[][] testIeiList = new byte[][] { + CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) }; + byte[] testData = getTestData(1, testIeiList); + ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData)); + } + + /** + * Verify that Protocol will be thrown when parsing a truncated buffer (missing a + * byte at the end), which will cause a inconsistency between the length value and + * the buffer size. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithIncompleteData() throws Exception { + byte[][] testIeiList = new byte[][] { + CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES) }; + byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList); + ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData, 0, testData.length - 1)); + } + + /** + * Verify that the expected ThreeGPPNetworkElement is returned when parsing a buffer contained + * the test data. + * + * @throws Exception + */ + @Test + public void parseBufferWithTestData() throws Exception { + byte[][] testIeiList = new byte[][] { + CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK1_PLMN_BYTES), + CellularNetworkTestUtil.formatPLMNListIEI(TEST_NETWORK2_PLMN_BYTES) }; + byte[] testData = getTestData(ThreeGPPNetworkElement.GUD_VERSION_1, testIeiList); + + // Setup the expected ThreeGPPNetworkElement. + List<CellularNetwork> networkList = new ArrayList<>(); + networkList.add(TEST_NETWORK1); + networkList.add(TEST_NETWORK2); + ThreeGPPNetworkElement expected = new ThreeGPPNetworkElement(networkList); + + assertEquals(expected, ThreeGPPNetworkElement.parse(ByteBuffer.wrap(testData))); + } +} |