diff options
author | Rebecca Silberstein <silberst@google.com> | 2017-03-29 13:14:15 -0700 |
---|---|---|
committer | Rebecca Silberstein <silberst@google.com> | 2017-04-28 22:24:05 -0700 |
commit | 768b7a7cb87525e92eb1a59a7a3e0c98b9f6f238 (patch) | |
tree | 3a227d9e79fb60ebf17ee37942449ee58fb76a12 | |
parent | 003b695597a1f010724662fbf4ac76953a71e3b8 (diff) |
LocalOnlyHotspotRequestInfo: track LOHS requests
Add a new class to keep track of apps Binder instances that request
LocalOnlyHotspot. When the app dies, it will trigger a callback to
remove the request. We will also unlinkToDeath when the app calls to
stop using LocalOnlyHotspot. This object also contains a Messenger that
will allow WifiServiceImpl to communicate updates to WifiManager for the
requesting app.
Bug: 36732530
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: Ia366a3cc7339cdea94a137d62905b15b72cf05c0
-rw-r--r-- | service/java/com/android/server/wifi/LocalOnlyHotspotRequestInfo.java | 96 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/LocalOnlyHotspotRequestInfoTest.java | 151 |
2 files changed, 247 insertions, 0 deletions
diff --git a/service/java/com/android/server/wifi/LocalOnlyHotspotRequestInfo.java b/service/java/com/android/server/wifi/LocalOnlyHotspotRequestInfo.java new file mode 100644 index 000000000..2925d966b --- /dev/null +++ b/service/java/com/android/server/wifi/LocalOnlyHotspotRequestInfo.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2017 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 android.annotation.NonNull; +import android.os.Binder; +import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; + +import com.android.internal.util.Preconditions; + +/** + * Tracks information about applications requesting use of the LocalOnlyHotspot. + * + * @hide + */ +public class LocalOnlyHotspotRequestInfo implements IBinder.DeathRecipient { + private final int mUid; + private final IBinder mBinder; + private final RequestingApplicationDeathCallback mCallback; + private final Messenger mMessenger; + + /** + * Callback for use with LocalOnlyHotspot to unregister requesting applications upon death. + */ + public interface RequestingApplicationDeathCallback { + /** + * Called when requesting app has died. + */ + void onLocalOnlyHotspotRequestorDeath(LocalOnlyHotspotRequestInfo requestor); + } + + LocalOnlyHotspotRequestInfo(@NonNull IBinder binder, @NonNull Messenger messenger, + @NonNull RequestingApplicationDeathCallback callback) { + mUid = Binder.getCallingUid(); + mBinder = Preconditions.checkNotNull(binder); + mMessenger = Preconditions.checkNotNull(messenger); + mCallback = Preconditions.checkNotNull(callback); + + try { + mBinder.linkToDeath(this, 0); + } catch (RemoteException e) { + binderDied(); + } + } + + /** + * Allow caller to unlink this object from binder death. + */ + public void unlinkDeathRecipient() { + mBinder.unlinkToDeath(this, 0); + } + + /** + * Application requesting LocalOnlyHotspot died + */ + @Override + public void binderDied() { + mCallback.onLocalOnlyHotspotRequestorDeath(this); + } + + /** + * Send a message to WifiManager for the calling application. + * + * @param what Message type to send + * @param arg1 arg1 for the message + * + * @throws RemoteException + */ + public void sendMessage(int what, int arg1) throws RemoteException { + Message message = Message.obtain(); + message.what = what; + message.arg1 = arg1; + mMessenger.send(message); + } + + public int getUid() { + return mUid; + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/LocalOnlyHotspotRequestInfoTest.java b/tests/wifitests/src/com/android/server/wifi/LocalOnlyHotspotRequestInfoTest.java new file mode 100644 index 000000000..0e8ead2e6 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/LocalOnlyHotspotRequestInfoTest.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2017 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 org.junit.Assert.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.verify; + +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; +import android.os.Process; +import android.os.RemoteException; +import android.os.test.TestLooper; +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/* + * Unit tests for {@link com.android.server.wifi.LocalOnlyHotspotRequestInfo}. + */ +@SmallTest +public class LocalOnlyHotspotRequestInfoTest { + + private static final String TAG = "LocalOnlyHotspotRequestInfoTest"; + @Mock IBinder mAppBinder; + @Mock LocalOnlyHotspotRequestInfo.RequestingApplicationDeathCallback mCallback; + private Handler mHandler; + private Messenger mMessenger; + private TestLooper mTestLooper; + RemoteException mRemoteException; + private LocalOnlyHotspotRequestInfo mLOHSRequestInfo; + + /** + * Setup test. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mTestLooper = new TestLooper(); + mHandler = new Handler(mTestLooper.getLooper()); + mMessenger = new Messenger(mHandler); + mRemoteException = new RemoteException("Test Remote Exception"); + } + + /** + * Make sure we link the call to request LocalOnlyHotspot by an app is linked to their Binder + * call. This allows us to clean up if the app dies. + */ + @Test + public void verifyBinderLinkToDeathIsCalled() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + verify(mAppBinder).linkToDeath(eq(mLOHSRequestInfo), eq(0)); + } + + /** + * Calls to create the requestor to binder death should not pass null callback. + */ + @Test(expected = NullPointerException.class) + public void verifyNullCallbackChecked() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, null); + } + + /** + * Calls to create the request info object should not pass a null messenger. + */ + @Test(expected = NullPointerException.class) + public void verifyNullMessengerChecked() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, null, mCallback); + } + + /** + * Calls to link the requestor to binder death should not pass null binder + */ + @Test(expected = NullPointerException.class) + public void verifyNullBinderChecked() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(null, mMessenger, mCallback); + } + + /** + * Calls to unlink the DeathRecipient should call to unlink from Binder. + */ + @Test + public void verifyUnlinkDeathRecipientUnlinksFromBinder() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + mLOHSRequestInfo.unlinkDeathRecipient(); + verify(mAppBinder).unlinkToDeath(eq(mLOHSRequestInfo), eq(0)); + } + + /** + * Binder death notification should trigger a callback on the requestor. + */ + @Test + public void verifyBinderDeathTriggersCallback() { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + mLOHSRequestInfo.binderDied(); + verify(mCallback).onLocalOnlyHotspotRequestorDeath(eq(mLOHSRequestInfo)); + } + + /** + * Verify a RemoteException when calling linkToDeath triggers the callback. + */ + @Test + public void verifyRemoteExceptionTriggersCallback() throws Exception { + doThrow(mRemoteException).when(mAppBinder) + .linkToDeath(any(IBinder.DeathRecipient.class), eq(0)); + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + verify(mCallback).onLocalOnlyHotspotRequestorDeath(eq(mLOHSRequestInfo)); + } + + /** + * Verify the uid is properly set. + */ + @Test + public void verifyUid() { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + assertEquals(Process.myUid(), mLOHSRequestInfo.getUid()); + } + + /** + * Verify that sendMessage does send a Message properly + */ + @Test + public void verifySendMessenger() throws Exception { + mLOHSRequestInfo = new LocalOnlyHotspotRequestInfo(mAppBinder, mMessenger, mCallback); + mLOHSRequestInfo.sendMessage(1, 1); + Message message = mTestLooper.nextMessage(); + assertEquals(1, message.what); + assertEquals(1, message.arg1); + } +} |