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.car; 18 19 import static org.junit.Assert.assertTrue; 20 21 import android.car.VehicleAreaType; 22 import android.content.pm.PackageManager; 23 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 24 import android.hardware.automotive.vehicle.V2_0.VehicleProperty; 25 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess; 26 import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode; 27 import android.hardware.automotive.vehicle.V2_0.VmsBaseMessageIntegerValuesIndex; 28 import android.hardware.automotive.vehicle.V2_0.VmsMessageType; 29 import android.support.test.filters.MediumTest; 30 import android.support.test.runner.AndroidJUnit4; 31 32 import com.android.car.vehiclehal.VehiclePropValueBuilder; 33 import com.android.car.vehiclehal.test.MockedVehicleHal; 34 import com.android.car.vehiclehal.test.MockedVehicleHal.VehicleHalPropertyHandler; 35 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import java.util.ArrayList; 40 import java.util.concurrent.Semaphore; 41 import java.util.concurrent.TimeUnit; 42 43 @RunWith(AndroidJUnit4.class) 44 @MediumTest 45 public class VmsPublisherPermissionsTest extends MockedCarTestBase { 46 private static final String TAG = "VmsPublisherTest"; 47 private static final int MOCK_PUBLISHER_LAYER_ID = 0; 48 private static final int MOCK_PUBLISHER_LAYER_VERSION = 0; 49 private static final int MOCK_PUBLISHER_LAYER_FUSION_INT_VALUE = 0; 50 51 private HalHandler mHalHandler; 52 // Used to block until the HAL property is updated in HalHandler.onPropertySet. 53 private Semaphore mHalHandlerSemaphore; 54 55 @Override configureMockedHal()56 protected synchronized void configureMockedHal() { 57 mHalHandler = new HalHandler(); 58 addProperty(VehicleProperty.VEHICLE_MAP_SERVICE, mHalHandler) 59 .setChangeMode(VehiclePropertyChangeMode.ON_CHANGE) 60 .setAccess(VehiclePropertyAccess.READ_WRITE) 61 .addAreaConfig(VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, 0, 0); 62 } 63 64 @Override configureResourceOverrides(MockResources resources)65 protected synchronized void configureResourceOverrides(MockResources resources) { 66 resources.overrideResource(R.array.vmsPublisherClients, new String[]{ 67 "com.google.android.car.vms.publisher/.VmsPublisherClientSampleService"}) 68 .overrideResource(R.array.vmsSafePermissions, 69 new String[]{"android.permission.ACCESS_FINE_LOCATION"}); 70 } 71 getHalSubscriptionRequest()72 private VehiclePropValue getHalSubscriptionRequest() { 73 return VehiclePropValueBuilder.newBuilder(VehicleProperty.VEHICLE_MAP_SERVICE) 74 .addIntValue(VmsMessageType.SUBSCRIBE) 75 .addIntValue(MOCK_PUBLISHER_LAYER_ID) 76 .addIntValue(MOCK_PUBLISHER_LAYER_VERSION) 77 .addIntValue(MOCK_PUBLISHER_LAYER_FUSION_INT_VALUE) 78 .build(); 79 } 80 81 @Override setUp()82 public void setUp() throws Exception { 83 /** 84 * First init the semaphore, setUp will start a series of events that will ultimately 85 * update the HAL layer and release this semaphore. 86 */ 87 mHalHandlerSemaphore = new Semaphore(0); 88 super.setUp(); 89 90 // Inject a subscribe event which simulates the HAL is subscribed to the Sample Publisher. 91 MockedVehicleHal mHal = getMockedVehicleHal(); 92 mHal.injectEvent(getHalSubscriptionRequest()); 93 } 94 95 /** 96 * The method setUp initializes all the Car services, including the VmsPublisherService. 97 * The VmsPublisherService will start and configure its list of clients. This list was 98 * overridden in the method getCarServiceContext. 99 * Therefore, only VmsPublisherClientSampleService will be started. 100 * The service VmsPublisherClientSampleService will publish one message, which is validated in 101 * this test. 102 */ 103 @Test testPermissions()104 public void testPermissions() throws Exception { 105 assertTrue(mHalHandlerSemaphore.tryAcquire(2L, TimeUnit.SECONDS)); 106 // At this point the client initialization finished. Let's validate the permissions. 107 // The VMS service should be allowed to grant ACCESS_FINE_LOCATION. 108 assertTrue( 109 getContext().getPackageManager().checkPermission( 110 "android.permission.ACCESS_FINE_LOCATION", 111 "com.google.android.car.vms.publisher") 112 == PackageManager.PERMISSION_GRANTED); 113 } 114 115 private class HalHandler implements VehicleHalPropertyHandler { 116 @Override onPropertySet(VehiclePropValue value)117 public synchronized void onPropertySet(VehiclePropValue value) { 118 // If this is the data message release the semaphore so the test can continue. 119 ArrayList<Integer> int32Values = value.value.int32Values; 120 if (int32Values.get(VmsBaseMessageIntegerValuesIndex.MESSAGE_TYPE) == 121 VmsMessageType.DATA) { 122 mHalHandlerSemaphore.release(); 123 } 124 } 125 } 126 } 127