diff options
author | Wei Wang <weiwa@google.com> | 2016-03-15 21:31:57 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-03-15 21:32:03 +0000 |
commit | 7462584f8a30f6dba5dc979b3dfbe3821b739d9c (patch) | |
tree | 6792e6e36205107e5409d424ef66df36f2e8c6c6 /tests | |
parent | e6ee3780bad3664ce5d2c61f9a4e76a5daae1a7d (diff) | |
parent | 202fdf96e1e0191be37a916d97116df35fe3cbfc (diff) |
Merge "Fix native crash caused by null rtt params." into nyc-dev
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/RttManagerTest.java | 93 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/RttServiceTest.java | 36 |
2 files changed, 128 insertions, 1 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/RttManagerTest.java b/tests/wifitests/src/com/android/server/wifi/RttManagerTest.java new file mode 100644 index 000000000..7a800a3de --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/RttManagerTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static junit.framework.Assert.assertTrue; + +import android.net.wifi.RttManager; +import android.net.wifi.RttManager.ParcelableRttParams; +import android.net.wifi.RttManager.RttParams; +import android.os.Parcel; + +import org.junit.Test; + +/** + * Unit test for {@link RttManager} + */ +public class RttManagerTest { + + // Verify ParcelableRttParams are the same after writing and reading from parcel. + private void verifyReadWriteParcelForRttParams(ParcelableRttParams params) { + Parcel parcel = Parcel.obtain(); + params.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + ParcelableRttParams paramsFromParcel = ParcelableRttParams.CREATOR.createFromParcel(parcel); + assertTrue(verifyEquals(params, paramsFromParcel)); + } + + // Check if two ParcelableRttParams equals. + private boolean verifyEquals(ParcelableRttParams params, ParcelableRttParams params2) { + if (params.mParams == params2.mParams) { + return true; + } + if (params == null || params2.mParams == null) { + return false; + } + RttParams[] paramsArray = params.mParams; + RttParams[] paramsArray2 = params2.mParams; + if (paramsArray.length != paramsArray2.length) { + return false; + } + for (int i = 0; i < paramsArray.length; i++) { + if (!rttParamsEquals(paramsArray[i], paramsArray2[i])) { + return false; + } + } + return true; + } + + // Check if two RttParams equals. Note only a subset of fields are checked. + private boolean rttParamsEquals(RttParams params1, RttParams params2) { + return params1.bssid.equals(params2.bssid) + && params1.secure == params2.secure + && params1.frequency == params2.frequency; + } + + /** + * Test writing and reading {@link RttParams} from Parcel. + */ + @Test + public void testRttParamsReadWriteParcel() throws Exception { + RttParams params = new RttParams(); + params.bssid = "12-34-56-78-9A-BC"; + params.secure = true; + params.frequency = 5240; + + RttParams params2 = new RttParams(); + params2.bssid = "12-34-56-78-9B-CD"; + params2.secure = false; + params2.frequency = 5220; + + ParcelableRttParams parcelableParams = new ParcelableRttParams(new RttParams[] { + params, params2 + }); + verifyReadWriteParcelForRttParams(parcelableParams); + // Make sure writing/reading parcel doesn't change value for empty RttParams. + verifyReadWriteParcelForRttParams(new ParcelableRttParams(new RttParams[0])); + } + +} diff --git a/tests/wifitests/src/com/android/server/wifi/RttServiceTest.java b/tests/wifitests/src/com/android/server/wifi/RttServiceTest.java index b44f56cdc..3c6f678e0 100644 --- a/tests/wifitests/src/com/android/server/wifi/RttServiceTest.java +++ b/tests/wifitests/src/com/android/server/wifi/RttServiceTest.java @@ -31,6 +31,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.net.wifi.RttManager; +import android.net.wifi.RttManager.ParcelableRttParams; import android.net.wifi.RttManager.ResponderConfig; import android.net.wifi.WifiManager; import android.os.Handler; @@ -96,6 +97,28 @@ public class RttServiceTest { return channel; } + private void sendRangingRequestFailed(BidirectionalAsyncChannel channel, Handler handler, + int clientKey, ParcelableRttParams params) { + Message message = sendRangingRequest(channel, handler, clientKey, params); + assertEquals("ranging request did not fail", + RttManager.CMD_OP_FAILED, message.what); + verifyNoMoreInteractions(mWifiNative); + } + + // Send rtt ranging request message and verify failure. + private Message sendRangingRequest(BidirectionalAsyncChannel channel, Handler handler, + int clientKey, ParcelableRttParams params) { + Message message = new Message(); + message.what = RttManager.CMD_OP_START_RANGING; + message.arg2 = clientKey; + message.obj = params; + channel.sendMessage(message); + mLooper.dispatchAll(); + ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); + verify(handler, atLeastOnce()).handleMessage(messageCaptor.capture()); + return messageCaptor.getValue(); + } + // Send enable responder message and verify success. private void sendEnableResponderSucceed(BidirectionalAsyncChannel channel, Handler handler, int clientKey) { @@ -107,7 +130,7 @@ public class RttServiceTest { assertEquals("mac address mismatch", MAC, actualMac); } - // Send enable responder message and veify failure. + // Send enable responder message and verify failure. private void sendEnableResponderFailed(BidirectionalAsyncChannel channel, Handler handler, int clientKey) { Message message = sendEnableResponder(channel, handler, clientKey, null); @@ -219,4 +242,15 @@ public class RttServiceTest { verify(mWifiNative, times(1)).getMacAddress(); verifyNoMoreInteractions(mWifiNative); } + + /** + * Test RTT ranging with empty RttParams. + */ + @Test + public void testInitiatorEmptyParams() throws Exception { + startWifi(); + Handler handler = mock(Handler.class); + BidirectionalAsyncChannel channel = connectChannel(handler); + sendRangingRequestFailed(channel, handler, CLIENT_KEY1, new ParcelableRttParams(null)); + } } |