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 | |
parent | e6ee3780bad3664ce5d2c61f9a4e76a5daae1a7d (diff) | |
parent | 202fdf96e1e0191be37a916d97116df35fe3cbfc (diff) |
Merge "Fix native crash caused by null rtt params." into nyc-dev
4 files changed, 139 insertions, 2 deletions
diff --git a/service/java/com/android/server/wifi/RttService.java b/service/java/com/android/server/wifi/RttService.java index 3a2986574..7afe354fc 100644 --- a/service/java/com/android/server/wifi/RttService.java +++ b/service/java/com/android/server/wifi/RttService.java @@ -332,7 +332,8 @@ public final class RttService extends SystemService { RttManager.ParcelableRttParams params = (RttManager.ParcelableRttParams)msg.obj; - if (params == null) { + if (params == null || params.mParams == null + || params.mParams.length == 0) { replyFailed(msg, RttManager.REASON_INVALID_REQUEST, "No params"); } else if (ci.addRttRequest(msg.arg2, params) == false) { diff --git a/service/jni/com_android_server_wifi_WifiNative.cpp b/service/jni/com_android_server_wifi_WifiNative.cpp index add0d17fe..5b38495ab 100644 --- a/service/jni/com_android_server_wifi_WifiNative.cpp +++ b/service/jni/com_android_server_wifi_WifiNative.cpp @@ -1144,6 +1144,10 @@ static jboolean android_net_wifi_requestRange( wifi_interface_handle handle = getIfaceHandle(helper, cls, iface); ALOGD("sending rtt request [%d] = %p", id, handle); + if (params == NULL) { + ALOGE("ranging params are empty"); + return false; + } wifi_rtt_config configs[MaxRttConfigs]; memset(&configs, 0, sizeof(configs)); @@ -1209,6 +1213,11 @@ static jboolean android_net_wifi_cancelRange( wifi_interface_handle handle = getIfaceHandle(helper, cls, iface); ALOGD("cancelling rtt request [%d] = %p", id, handle); + if (params == NULL) { + ALOGE("ranging params are empty"); + return false; + } + mac_addr addrs[MaxRttConfigs]; memset(&addrs, 0, sizeof(addrs)); 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)); + } } |