[AWARE] Add target API check to the Responder to ANYONE config
Responder to ANYONE was deprecated for API >= P. But the manager
code did not check for target API - i.e. even older apps would have
been rejected. Added the target API check to the manager code.
Bug: 72175022
Test: unit test + integration tests
Change-Id: I52e0c877e0af0756816b5b13e42f432504461e20
diff --git a/wifi/java/android/net/wifi/aware/WifiAwareManager.java b/wifi/java/android/net/wifi/aware/WifiAwareManager.java
index 06a5c2e..8529a89 100644
--- a/wifi/java/android/net/wifi/aware/WifiAwareManager.java
+++ b/wifi/java/android/net/wifi/aware/WifiAwareManager.java
@@ -27,6 +27,7 @@
import android.net.NetworkRequest;
import android.net.NetworkSpecifier;
import android.os.Binder;
+import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@@ -420,9 +421,12 @@
"createNetworkSpecifier: Invalid 'role' argument when creating a network "
+ "specifier");
}
- if (peerHandle == null) {
- throw new IllegalArgumentException(
- "createNetworkSpecifier: Invalid peer handle - cannot be null");
+ if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR || !WifiAwareUtils.isLegacyVersion(mContext,
+ Build.VERSION_CODES.P)) {
+ if (peerHandle == null) {
+ throw new IllegalArgumentException(
+ "createNetworkSpecifier: Invalid peer handle - cannot be null");
+ }
}
return new WifiAwareNetworkSpecifier(
@@ -453,9 +457,12 @@
"createNetworkSpecifier: Invalid 'role' argument when creating a network "
+ "specifier");
}
- if (peer == null) {
- throw new IllegalArgumentException(
- "createNetworkSpecifier: Invalid peer MAC - cannot be null");
+ if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR || !WifiAwareUtils.isLegacyVersion(mContext,
+ Build.VERSION_CODES.P)) {
+ if (peer == null) {
+ throw new IllegalArgumentException(
+ "createNetworkSpecifier: Invalid peer MAC - cannot be null");
+ }
}
if (peer != null && peer.length != 6) {
throw new IllegalArgumentException("createNetworkSpecifier: Invalid peer MAC address");
diff --git a/wifi/java/android/net/wifi/aware/WifiAwareUtils.java b/wifi/java/android/net/wifi/aware/WifiAwareUtils.java
index fda7a9a..3ece93d 100644
--- a/wifi/java/android/net/wifi/aware/WifiAwareUtils.java
+++ b/wifi/java/android/net/wifi/aware/WifiAwareUtils.java
@@ -16,6 +16,8 @@
package android.net.wifi.aware;
+import android.content.Context;
+import android.content.pm.PackageManager;
import android.hardware.wifi.V1_0.Constants;
/**
@@ -84,4 +86,21 @@
return true;
}
+
+ /**
+ * Returns true if the App version is older than minVersion.
+ */
+ public static boolean isLegacyVersion(Context context, int minVersion) {
+ try {
+ if (context.getPackageManager().getApplicationInfo(context.getOpPackageName(), 0)
+ .targetSdkVersion < minVersion) {
+ return true;
+ }
+ } catch (PackageManager.NameNotFoundException e) {
+ // In case of exception, assume known app (more strict checking)
+ // Note: This case will never happen since checkPackage is
+ // called to verify valididity before checking App's version.
+ }
+ return false;
+ }
}
diff --git a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
index 84e3ed9..272f727 100644
--- a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
+++ b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java
@@ -19,14 +19,20 @@
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
import android.net.wifi.RttManager;
+import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Parcel;
@@ -79,12 +85,32 @@
@Mock
public RttManager.RttListener mockRttListener;
+ @Mock
+ public PackageManager mockPackageManager;
+
+ @Mock
+ public ApplicationInfo mockApplicationInfo;
+
private static final int AWARE_STATUS_ERROR = -1;
+ private static final byte[] PMK_VALID = "01234567890123456789012345678901".getBytes();
+ private static final byte[] PMK_INVALID = "012".getBytes();
+
+ private static final String PASSPHRASE_VALID = "SomeLongEnoughPassphrase";
+ private static final String PASSPHRASE_TOO_SHORT = "012";
+ private static final String PASSPHRASE_TOO_LONG =
+ "0123456789012345678901234567890123456789012345678901234567890123456789";
+
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
+ mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P;
+ when(mockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
+ mockApplicationInfo);
+ when(mockContext.getOpPackageName()).thenReturn("XXX");
+ when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
+
mDut = new WifiAwareManager(mockContext, mockAwareService);
mMockLooper = new TestLooper();
mMockLooperHandler = new Handler(mMockLooper.getLooper());
@@ -884,8 +910,8 @@
final int sessionId = 123;
final PeerHandle peerHandle = new PeerHandle(123412);
final int role = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;
- final byte[] pmk = "01234567890123456789012345678901".getBytes();
- final String passphrase = "A really bad password";
+ final byte[] pmk = PMK_VALID;
+ final String passphrase = PASSPHRASE_VALID;
final ConfigRequest configRequest = new ConfigRequest.Builder().build();
final PublishConfig publishConfig = new PublishConfig.Builder().build();
@@ -965,8 +991,8 @@
final ConfigRequest configRequest = new ConfigRequest.Builder().build();
final byte[] someMac = HexEncoding.decode("000102030405".toCharArray(), false);
final int role = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR;
- final byte[] pmk = "01234567890123456789012345678901".getBytes();
- final String passphrase = "A really bad password";
+ final byte[] pmk = PMK_VALID;
+ final String passphrase = PASSPHRASE_VALID;
ArgumentCaptor<WifiAwareSession> sessionCaptor = ArgumentCaptor.forClass(
WifiAwareSession.class);
@@ -1030,7 +1056,7 @@
*/
@Test(expected = IllegalArgumentException.class)
public void testNetworkSpecifierWithClientIncorrectLengthPmk() throws Exception {
- executeNetworkSpecifierWithClient(new PeerHandle(123412), true, "012".getBytes(), null);
+ executeNetworkSpecifierWithClient(new PeerHandle(123412), true, PMK_INVALID, null);
}
/**
@@ -1045,17 +1071,17 @@
* Validate that a too short Passphrase triggers an exception.
*/
@Test(expected = IllegalArgumentException.class)
- public void testNetworkSpecifierWithClientShortPassphrase() throws Exception {
- executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, "012");
+ public void testNetworkSpecifierWithClientTooShortPassphrase() throws Exception {
+ executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null,
+ PASSPHRASE_TOO_SHORT);
}
/**
* Validate that a too long Passphrase triggers an exception.
*/
@Test(expected = IllegalArgumentException.class)
- public void testNetworkSpecifierWithClientLongPassphrase() throws Exception {
- executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null,
- "0123456789012345678901234567890123456789012345678901234567890123456789");
+ public void testNetworkSpecifierWithClientTooLongPassphrase() throws Exception {
+ executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, PASSPHRASE_TOO_LONG);
}
/**
@@ -1063,8 +1089,16 @@
*/
@Test(expected = IllegalArgumentException.class)
public void testNetworkSpecifierWithClientNullPeer() throws Exception {
- executeNetworkSpecifierWithClient(null, false, null,
- "0123456789012345678901234567890123456789012345678901234567890123456789");
+ executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID);
+ }
+
+ /**
+ * Validate that a null PeerHandle does not trigger an exception for legacy API.
+ */
+ @Test
+ public void testNetworkSpecifierWithClientNullPeerLegacyApi() throws Exception {
+ mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
+ executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID);
}
private void executeNetworkSpecifierWithClient(PeerHandle peerHandle, boolean doPmk, byte[] pmk,
@@ -1117,7 +1151,7 @@
@Test(expected = IllegalArgumentException.class)
public void testNetworkSpecifierDirectNullPmk() throws Exception {
executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), true,
- null, null);
+ null, null, true);
}
/**
@@ -1126,7 +1160,7 @@
@Test(expected = IllegalArgumentException.class)
public void testNetworkSpecifierDirectIncorrectLengthPmk() throws Exception {
executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), true,
- "012".getBytes(), null);
+ PMK_INVALID, null, true);
}
/**
@@ -1135,40 +1169,57 @@
@Test(expected = IllegalArgumentException.class)
public void testNetworkSpecifierDirectNullPassphrase() throws Exception {
executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false),
- false, null, null);
+ false, null, null, true);
}
/**
* Validate that a too short Passphrase triggers an exception.
*/
@Test(expected = IllegalArgumentException.class)
- public void testNetworkSpecifierDirectShortPassphrase() throws Exception {
+ public void testNetworkSpecifierDirectTooShortPassphrase() throws Exception {
executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false),
- false, null, "012");
+ false, null, PASSPHRASE_TOO_SHORT, true);
}
/**
* Validate that a too long Passphrase triggers an exception.
*/
@Test(expected = IllegalArgumentException.class)
- public void testNetworkSpecifierDirectLongPassphrase() throws Exception {
+ public void testNetworkSpecifierDirectTooLongPassphrase() throws Exception {
executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false),
- false, null,
- "0123456789012345678901234567890123456789012345678901234567890123456789");
+ false, null, PASSPHRASE_TOO_LONG, true);
}
/**
- * Validate that a null peer MAC triggers an exception.
+ * Validate that a null peer MAC triggers an exception for an Initiator.
*/
@Test(expected = IllegalArgumentException.class)
- public void testNetworkSpecifierDirectNullPeer() throws Exception {
- executeNetworkSpecifierDirect(null, false, null, null);
+ public void testNetworkSpecifierDirectNullPeerInitiator() throws Exception {
+ executeNetworkSpecifierDirect(null, false, null, PASSPHRASE_VALID, true);
+ }
+
+ /**
+ * Validate that a null peer MAC triggers an exception for a Resonder.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void testNetworkSpecifierDirectNullPeerResponder() throws Exception {
+ executeNetworkSpecifierDirect(null, false, null, PASSPHRASE_VALID, false);
+ }
+
+ /**
+ * Validate that a null peer MAC does not trigger an exception for a Resonder on legacy API.
+ */
+ @Test
+ public void testNetworkSpecifierDirectNullPeerResponderLegacyApi() throws Exception {
+ mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O;
+ executeNetworkSpecifierDirect(null, false, null, PASSPHRASE_VALID, false);
}
private void executeNetworkSpecifierDirect(byte[] someMac, boolean doPmk, byte[] pmk,
- String passphrase) throws Exception {
+ String passphrase, boolean doInitiator) throws Exception {
final int clientId = 134;
- final int role = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR;
+ final int role = doInitiator ? WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
+ : WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;
final ConfigRequest configRequest = new ConfigRequest.Builder().build();
ArgumentCaptor<WifiAwareSession> sessionCaptor = ArgumentCaptor.forClass(