summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Qiu <zqiu@google.com>2017-05-24 09:41:23 -0700
committerPeter Qiu <zqiu@google.com>2017-05-26 09:16:44 -0700
commitefe18a90941645b907fa2b50d922e7373303613e (patch)
treeb485144de253a70bf4c1c0532d1432685f6e3ae8
parentbcbabd9b007a0ab39dbed13d13e56c46ee0f25c3 (diff)
hotspot2: anqp: add parsing support OSU Providers List ANQP element
Bug: 38351209 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I14f26966b073e44291241bd69c4160b91a47e264
-rw-r--r--service/java/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElement.java118
-rw-r--r--tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElementTest.java114
2 files changed, 232 insertions, 0 deletions
diff --git a/service/java/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElement.java b/service/java/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElement.java
new file mode 100644
index 000000000..146a44ca9
--- /dev/null
+++ b/service/java/com/android/server/wifi/hotspot2/anqp/HSOsuProvidersElement.java
@@ -0,0 +1,118 @@
+/*
+ * 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 android.net.wifi.WifiSsid;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.net.ProtocolException;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * The OSU Providers List vendor specific ANQP Element,
+ * Wi-Fi Alliance Hotspot 2.0 (Release 2) Technical Specification - Version 5.00,
+ * section 4.8.
+ *
+ * Format:
+ *
+ * | OSU SSID Length | OSU SSID | Number of OSU Providers | Provider #1 | ...
+ * 1 variable 1 variable
+ *
+ */
+public class HSOsuProvidersElement extends ANQPElement {
+ /**
+ * Maximum length for a SSID. Refer to IEEE 802.11-2012 Section 8.4.2.2
+ * for more info.
+ */
+ @VisibleForTesting
+ public static final int MAXIMUM_OSU_SSID_LENGTH = 32;
+
+ private final WifiSsid mOsuSsid;
+ private final List<OsuProviderInfo> mProviders;
+
+ @VisibleForTesting
+ public HSOsuProvidersElement(WifiSsid osuSsid, List<OsuProviderInfo> providers) {
+ super(Constants.ANQPElementType.HSOSUProviders);
+ mOsuSsid = osuSsid;
+ mProviders = providers;
+ }
+
+ /**
+ * Parse a HSOsuProvidersElement from the given buffer.
+ *
+ * @param payload The buffer to read from
+ * @return {@link HSOsuProvidersElement}
+ * @throws BufferUnderflowException
+ * @throws ProtocolException
+ */
+ public static HSOsuProvidersElement parse(ByteBuffer payload)
+ throws ProtocolException {
+ int ssidLength = payload.get() & 0xFF;
+ if (ssidLength > MAXIMUM_OSU_SSID_LENGTH) {
+ throw new ProtocolException("Invalid SSID length: " + ssidLength);
+ }
+ byte[] ssidBytes = new byte[ssidLength];
+ payload.get(ssidBytes);
+
+ int numProviders = payload.get() & 0xFF;
+ List<OsuProviderInfo> providers = new ArrayList<>();
+ while (numProviders > 0) {
+ providers.add(OsuProviderInfo.parse(payload));
+ numProviders--;
+ }
+
+ return new HSOsuProvidersElement(WifiSsid.createFromByteArray(ssidBytes), providers);
+ }
+
+ public WifiSsid getOsuSsid() {
+ return mOsuSsid;
+ }
+
+ public List<OsuProviderInfo> getProviders() {
+ return Collections.unmodifiableList(mProviders);
+ }
+
+ @Override
+ public boolean equals(Object thatObject) {
+ if (this == thatObject) {
+ return true;
+ }
+ if (!(thatObject instanceof HSOsuProvidersElement)) {
+ return false;
+ }
+ HSOsuProvidersElement that = (HSOsuProvidersElement) thatObject;
+ return (mOsuSsid == null ? that.mOsuSsid == null : mOsuSsid.equals(that.mOsuSsid))
+ && (mProviders == null ? that.mProviders == null
+ : mProviders.equals(that.mProviders));
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(mOsuSsid, mProviders);
+ }
+
+ @Override
+ public String toString() {
+ return "OSUProviders{" + "mOsuSsid=" + mOsuSsid + ", mProviders=" + mProviders + "}";
+ }
+}
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));
+ }
+}