Do not allow L2CAP specifier with ROLE_SERVER and remote address set
This is an invalid combination that never seems useful. Note that this
still allows ROLE_ANY in combination with a remote address. This still
doesn't seem particularly useful, but there is no good reason to
disallow it.
Test: TH
Change-Id: Ib0b7284cb99ee92f14113f8184af978eeb2318e7
diff --git a/framework/src/android/net/L2capNetworkSpecifier.java b/framework/src/android/net/L2capNetworkSpecifier.java
index b4a3416..3331123 100644
--- a/framework/src/android/net/L2capNetworkSpecifier.java
+++ b/framework/src/android/net/L2capNetworkSpecifier.java
@@ -240,7 +240,10 @@
/** Create the L2capNetworkSpecifier object. */
@NonNull
public L2capNetworkSpecifier build() {
- // TODO: throw an exception for combinations that cannot be supported.
+ if (mRole == ROLE_SERVER && mRemoteAddress != null) {
+ throw new IllegalArgumentException(
+ "Specifying a remote address is not valid for server role.");
+ }
return new L2capNetworkSpecifier(mRole, mHeaderCompression, mRemoteAddress, mPsm);
}
}
diff --git a/tests/cts/net/src/android/net/cts/L2capNetworkSpecifierTest.kt b/tests/cts/net/src/android/net/cts/L2capNetworkSpecifierTest.kt
index b593baf..51ccdd5 100644
--- a/tests/cts/net/src/android/net/cts/L2capNetworkSpecifierTest.kt
+++ b/tests/cts/net/src/android/net/cts/L2capNetworkSpecifierTest.kt
@@ -20,7 +20,6 @@
import android.net.L2capNetworkSpecifier.HEADER_COMPRESSION_6LOWPAN
import android.net.L2capNetworkSpecifier.HEADER_COMPRESSION_NONE
import android.net.L2capNetworkSpecifier.ROLE_CLIENT
-import android.net.L2capNetworkSpecifier.ROLE_SERVER
import android.net.MacAddress
import android.os.Build
import com.android.testutils.ConnectivityModuleTest
@@ -51,12 +50,12 @@
fun testGetters() {
val remoteMac = MacAddress.fromString("11:22:33:44:55:66")
val specifier = L2capNetworkSpecifier.Builder()
- .setRole(ROLE_SERVER)
+ .setRole(ROLE_CLIENT)
.setHeaderCompression(HEADER_COMPRESSION_NONE)
.setPsm(123)
.setRemoteAddress(remoteMac)
.build()
- assertEquals(ROLE_SERVER, specifier.getRole())
+ assertEquals(ROLE_CLIENT, specifier.getRole())
assertEquals(HEADER_COMPRESSION_NONE, specifier.getHeaderCompression())
assertEquals(123, specifier.getPsm())
assertEquals(remoteMac, specifier.getRemoteAddress())