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