diff options
4 files changed, 102 insertions, 47 deletions
diff --git a/wifi/java/android/net/wifi/aware/DiscoverySession.java b/wifi/java/android/net/wifi/aware/DiscoverySession.java index 9f7362234ad8..699f54cf13fb 100644 --- a/wifi/java/android/net/wifi/aware/DiscoverySession.java +++ b/wifi/java/android/net/wifi/aware/DiscoverySession.java @@ -22,6 +22,8 @@ import android.annotation.SystemApi; import android.net.NetworkSpecifier; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; + import dalvik.system.CloseGuard; import java.lang.ref.WeakReference; @@ -142,6 +144,34 @@ public class DiscoverySession implements AutoCloseable { } /** + * Access the client ID of the Aware session. + * + * Note: internal visibility for testing. + * + * @return The internal client ID. + * + * @hide + */ + @VisibleForTesting + public int getClientId() { + return mClientId; + } + + /** + * Access the discovery session ID of the Aware session. + * + * Note: internal visibility for testing. + * + * @return The internal discovery session ID. + * + * @hide + */ + @VisibleForTesting + public int getSessionId() { + return mSessionId; + } + + /** * Sends a message to the specified destination. Aware messages are transmitted in the context * of a discovery session - executed subsequent to a publish/subscribe * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, @@ -246,8 +276,7 @@ public class DiscoverySession implements AutoCloseable { * or * {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, byte[])}. * On a RESPONDER this value is used to gate the acceptance of a connection - * request from only that peer. A RESPONDER may specify a {@code null} - - * indicating that it will accept connection requests from any device. + * request from only that peer. * * @return A {@link NetworkSpecifier} to be used to construct * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} to pass to @@ -255,7 +284,7 @@ public class DiscoverySession implements AutoCloseable { * android.net.ConnectivityManager.NetworkCallback)} * [or other varieties of that API]. */ - public NetworkSpecifier createNetworkSpecifierOpen(@Nullable PeerHandle peerHandle) { + public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) { if (mTerminated) { Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session"); return null; @@ -295,8 +324,7 @@ public class DiscoverySession implements AutoCloseable { * byte[], java.util.List)} or * {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, * byte[])}. On a RESPONDER this value is used to gate the acceptance of a connection request - * from only that peer. A RESPONDER may specify a {@code null} - indicating - * that it will accept connection requests from any device. + * from only that peer. * @param passphrase The passphrase to be used to encrypt the link. The PMK is generated from * the passphrase. Use the * {@link #createNetworkSpecifierOpen(PeerHandle)} API to @@ -309,7 +337,7 @@ public class DiscoverySession implements AutoCloseable { * [or other varieties of that API]. */ public NetworkSpecifier createNetworkSpecifierPassphrase( - @Nullable PeerHandle peerHandle, @NonNull String passphrase) { + @NonNull PeerHandle peerHandle, @NonNull String passphrase) { if (!WifiAwareUtils.validatePassphrase(passphrase)) { throw new IllegalArgumentException("Passphrase must meet length requirements"); } @@ -354,8 +382,7 @@ public class DiscoverySession implements AutoCloseable { * byte[], java.util.List)} or * {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, * byte[])}. On a RESPONDER this value is used to gate the acceptance of a connection request - * from only that peer. A RESPONDER may specify a null - indicating that - * it will accept connection requests from any device. + * from only that peer. * @param pmk A PMK (pairwise master key, see IEEE 802.11i) specifying the key to use for * encrypting the data-path. Use the * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} to specify a @@ -371,7 +398,7 @@ public class DiscoverySession implements AutoCloseable { * @hide */ @SystemApi - public NetworkSpecifier createNetworkSpecifierPmk(@Nullable PeerHandle peerHandle, + public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle, @NonNull byte[] pmk) { if (!WifiAwareUtils.validatePmk(pmk)) { throw new IllegalArgumentException("PMK must 32 bytes"); diff --git a/wifi/java/android/net/wifi/aware/WifiAwareManager.java b/wifi/java/android/net/wifi/aware/WifiAwareManager.java index 2f0c3163aa0c..06a5c2e2710b 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareManager.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareManager.java @@ -406,7 +406,7 @@ public class WifiAwareManager { /** @hide */ public NetworkSpecifier createNetworkSpecifier(int clientId, int role, int sessionId, - PeerHandle peerHandle, @Nullable byte[] pmk, @Nullable String passphrase) { + @NonNull PeerHandle peerHandle, @Nullable byte[] pmk, @Nullable String passphrase) { if (VDBG) { Log.v(TAG, "createNetworkSpecifier: role=" + role + ", sessionId=" + sessionId + ", peerHandle=" + ((peerHandle == null) ? peerHandle : peerHandle.peerId) @@ -420,12 +420,9 @@ public class WifiAwareManager { "createNetworkSpecifier: Invalid 'role' argument when creating a network " + "specifier"); } - if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR) { - if (peerHandle == null) { - throw new IllegalArgumentException( - "createNetworkSpecifier: Invalid peer handle (value of null) - not " - + "permitted on INITIATOR"); - } + if (peerHandle == null) { + throw new IllegalArgumentException( + "createNetworkSpecifier: Invalid peer handle - cannot be null"); } return new WifiAwareNetworkSpecifier( @@ -443,7 +440,7 @@ public class WifiAwareManager { /** @hide */ public NetworkSpecifier createNetworkSpecifier(int clientId, @DataPathRole int role, - @Nullable byte[] peer, @Nullable byte[] pmk, @Nullable String passphrase) { + @NonNull byte[] peer, @Nullable byte[] pmk, @Nullable String passphrase) { if (VDBG) { Log.v(TAG, "createNetworkSpecifier: role=" + role + ", pmk=" + ((pmk == null) ? "null" : "non-null") @@ -456,11 +453,9 @@ public class WifiAwareManager { "createNetworkSpecifier: Invalid 'role' argument when creating a network " + "specifier"); } - if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR) { - if (peer == null) { - throw new IllegalArgumentException("createNetworkSpecifier: Invalid peer MAC " - + "address - null not permitted on INITIATOR"); - } + 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/WifiAwareSession.java b/wifi/java/android/net/wifi/aware/WifiAwareSession.java index f26b9f5aacfc..321965330e0b 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareSession.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareSession.java @@ -25,6 +25,8 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; + import dalvik.system.CloseGuard; import java.lang.ref.WeakReference; @@ -97,6 +99,20 @@ public class WifiAwareSession implements AutoCloseable { } /** + * Access the client ID of the Aware session. + * + * Note: internal visibility for testing. + * + * @return The internal client ID. + * + * @hide + */ + @VisibleForTesting + public int getClientId() { + return mClientId; + } + + /** * Issue a request to the Aware service to create a new Aware publish discovery session, using * the specified {@code publishConfig} configuration. The results of the publish operation * are routed to the callbacks of {@link DiscoverySessionCallback}: @@ -207,8 +223,7 @@ public class WifiAwareSession implements AutoCloseable { * {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER} * @param peer The MAC address of the peer's Aware discovery interface. On a RESPONDER this * value is used to gate the acceptance of a connection request from only that - * peer. A RESPONDER may specify a {@code null} - indicating that it will accept - * connection requests from any device. + * peer. * * @return A {@link NetworkSpecifier} to be used to construct * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} to pass to @@ -217,7 +232,7 @@ public class WifiAwareSession implements AutoCloseable { * [or other varieties of that API]. */ public NetworkSpecifier createNetworkSpecifierOpen( - @WifiAwareManager.DataPathRole int role, @Nullable byte[] peer) { + @WifiAwareManager.DataPathRole int role, @NonNull byte[] peer) { WifiAwareManager mgr = mMgr.get(); if (mgr == null) { Log.e(TAG, "createNetworkSpecifierOpen: called post GC on WifiAwareManager"); @@ -246,8 +261,7 @@ public class WifiAwareSession implements AutoCloseable { * {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER} * @param peer The MAC address of the peer's Aware discovery interface. On a RESPONDER this * value is used to gate the acceptance of a connection request from only that - * peer. A RESPONDER may specify a {@code null} - indicating that it will accept - * connection requests from any device. + * peer. * @param passphrase The passphrase to be used to encrypt the link. The PMK is generated from * the passphrase. Use {@link #createNetworkSpecifierOpen(int, byte[])} to * specify an open (unencrypted) link. @@ -259,7 +273,7 @@ public class WifiAwareSession implements AutoCloseable { * [or other varieties of that API]. */ public NetworkSpecifier createNetworkSpecifierPassphrase( - @WifiAwareManager.DataPathRole int role, @Nullable byte[] peer, + @WifiAwareManager.DataPathRole int role, @NonNull byte[] peer, @NonNull String passphrase) { WifiAwareManager mgr = mMgr.get(); if (mgr == null) { @@ -293,8 +307,7 @@ public class WifiAwareSession implements AutoCloseable { * {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_RESPONDER} * @param peer The MAC address of the peer's Aware discovery interface. On a RESPONDER this * value is used to gate the acceptance of a connection request from only that - * peer. A RESPONDER may specify a null - indicating that it will accept - * connection requests from any device. + * peer. * @param pmk A PMK (pairwise master key, see IEEE 802.11i) specifying the key to use for * encrypting the data-path. Use the * {@link #createNetworkSpecifierPassphrase(int, byte[], String)} to specify a @@ -311,7 +324,7 @@ public class WifiAwareSession implements AutoCloseable { */ @SystemApi public NetworkSpecifier createNetworkSpecifierPmk( - @WifiAwareManager.DataPathRole int role, @Nullable byte[] peer, @NonNull byte[] pmk) { + @WifiAwareManager.DataPathRole int role, @NonNull byte[] peer, @NonNull byte[] pmk) { WifiAwareManager mgr = mMgr.get(); if (mgr == null) { Log.e(TAG, "createNetworkSpecifierPmk: called post GC on WifiAwareManager"); diff --git a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java index 9cab66a240a7..dbca02295268 100644 --- a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java +++ b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java @@ -1022,7 +1022,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientNullPmk() throws Exception { - executeNetworkSpecifierWithClient(true, null, null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, null, null); } /** @@ -1030,7 +1030,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientIncorrectLengthPmk() throws Exception { - executeNetworkSpecifierWithClient(true, "012".getBytes(), null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, "012".getBytes(), null); } /** @@ -1038,7 +1038,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientNullPassphrase() throws Exception { - executeNetworkSpecifierWithClient(false, null, null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, null); } /** @@ -1046,7 +1046,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientShortPassphrase() throws Exception { - executeNetworkSpecifierWithClient(false, null, "012"); + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, "012"); } /** @@ -1054,15 +1054,23 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientLongPassphrase() throws Exception { - executeNetworkSpecifierWithClient(false, null, + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, "0123456789012345678901234567890123456789012345678901234567890123456789"); } - private void executeNetworkSpecifierWithClient(boolean doPmk, byte[] pmk, String passphrase) - throws Exception { + /** + * Validate that a null PeerHandle triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientNullPeer() throws Exception { + executeNetworkSpecifierWithClient(null, false, null, + "0123456789012345678901234567890123456789012345678901234567890123456789"); + } + + private void executeNetworkSpecifierWithClient(PeerHandle peerHandle, boolean doPmk, byte[] pmk, + String passphrase) throws Exception { final int clientId = 4565; final int sessionId = 123; - final PeerHandle peerHandle = new PeerHandle(123412); final ConfigRequest configRequest = new ConfigRequest.Builder().build(); final PublishConfig publishConfig = new PublishConfig.Builder().build(); @@ -1108,7 +1116,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierDirectNullPmk() throws Exception { - executeNetworkSpecifierDirect(true, null, null); + executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), true, + null, null); } /** @@ -1116,7 +1125,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierDirectIncorrectLengthPmk() throws Exception { - executeNetworkSpecifierDirect(true, "012".getBytes(), null); + executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), true, + "012".getBytes(), null); } /** @@ -1124,7 +1134,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierDirectNullPassphrase() throws Exception { - executeNetworkSpecifierDirect(false, null, null); + executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), + false, null, null); } /** @@ -1132,7 +1143,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierDirectShortPassphrase() throws Exception { - executeNetworkSpecifierDirect(false, null, "012"); + executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), + false, null, "012"); } /** @@ -1140,14 +1152,22 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierDirectLongPassphrase() throws Exception { - executeNetworkSpecifierDirect(false, null, + executeNetworkSpecifierDirect(HexEncoding.decode("000102030405".toCharArray(), false), + false, null, "0123456789012345678901234567890123456789012345678901234567890123456789"); } - private void executeNetworkSpecifierDirect(boolean doPmk, byte[] pmk, String passphrase) - throws Exception { + /** + * Validate that a null peer MAC triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierDirectNullPeer() throws Exception { + executeNetworkSpecifierDirect(null, false, null, null); + } + + private void executeNetworkSpecifierDirect(byte[] someMac, boolean doPmk, byte[] pmk, + String passphrase) throws Exception { final int clientId = 134; - final byte[] someMac = HexEncoding.decode("000102030405".toCharArray(), false); final int role = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR; final ConfigRequest configRequest = new ConfigRequest.Builder().build(); |