p2p: check the length of the network name bytes

Fixes: 144472136
Bug: 144472136
Test: atest FrameworksWifiApiTests
Change-Id: I80b8e0330499e6c655efb1712811891a8e8229cb
This commit is contained in:
Jimmy Chen
2019-11-15 10:06:27 +08:00
parent 87af96e8de
commit a0900344f4
2 changed files with 22 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ import android.text.TextUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.charset.StandardCharsets;
import java.util.regex.PatternSyntaxException;
/**
@@ -228,6 +229,10 @@ public class WifiP2pConfig implements Parcelable {
private static final MacAddress MAC_ANY_ADDRESS =
MacAddress.fromString("02:00:00:00:00:00");
/**
* Maximum number of bytes allowed for a SSID.
*/
private static final int MAX_SSID_BYTES = 32;
private MacAddress mDeviceAddress = MAC_ANY_ADDRESS;
private String mNetworkName = "";
@@ -279,6 +284,10 @@ public class WifiP2pConfig implements Parcelable {
throw new IllegalArgumentException(
"network name must be non-empty.");
}
if (networkName.getBytes(StandardCharsets.UTF_8).length > MAX_SSID_BYTES) {
throw new IllegalArgumentException(
"network name exceeds " + MAX_SSID_BYTES + " bytes.");
}
try {
if (!networkName.matches("^DIRECT-[a-zA-Z0-9]{2}.*")) {
throw new IllegalArgumentException(

View File

@@ -53,6 +53,13 @@ public class WifiP2pConfigTest {
fail("Unexpected IllegalArgumentException");
}
// sunny case with maximum bytes for the network name
try {
b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxy");
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException");
}
// less than 9 characters.
try {
b.setNetworkName("DIRECT-z");
@@ -77,6 +84,12 @@ public class WifiP2pConfigTest {
b.setNetworkName("direct-a?");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException e) { }
// over maximum bytes
try {
b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxyz");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException e) { }
}
/**