From 55840d018efd8a3d1d35883e6a5774aad95e99eb Mon Sep 17 00:00:00 2001 From: Peter Qiu Date: Thu, 1 Jun 2017 13:45:30 -0700 Subject: [PATCH] wifi: hotspot2: implement Parcelable class for OSU provider Bug: 62235196 Test: frameworks/base/wifi/tests/runtests.sh Change-Id: I3327981569fccbe20aba780f4c3f0f5bcb432f31 --- .../net/wifi/hotspot2/OsuProvider.aidl | 19 ++ .../net/wifi/hotspot2/OsuProvider.java | 231 ++++++++++++++++++ .../net/wifi/hotspot2/OsuProviderTest.java | 125 ++++++++++ 3 files changed, 375 insertions(+) create mode 100644 wifi/java/android/net/wifi/hotspot2/OsuProvider.aidl create mode 100644 wifi/java/android/net/wifi/hotspot2/OsuProvider.java create mode 100644 wifi/tests/src/android/net/wifi/hotspot2/OsuProviderTest.java diff --git a/wifi/java/android/net/wifi/hotspot2/OsuProvider.aidl b/wifi/java/android/net/wifi/hotspot2/OsuProvider.aidl new file mode 100644 index 0000000000000..23d0f22e866b7 --- /dev/null +++ b/wifi/java/android/net/wifi/hotspot2/OsuProvider.aidl @@ -0,0 +1,19 @@ +/* + * 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 android.net.wifi.hotspot2; + +parcelable OsuProvider; diff --git a/wifi/java/android/net/wifi/hotspot2/OsuProvider.java b/wifi/java/android/net/wifi/hotspot2/OsuProvider.java new file mode 100644 index 0000000000000..25dcdd8f3fff2 --- /dev/null +++ b/wifi/java/android/net/wifi/hotspot2/OsuProvider.java @@ -0,0 +1,231 @@ +/* + * 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 android.net.wifi.hotspot2; + +import android.graphics.drawable.Icon; +import android.net.Uri; +import android.net.wifi.WifiSsid; +import android.os.Parcel; +import android.os.Parcelable; +import android.text.TextUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +/** + * Contained information for a Hotspot 2.0 OSU (Online Sign-Up provider). + * + * @hide + */ +public final class OsuProvider implements Parcelable { + /** + * OSU (Online Sign-Up) method: OMA DM (Open Mobile Alliance Device Management). + * For more info, refer to Section 8.3 of the Hotspot 2.0 Release 2 Technical Specification. + */ + public static final int METHOD_OMA_DM = 0; + + /** + * OSU (Online Sign-Up) method: SOAP XML SPP (Subscription Provisioning Protocol). + * For more info, refer to Section 8.4 of the Hotspot 2.0 Release 2 Technical Specification. + */ + public static final int METHOD_SOAP_XML_SPP = 1; + + /** + * SSID of the network to connect for service sign-up. + */ + private final WifiSsid mOsuSsid; + + /** + * Friendly name of the OSU provider. + */ + private final String mFriendlyName; + + /** + * Description of the OSU provider. + */ + private final String mServiceDescription; + + /** + * URI to browse to for service sign-up. + */ + private final Uri mServerUri; + + /** + * Network Access Identifier used for authenticating with the OSU network when OSEN is used. + */ + private final String mNetworkAccessIdentifier; + + /** + * List of OSU (Online Sign-Up) method supported. + */ + private final List mMethodList; + + /** + * Icon data for the OSU (Online Sign-Up) provider. + */ + private final Icon mIcon; + + public OsuProvider(WifiSsid osuSsid, String friendlyName, String serviceDescription, + Uri serverUri, String nai, List methodList, Icon icon) { + mOsuSsid = osuSsid; + mFriendlyName = friendlyName; + mServiceDescription = serviceDescription; + mServerUri = serverUri; + mNetworkAccessIdentifier = nai; + if (methodList == null) { + mMethodList = new ArrayList<>(); + } else { + mMethodList = new ArrayList<>(methodList); + } + mIcon = icon; + } + + /** + * Copy constructor. + * + * @param source The source to copy from + */ + public OsuProvider(OsuProvider source) { + if (source == null) { + mOsuSsid = null; + mFriendlyName = null; + mServiceDescription = null; + mServerUri = null; + mNetworkAccessIdentifier = null; + mMethodList = new ArrayList<>(); + mIcon = null; + return; + } + + mOsuSsid = source.mOsuSsid; + mFriendlyName = source.mFriendlyName; + mServiceDescription = source.mServiceDescription; + mServerUri = source.mServerUri; + mNetworkAccessIdentifier = source.mNetworkAccessIdentifier; + if (source.mMethodList == null) { + mMethodList = new ArrayList<>(); + } else { + mMethodList = new ArrayList<>(source.mMethodList); + } + mIcon = source.mIcon; + } + + public WifiSsid getOsuSsid() { + return mOsuSsid; + } + + public String getFriendlyName() { + return mFriendlyName; + } + + public String getServiceDescription() { + return mServiceDescription; + } + + public Uri getServerUri() { + return mServerUri; + } + + public String getNetworkAccessIdentifier() { + return mNetworkAccessIdentifier; + } + + public List getMethodList() { + return Collections.unmodifiableList(mMethodList); + } + + public Icon getIcon() { + return mIcon; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeParcelable(mOsuSsid, flags); + dest.writeString(mFriendlyName); + dest.writeString(mServiceDescription); + dest.writeParcelable(mServerUri, flags); + dest.writeString(mNetworkAccessIdentifier); + dest.writeList(mMethodList); + dest.writeParcelable(mIcon, flags); + } + + @Override + public boolean equals(Object thatObject) { + if (this == thatObject) { + return true; + } + if (!(thatObject instanceof OsuProvider)) { + return false; + } + OsuProvider that = (OsuProvider) thatObject; + return (mOsuSsid == null ? that.mOsuSsid == null : mOsuSsid.equals(that.mOsuSsid)) + && TextUtils.equals(mFriendlyName, that.mFriendlyName) + && TextUtils.equals(mServiceDescription, that.mServiceDescription) + && (mServerUri == null ? that.mServerUri == null + : mServerUri.equals(that.mServerUri)) + && TextUtils.equals(mNetworkAccessIdentifier, that.mNetworkAccessIdentifier) + && (mMethodList == null ? that.mMethodList == null + : mMethodList.equals(that.mMethodList)) + && (mIcon == null ? that.mIcon == null : mIcon.sameAs(that.mIcon)); + } + + @Override + public int hashCode() { + return Objects.hash(mOsuSsid, mFriendlyName, mServiceDescription, mServerUri, + mNetworkAccessIdentifier, mMethodList, mIcon); + } + + @Override + public String toString() { + return "OsuProvider{mOsuSsid=" + mOsuSsid + + " mFriendlyName=" + mFriendlyName + + " mServiceDescription=" + mServiceDescription + + " mServerUri=" + mServerUri + + " mNetworkAccessIdentifier=" + mNetworkAccessIdentifier + + " mMethodList=" + mMethodList + + " mIcon=" + mIcon; + } + + public static final Creator CREATOR = + new Creator() { + @Override + public OsuProvider createFromParcel(Parcel in) { + WifiSsid osuSsid = (WifiSsid) in.readParcelable(null); + String friendlyName = in.readString(); + String serviceDescription = in.readString(); + Uri serverUri = (Uri) in.readParcelable(null); + String nai = in.readString(); + List methodList = new ArrayList<>(); + in.readList(methodList, null); + Icon icon = (Icon) in.readParcelable(null); + return new OsuProvider(osuSsid, friendlyName, serviceDescription, serverUri, + nai, methodList, icon); + } + + @Override + public OsuProvider[] newArray(int size) { + return new OsuProvider[size]; + } + }; +} diff --git a/wifi/tests/src/android/net/wifi/hotspot2/OsuProviderTest.java b/wifi/tests/src/android/net/wifi/hotspot2/OsuProviderTest.java new file mode 100644 index 0000000000000..9670bfab89f80 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/hotspot2/OsuProviderTest.java @@ -0,0 +1,125 @@ +/* + * 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 android.net.wifi.hotspot2; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.graphics.drawable.Icon; +import android.net.Uri; +import android.net.wifi.WifiSsid; +import android.os.Parcel; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Unit tests for {@link android.net.wifi.hotspot2.OsuProvider}. + */ +@SmallTest +public class OsuProviderTest { + private static final WifiSsid TEST_SSID = + WifiSsid.createFromByteArray("TEST SSID".getBytes(StandardCharsets.UTF_8)); + private static final String TEST_FRIENDLY_NAME = "Friendly Name"; + private static final String TEST_SERVICE_DESCRIPTION = "Dummy Service"; + private static final Uri TEST_SERVER_URI = Uri.parse("https://test.com"); + private static final String TEST_NAI = "test.access.com"; + private static final List TEST_METHOD_LIST = + Arrays.asList(OsuProvider.METHOD_SOAP_XML_SPP); + private static final Icon TEST_ICON = Icon.createWithData(new byte[10], 0, 10); + + /** + * Verify parcel write and read consistency for the given {@link OsuProvider}. + * + * @param writeInfo The {@link OsuProvider} to verify + * @throws Exception + */ + private static void verifyParcel(OsuProvider writeInfo) throws Exception { + Parcel parcel = Parcel.obtain(); + writeInfo.writeToParcel(parcel, 0); + + parcel.setDataPosition(0); // Rewind data position back to the beginning for read. + OsuProvider readInfo = OsuProvider.CREATOR.createFromParcel(parcel); + assertEquals(writeInfo, readInfo); + } + + /** + * Verify parcel read/write for an OSU provider containing no information. + * + * @throws Exception + */ + @Test + public void verifyParcelWithEmptyProviderInfo() throws Exception { + verifyParcel(new OsuProvider(null, null, null, null, null, null, null)); + } + + /** + * Verify parcel read/write for an OSU provider containing full information. + * + * @throws Exception + */ + @Test + public void verifyParcelWithFullProviderInfo() throws Exception { + verifyParcel(new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME, TEST_SERVICE_DESCRIPTION, + TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON)); + } + + /** + * Verify copy constructor with a null source. + * @throws Exception + */ + @Test + public void verifyCopyConstructorWithNullSource() throws Exception { + OsuProvider expected = new OsuProvider(null, null, null, null, null, null, null); + assertEquals(expected, new OsuProvider(null)); + } + + /** + * Verify copy constructor with a valid source. + * + * @throws Exception + */ + @Test + public void verifyCopyConstructorWithValidSource() throws Exception { + OsuProvider source = new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME, TEST_SERVICE_DESCRIPTION, + TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON); + assertEquals(source, new OsuProvider(source)); + } + + /** + * Verify getter methods. + * + * @throws Exception + */ + @Test + public void verifyGetters() throws Exception { + OsuProvider provider = new OsuProvider(TEST_SSID, TEST_FRIENDLY_NAME, + TEST_SERVICE_DESCRIPTION, TEST_SERVER_URI, TEST_NAI, TEST_METHOD_LIST, TEST_ICON); + assertTrue(TEST_SSID.equals(provider.getOsuSsid())); + assertTrue(TEST_FRIENDLY_NAME.equals(provider.getFriendlyName())); + assertTrue(TEST_SERVICE_DESCRIPTION.equals(provider.getServiceDescription())); + assertTrue(TEST_SERVER_URI.equals(provider.getServerUri())); + assertTrue(TEST_NAI.equals(provider.getNetworkAccessIdentifier())); + assertTrue(TEST_METHOD_LIST.equals(provider.getMethodList())); + assertTrue(TEST_ICON.sameAs(provider.getIcon())); + } +}