Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.backup.transport; |
| 18 | |
| 19 | import static com.android.server.backup.TransportManager.SERVICE_ACTION_TRANSPORT_HOST; |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 20 | import static com.google.common.truth.Truth.assertThat; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.ArgumentMatchers.isNull; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | import static org.robolectric.Shadows.shadowOf; |
| 28 | |
| 29 | import android.content.ComponentName; |
| 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
| 32 | import android.content.ServiceConnection; |
| 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
| 35 | import android.os.UserHandle; |
| 36 | import android.platform.test.annotations.Presubmit; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 37 | import com.android.internal.backup.IBackupTransport; |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 38 | import com.android.server.EventLogTags; |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 39 | import com.android.server.backup.TransportManager; |
| 40 | import com.android.server.testing.FrameworkRobolectricTestRunner; |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 41 | import com.android.server.testing.ShadowEventLog; |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 42 | import com.android.server.testing.SystemLoaderClasses; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.mockito.ArgumentCaptor; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.MockitoAnnotations; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 49 | import org.robolectric.annotation.Config; |
| 50 | import org.robolectric.shadows.ShadowLooper; |
| 51 | |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 52 | @RunWith(FrameworkRobolectricTestRunner.class) |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 53 | @Config(manifest = Config.NONE, sdk = 26, shadows = {ShadowEventLog.class}) |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 54 | @SystemLoaderClasses({TransportManager.class, TransportClient.class}) |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 55 | @Presubmit |
| 56 | public class TransportClientTest { |
| 57 | private static final String PACKAGE_NAME = "some.package.name"; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 58 | |
| 59 | @Mock private Context mContext; |
| 60 | @Mock private TransportConnectionListener mTransportConnectionListener; |
| 61 | @Mock private TransportConnectionListener mTransportConnectionListener2; |
| 62 | @Mock private IBackupTransport.Stub mIBackupTransport; |
| 63 | private TransportClient mTransportClient; |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 64 | private ComponentName mTransportComponent; |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 65 | private String mTransportString; |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 66 | private Intent mBindIntent; |
| 67 | private ShadowLooper mShadowLooper; |
| 68 | |
| 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | MockitoAnnotations.initMocks(this); |
| 72 | |
| 73 | Looper mainLooper = Looper.getMainLooper(); |
| 74 | mShadowLooper = shadowOf(mainLooper); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 75 | mTransportComponent = |
| 76 | new ComponentName(PACKAGE_NAME, PACKAGE_NAME + ".transport.Transport"); |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 77 | mTransportString = mTransportComponent.flattenToShortString(); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 78 | mBindIntent = new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(mTransportComponent); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 79 | mTransportClient = |
| 80 | new TransportClient( |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 81 | mContext, mBindIntent, mTransportComponent, "1", new Handler(mainLooper)); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 82 | |
| 83 | when(mContext.bindServiceAsUser( |
| 84 | eq(mBindIntent), |
| 85 | any(ServiceConnection.class), |
| 86 | anyInt(), |
| 87 | any(UserHandle.class))) |
| 88 | .thenReturn(true); |
| 89 | } |
| 90 | |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 91 | @Test |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 92 | public void testGetTransportComponent_returnsTransportComponent() { |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 93 | assertThat(mTransportClient.getTransportComponent()).isEqualTo(mTransportComponent); |
Bernardo Rufino | ab95333 | 2017-11-22 22:10:32 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 96 | @Test |
| 97 | public void testConnectAsync_callsBindService() throws Exception { |
| 98 | mTransportClient.connectAsync(mTransportConnectionListener, "caller"); |
| 99 | |
| 100 | verify(mContext) |
| 101 | .bindServiceAsUser( |
| 102 | eq(mBindIntent), |
| 103 | any(ServiceConnection.class), |
| 104 | anyInt(), |
| 105 | any(UserHandle.class)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testConnectAsync_callsListenerWhenConnected() throws Exception { |
| 110 | mTransportClient.connectAsync(mTransportConnectionListener, "caller"); |
| 111 | |
| 112 | // Simulate framework connecting |
| 113 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 114 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 115 | |
| 116 | mShadowLooper.runToEndOfTasks(); |
| 117 | verify(mTransportConnectionListener) |
| 118 | .onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportClient)); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void testConnectAsync_whenPendingConnection_callsAllListenersWhenConnected() |
| 123 | throws Exception { |
| 124 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 125 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 126 | |
| 127 | mTransportClient.connectAsync(mTransportConnectionListener2, "caller2"); |
| 128 | |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 129 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 130 | |
| 131 | mShadowLooper.runToEndOfTasks(); |
| 132 | verify(mTransportConnectionListener) |
| 133 | .onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportClient)); |
| 134 | verify(mTransportConnectionListener2) |
| 135 | .onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportClient)); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testConnectAsync_whenAlreadyConnected_callsListener() throws Exception { |
| 140 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 141 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 142 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 143 | |
| 144 | mTransportClient.connectAsync(mTransportConnectionListener2, "caller2"); |
| 145 | |
| 146 | mShadowLooper.runToEndOfTasks(); |
| 147 | verify(mTransportConnectionListener2) |
| 148 | .onTransportConnectionResult(any(IBackupTransport.class), eq(mTransportClient)); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testConnectAsync_whenFrameworkDoesntBind_callsListener() throws Exception { |
| 153 | when(mContext.bindServiceAsUser( |
| 154 | eq(mBindIntent), |
| 155 | any(ServiceConnection.class), |
| 156 | anyInt(), |
| 157 | any(UserHandle.class))) |
| 158 | .thenReturn(false); |
| 159 | |
| 160 | mTransportClient.connectAsync(mTransportConnectionListener, "caller"); |
| 161 | |
| 162 | mShadowLooper.runToEndOfTasks(); |
| 163 | verify(mTransportConnectionListener) |
| 164 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
| 165 | } |
| 166 | |
| 167 | @Test |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 168 | public void testConnectAsync_whenFrameworkDoesNotBind_releasesConnection() throws Exception { |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 169 | when(mContext.bindServiceAsUser( |
| 170 | eq(mBindIntent), |
| 171 | any(ServiceConnection.class), |
| 172 | anyInt(), |
| 173 | any(UserHandle.class))) |
| 174 | .thenReturn(false); |
| 175 | |
| 176 | mTransportClient.connectAsync(mTransportConnectionListener, "caller"); |
| 177 | |
| 178 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 179 | verify(mContext).unbindService(eq(connection)); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testConnectAsync_afterServiceDisconnectedBeforeNewConnection_callsListener() |
| 184 | throws Exception { |
| 185 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 186 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 187 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 188 | connection.onServiceDisconnected(mTransportComponent); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 189 | |
| 190 | mTransportClient.connectAsync(mTransportConnectionListener2, "caller1"); |
| 191 | |
| 192 | verify(mTransportConnectionListener2) |
| 193 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void testConnectAsync_afterServiceDisconnectedAfterNewConnection_callsListener() |
| 198 | throws Exception { |
| 199 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 200 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 201 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 202 | connection.onServiceDisconnected(mTransportComponent); |
| 203 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 204 | |
| 205 | mTransportClient.connectAsync(mTransportConnectionListener2, "caller1"); |
| 206 | |
| 207 | // Yes, it should return null because the object became unusable, check design doc |
| 208 | verify(mTransportConnectionListener2) |
| 209 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
| 210 | } |
| 211 | |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 212 | @Test |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 213 | public void testConnectAsync_callsListenerIfBindingDies() throws Exception { |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 214 | mTransportClient.connectAsync(mTransportConnectionListener, "caller"); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 215 | |
| 216 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 217 | connection.onBindingDied(mTransportComponent); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 218 | |
| 219 | mShadowLooper.runToEndOfTasks(); |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 220 | verify(mTransportConnectionListener) |
| 221 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void testConnectAsync_whenPendingConnection_callsListenersIfBindingDies() |
| 226 | throws Exception { |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 227 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 228 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 229 | |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 230 | mTransportClient.connectAsync(mTransportConnectionListener2, "caller2"); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 231 | |
Bernardo Rufino | 90a8802 | 2017-11-13 17:19:15 +0000 | [diff] [blame] | 232 | connection.onBindingDied(mTransportComponent); |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 233 | |
| 234 | mShadowLooper.runToEndOfTasks(); |
Annie Meng | 789bf28 | 2018-01-15 12:10:38 +0000 | [diff] [blame] | 235 | verify(mTransportConnectionListener) |
| 236 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
| 237 | verify(mTransportConnectionListener2) |
| 238 | .onTransportConnectionResult(isNull(), eq(mTransportClient)); |
| 239 | } |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 240 | |
Bernardo Rufino | aa56a6c | 2018-01-16 14:10:19 +0000 | [diff] [blame] | 241 | @Test |
| 242 | public void testConnectAsync_beforeFrameworkCall_logsBoundTransition() { |
| 243 | ShadowEventLog.clearEvents(); |
| 244 | |
| 245 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 246 | |
| 247 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 1); |
| 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void testConnectAsync_afterOnServiceConnected_logsBoundAndConnectedTransitions() { |
| 252 | ShadowEventLog.clearEvents(); |
| 253 | |
| 254 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 255 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 256 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 257 | |
| 258 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 1); |
| 259 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_CONNECTION, mTransportString, 1); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testConnectAsync_afterOnBindingDied_logsBoundAndUnboundTransitions() { |
| 264 | ShadowEventLog.clearEvents(); |
| 265 | |
| 266 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 267 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 268 | connection.onBindingDied(mTransportComponent); |
| 269 | |
| 270 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 1); |
| 271 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 0); |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testUnbind_whenConnected_logsDisconnectedAndUnboundTransitions() { |
| 276 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 277 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 278 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 279 | ShadowEventLog.clearEvents(); |
| 280 | |
| 281 | mTransportClient.unbind("caller1"); |
| 282 | |
| 283 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_CONNECTION, mTransportString, 0); |
| 284 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 0); |
| 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void testOnServiceDisconnected_whenConnected_logsDisconnectedAndUnboundTransitions() { |
| 289 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 290 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 291 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 292 | ShadowEventLog.clearEvents(); |
| 293 | |
| 294 | connection.onServiceDisconnected(mTransportComponent); |
| 295 | |
| 296 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_CONNECTION, mTransportString, 0); |
| 297 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 0); |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | public void testOnBindingDied_whenConnected_logsDisconnectedAndUnboundTransitions() { |
| 302 | mTransportClient.connectAsync(mTransportConnectionListener, "caller1"); |
| 303 | ServiceConnection connection = verifyBindServiceAsUserAndCaptureServiceConnection(mContext); |
| 304 | connection.onServiceConnected(mTransportComponent, mIBackupTransport); |
| 305 | ShadowEventLog.clearEvents(); |
| 306 | |
| 307 | connection.onBindingDied(mTransportComponent); |
| 308 | |
| 309 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_CONNECTION, mTransportString, 0); |
| 310 | assertEventLogged(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, mTransportString, 0); |
| 311 | } |
| 312 | |
| 313 | private void assertEventLogged(int tag, Object... values) { |
| 314 | assertThat(ShadowEventLog.hasEvent(tag, values)).isTrue(); |
| 315 | } |
| 316 | |
Bernardo Rufino | af547f4 | 2017-11-13 15:49:41 +0000 | [diff] [blame] | 317 | private ServiceConnection verifyBindServiceAsUserAndCaptureServiceConnection(Context context) { |
| 318 | ArgumentCaptor<ServiceConnection> connectionCaptor = |
| 319 | ArgumentCaptor.forClass(ServiceConnection.class); |
| 320 | verify(context) |
| 321 | .bindServiceAsUser( |
| 322 | any(Intent.class), |
| 323 | connectionCaptor.capture(), |
| 324 | anyInt(), |
| 325 | any(UserHandle.class)); |
| 326 | return connectionCaptor.getValue(); |
| 327 | } |
| 328 | } |