Merge "p2p: sanity check for network name"
This commit is contained in:
@@ -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.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
|
||||
76
wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java
Normal file
76
wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java
Normal file
@@ -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) { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user