From d5d85c28a7abcef5ce07ff0e42910c5a5649cbed Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 18:15:26 +0800 Subject: [PATCH 1/9] p2p: unit tests for WifiP2pGroupList Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I51f6fb88983ce8d0f45139241954ca1de1553fcf Merged-In: I51f6fb88983ce8d0f45139241954ca1de1553fcf --- .../net/wifi/p2p/WifiP2pGroupListTest.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupListTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupListTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupListTest.java new file mode 100644 index 0000000000000..2402f5bb46580 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupListTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pGroupList} + */ +@SmallTest +public class WifiP2pGroupListTest { + + private static final WifiP2pDevice TEST_GROUP_OWNER_1 = new WifiP2pDevice("aa:bb:cc:dd:ee:f0"); + private static final WifiP2pDevice TEST_GROUP_OWNER_2 = new WifiP2pDevice("aa:bb:cc:dd:ee:f1"); + private static final WifiP2pDevice TEST_GROUP_OWNER_3 = new WifiP2pDevice("aa:bb:cc:dd:ee:f2"); + private static final WifiP2pDevice TEST_GROUP_OWNER_OTHER = + new WifiP2pDevice("aa:bb:cc:dd:ee:f3"); + + private WifiP2pGroup mTestGroup1; + private WifiP2pGroup mTestGroup2; + private WifiP2pGroup mTestGroup3; + private WifiP2pGroup mTestGroup4; + + private WifiP2pGroup createGroup( + int networkId, String networkName, + String passphrase, boolean isGo, + WifiP2pDevice goDev) { + WifiP2pGroup group = new WifiP2pGroup(); + group.setNetworkId(networkId); + group.setNetworkName(networkName); + group.setPassphrase(passphrase); + group.setIsGroupOwner(isGo); + group.setOwner(goDev); + return group; + } + + @Before + public void setUp() throws Exception { + mTestGroup1 = createGroup(0, "testGroup1", "12345678", false, TEST_GROUP_OWNER_1); + mTestGroup2 = createGroup(1, "testGroup2", "12345678", true, TEST_GROUP_OWNER_2); + mTestGroup3 = createGroup(2, "testGroup3", "12345678", false, TEST_GROUP_OWNER_3); + mTestGroup4 = createGroup(3, "testGroup4", "12345678", false, TEST_GROUP_OWNER_1); + } + + /** + * Verify basic operations. + */ + @Test + public void testListOperations() throws Exception { + WifiP2pGroupList list = new WifiP2pGroupList(); + list.add(mTestGroup1); + list.add(mTestGroup2); + list.add(mTestGroup3); + list.add(mTestGroup4); + assertEquals(4, list.getGroupList().size()); + + // in list + assertEquals(mTestGroup2.getNetworkId(), + list.getNetworkId(TEST_GROUP_OWNER_2.deviceAddress)); + assertEquals(TEST_GROUP_OWNER_2.deviceAddress, + list.getOwnerAddr(mTestGroup2.getNetworkId())); + // not in list + assertEquals(-1, list.getNetworkId(TEST_GROUP_OWNER_OTHER.deviceAddress)); + // if there are groups with the same GO, return the first one found. + assertEquals(mTestGroup1.getNetworkId(), + list.getNetworkId(TEST_GROUP_OWNER_1.deviceAddress)); + // identify groups with the same GO, but different network names. + assertEquals(mTestGroup4.getNetworkId(), + list.getNetworkId(TEST_GROUP_OWNER_1.deviceAddress, "testGroup4")); + + list.remove(mTestGroup3.getNetworkId()); + assertEquals(-1, list.getNetworkId(TEST_GROUP_OWNER_3.deviceAddress)); + assertFalse(list.contains(mTestGroup3.getNetworkId())); + + assertEquals(3, list.getGroupList().size()); + + Parcel parcelW = Parcel.obtain(); + list.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pGroupList fromParcel = WifiP2pGroupList.CREATOR.createFromParcel(parcelR); + + assertEquals(list.toString(), fromParcel.toString()); + + list.clear(); + assertEquals(0, list.getGroupList().size()); + + } +} From 97e310111fa8b0ce512661870926caf2bd707436 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 17:41:04 +0800 Subject: [PATCH 2/9] p2p: unit tests for WifiP2pGroup Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I40818ac3ab02facecb0976cf4cd5ab0ca18d84d5 Merged-In: I40818ac3ab02facecb0976cf4cd5ab0ca18d84d5 --- .../net/wifi/p2p/WifiP2pGroupTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupTest.java new file mode 100644 index 0000000000000..9473e42184e3e --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pGroupTest.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pGroup} + */ +@SmallTest +public class WifiP2pGroupTest { + + private static final String INTERFACE = "p2p-p2p0-3"; + private static final int NETWORK_ID = 9; + private static final String NETWORK_NAME = "DIRECT-xy-Hello"; + private static final String PASSPHRASE = "HelloWorld"; + private static final WifiP2pDevice GROUP_OWNER = new WifiP2pDevice("de:ad:be:ef:00:01"); + private static final int FREQUENCY = 5300; + private static final WifiP2pDevice CLIENT_1 = new WifiP2pDevice("aa:bb:cc:dd:ee:01"); + private static final WifiP2pDevice CLIENT_2 = new WifiP2pDevice("aa:bb:cc:dd:ee:02"); + + /** + * Verify setter/getter functions. + */ + @Test + public void testSetterGetter() throws Exception { + WifiP2pGroup group = new WifiP2pGroup(); + + group.setInterface(INTERFACE); + group.setNetworkId(NETWORK_ID); + group.setNetworkName(NETWORK_NAME); + group.setPassphrase(PASSPHRASE); + group.setIsGroupOwner(false); + group.setOwner(GROUP_OWNER); + group.setFrequency(FREQUENCY); + group.addClient(CLIENT_1.deviceAddress); + group.addClient(CLIENT_2); + + assertEquals(INTERFACE, group.getInterface()); + assertEquals(NETWORK_ID, group.getNetworkId()); + assertEquals(NETWORK_NAME, group.getNetworkName()); + assertEquals(PASSPHRASE, group.getPassphrase()); + assertFalse(group.isGroupOwner()); + assertEquals(GROUP_OWNER, group.getOwner()); + assertEquals(FREQUENCY, group.getFrequency()); + + assertFalse(group.isClientListEmpty()); + assertTrue(group.contains(CLIENT_1)); + + assertEquals(2, group.getClientList().size()); + + group.removeClient(CLIENT_1); + group.removeClient(CLIENT_2.deviceAddress); + assertFalse(group.contains(CLIENT_1)); + assertTrue(group.isClientListEmpty()); + + Parcel parcelW = Parcel.obtain(); + group.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pGroup fromParcel = WifiP2pGroup.CREATOR.createFromParcel(parcelR); + + assertEquals(group.toString(), fromParcel.toString()); + + } +} From 50902313c2b14969ddd40126cf0bbb524379d324 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 16:27:12 +0800 Subject: [PATCH 3/9] p2p: unit tests for P2P DNS SD service discovery Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I454441ab024f1a72f2e5a92e08894993be749d3d Merged-In: I454441ab024f1a72f2e5a92e08894993be749d3d --- .../p2p/nsd/WifiP2pDnsSdServiceInfoTest.java | 88 +++++++++++++++++++ .../nsd/WifiP2pDnsSdServiceRequestTest.java | 72 +++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceInfoTest.java create mode 100644 wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceRequestTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceInfoTest.java b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceInfoTest.java new file mode 100644 index 0000000000000..e1cffee361e88 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceInfoTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2019 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.p2p.nsd; + +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * Unit test harness for {@link android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo} + */ +@SmallTest +public class WifiP2pDnsSdServiceInfoTest { + + private static final String INSTANCE_NAME = "MyPrinter"; + private static final String SERVICE_TYPE = "_ipp._tcp"; + private static final String TXTRECORD_PROP_AVAILABLE = "available"; + private static final String TXTRECORD_PROP_AVAILABLE_VISABLE = "visable"; + + private Map mTxtMap = new HashMap<>(); + + @Before + public void setUp() throws Exception { + mTxtMap.put(TXTRECORD_PROP_AVAILABLE, TXTRECORD_PROP_AVAILABLE_VISABLE); + } + + /** + * Verify newInstance API + */ + @Test + public void testNewInstance() throws Exception { + WifiP2pDnsSdServiceInfo info = null; + + // the least arguments + info = WifiP2pDnsSdServiceInfo.newInstance( + INSTANCE_NAME, + SERVICE_TYPE, + null); + + // all arguments are given. + info = WifiP2pDnsSdServiceInfo.newInstance( + INSTANCE_NAME, + SERVICE_TYPE, + mTxtMap); + + // failure case due to no instance name. + try { + info = WifiP2pDnsSdServiceInfo.newInstance( + null, + SERVICE_TYPE, + null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + + // failure case due to no service type. + try { + info = WifiP2pDnsSdServiceInfo.newInstance( + INSTANCE_NAME, + null, + null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + } +} diff --git a/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceRequestTest.java b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceRequestTest.java new file mode 100644 index 0000000000000..7d46a5f3a79db --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pDnsSdServiceRequestTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2019 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.p2p.nsd; + +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest} + */ +@SmallTest +public class WifiP2pDnsSdServiceRequestTest { + + private static final String SERVICE_NAME = "MyPrinter"; + private static final String SERVICE_TYPE = "_ipp._tcp"; + + @Test + public void testNewInstance() throws Exception { + WifiP2pDnsSdServiceRequest request = null; + + // default new instance + request = WifiP2pDnsSdServiceRequest.newInstance(); + + // set service type + request = WifiP2pDnsSdServiceRequest.newInstance(SERVICE_TYPE); + + // set service type + request = WifiP2pDnsSdServiceRequest.newInstance(SERVICE_NAME, SERVICE_TYPE); + + // failure case due to null service type + try { + request = WifiP2pDnsSdServiceRequest.newInstance(null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + + // failure case due to null service name + try { + request = WifiP2pDnsSdServiceRequest.newInstance(SERVICE_NAME, null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + + // failure case due to null service type + try { + request = WifiP2pDnsSdServiceRequest.newInstance(null, SERVICE_TYPE); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + + } +} From 3fbdff2dece04c58816d22979f2aa82b434b649f Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 16:37:19 +0800 Subject: [PATCH 4/9] p2p: unit tests for P2P UPNP service discovery Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: Id3218378e6548ca013484f5f693db1863bf419e1 Merged-In: Id3218378e6548ca013484f5f693db1863bf419e1 --- .../p2p/nsd/WifiP2pUpnpServiceInfoTest.java | 80 +++++++++++++++++++ .../nsd/WifiP2pUpnpServiceRequestTest.java | 49 ++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceInfoTest.java create mode 100644 wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceRequestTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceInfoTest.java b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceInfoTest.java new file mode 100644 index 0000000000000..49ead11065aeb --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceInfoTest.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2019 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.p2p.nsd; + +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * Unit test harness for {@link android.net.wifi.p2p.nsd.WifiP2pUpnpServiceInfo} + */ +@SmallTest +public class WifiP2pUpnpServiceInfoTest { + + private static final String UUID = "6859dede-8574-59ab-9332-123456789012"; + private static final String DEVICE = "aa:bb:cc:dd:ee:ff"; + + private List mServiceList = new ArrayList<>(); + + @Before + public void setUp() throws Exception { + mServiceList.add("urn:schemas-upnp-org:service:ContentDirectory:1"); + } + + /** + * Verify newInstance API + */ + @Test + public void testNewInstance() throws Exception { + WifiP2pUpnpServiceInfo info = null; + + // the least arguments + info = WifiP2pUpnpServiceInfo.newInstance( + UUID, DEVICE, null); + + // all arguments are given. + info = WifiP2pUpnpServiceInfo.newInstance( + UUID, DEVICE, mServiceList); + + // failure case due to no UUID. + try { + info = WifiP2pUpnpServiceInfo.newInstance( + null, DEVICE, null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + + // failure case due to no device. + try { + info = WifiP2pUpnpServiceInfo.newInstance( + UUID, + null, + null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + } +} diff --git a/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceRequestTest.java b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceRequestTest.java new file mode 100644 index 0000000000000..79930dc833727 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/nsd/WifiP2pUpnpServiceRequestTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2019 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.p2p.nsd; + +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.nsd.WifiP2pUpnpServiceRequest} + */ +@SmallTest +public class WifiP2pUpnpServiceRequestTest { + + @Test + public void testNewInstance() throws Exception { + WifiP2pUpnpServiceRequest request = null; + + // Create a service discovery request to search all UPnP services. + request = WifiP2pUpnpServiceRequest.newInstance(); + + // Create a service discovery request to search specified UPnP services. + request = WifiP2pUpnpServiceRequest.newInstance("ssdp:all"); + + // failure case due to null target string + try { + request = WifiP2pUpnpServiceRequest.newInstance(null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + // expected exception. + } + } +} From be65fa391d71300cc677301d64106dd41ea9e63b Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 16:55:27 +0800 Subject: [PATCH 5/9] p2p: unit tests for WifiP2pConfig Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I5cb6c9e0c0125d87672401e0dbd227b646a3a9d4 Merged-In: I5cb6c9e0c0125d87672401e0dbd227b646a3a9d4 --- .../net/wifi/p2p/WifiP2pConfigTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java index 01a4c53d618b5..41f109a637597 100644 --- a/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java @@ -16,8 +16,11 @@ package android.net.wifi.p2p; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import android.os.Parcel; + import androidx.test.filters.SmallTest; import org.junit.Test; @@ -27,6 +30,8 @@ import org.junit.Test; */ @SmallTest public class WifiP2pConfigTest { + + private static final String DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff"; /** * Check network name setter */ @@ -113,4 +118,43 @@ public class WifiP2pConfigTest { // expected exception. } } + + @Test + /* + * Verify WifiP2pConfig basic operations + */ + public void testWifiP2pConfig() throws Exception { + WifiP2pConfig config = new WifiP2pConfig(); + config.deviceAddress = DEVICE_ADDRESS; + + WifiP2pConfig copiedConfig = new WifiP2pConfig(config); + // no equals operator, use toString for comparison. + assertEquals(config.toString(), copiedConfig.toString()); + + Parcel parcelW = Parcel.obtain(); + config.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pConfig configFromParcel = WifiP2pConfig.CREATOR.createFromParcel(parcelR); + + // no equals operator, use toString for comparison. + assertEquals(config.toString(), configFromParcel.toString()); + + } + + @Test + /* + * Verify WifiP2pConfig invalidate API + */ + public void testInvalidate() throws Exception { + WifiP2pConfig config = new WifiP2pConfig(); + config.deviceAddress = DEVICE_ADDRESS; + config.invalidate(); + assertEquals("", config.deviceAddress); + } + } From 25eb12cd7f7ceaa15dee988754ddbc5725bc3147 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Mon, 8 Apr 2019 17:40:37 +0800 Subject: [PATCH 6/9] p2p: unit tests for WifiP2pDeviceList Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I53d05702a50509117720a10f50df93e496ff9d65 Merged-In: I53d05702a50509117720a10f50df93e496ff9d65 --- .../net/wifi/p2p/WifiP2pDeviceListTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pDeviceListTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pDeviceListTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pDeviceListTest.java new file mode 100644 index 0000000000000..22936bd352fea --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pDeviceListTest.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; + +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pDeviceList} + */ +@SmallTest +public class WifiP2pDeviceListTest { + + private static final WifiP2pDevice TEST_DEVICE_1 = new WifiP2pDevice("aa:bb:cc:dd:ee:ff"); + private static final WifiP2pDevice TEST_DEVICE_2 = new WifiP2pDevice("aa:bb:cc:dd:ee:f1"); + private static final WifiP2pDevice TEST_DEVICE_3 = new WifiP2pDevice("11:22:33:44:55:66"); + private static final WifiP2pDevice TEST_DEVICE_4 = new WifiP2pDevice("a0:b0:c0:d0:e0:f0"); + + /** + * Verify basic operations. + */ + @Test + public void testListOperations() throws Exception { + WifiP2pDeviceList list = new WifiP2pDeviceList(); + list.update(TEST_DEVICE_1); + list.update(TEST_DEVICE_2); + list.update(TEST_DEVICE_3); + assertEquals(3, list.getDeviceList().size()); + + assertEquals(TEST_DEVICE_1, list.get(TEST_DEVICE_1.deviceAddress)); + assertEquals(null, list.get(TEST_DEVICE_4.deviceAddress)); + + list.remove(TEST_DEVICE_2.deviceAddress); + assertEquals(null, list.get(TEST_DEVICE_2.deviceAddress)); + + list.remove(TEST_DEVICE_3); + assertEquals(null, list.get(TEST_DEVICE_3.deviceAddress)); + + assertEquals(1, list.getDeviceList().size()); + + list.clear(); + assertEquals(0, list.getDeviceList().size()); + + Parcel parcelW = Parcel.obtain(); + list.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pDeviceList fromParcel = WifiP2pDeviceList.CREATOR.createFromParcel(parcelR); + + assertEquals(list.toString(), fromParcel.toString()); + } +} From b20dfbbf236c7151664bbe7e4c24db09575cce53 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Wed, 10 Apr 2019 17:33:20 +0800 Subject: [PATCH 7/9] p2p: unit tests for WifiP2pProvDiscEvent Bug: 130138124 Test: atest FrameworksWifiApiTests Change-Id: I3eb9f7bcd19e1ec074174cdb2a6822fb983976ba Merged-In: I3eb9f7bcd19e1ec074174cdb2a6822fb983976ba --- .../wifi/p2p/WifiP2pProvDiscEventTest.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pProvDiscEventTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pProvDiscEventTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pProvDiscEventTest.java new file mode 100644 index 0000000000000..e3b10a7a52f0b --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pProvDiscEventTest.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pProvDiscEvent} + */ +@SmallTest +public class WifiP2pProvDiscEventTest { + + private static final String DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff"; + private static final String EVENT_PBC_REQ_STRING = "P2P-PROV-DISC-PBC-REQ"; + private static final String EVENT_PBC_RSP_STRING = "P2P-PROV-DISC-PBC-RESP"; + private static final String EVENT_ENTER_PIN_STRING = "P2P-PROV-DISC-ENTER-PIN"; + private static final String EVENT_SHOW_PIN_STRING = "P2P-PROV-DISC-SHOW-PIN"; + private static final String TEST_PIN = "44490607"; + + /** + * Test parsing PBC request event. + */ + @Test + public void testPbcReqEvent() throws Exception { + WifiP2pProvDiscEvent event = + new WifiP2pProvDiscEvent(EVENT_PBC_REQ_STRING + " " + DEVICE_ADDRESS); + assertEquals(WifiP2pProvDiscEvent.PBC_REQ, event.event); + assertEquals(DEVICE_ADDRESS, event.device.deviceAddress); + } + + + /** + * Test parsing PBC response event. + */ + @Test + public void testPbcRespEvent() throws Exception { + WifiP2pProvDiscEvent event = + new WifiP2pProvDiscEvent(EVENT_PBC_RSP_STRING + " " + DEVICE_ADDRESS); + assertEquals(WifiP2pProvDiscEvent.PBC_RSP, event.event); + assertEquals(DEVICE_ADDRESS, event.device.deviceAddress); + } + + /** + * Test parsing ENTER-PIN event. + */ + @Test + public void testEnterPinEvent() throws Exception { + WifiP2pProvDiscEvent event = + new WifiP2pProvDiscEvent(EVENT_ENTER_PIN_STRING + " " + DEVICE_ADDRESS); + assertEquals(WifiP2pProvDiscEvent.ENTER_PIN, event.event); + assertEquals(DEVICE_ADDRESS, event.device.deviceAddress); + } + + /** + * Test parsing SHOW-PIN event. + */ + @Test + public void testShowPinEvent() throws Exception { + WifiP2pProvDiscEvent event = + new WifiP2pProvDiscEvent( + EVENT_SHOW_PIN_STRING + " " + DEVICE_ADDRESS + " " + TEST_PIN); + assertEquals(WifiP2pProvDiscEvent.SHOW_PIN, event.event); + assertEquals(DEVICE_ADDRESS, event.device.deviceAddress); + assertEquals(TEST_PIN, event.pin); + } + + /** + * Test parsing malformed input. + */ + @Test + public void testMalformedInput() throws Exception { + try { + WifiP2pProvDiscEvent event = new WifiP2pProvDiscEvent("OneToken"); + fail("Should throw IllegalArgumentException exception."); + } catch (IllegalArgumentException ex) { + // expected exception. + } + } + + /** + * Test parsing malformed event. + */ + @Test + public void testMalformedEvent() throws Exception { + try { + WifiP2pProvDiscEvent event = new WifiP2pProvDiscEvent("XXX " + DEVICE_ADDRESS); + fail("Should throw IllegalArgumentException exception."); + } catch (IllegalArgumentException ex) { + // expected exception. + } + } +} From 1c4257ddb2e2dfd71311565fd2c7908bf7bee401 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Wed, 10 Apr 2019 17:50:10 +0800 Subject: [PATCH 8/9] p2p: unit tests for WifiP2pInfo Bug: 130138124 Test: atest FrameworksWifiApiTest Change-Id: I7a6bec4faf81ec6e9164b8737588a51503caaaf7 Merged-In: I7a6bec4faf81ec6e9164b8737588a51503caaaf7 --- .../android/net/wifi/p2p/WifiP2pInfoTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pInfoTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pInfoTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pInfoTest.java new file mode 100644 index 0000000000000..e207ca1f3e7aa --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pInfoTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; + +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; + +import java.net.InetAddress; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pInfo} + */ +@SmallTest +public class WifiP2pInfoTest { + + private InetAddress mGroupOnwerIpv4Address; + + @Before + public void setUp() throws Exception { + byte[] ipv4 = {(byte) 192, (byte) 168, (byte) 49, (byte) 1}; + mGroupOnwerIpv4Address = InetAddress.getByAddress(ipv4); + } + + /** + * Verifies copy constructor. + */ + @Test + public void testCopyOperator() throws Exception { + WifiP2pInfo info = new WifiP2pInfo(); + info.groupFormed = true; + info.isGroupOwner = true; + info.groupOwnerAddress = mGroupOnwerIpv4Address; + + WifiP2pInfo copiedInfo = new WifiP2pInfo(info); + + // no equals operator, use toString for data comparison. + assertEquals(info.toString(), copiedInfo.toString()); + } + + /** + * Verifies parcel serialization/deserialization. + */ + @Test + public void testParcelOperation() throws Exception { + WifiP2pInfo info = new WifiP2pInfo(); + info.groupFormed = true; + info.isGroupOwner = true; + info.groupOwnerAddress = mGroupOnwerIpv4Address; + + Parcel parcelW = Parcel.obtain(); + info.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pInfo fromParcel = WifiP2pInfo.CREATOR.createFromParcel(parcelR); + + // no equals operator, use toString for data comparison. + assertEquals(info.toString(), fromParcel.toString()); + } +} From 834487249dc6b770ac3c30a23d8638d67b3ff557 Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Wed, 10 Apr 2019 18:37:58 +0800 Subject: [PATCH 9/9] p2p: unit tests for WifiP2pWfdInfo Bug: 130138124 Test: atest FrameworksWifiApiTest Change-Id: I7368f9b4f3e67985f404f701e92dfe82286935be Merged-In: I7368f9b4f3e67985f404f701e92dfe82286935be --- .../net/wifi/p2p/WifiP2pWfdInfoTest.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pWfdInfoTest.java diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pWfdInfoTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pWfdInfoTest.java new file mode 100644 index 0000000000000..d2f11688e4e50 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pWfdInfoTest.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2019 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.p2p; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pWfdInfo} + */ +@SmallTest +public class WifiP2pWfdInfoTest { + + private static final int TEST_CTRL_PORT = 9999; + private static final int TEST_MAX_TPUT = 1024; + + private WifiP2pWfdInfo mSourceInfo = new WifiP2pWfdInfo( + 0, + TEST_CTRL_PORT, + TEST_MAX_TPUT); + + @Before + public void setUp() { + // initialize device info flags. + mSourceInfo.setDeviceType(WifiP2pWfdInfo.WFD_SOURCE); + mSourceInfo.setSessionAvailable(true); + } + + /** + * Verifies setters/getters. + */ + @Test + public void testSettersGetters() throws Exception { + WifiP2pWfdInfo info = new WifiP2pWfdInfo(); + + info.setWfdEnabled(true); + assertTrue(info.isWfdEnabled()); + + info.setDeviceType(WifiP2pWfdInfo.WFD_SOURCE); + assertEquals(WifiP2pWfdInfo.WFD_SOURCE, info.getDeviceType()); + + info.setCoupledSinkSupportAtSource(true); + assertTrue(info.isCoupledSinkSupportedAtSource()); + + info.setCoupledSinkSupportAtSink(true); + assertTrue(info.isCoupledSinkSupportedAtSink()); + + info.setSessionAvailable(true); + assertTrue(info.isSessionAvailable()); + + info.setControlPort(TEST_CTRL_PORT); + assertEquals(TEST_CTRL_PORT, info.getControlPort()); + + info.setMaxThroughput(TEST_MAX_TPUT); + assertEquals(TEST_MAX_TPUT, info.getMaxThroughput()); + + assertEquals("0018270f0400", info.getDeviceInfoHex()); + } + + /** + * Verifies copy constructor. + */ + @Test + public void testCopyOperator() throws Exception { + WifiP2pWfdInfo copiedInfo = new WifiP2pWfdInfo(mSourceInfo); + + // no equals operator, use toString for data comparison. + assertEquals(mSourceInfo.toString(), copiedInfo.toString()); + } + + /** + * Verifies parcel serialization/deserialization. + */ + @Test + public void testParcelOperation() throws Exception { + Parcel parcelW = Parcel.obtain(); + mSourceInfo.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + WifiP2pWfdInfo fromParcel = WifiP2pWfdInfo.CREATOR.createFromParcel(parcelR); + + // no equals operator, use toString for data comparison. + assertEquals(mSourceInfo.toString(), fromParcel.toString()); + } +}