Andrei Litvin | b298d4c | 2020-02-27 13:29:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | package com.android.server.tv; |
| 17 | |
| 18 | import static org.hamcrest.Matchers.not; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.anyInt; |
| 22 | import static org.mockito.ArgumentMatchers.anyString; |
| 23 | import static org.mockito.Mockito.reset; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | import static org.mockito.hamcrest.MockitoHamcrest.argThat; |
| 26 | |
| 27 | import android.Manifest; |
| 28 | import android.content.Context; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ServiceInfo; |
| 31 | import android.content.res.Resources; |
| 32 | import android.os.Looper; |
| 33 | |
| 34 | import androidx.test.filters.SmallTest; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.mockito.Mock; |
| 39 | import org.mockito.MockitoAnnotations; |
| 40 | |
| 41 | @SmallTest |
| 42 | public class TvRemoteProviderWatcherTest { |
| 43 | private static final String TV_REMOTE_SERVICE_PACKAGE_NAME = |
| 44 | "com.google.android.tv.remote.service"; |
| 45 | |
| 46 | @Mock |
| 47 | Context mMockContext; |
| 48 | @Mock |
| 49 | PackageManager mMockPackageManager; |
| 50 | @Mock |
| 51 | Resources mMockResources; |
| 52 | |
| 53 | private TvRemoteProviderWatcher mTvRemoteProviderWatcher; |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
| 57 | MockitoAnnotations.initMocks(this); |
| 58 | |
| 59 | if (Looper.myLooper() == null) { |
| 60 | Looper.prepare(); |
| 61 | } |
| 62 | |
| 63 | when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager); |
| 64 | when(mMockContext.getResources()).thenReturn(mMockResources); |
| 65 | |
| 66 | when(mMockResources.getString(com.android.internal.R.string.config_tvRemoteServicePackage)) |
| 67 | .thenReturn(TV_REMOTE_SERVICE_PACKAGE_NAME); |
| 68 | |
| 69 | |
| 70 | when(mMockPackageManager.checkPermission( |
| 71 | argThat(not(Manifest.permission.TV_VIRTUAL_REMOTE_CONTROLLER)), |
| 72 | anyString())).thenReturn( |
| 73 | PackageManager.PERMISSION_DENIED); |
| 74 | |
| 75 | when(mMockPackageManager.checkPermission( |
| 76 | anyString(), |
| 77 | argThat(not(TV_REMOTE_SERVICE_PACKAGE_NAME)))).thenReturn( |
| 78 | PackageManager.PERMISSION_DENIED); |
| 79 | |
| 80 | when(mMockPackageManager.checkPermission(Manifest.permission.TV_VIRTUAL_REMOTE_CONTROLLER, |
| 81 | TV_REMOTE_SERVICE_PACKAGE_NAME)).thenReturn(PackageManager.PERMISSION_GRANTED); |
| 82 | |
| 83 | mTvRemoteProviderWatcher = new TvRemoteProviderWatcher(mMockContext, new Object()); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void tvServiceIsTrusted() { |
| 88 | assertTrue(mTvRemoteProviderWatcher.verifyServiceTrusted(createTvServiceInfo())); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void permissionIsRequired() { |
| 93 | ServiceInfo serviceInfo = createTvServiceInfo(); |
| 94 | serviceInfo.permission = null; |
| 95 | |
| 96 | assertFalse(mTvRemoteProviderWatcher.verifyServiceTrusted(serviceInfo)); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void permissionMustBeBindRemote() { |
| 101 | ServiceInfo serviceInfo = createTvServiceInfo(); |
| 102 | serviceInfo.permission = Manifest.permission.BIND_TV_INPUT; |
| 103 | assertFalse(mTvRemoteProviderWatcher.verifyServiceTrusted(serviceInfo)); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void packageNameMustMatch() { |
| 108 | ServiceInfo serviceInfo = createTvServiceInfo(); |
| 109 | serviceInfo.packageName = "some.random.package"; |
| 110 | assertFalse(mTvRemoteProviderWatcher.verifyServiceTrusted(serviceInfo)); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void packageManagerPermissionIsRequired() { |
| 115 | reset(mMockPackageManager); |
| 116 | when(mMockPackageManager.checkPermission(anyString(), anyString())).thenReturn( |
| 117 | PackageManager.PERMISSION_DENIED); |
| 118 | |
| 119 | assertFalse(mTvRemoteProviderWatcher.verifyServiceTrusted(createTvServiceInfo())); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void whitelistingPackageNameIsRequired() { |
| 124 | reset(mMockResources); |
| 125 | when(mMockResources.getString(anyInt())).thenReturn(""); |
| 126 | |
| 127 | // Create a new watcher, as the resources are read in the constructor of the class |
| 128 | if (Looper.myLooper() == null) { |
| 129 | Looper.prepare(); |
| 130 | } |
| 131 | |
| 132 | TvRemoteProviderWatcher watcher = |
| 133 | new TvRemoteProviderWatcher(mMockContext, new Object()); |
| 134 | assertFalse(watcher.verifyServiceTrusted(createTvServiceInfo())); |
| 135 | } |
| 136 | |
| 137 | private ServiceInfo createTvServiceInfo() { |
| 138 | ServiceInfo serviceInfo = new ServiceInfo(); |
| 139 | |
| 140 | serviceInfo.name = "ATV Remote Service"; |
| 141 | serviceInfo.packageName = TV_REMOTE_SERVICE_PACKAGE_NAME; |
| 142 | serviceInfo.permission = Manifest.permission.BIND_TV_REMOTE_SERVICE; |
| 143 | |
| 144 | return serviceInfo; |
| 145 | } |
| 146 | } |