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