• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car;
18 
19 import static com.android.car.MockedVmsTestBase.PUBLISHER_BIND_TIMEOUT_SECS;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.car.vms.VmsPublisherClientService;
25 import android.car.vms.VmsSubscriptionState;
26 import android.content.Intent;
27 import android.os.UserHandle;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 
35 public class VmsPublisherClientPermissionTest extends MockedCarTestBase {
36     private static CountDownLatch sPublisherExpectedPermission = new CountDownLatch(1);
37     private static CountDownLatch sPublisherWrongPermission = new CountDownLatch(1);
38     private static CountDownLatch sPublisherMissingPermission = new CountDownLatch(1);
39 
40     private static class MockPublisherClient extends VmsPublisherClientService {
41         private CountDownLatch mReadyLatch;
MockPublisherClient(CountDownLatch readyLatch)42         MockPublisherClient(CountDownLatch readyLatch) {
43             mReadyLatch = readyLatch;
44         }
45         @Override
onVmsPublisherServiceReady()46         protected void onVmsPublisherServiceReady() {
47             mReadyLatch.countDown();
48         }
49 
50         @Override
onVmsSubscriptionChange(VmsSubscriptionState subscriptionState)51         public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) {}
52     }
53 
54 
55     // AndroidManifest.xml:
56     // <service android:name="com.android.car.VmsPublisherClientPermissionTest$PublisherClientExpectedPermission"
57     //         android:exported="true"
58     //         android:permission="android.car.permission.BIND_VMS_CLIENT"/>
59     public static class PublisherClientExpectedPermission extends MockPublisherClient {
PublisherClientExpectedPermission()60         public PublisherClientExpectedPermission() {
61             super(sPublisherExpectedPermission);
62         }
63     }
64 
65     // AndroidManifest.xml:
66     // <service android:name="com.android.car.VmsPublisherClientPermissionTest$PublisherClientWrongPermission"
67     //         android:exported="true"
68     //         android:permission="android.car.permission.VMS_PUBLISHER"/>
69     public static class PublisherClientWrongPermission extends MockPublisherClient {
PublisherClientWrongPermission()70         public PublisherClientWrongPermission() {
71             super(sPublisherWrongPermission);
72         }
73     }
74 
75     // AndroidManifest.xml:
76     // <service android:name="com.android.car.VmsPublisherClientPermissionTest$PublisherClientMissingPermission"
77     //         android:exported="true"/>
78     public static class PublisherClientMissingPermission extends MockPublisherClient {
PublisherClientMissingPermission()79         public PublisherClientMissingPermission() {
80             super(sPublisherMissingPermission);
81         }
82     }
83 
84     @Override
configureResourceOverrides(MockResources resources)85     protected synchronized void configureResourceOverrides(MockResources resources) {
86         super.configureResourceOverrides(resources);
87         resources.overrideResource(com.android.car.R.array.vmsPublisherSystemClients, new String[]{
88                 getFlattenComponent(PublisherClientExpectedPermission.class),
89                 getFlattenComponent(PublisherClientWrongPermission.class),
90                 getFlattenComponent(PublisherClientMissingPermission.class)
91         });
92         resources.overrideResource(com.android.car.R.array.vmsPublisherUserClients, new String[]{
93                 getFlattenComponent(PublisherClientExpectedPermission.class),
94                 getFlattenComponent(PublisherClientWrongPermission.class),
95                 getFlattenComponent(PublisherClientMissingPermission.class)
96         });
97     }
98 
99     @Before
triggerClientBinding()100     public void triggerClientBinding() {
101         getContext().sendBroadcastAsUser(new Intent(Intent.ACTION_USER_UNLOCKED), UserHandle.ALL);
102     }
103 
104     @Test
testExpectedPermission()105     public void testExpectedPermission() throws Exception {
106         assertTrue(
107                 "Timeout while waiting for publisher client to be ready",
108                 sPublisherExpectedPermission.await(PUBLISHER_BIND_TIMEOUT_SECS, TimeUnit.SECONDS));
109     }
110 
111     @Test
testWrongPermission()112     public void testWrongPermission() throws Exception {
113         assertFalse(
114                 "Publisher with wrong android:permission was bound unexpectedly",
115                 sPublisherWrongPermission.await(PUBLISHER_BIND_TIMEOUT_SECS, TimeUnit.SECONDS));
116     }
117 
118     @Test
testMissingPermission()119     public void testMissingPermission() throws Exception {
120         assertFalse(
121                 "Publisher with missing android:permission was bound unexpectedly",
122                 sPublisherMissingPermission.await(PUBLISHER_BIND_TIMEOUT_SECS, TimeUnit.SECONDS));
123     }
124 }
125