From f33cb878f4d415244897957ddbeefd74fc9af20a Mon Sep 17 00:00:00 2001 From: Jimmy Chen Date: Fri, 22 Feb 2019 10:47:07 +0800 Subject: [PATCH] p2p: sanity check for network name Bug: 120259460 Test: Unit test - atest android.net.wifi.p2p.WifiP2pConfigTest Change-Id: I4ba5643a9fdded7b9f9837a21b24a89fac98a5ce --- .../android/net/wifi/p2p/WifiP2pConfig.java | 16 +++- .../net/wifi/p2p/WifiP2pConfigTest.java | 76 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java b/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java index f931ad2bdf6dd..479adbcb1a00a 100644 --- a/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java +++ b/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java @@ -26,6 +26,7 @@ import android.text.TextUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.regex.PatternSyntaxException; /** * A class representing a Wi-Fi P2p configuration for setting up a connection @@ -252,7 +253,12 @@ public class WifiP2pConfig implements Parcelable { * Specify the network name, a.k.a. group name, * for creating or joining a group. *

- * Must be called - an empty network name is not valid. + * A network name shall begin with "DIRECT-xy". x and y are selected + * from the following character set: upper case letters, lower case + * letters and numbers. + *

+ * Must be called - an empty network name or an network name + * not conforming to the P2P Group ID naming rule is not valid. * * @param networkName network name of a group. * @return The builder to facilitate chaining @@ -263,6 +269,14 @@ public class WifiP2pConfig implements Parcelable { throw new IllegalArgumentException( "network name must be non-empty."); } + try { + if (!networkName.matches("^DIRECT-[a-zA-Z0-9]{2}.*")) { + throw new IllegalArgumentException( + "network name must starts with the prefix DIRECT-xy."); + } + } catch (PatternSyntaxException e) { + // can never happen (fixed pattern) + } mNetworkName = networkName; return this; } diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java new file mode 100644 index 0000000000000..560c88ee1d077 --- /dev/null +++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java @@ -0,0 +1,76 @@ +/* + * 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.fail; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +/** + * Unit test harness for {@link android.net.wifi.p2p.WifiP2pConfig} + */ +@SmallTest +public class WifiP2pConfigTest { + /** + * Check network name setter + */ + @Test + public void testBuilderInvalidNetworkName() throws Exception { + WifiP2pConfig.Builder b = new WifiP2pConfig.Builder(); + + // sunny case + try { + b.setNetworkName("DIRECT-ab-Hello"); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException"); + } + + // sunny case, no trailing string + try { + b.setNetworkName("DIRECT-WR"); + } catch (IllegalArgumentException e) { + fail("Unexpected IllegalArgumentException"); + } + + // less than 9 characters. + try { + b.setNetworkName("DIRECT-z"); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { } + + // not starts with DIRECT-xy. + try { + b.setNetworkName("ABCDEFGHIJK"); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { } + + // not starts with uppercase DIRECT-xy + try { + b.setNetworkName("direct-ab"); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { } + + // x and y are not selected from upper case letters, lower case letters or + // numbers. + try { + b.setNetworkName("direct-a?"); + fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { } + } +}