blob: 0a2bb620eb116d0be9c6498720a2a870b035e527 [file] [log] [blame]
Andrei Litvinb298d4c2020-02-27 13:29:20 -05001/*
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 */
16package com.android.server.tv;
17
18import static org.hamcrest.Matchers.not;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.ArgumentMatchers.anyString;
23import static org.mockito.Mockito.reset;
24import static org.mockito.Mockito.when;
25import static org.mockito.hamcrest.MockitoHamcrest.argThat;
26
27import android.Manifest;
28import android.content.Context;
29import android.content.pm.PackageManager;
30import android.content.pm.ServiceInfo;
31import android.content.res.Resources;
32import android.os.Looper;
33
34import androidx.test.filters.SmallTest;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.mockito.Mock;
39import org.mockito.MockitoAnnotations;
40
41@SmallTest
42public 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}