• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #define LOG_NDEBUG 0
18 #define LOG_TAG "CameraCharacteristicsPermission"
19 
20 #include <gtest/gtest.h>
21 
22 #include <binder/ProcessState.h>
23 #include <utils/Errors.h>
24 #include <utils/Log.h>
25 #include <camera/CameraMetadata.h>
26 #include <camera/Camera.h>
27 #include <camera/CameraUtils.h>
28 #include <android/hardware/ICameraService.h>
29 
30 using namespace android;
31 using namespace android::hardware;
32 
33 class CameraCharacteristicsPermission : public ::testing::Test {
34 protected:
CameraCharacteristicsPermission()35     CameraCharacteristicsPermission() : numCameras(0){}
36     //Gtest interface
37     void SetUp() override;
38     void TearDown() override;
39 
40     int32_t numCameras;
41     sp<ICameraService> mCameraService;
42 };
43 
SetUp()44 void CameraCharacteristicsPermission::SetUp() {
45     ::android::binder::Status rc;
46     ProcessState::self()->startThreadPool();
47     sp<IServiceManager> sm = defaultServiceManager();
48     sp<IBinder> binder = sm->getService(String16("media.camera"));
49     mCameraService = interface_cast<ICameraService>(binder);
50     rc = mCameraService->getNumberOfCameras(
51             hardware::ICameraService::CAMERA_TYPE_ALL, kDefaultDeviceId, /*devicePolicy*/0,
52             &numCameras);
53     EXPECT_TRUE(rc.isOk());
54 }
55 
TearDown()56 void CameraCharacteristicsPermission::TearDown() {
57     mCameraService.clear();
58 }
59 
60 // Revoking and acquiring permissions automatically might not be possible.
61 // Test the functionality for removal of camera characteristics needing
62 // a camera permission.
TEST_F(CameraCharacteristicsPermission,TestCameraPermission)63 TEST_F(CameraCharacteristicsPermission, TestCameraPermission) {
64     for (int32_t cameraId = 0; cameraId < numCameras; cameraId++) {
65         std::string cameraIdStr = std::to_string(cameraId);
66         bool isSupported = false;
67         auto rc = mCameraService->supportsCameraApi(cameraIdStr,
68                 hardware::ICameraService::API_VERSION_2, &isSupported);
69         EXPECT_TRUE(rc.isOk());
70         if (!isSupported) {
71             continue;
72         }
73 
74         CameraMetadata metadata;
75         std::vector<int32_t> tagsNeedingPermission;
76         rc = mCameraService->getCameraCharacteristics(cameraIdStr,
77                 /*targetSdkVersion*/__ANDROID_API_FUTURE__,
78                 /*overrideToPortrait*/false, kDefaultDeviceId, /*devicePolicy*/0, &metadata);
79         ASSERT_TRUE(rc.isOk());
80         EXPECT_FALSE(metadata.isEmpty());
81         EXPECT_EQ(metadata.removePermissionEntries(CAMERA_METADATA_INVALID_VENDOR_ID,
82                     &tagsNeedingPermission), NO_ERROR);
83         camera_metadata_entry_t availableCharacteristics =
84                 metadata.find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
85         EXPECT_TRUE(0 < availableCharacteristics.count);
86 
87         std::vector<uint32_t> availableKeys;
88         availableKeys.reserve(availableCharacteristics.count);
89         availableKeys.insert(availableKeys.begin(), availableCharacteristics.data.i32,
90                 availableCharacteristics.data.i32 + availableCharacteristics.count);
91 
92         for (const auto &key : tagsNeedingPermission) {
93             ASSERT_FALSE(metadata.exists(key));
94             auto it = std::find(availableKeys.begin(), availableKeys.end(), key);
95             ASSERT_TRUE(it == availableKeys.end());
96         }
97     }
98 }
99