diff options
| -rw-r--r-- | api/current.txt | 12 | ||||
| -rw-r--r-- | api/system-current.txt | 6 | ||||
| -rw-r--r-- | wifi/java/android/net/wifi/aware/DiscoverySession.java | 14 | ||||
| -rw-r--r-- | wifi/java/android/net/wifi/aware/WifiAwareManager.java | 152 | ||||
| -rw-r--r-- | wifi/java/android/net/wifi/aware/WifiAwareSession.java | 6 | ||||
| -rw-r--r-- | wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java | 128 |
6 files changed, 284 insertions, 34 deletions
diff --git a/api/current.txt b/api/current.txt index 37627d943edd..328dd2d83ee4 100644 --- a/api/current.txt +++ b/api/current.txt @@ -29557,8 +29557,8 @@ package android.net.wifi.aware { public class DiscoverySession implements java.lang.AutoCloseable { method public void close(); - method public android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle); - method public android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String); + method public deprecated android.net.NetworkSpecifier createNetworkSpecifierOpen(android.net.wifi.aware.PeerHandle); + method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPassphrase(android.net.wifi.aware.PeerHandle, java.lang.String); method public void sendMessage(android.net.wifi.aware.PeerHandle, int, byte[]); } @@ -29643,6 +29643,14 @@ package android.net.wifi.aware { field public static final int WIFI_AWARE_DATA_PATH_ROLE_RESPONDER = 1; // 0x1 } + public static class WifiAwareManager.NetworkSpecifierBuilder { + ctor public WifiAwareManager.NetworkSpecifierBuilder(); + method public android.net.NetworkSpecifier build(); + method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setDiscoverySession(android.net.wifi.aware.DiscoverySession); + method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPeerHandle(android.net.wifi.aware.PeerHandle); + method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPskPassphrase(java.lang.String); + } + public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo { method public int describeContents(); method public java.net.Inet6Address getPeerIpv6Addr(); diff --git a/api/system-current.txt b/api/system-current.txt index 6db4ec75632d..b666ccf99358 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -3938,7 +3938,11 @@ package android.net.wifi { package android.net.wifi.aware { public class DiscoverySession implements java.lang.AutoCloseable { - method public android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]); + method public deprecated android.net.NetworkSpecifier createNetworkSpecifierPmk(android.net.wifi.aware.PeerHandle, byte[]); + } + + public static class WifiAwareManager.NetworkSpecifierBuilder { + method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPmk(byte[]); } public class WifiAwareSession implements java.lang.AutoCloseable { diff --git a/wifi/java/android/net/wifi/aware/DiscoverySession.java b/wifi/java/android/net/wifi/aware/DiscoverySession.java index 699f54cf13fb..a47e70b6f30f 100644 --- a/wifi/java/android/net/wifi/aware/DiscoverySession.java +++ b/wifi/java/android/net/wifi/aware/DiscoverySession.java @@ -34,11 +34,11 @@ import java.lang.ref.WeakReference; * {@link PublishDiscoverySession} and {@link SubscribeDiscoverySession}. This * class provides functionality common to both publish and subscribe discovery sessions: * <ul> - * <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method. - * <li>Creating a network-specifier when requesting a Aware connection: - * {@link #createNetworkSpecifierOpen(PeerHandle)} or - * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)}. + * <li>Sending messages: {@link #sendMessage(PeerHandle, int, byte[])} method. + * <li>Creating a network-specifier when requesting a Aware connection using + * {@link WifiAwareManager.NetworkSpecifierBuilder}. * </ul> + * <p> * The {@link #close()} method must be called to destroy discovery sessions once they are * no longer needed. */ @@ -270,6 +270,7 @@ public class DiscoverySession implements AutoCloseable { * <p> * To set up an encrypted link use the * {@link #createNetworkSpecifierPassphrase(PeerHandle, String)} API. + * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)} @@ -284,6 +285,7 @@ public class DiscoverySession implements AutoCloseable { * android.net.ConnectivityManager.NetworkCallback)} * [or other varieties of that API]. */ + @Deprecated public NetworkSpecifier createNetworkSpecifierOpen(@NonNull PeerHandle peerHandle) { if (mTerminated) { Log.w(TAG, "createNetworkSpecifierOpen: called on terminated session"); @@ -318,6 +320,7 @@ public class DiscoverySession implements AutoCloseable { * <p> * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR * and a Publisher is a RESPONDER. + * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, @@ -336,6 +339,7 @@ public class DiscoverySession implements AutoCloseable { * android.net.ConnectivityManager.NetworkCallback)} * [or other varieties of that API]. */ + @Deprecated public NetworkSpecifier createNetworkSpecifierPassphrase( @NonNull PeerHandle peerHandle, @NonNull String passphrase) { if (!WifiAwareUtils.validatePassphrase(passphrase)) { @@ -376,6 +380,7 @@ public class DiscoverySession implements AutoCloseable { * <p> * Note: per the Wi-Fi Aware specification the roles are fixed - a Subscriber is an INITIATOR * and a Publisher is a RESPONDER. + * @deprecated Use the replacement {@link WifiAwareManager.NetworkSpecifierBuilder}. * * @param peerHandle The peer's handle obtained through * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, @@ -397,6 +402,7 @@ public class DiscoverySession implements AutoCloseable { * * @hide */ + @Deprecated @SystemApi public NetworkSpecifier createNetworkSpecifierPmk(@NonNull PeerHandle peerHandle, @NonNull byte[] pmk) { diff --git a/wifi/java/android/net/wifi/aware/WifiAwareManager.java b/wifi/java/android/net/wifi/aware/WifiAwareManager.java index 8529a89a9914..26a6c08bee29 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareManager.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareManager.java @@ -21,6 +21,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; +import android.annotation.SystemApi; import android.annotation.SystemService; import android.content.Context; import android.net.ConnectivityManager; @@ -57,11 +58,7 @@ import java.util.List; * {@link WifiAwareSession#subscribe(SubscribeConfig, DiscoverySessionCallback, Handler)}. * <li>Create a Aware network specifier to be used with * {@link ConnectivityManager#requestNetwork(NetworkRequest, ConnectivityManager.NetworkCallback)} - * to set-up a Aware connection with a peer. Refer to - * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}, - * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}, - * {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])}, and - * {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)}. + * to set-up a Aware connection with a peer. Refer to {@link NetworkSpecifierBuilder}. * </ul> * <p> * Aware may not be usable when Wi-Fi is disabled (and other conditions). To validate that @@ -110,10 +107,7 @@ import java.util.List; * <li>{@link NetworkRequest.Builder#addTransportType(int)} of * {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}. * <li>{@link NetworkRequest.Builder#setNetworkSpecifier(String)} using - * {@link WifiAwareSession#createNetworkSpecifierOpen(int, byte[])}, - * {@link WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String)}, - * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}, or - * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}. + * {@link NetworkSpecifierBuilder}. * </ul> */ @SystemService(Context.WIFI_AWARE_SERVICE) @@ -145,8 +139,6 @@ public class WifiAwareManager { * Connection creation role is that of INITIATOR. Used to create a network specifier string * when requesting a Aware network. * - * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle) - * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String) * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[]) * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String) */ @@ -156,8 +148,6 @@ public class WifiAwareManager { * Connection creation role is that of RESPONDER. Used to create a network specifier string * when requesting a Aware network. * - * @see DiscoverySession#createNetworkSpecifierOpen(PeerHandle) - * @see DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String) * @see WifiAwareSession#createNetworkSpecifierOpen(int, byte[]) * @see WifiAwareSession#createNetworkSpecifierPassphrase(int, byte[], String) */ @@ -415,6 +405,11 @@ public class WifiAwareManager { + ", passphrase=" + ((passphrase == null) ? "null" : "non-null")); } + if (!WifiAwareUtils.isLegacyVersion(mContext, Build.VERSION_CODES.Q)) { + throw new UnsupportedOperationException( + "API not deprecated - use WifiAwareManager.NetworkSpecifierBuilder"); + } + if (role != WIFI_AWARE_DATA_PATH_ROLE_INITIATOR && role != WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) { throw new IllegalArgumentException( @@ -813,4 +808,135 @@ public class WifiAwareManager { mOriginalCallback.onSessionTerminated(); } } + + /** + * A builder class for a Wi-Fi Aware network specifier to set up an Aware connection with a + * peer. + * <p> + * Note that all Wi-Fi Aware connection specifier objects must call the + * {@link NetworkSpecifierBuilder#setDiscoverySession(DiscoverySession)} to specify the context + * within which the connection is created, and + * {@link NetworkSpecifierBuilder#setPeerHandle(PeerHandle)} to specify the peer to which the + * connection is created. + */ + public static class NetworkSpecifierBuilder { + private DiscoverySession mDiscoverySession; + private PeerHandle mPeerHandle; + private String mPskPassphrase; + private byte[] mPmk; + + /** + * Configure the {@link PublishDiscoverySession} or {@link SubscribeDiscoverySession} + * discovery session in whose context the connection is created. + * <p> + * Note: this method must be called for any connection request! + * + * @param discoverySession A Wi-Fi Aware discovery session. + * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder + * methods. + */ + public @NonNull NetworkSpecifierBuilder setDiscoverySession( + @NonNull DiscoverySession discoverySession) { + if (discoverySession == null) { + throw new IllegalArgumentException("Non-null discoverySession required"); + } + mDiscoverySession = discoverySession; + return this; + } + + /** + * Configure the {@link PeerHandle} of the peer to which the Wi-Fi Aware connection is + * requested. The peer is discovered through Wi-Fi Aware discovery, + * <p> + * Note: this method must be called for any connection request! + * + * @param peerHandle The peer's handle obtained through + * {@link DiscoverySessionCallback#onServiceDiscovered(PeerHandle, byte[], java.util.List)} + * or + * {@link DiscoverySessionCallback#onMessageReceived(PeerHandle, byte[])}. + * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder + * methods. + */ + public @NonNull NetworkSpecifierBuilder setPeerHandle(@NonNull PeerHandle peerHandle) { + if (peerHandle == null) { + throw new IllegalArgumentException("Non-null peerHandle required"); + } + mPeerHandle = peerHandle; + return this; + } + + /** + * Configure the PSK Passphrase for the Wi-Fi Aware connection being requested. This method + * is optional - if not called, then an Open (unencrypted) connection will be created. + * + * @param pskPassphrase The (optional) passphrase to be used to encrypt the link. + * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder + * methods. + */ + public @NonNull NetworkSpecifierBuilder setPskPassphrase(@NonNull String pskPassphrase) { + if (!WifiAwareUtils.validatePassphrase(pskPassphrase)) { + throw new IllegalArgumentException("Passphrase must meet length requirements"); + } + mPskPassphrase = pskPassphrase; + return this; + } + + /** + * Configure the PMK for the Wi-Fi Aware connection being requested. This method + * is optional - if not called, then an Open (unencrypted) connection will be created. + * + * @param pmk A PMK (pairwise master key, see IEEE 802.11i) specifying the key to use for + * encrypting the data-path. Use the {@link #setPskPassphrase(String)} to + * specify a Passphrase. + * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder + * methods. + * @hide + */ + @SystemApi + public @NonNull NetworkSpecifierBuilder setPmk(@NonNull byte[] pmk) { + if (!WifiAwareUtils.validatePmk(pmk)) { + throw new IllegalArgumentException("PMK must 32 bytes"); + } + mPmk = pmk; + return this; + } + + /** + * Create a {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} + * for a WiFi Aware connection (link) to the specified peer. The + * {@link android.net.NetworkRequest.Builder#addTransportType(int)} should be set to + * {@link android.net.NetworkCapabilities#TRANSPORT_WIFI_AWARE}. + * <p> The default builder constructor will initialize a NetworkSpecifier which requests an + * open (non-encrypted) link. To request an encrypted link use the + * {@link #setPskPassphrase(String)} builder method. + * + * @return A {@link NetworkSpecifier} to be used to construct + * {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)} to pass + * to {@link android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, + * android.net.ConnectivityManager.NetworkCallback)} + * [or other varieties of that API]. + */ + public @NonNull NetworkSpecifier build() { + if (mDiscoverySession == null) { + throw new IllegalStateException("Null discovery session!?"); + } + if (mPskPassphrase != null & mPmk != null) { + throw new IllegalStateException( + "Can only specify a Passphrase or a PMK - not both!"); + } + + int role = mDiscoverySession instanceof SubscribeDiscoverySession + ? WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR + : WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER; + + if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR && mPeerHandle == null) { + throw new IllegalStateException("Null peerHandle!?"); + } + + return new WifiAwareNetworkSpecifier( + WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB, role, + mDiscoverySession.mClientId, mDiscoverySession.mSessionId, mPeerHandle.peerId, + null, mPmk, mPskPassphrase, Process.myUid()); + } + } } diff --git a/wifi/java/android/net/wifi/aware/WifiAwareSession.java b/wifi/java/android/net/wifi/aware/WifiAwareSession.java index 321965330e0b..5f8841cb0148 100644 --- a/wifi/java/android/net/wifi/aware/WifiAwareSession.java +++ b/wifi/java/android/net/wifi/aware/WifiAwareSession.java @@ -213,7 +213,7 @@ public class WifiAwareSession implements AutoCloseable { * This API is targeted for applications which can obtain the peer MAC address using OOB * (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer - * when using Aware discovery use the alternative network specifier method - - * {@link DiscoverySession#createNetworkSpecifierOpen(PeerHandle)}. + * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}. * <p> * To set up an encrypted link use the * {@link #createNetworkSpecifierPassphrase(int, byte[], String)} API. @@ -254,7 +254,7 @@ public class WifiAwareSession implements AutoCloseable { * This API is targeted for applications which can obtain the peer MAC address using OOB * (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer - * when using Aware discovery use the alternative network specifier method - - * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}. + * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}. * * @param role The role of this device: * {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or @@ -300,7 +300,7 @@ public class WifiAwareSession implements AutoCloseable { * This API is targeted for applications which can obtain the peer MAC address using OOB * (out-of-band) discovery. Aware discovery does not provide the MAC address of the peer - * when using Aware discovery use the alternative network specifier method - - * {@link DiscoverySession#createNetworkSpecifierPassphrase(PeerHandle, String)}. + * {@link android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder}. * * @param role The role of this device: * {@link WifiAwareManager#WIFI_AWARE_DATA_PATH_ROLE_INITIATOR} or diff --git a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java index fd383ef6fd38..45e17201bd1f 100644 --- a/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java +++ b/wifi/tests/src/android/net/wifi/aware/WifiAwareManagerTest.java @@ -108,7 +108,7 @@ public class WifiAwareManagerTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P; + mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q; when(mockPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn( mockApplicationInfo); when(mockContext.getOpPackageName()).thenReturn("XXX"); @@ -918,6 +918,8 @@ public class WifiAwareManagerTest { final ConfigRequest configRequest = new ConfigRequest.Builder().build(); final PublishConfig publishConfig = new PublishConfig.Builder().build(); + mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P; + ArgumentCaptor<WifiAwareSession> sessionCaptor = ArgumentCaptor.forClass( WifiAwareSession.class); ArgumentCaptor<IWifiAwareEventCallback> clientProxyCallback = ArgumentCaptor @@ -951,6 +953,9 @@ public class WifiAwareManagerTest { WifiAwareNetworkSpecifier ns = (WifiAwareNetworkSpecifier) publishSession.getValue().createNetworkSpecifierOpen( peerHandle); + WifiAwareNetworkSpecifier nsb = (WifiAwareNetworkSpecifier) new WifiAwareManager + .NetworkSpecifierBuilder().setDiscoverySession(publishSession.getValue()) + .setPeerHandle(peerHandle).build(); // validate format collector.checkThat("role", role, equalTo(ns.role)); @@ -958,9 +963,18 @@ public class WifiAwareManagerTest { collector.checkThat("session_id", sessionId, equalTo(ns.sessionId)); collector.checkThat("peer_id", peerHandle.peerId, equalTo(ns.peerId)); + collector.checkThat("role", role, equalTo(nsb.role)); + collector.checkThat("client_id", clientId, equalTo(nsb.clientId)); + collector.checkThat("session_id", sessionId, equalTo(nsb.sessionId)); + collector.checkThat("peer_id", peerHandle.peerId, equalTo(nsb.peerId)); + // (4) request an encrypted (PMK) network specifier from the session ns = (WifiAwareNetworkSpecifier) publishSession.getValue().createNetworkSpecifierPmk( peerHandle, pmk); + nsb = + (WifiAwareNetworkSpecifier) new WifiAwareManager.NetworkSpecifierBuilder() + .setDiscoverySession( + publishSession.getValue()).setPeerHandle(peerHandle).setPmk(pmk).build(); // validate format collector.checkThat("role", role, equalTo(ns.role)); @@ -969,9 +983,18 @@ public class WifiAwareManagerTest { collector.checkThat("peer_id", peerHandle.peerId, equalTo(ns.peerId)); collector.checkThat("pmk", pmk , equalTo(ns.pmk)); + collector.checkThat("role", role, equalTo(nsb.role)); + collector.checkThat("client_id", clientId, equalTo(nsb.clientId)); + collector.checkThat("session_id", sessionId, equalTo(nsb.sessionId)); + collector.checkThat("peer_id", peerHandle.peerId, equalTo(nsb.peerId)); + collector.checkThat("pmk", pmk , equalTo(nsb.pmk)); + // (5) request an encrypted (Passphrase) network specifier from the session ns = (WifiAwareNetworkSpecifier) publishSession.getValue().createNetworkSpecifierPassphrase( peerHandle, passphrase); + nsb = (WifiAwareNetworkSpecifier) new WifiAwareManager.NetworkSpecifierBuilder() + .setDiscoverySession(publishSession.getValue()).setPeerHandle(peerHandle) + .setPskPassphrase(passphrase).build(); // validate format collector.checkThat("role", role, equalTo(ns.role)); @@ -980,6 +1003,12 @@ public class WifiAwareManagerTest { collector.checkThat("peer_id", peerHandle.peerId, equalTo(ns.peerId)); collector.checkThat("passphrase", passphrase, equalTo(ns.passphrase)); + collector.checkThat("role", role, equalTo(nsb.role)); + collector.checkThat("client_id", clientId, equalTo(nsb.clientId)); + collector.checkThat("session_id", sessionId, equalTo(nsb.sessionId)); + collector.checkThat("peer_id", peerHandle.peerId, equalTo(nsb.peerId)); + collector.checkThat("passphrase", passphrase, equalTo(nsb.passphrase)); + verifyNoMoreInteractions(mockCallback, mockSessionCallback, mockAwareService, mockPublishSession, mockRttListener); } @@ -1051,7 +1080,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientNullPmk() throws Exception { - executeNetworkSpecifierWithClient(new PeerHandle(123412), true, null, null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, null, null, false); } /** @@ -1059,7 +1088,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientIncorrectLengthPmk() throws Exception { - executeNetworkSpecifierWithClient(new PeerHandle(123412), true, PMK_INVALID, null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, PMK_INVALID, null, false); } /** @@ -1067,7 +1096,7 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientNullPassphrase() throws Exception { - executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, null); + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, null, false); } /** @@ -1076,7 +1105,7 @@ public class WifiAwareManagerTest { @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientTooShortPassphrase() throws Exception { executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, - PASSPHRASE_TOO_SHORT); + PASSPHRASE_TOO_SHORT, false); } /** @@ -1084,7 +1113,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientTooLongPassphrase() throws Exception { - executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, PASSPHRASE_TOO_LONG); + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, PASSPHRASE_TOO_LONG, + false); } /** @@ -1092,7 +1122,8 @@ public class WifiAwareManagerTest { */ @Test(expected = IllegalArgumentException.class) public void testNetworkSpecifierWithClientNullPeer() throws Exception { - executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID); + mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.P; + executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID, false); } /** @@ -1101,11 +1132,75 @@ public class WifiAwareManagerTest { @Test public void testNetworkSpecifierWithClientNullPeerLegacyApi() throws Exception { mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O; - executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID); + executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID, false); + } + + /** + * Validate that a null PMK triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientNullPmkBuilder() throws Exception { + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, null, null, true); + } + + /** + * Validate that a non-32-bytes PMK triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientIncorrectLengthPmkBuilder() throws Exception { + executeNetworkSpecifierWithClient(new PeerHandle(123412), true, PMK_INVALID, null, true); + } + + /** + * Validate that a null Passphrase triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientNullPassphraseBuilder() throws Exception { + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, null, true); + } + + /** + * Validate that a too short Passphrase triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientTooShortPassphraseBuilder() throws Exception { + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, + PASSPHRASE_TOO_SHORT, true); + } + + /** + * Validate that a too long Passphrase triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientTooLongPassphraseBuilder() throws Exception { + executeNetworkSpecifierWithClient(new PeerHandle(123412), false, null, PASSPHRASE_TOO_LONG, + true); + } + + /** + * Validate that a null PeerHandle triggers an exception. + */ + @Test(expected = IllegalArgumentException.class) + public void testNetworkSpecifierWithClientNullPeerBuilder() throws Exception { + executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID, true); + } + + /** + * Validate that a null PeerHandle does not trigger an exception for legacy API. + */ + @Test + public void testNetworkSpecifierWithClientNullPeerLegacyApiBuilder() throws Exception { + mockApplicationInfo.targetSdkVersion = Build.VERSION_CODES.O; + executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID, false); + } + + @Test(expected = UnsupportedOperationException.class) + public void testNetworkSpecifierDeprecatedOnNewApi() throws Exception { + executeNetworkSpecifierWithClient(null, false, null, PASSPHRASE_VALID, false); } private void executeNetworkSpecifierWithClient(PeerHandle peerHandle, boolean doPmk, byte[] pmk, - String passphrase) throws Exception { + String passphrase, boolean useBuilder) throws Exception { final int clientId = 4565; final int sessionId = 123; final ConfigRequest configRequest = new ConfigRequest.Builder().build(); @@ -1142,9 +1237,20 @@ public class WifiAwareManagerTest { // (3) create network specifier if (doPmk) { - publishSession.getValue().createNetworkSpecifierPmk(peerHandle, pmk); + if (useBuilder) { + new WifiAwareManager.NetworkSpecifierBuilder().setDiscoverySession( + publishSession.getValue()).setPeerHandle(peerHandle).setPmk(pmk).build(); + } else { + publishSession.getValue().createNetworkSpecifierPmk(peerHandle, pmk); + } } else { - publishSession.getValue().createNetworkSpecifierPassphrase(peerHandle, passphrase); + if (useBuilder) { + new WifiAwareManager.NetworkSpecifierBuilder().setDiscoverySession( + publishSession.getValue()).setPeerHandle(peerHandle).setPskPassphrase( + passphrase).build(); + } else { + publishSession.getValue().createNetworkSpecifierPassphrase(peerHandle, passphrase); + } } } |