diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-05-26 19:58:59 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-05-26 19:59:00 +0000 |
commit | 3032286ca3dccf48de56d1738ec0beaf4b2c9216 (patch) | |
tree | c6cc5d3fa113fe4a178e05cfffff1c2c0da2ea56 /tests | |
parent | ee6798b35c709aa38e433c163602998466c151e5 (diff) | |
parent | 4c29caec80ebfc32a3c4047679082dca52cc18cc (diff) |
Merge changes Idc15c4b0,I14f26966
* changes:
hotspot2: parse OSU Providers ANQP element
hotspot2: anqp: add parsing support OSU Providers List ANQP element
Diffstat (limited to 'tests')
3 files changed, 136 insertions, 36 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java index 70ae354b9..b55aaafde 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java @@ -18,9 +18,7 @@ package com.android.server.wifi.hotspot2; import static android.net.wifi.WifiManager.ACTION_PASSPOINT_DEAUTH_IMMINENT; import static android.net.wifi.WifiManager.ACTION_PASSPOINT_ICON; -import static android.net.wifi.WifiManager.ACTION_PASSPOINT_OSU_PROVIDERS_LIST; import static android.net.wifi.WifiManager.ACTION_PASSPOINT_SUBSCRIPTION_REMEDIATION; -import static android.net.wifi.WifiManager.EXTRA_ANQP_ELEMENT_DATA; import static android.net.wifi.WifiManager.EXTRA_BSSID_LONG; import static android.net.wifi.WifiManager.EXTRA_DELAY; import static android.net.wifi.WifiManager.EXTRA_ESS; @@ -72,7 +70,6 @@ import com.android.server.wifi.WifiNative; import com.android.server.wifi.hotspot2.anqp.ANQPElement; import com.android.server.wifi.hotspot2.anqp.Constants.ANQPElementType; import com.android.server.wifi.hotspot2.anqp.DomainNameElement; -import com.android.server.wifi.hotspot2.anqp.RawByteElement; import com.android.server.wifi.util.ScanResultUtil; import org.junit.Before; @@ -291,36 +288,6 @@ public class PasspointManagerTest { } /** - * Verify that the ANQP elements will be added to the AQNP cache and an - * {@link WifiManager#ACTION_PASSPOINT_OSU_PROVIDER_LIST} intent will be broadcasted when - * receiving an ANQP response containing OSU Providers element. - * - * @throws Exception - */ - @Test - public void anqpResponseWithOSUProviders() throws Exception { - Map<ANQPElementType, ANQPElement> anqpElementMap = new HashMap<>(); - byte[] testData = new byte[] {0x12, 0x34, 0x56, 0x78}; - anqpElementMap.put(ANQPElementType.HSOSUProviders, - new RawByteElement(ANQPElementType.HSOSUProviders, testData)); - - when(mAnqpRequestManager.onRequestCompleted(TEST_BSSID, true)).thenReturn(TEST_ANQP_KEY); - mCallbacks.onANQPResponse(TEST_BSSID, anqpElementMap); - verify(mAnqpCache).addEntry(TEST_ANQP_KEY, anqpElementMap); - - // Verify the broadcast intent for OSU providers. - ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class); - verify(mContext).sendBroadcastAsUser(intent.capture(), eq(UserHandle.ALL), - eq(android.Manifest.permission.ACCESS_WIFI_STATE)); - assertEquals(ACTION_PASSPOINT_OSU_PROVIDERS_LIST, intent.getValue().getAction()); - assertTrue(intent.getValue().getExtras().containsKey(EXTRA_BSSID_LONG)); - assertEquals(TEST_BSSID, intent.getValue().getExtras().getLong(EXTRA_BSSID_LONG)); - assertTrue(intent.getValue().getExtras().containsKey(EXTRA_ANQP_ELEMENT_DATA)); - assertTrue(Arrays.equals(testData, - intent.getValue().getExtras().getByteArray(EXTRA_ANQP_ELEMENT_DATA))); - } - - /** * Verify that no ANQP elements will be added to the ANQP cache on receiving a successful * response for a request that's not sent by us. * diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ANQPParserTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ANQPParserTest.java index 8f019e017..59332e4fb 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ANQPParserTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/ANQPParserTest.java @@ -18,6 +18,7 @@ package com.android.server.wifi.hotspot2.anqp; import static org.junit.Assert.assertEquals; +import android.net.wifi.WifiSsid; import android.test.suitebuilder.annotation.SmallTest; import org.junit.Test; @@ -216,6 +217,22 @@ public class ANQPParserTest { } /** + * Helper function for generating payload for a Hotspot 2.0 OSU Providers List ANQP + * element. + * + * @param osuSsidBytes Bytes of OSU SSID + * @return byte[] + */ + private static byte[] getHSOsuProvidersPayload(byte[] osuSsidBytes) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write((byte) osuSsidBytes.length); + out.write(osuSsidBytes); + out.write((byte) 1); + out.write(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO_RAW_BYTES); + return out.toByteArray(); + } + + /** * Helper function for generating payload for a list of I18Name. * * @param language Array of language @@ -473,10 +490,12 @@ public class ANQPParserTest { */ @Test public void parseHSOUSProvidersElement() throws Exception { - byte[] data = new byte[10]; + byte[] osuSsidBytes = "Test SSID".getBytes(StandardCharsets.UTF_8); + byte[] data = getHSOsuProvidersPayload(osuSsidBytes); - RawByteElement expected = - new RawByteElement(Constants.ANQPElementType.HSOSUProviders, data); + HSOsuProvidersElement expected = new HSOsuProvidersElement( + WifiSsid.createFromByteArray(osuSsidBytes), + Arrays.asList(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO)); ByteBuffer buffer = ByteBuffer.wrap(data); assertEquals(expected, diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java new file mode 100644 index 000000000..c4f73871b --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2017 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.net.wifi.WifiSsid; +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.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSOsuProvidersElement}. + */ +@SmallTest +public class HSOsuProvidersElementTest { + private static final byte[] TEST_OSU_SSID_BYTES = "Test SSID".getBytes(StandardCharsets.UTF_8); + private static final WifiSsid TEST_OSU_SSID = + WifiSsid.createFromByteArray(TEST_OSU_SSID_BYTES); + private static final List<OsuProviderInfo> TEST_PROVIDER_LIST = + Arrays.asList(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO); + + private static final HSOsuProvidersElement TEST_OSU_PROVIDERS_ELEMENT = + new HSOsuProvidersElement(TEST_OSU_SSID, TEST_PROVIDER_LIST); + + /** + * Utility function for generating test data. + * + * @param osuSsidBytes The OSU SSID bytes + * @return byte[] + */ + private static byte[] getTestData(byte[] osuSsidBytes) { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write((byte) osuSsidBytes.length); + out.write(osuSsidBytes); + out.write((byte) TEST_PROVIDER_LIST.size()); + out.write(OsuProviderInfoTestUtil.TEST_OSU_PROVIDER_INFO_RAW_BYTES); + return out.toByteArray(); + } catch (IOException e) { + return null; + } + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + HSOsuProvidersElement.parse(ByteBuffer.allocate(0)); + } + + /** + * 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 parseTruncatedBuffer() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES)); + buffer.limit(buffer.remaining() - 1); + HSOsuProvidersElement.parse(buffer); + } + + /** + * Verify that ProtocolException will be thrown when parsing a buffer containing an + * invalid OSU SSID. + * + * @throws Exception + */ + @Test(expected = ProtocolException.class) + public void parseBufferWithInvalidLength() throws Exception { + byte[] invalidSsidBytes = new byte[HSOsuProvidersElement.MAXIMUM_OSU_SSID_LENGTH + 1]; + ByteBuffer buffer = ByteBuffer.wrap(getTestData(invalidSsidBytes)); + HSOsuProvidersElement.parse(buffer); + } + + /** + * Verify that an expected {@link HSOsuProvidersElement} will be returned when parsing a buffer + * containing pre-defined test data. + * + * @throws Exception + */ + @Test + public void parseBufferWithTestData() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(getTestData(TEST_OSU_SSID_BYTES)); + assertEquals(TEST_OSU_PROVIDERS_ELEMENT, HSOsuProvidersElement.parse(buffer)); + } +} |