Kai | 92f030d | 2019-10-24 13:07:48 -0700 | [diff] [blame^] | 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.hal; |
| 18 | |
| 19 | import android.car.Car; |
| 20 | import android.car.VehiclePropertyIds; |
| 21 | import android.hardware.automotive.vehicle.V2_0.VehicleVendorPermission; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import androidx.test.runner.AndroidJUnit4; |
| 25 | |
| 26 | import org.junit.Assert; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Rule; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.mockito.junit.MockitoJUnit; |
| 32 | import org.mockito.junit.MockitoRule; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
| 37 | @RunWith(AndroidJUnit4.class) |
| 38 | public class PropertyHalServiceIdsTest { |
| 39 | @Rule |
| 40 | public MockitoRule mMockitoRule = MockitoJUnit.rule(); |
| 41 | |
| 42 | private PropertyHalServiceIds mPropertyHalServiceIds; |
| 43 | |
| 44 | private static final String TAG = PropertyHalServiceIdsTest.class.getSimpleName(); |
| 45 | private static final int VENDOR_PROPERTY_1 = 0x21e01111; |
| 46 | private static final int VENDOR_PROPERTY_2 = 0x21e01112; |
| 47 | private static final int VENDOR_PROPERTY_3 = 0x21e01113; |
| 48 | private static final int VENDOR_PROPERTY_4 = 0x21e01114; |
| 49 | private static final int[] VENDOR_PROPERTY_IDS = { |
| 50 | VENDOR_PROPERTY_1, VENDOR_PROPERTY_2, VENDOR_PROPERTY_3, VENDOR_PROPERTY_4}; |
| 51 | private static final int[] SYSTEM_PROPERTY_IDS = {VehiclePropertyIds.ENGINE_OIL_LEVEL, |
| 52 | VehiclePropertyIds.CURRENT_GEAR, VehiclePropertyIds.NIGHT_MODE, |
| 53 | VehiclePropertyIds.HVAC_FAN_SPEED, VehiclePropertyIds.DOOR_LOCK}; |
| 54 | private static final List<Integer> CONFIG_ARRAY = new ArrayList<>(); |
| 55 | private static final List<Integer> CONFIG_ARRAY_INVALID = new ArrayList<>(); |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | mPropertyHalServiceIds = new PropertyHalServiceIds(); |
| 59 | // set up read permission and write permission to VENDOR_PROPERTY_1 |
| 60 | CONFIG_ARRAY.add(VENDOR_PROPERTY_1); |
| 61 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_DEFAULT); |
| 62 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_NOT_ACCESSIBLE); |
| 63 | // set up read permission and write permission to VENDOR_PROPERTY_2 |
| 64 | CONFIG_ARRAY.add(VENDOR_PROPERTY_2); |
| 65 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_GET_VENDOR_CATEGORY_ENGINE); |
| 66 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_SET_VENDOR_CATEGORY_ENGINE); |
| 67 | // set up read permission and write permission to VENDOR_PROPERTY_3 |
| 68 | CONFIG_ARRAY.add(VENDOR_PROPERTY_3); |
| 69 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_GET_VENDOR_CATEGORY_INFO); |
| 70 | CONFIG_ARRAY.add(VehicleVendorPermission.PERMISSION_DEFAULT); |
| 71 | |
| 72 | // set a invalid config |
| 73 | CONFIG_ARRAY_INVALID.add(VehiclePropertyIds.CURRENT_GEAR); |
| 74 | CONFIG_ARRAY_INVALID.add(VehicleVendorPermission.PERMISSION_GET_VENDOR_CATEGORY_ENGINE); |
| 75 | CONFIG_ARRAY_INVALID.add(VehicleVendorPermission.PERMISSION_SET_VENDOR_CATEGORY_ENGINE); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Test {@link PropertyHalServiceIds#getReadPermission(int)} |
| 80 | * and {@link PropertyHalServiceIds#getWritePermission(int)} for system properties |
| 81 | */ |
| 82 | @Test |
| 83 | public void checkPermissionForSystemProperty() { |
| 84 | Assert.assertEquals(Car.PERMISSION_CAR_ENGINE_DETAILED, |
| 85 | mPropertyHalServiceIds.getReadPermission(VehiclePropertyIds.ENGINE_OIL_LEVEL)); |
| 86 | Assert.assertNull( |
| 87 | mPropertyHalServiceIds.getWritePermission(VehiclePropertyIds.ENGINE_OIL_LEVEL)); |
| 88 | Assert.assertEquals(Car.PERMISSION_CONTROL_CAR_CLIMATE, |
| 89 | mPropertyHalServiceIds.getReadPermission(VehiclePropertyIds.HVAC_FAN_SPEED)); |
| 90 | Assert.assertEquals(Car.PERMISSION_CONTROL_CAR_CLIMATE, |
| 91 | mPropertyHalServiceIds.getWritePermission(VehiclePropertyIds.HVAC_FAN_SPEED)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Test {@link PropertyHalServiceIds#customizeVendorPermission(List)} |
| 96 | */ |
| 97 | @Test |
| 98 | public void checkPermissionForVendorProperty() { |
| 99 | for (int propId : VENDOR_PROPERTY_IDS) { |
| 100 | mPropertyHalServiceIds.insertVendorProperty(propId); |
| 101 | } |
| 102 | // test insert a valid config |
| 103 | mPropertyHalServiceIds.customizeVendorPermission(CONFIG_ARRAY); |
| 104 | |
| 105 | Assert.assertEquals(Car.PERMISSION_VENDOR_EXTENSION, |
| 106 | mPropertyHalServiceIds.getReadPermission(VENDOR_PROPERTY_1)); |
| 107 | Assert.assertNull(mPropertyHalServiceIds.getWritePermission(VENDOR_PROPERTY_1)); |
| 108 | |
| 109 | Assert.assertEquals(android.car.hardware.property |
| 110 | .VehicleVendorPermission.PERMISSION_GET_CAR_VENDOR_CATEGORY_ENGINE, |
| 111 | mPropertyHalServiceIds.getReadPermission(VENDOR_PROPERTY_2)); |
| 112 | Assert.assertEquals(android.car.hardware.property |
| 113 | .VehicleVendorPermission.PERMISSION_SET_CAR_VENDOR_CATEGORY_ENGINE, |
| 114 | mPropertyHalServiceIds.getWritePermission(VENDOR_PROPERTY_2)); |
| 115 | |
| 116 | Assert.assertEquals(android.car.hardware.property |
| 117 | .VehicleVendorPermission.PERMISSION_GET_CAR_VENDOR_CATEGORY_INFO, |
| 118 | mPropertyHalServiceIds.getReadPermission(VENDOR_PROPERTY_3)); |
| 119 | Assert.assertEquals(Car.PERMISSION_VENDOR_EXTENSION, |
| 120 | mPropertyHalServiceIds.getWritePermission(VENDOR_PROPERTY_3)); |
| 121 | |
| 122 | Assert.assertEquals(Car.PERMISSION_VENDOR_EXTENSION, |
| 123 | mPropertyHalServiceIds.getReadPermission(VENDOR_PROPERTY_4)); |
| 124 | Assert.assertEquals(Car.PERMISSION_VENDOR_EXTENSION, |
| 125 | mPropertyHalServiceIds.getWritePermission(VENDOR_PROPERTY_4)); |
| 126 | |
| 127 | // test insert invalid config |
| 128 | try { |
| 129 | mPropertyHalServiceIds.customizeVendorPermission(CONFIG_ARRAY_INVALID); |
| 130 | Assert.fail("Insert system properties with vendor permissions"); |
| 131 | } catch (IllegalArgumentException e) { |
| 132 | Log.v(TAG, e.getMessage()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Test {@link PropertyHalServiceIds#isSupportedProperty(int)} |
| 138 | */ |
| 139 | @Test |
| 140 | public void checkVendorPropertyId() { |
| 141 | for (int vendorProp : VENDOR_PROPERTY_IDS) { |
| 142 | Assert.assertTrue(mPropertyHalServiceIds.isSupportedProperty(vendorProp)); |
| 143 | } |
| 144 | for (int systemProp : SYSTEM_PROPERTY_IDS) { |
| 145 | Assert.assertTrue(mPropertyHalServiceIds.isSupportedProperty(systemProp)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } |