From 0c9a3591240a5d9f7ee74437bee7b0a16c8d49ff Mon Sep 17 00:00:00 2001 From: Etan Cohen Date: Tue, 27 Sep 2016 09:17:49 -0700 Subject: [NAN] Use a hidden class for peer ID opaque handle: updated unit tests Results in clearer run-time exception (relatively to using Integer). (cherry-pick of commit 1012e64f5bc3e4ba66a00099e12ba69023e6fe6a) Bug: 31689166 Test: unit-tests Change-Id: I020fd7f518ba65f164c6ec7b798ed3262b39a960 --- .../wifi/nan/WifiNanDataPathStateManagerTest.java | 27 +++++----- .../server/wifi/nan/WifiNanManagerTest.java | 57 ++++++++++++++-------- 2 files changed, 52 insertions(+), 32 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/nan/WifiNanDataPathStateManagerTest.java b/tests/wifitests/src/com/android/server/wifi/nan/WifiNanDataPathStateManagerTest.java index b2a85d2a4..13e3dd41c 100644 --- a/tests/wifitests/src/com/android/server/wifi/nan/WifiNanDataPathStateManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/nan/WifiNanDataPathStateManagerTest.java @@ -401,7 +401,8 @@ public class WifiNanDataPathStateManagerTest { boolean provideToken, boolean getConfirmation) throws Exception { final int clientId = 123; final int pubSubId = 11234; - final Object peerHandle = Integer.valueOf(1341234); + final WifiNanManager.OpaquePeerHandle peerHandle = new WifiNanManager.OpaquePeerHandle( + 1341234); final int ndpId = 2; final String token = "some token"; final String peerToken = "let's go!"; @@ -413,8 +414,8 @@ public class WifiNanDataPathStateManagerTest { InOrder inOrder = inOrder(mMockNative, mMockCm, mMockCallback, mMockSessionCallback); // (0) initialize - Pair res = initDataPathEndPoint(clientId, pubSubId, - (Integer) peerHandle, peerDiscoveryMac, inOrder); + Pair res = initDataPathEndPoint(clientId, pubSubId, peerHandle, + peerDiscoveryMac, inOrder); // (1) request network NetworkRequest nr; @@ -433,7 +434,7 @@ public class WifiNanDataPathStateManagerTest { res.second.send(reqNetworkMsg); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).initiateDataPath(transactionId.capture(), - eq(useDirect ? 0 : (Integer) peerHandle), + eq(useDirect ? 0 : peerHandle.peerId), eq(WifiNanNative.CHANNEL_REQUEST_TYPE_REQUESTED), eq(2437), eq(peerDiscoveryMac), eq("nan0"), eq(token.getBytes())); mDut.onInitiateDataPathResponseSuccess(transactionId.getValue(), ndpId); @@ -481,7 +482,8 @@ public class WifiNanDataPathStateManagerTest { boolean provideToken, boolean getConfirmation) throws Exception { final int clientId = 123; final int pubSubId = 11234; - final Object peerHandle = Integer.valueOf(1341234); + final WifiNanManager.OpaquePeerHandle peerHandle = new WifiNanManager.OpaquePeerHandle( + 1341234); final int ndpId = 2; final String token = "some token"; final String peerToken = "let's go!"; @@ -493,8 +495,8 @@ public class WifiNanDataPathStateManagerTest { InOrder inOrder = inOrder(mMockNative, mMockCm, mMockCallback, mMockSessionCallback); // (0) initialize - Pair res = initDataPathEndPoint(clientId, pubSubId, - (Integer) peerHandle, peerDiscoveryMac, inOrder); + Pair res = initDataPathEndPoint(clientId, pubSubId, peerHandle, + peerDiscoveryMac, inOrder); // (1) request network NetworkRequest nr; @@ -675,8 +677,9 @@ public class WifiNanDataPathStateManagerTest { return new NetworkRequest(nc, 0, 0); } - private Pair initDataPathEndPoint(int clientId, int pubSubId, int peerId, - byte[] peerDiscoveryMac, InOrder inOrder) throws Exception { + private Pair initDataPathEndPoint(int clientId, int pubSubId, + WifiNanManager.OpaquePeerHandle peerHandle, byte[] peerDiscoveryMac, InOrder inOrder) + throws Exception { final int uid = 1000; final int pid = 2000; final String callingPackage = "com.android.somePackage"; @@ -729,9 +732,11 @@ public class WifiNanDataPathStateManagerTest { mDut.onSessionConfigSuccessResponse(transactionId.getValue(), true, pubSubId); mMockLooper.dispatchAll(); inOrder.verify(mMockSessionCallback).onSessionStarted(sessionId.capture()); - mDut.onMessageReceivedNotification(pubSubId, peerId, peerDiscoveryMac, someMsg.getBytes()); + mDut.onMessageReceivedNotification(pubSubId, peerHandle.peerId, peerDiscoveryMac, + someMsg.getBytes()); mMockLooper.dispatchAll(); - inOrder.verify(mMockSessionCallback).onMessageReceived(peerId, someMsg.getBytes()); + inOrder.verify(mMockSessionCallback).onMessageReceived(peerHandle.peerId, + someMsg.getBytes()); return new Pair<>(sessionId.getValue(), messengerCaptor.getValue()); } diff --git a/tests/wifitests/src/com/android/server/wifi/nan/WifiNanManagerTest.java b/tests/wifitests/src/com/android/server/wifi/nan/WifiNanManagerTest.java index 628c9a5d1..0d68bd9c2 100644 --- a/tests/wifitests/src/com/android/server/wifi/nan/WifiNanManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/nan/WifiNanManagerTest.java @@ -274,7 +274,7 @@ public class WifiNanManagerTest { final int sessionId = 123; final ConfigRequest configRequest = new ConfigRequest.Builder().build(); final PublishConfig publishConfig = new PublishConfig.Builder().build(); - final int peerId = 873; + final WifiNanManager.OpaquePeerHandle peerHandle = new WifiNanManager.OpaquePeerHandle(873); final String string1 = "hey from here..."; final String string2 = "some other arbitrary string..."; final int messageId = 2123; @@ -290,6 +290,7 @@ public class WifiNanManagerTest { .forClass(IWifiNanDiscoverySessionCallback.class); ArgumentCaptor publishSession = ArgumentCaptor .forClass(WifiNanPublishDiscoverySession.class); + ArgumentCaptor peerIdCaptor = ArgumentCaptor.forClass(Object.class); // (0) connect + success mDut.attach(mMockLooperHandler, configRequest, mockCallback, null); @@ -311,18 +312,24 @@ public class WifiNanManagerTest { inOrder.verify(mockSessionCallback).onPublishStarted(publishSession.capture()); // (3) ... - publishSession.getValue().sendMessage(peerId, messageId, string1.getBytes()); - sessionProxyCallback.getValue().onMatch(peerId, string1.getBytes(), string2.getBytes()); - sessionProxyCallback.getValue().onMessageReceived(peerId, string1.getBytes()); + publishSession.getValue().sendMessage(peerHandle, messageId, string1.getBytes()); + sessionProxyCallback.getValue().onMatch(peerHandle.peerId, string1.getBytes(), + string2.getBytes()); + sessionProxyCallback.getValue().onMessageReceived(peerHandle.peerId, string1.getBytes()); sessionProxyCallback.getValue().onMessageSendFail(messageId, reason); sessionProxyCallback.getValue().onMessageSendSuccess(messageId); mMockLooper.dispatchAll(); - inOrder.verify(mockNanService).sendMessage(eq(clientId), eq(sessionId), eq(peerId), - eq(string1.getBytes()), eq(messageId), eq(0)); - inOrder.verify(mockSessionCallback).onServiceDiscovered(eq(peerId), eq(string1.getBytes()), - eq(string2.getBytes())); - inOrder.verify(mockSessionCallback).onMessageReceived(eq(peerId), eq(string1.getBytes())); + inOrder.verify(mockNanService).sendMessage(eq(clientId), eq(sessionId), + eq(peerHandle.peerId), eq(string1.getBytes()), eq(messageId), eq(0)); + inOrder.verify(mockSessionCallback).onServiceDiscovered(peerIdCaptor.capture(), + eq(string1.getBytes()), eq(string2.getBytes())); + assertEquals(((WifiNanManager.OpaquePeerHandle) peerIdCaptor.getValue()).peerId, + peerHandle.peerId); + inOrder.verify(mockSessionCallback).onMessageReceived(peerIdCaptor.capture(), + eq(string1.getBytes())); + assertEquals(((WifiNanManager.OpaquePeerHandle) peerIdCaptor.getValue()).peerId, + peerHandle.peerId); inOrder.verify(mockSessionCallback).onMessageSendFailed(eq(messageId)); inOrder.verify(mockSessionCallback).onMessageSent(eq(messageId)); @@ -408,7 +415,7 @@ public class WifiNanManagerTest { final int sessionId = 123; final ConfigRequest configRequest = new ConfigRequest.Builder().build(); final SubscribeConfig subscribeConfig = new SubscribeConfig.Builder().build(); - final int peerId = 873; + final WifiNanManager.OpaquePeerHandle peerHandle = new WifiNanManager.OpaquePeerHandle(873); final String string1 = "hey from here..."; final String string2 = "some other arbitrary string..."; final int messageId = 2123; @@ -424,6 +431,7 @@ public class WifiNanManagerTest { .forClass(IWifiNanDiscoverySessionCallback.class); ArgumentCaptor subscribeSession = ArgumentCaptor .forClass(WifiNanSubscribeDiscoverySession.class); + ArgumentCaptor peerIdCaptor = ArgumentCaptor.forClass(Object.class); // (0) connect + success mDut.attach(mMockLooperHandler, configRequest, mockCallback, null); @@ -445,18 +453,24 @@ public class WifiNanManagerTest { inOrder.verify(mockSessionCallback).onSubscribeStarted(subscribeSession.capture()); // (3) ... - subscribeSession.getValue().sendMessage(peerId, messageId, string1.getBytes()); - sessionProxyCallback.getValue().onMatch(peerId, string1.getBytes(), string2.getBytes()); - sessionProxyCallback.getValue().onMessageReceived(peerId, string1.getBytes()); + subscribeSession.getValue().sendMessage(peerHandle, messageId, string1.getBytes()); + sessionProxyCallback.getValue().onMatch(peerHandle.peerId, string1.getBytes(), + string2.getBytes()); + sessionProxyCallback.getValue().onMessageReceived(peerHandle.peerId, string1.getBytes()); sessionProxyCallback.getValue().onMessageSendFail(messageId, reason); sessionProxyCallback.getValue().onMessageSendSuccess(messageId); mMockLooper.dispatchAll(); - inOrder.verify(mockNanService).sendMessage(eq(clientId), eq(sessionId), eq(peerId), - eq(string1.getBytes()), eq(messageId), eq(0)); - inOrder.verify(mockSessionCallback).onServiceDiscovered(eq(peerId), eq(string1.getBytes()), - eq(string2.getBytes())); - inOrder.verify(mockSessionCallback).onMessageReceived(eq(peerId), eq(string1.getBytes())); + inOrder.verify(mockNanService).sendMessage(eq(clientId), eq(sessionId), + eq(peerHandle.peerId), eq(string1.getBytes()), eq(messageId), eq(0)); + inOrder.verify(mockSessionCallback).onServiceDiscovered(peerIdCaptor.capture(), + eq(string1.getBytes()), eq(string2.getBytes())); + assertEquals(((WifiNanManager.OpaquePeerHandle) peerIdCaptor.getValue()).peerId, + peerHandle.peerId); + inOrder.verify(mockSessionCallback).onMessageReceived(peerIdCaptor.capture(), + eq(string1.getBytes())); + assertEquals(((WifiNanManager.OpaquePeerHandle) peerIdCaptor.getValue()).peerId, + peerHandle.peerId); inOrder.verify(mockSessionCallback).onMessageSendFailed(eq(messageId)); inOrder.verify(mockSessionCallback).onMessageSent(eq(messageId)); @@ -928,7 +942,8 @@ public class WifiNanManagerTest { public void testNetworkSpecifierWithClient() throws Exception { final int clientId = 4565; final int sessionId = 123; - final int peerId = 123412; + final WifiNanManager.OpaquePeerHandle peerHandle = new WifiNanManager.OpaquePeerHandle( + 123412); final int role = WifiNanManager.WIFI_NAN_DATA_PATH_ROLE_INITIATOR; final String token = "Some arbitrary token string - can really be anything"; final ConfigRequest configRequest = new ConfigRequest.Builder().build(); @@ -966,7 +981,7 @@ public class WifiNanManagerTest { inOrder.verify(mockSessionCallback).onPublishStarted(publishSession.capture()); // (3) request a network specifier from the session - String networkSpecifier = publishSession.getValue().createNetworkSpecifier(role, peerId, + String networkSpecifier = publishSession.getValue().createNetworkSpecifier(role, peerHandle, token.getBytes()); // validate format @@ -977,7 +992,7 @@ public class WifiNanManagerTest { equalTo(jsonObject.getInt(WifiNanManager.NETWORK_SPECIFIER_KEY_CLIENT_ID))); collector.checkThat("session_id", sessionId, equalTo(jsonObject.getInt(WifiNanManager.NETWORK_SPECIFIER_KEY_SESSION_ID))); - collector.checkThat("peer_id", peerId, + collector.checkThat("peer_id", peerHandle.peerId, equalTo(jsonObject.getInt(WifiNanManager.NETWORK_SPECIFIER_KEY_PEER_ID))); collector.checkThat("token", tokenB64, equalTo(jsonObject.getString(WifiNanManager.NETWORK_SPECIFIER_KEY_TOKEN))); -- cgit v1.2.3