From efe18a90941645b907fa2b50d922e7373303613e Mon Sep 17 00:00:00 2001 From: Peter Qiu Date: Wed, 24 May 2017 09:41:23 -0700 Subject: hotspot2: anqp: add parsing support OSU Providers List ANQP element Bug: 38351209 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I14f26966b073e44291241bd69c4160b91a47e264 --- .../hotspot2/anqp/HSOsuProvidersElementTest.java | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java (limited to 'tests') 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 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)); + } +} -- cgit v1.2.3 From 4c29caec80ebfc32a3c4047679082dca52cc18cc Mon Sep 17 00:00:00 2001 From: Peter Qiu Date: Thu, 25 May 2017 11:00:32 -0700 Subject: hotspot2: parse OSU Providers ANQP element Instead of storing OSU providers list as raw bytes, parse it using the HSOsuProvidersElement. Also do not broadcast the raw bytes to the app, since it will be parsed by us now. The app will be able to retrieve the parsed information via a new API in the future (to be implemented). Bug: 38351209 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Test: manual testing with Boingo Passpoint AP, verify via debug logs Change-Id: Idc15c4b00580d65133bde57ba43be590d50321c0 --- .../server/wifi/hotspot2/PasspointManagerTest.java | 33 ---------------------- .../server/wifi/hotspot2/anqp/ANQPParserTest.java | 25 ++++++++++++++-- 2 files changed, 22 insertions(+), 36 deletions(-) (limited to 'tests') 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; @@ -290,36 +287,6 @@ public class PasspointManagerTest { any(String.class)); } - /** - * 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 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 = 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; @@ -215,6 +216,22 @@ public class ANQPParserTest { return stream.toByteArray(); } + /** + * 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. * @@ -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, -- cgit v1.2.3