Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #include <android/hardware/BnCameraServiceListener.h> |
| 18 | #include <android/hardware/BnCameraServiceProxy.h> |
| 19 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
| 20 | #include <android/hardware/ICameraService.h> |
| 21 | |
| 22 | #include <private/android_filesystem_config.h> |
| 23 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 24 | #include <camera/CameraUtils.h> |
| 25 | |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 26 | #include "../CameraService.h" |
| 27 | #include "../utils/CameraServiceProxyWrapper.h" |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | #include <memory> |
| 32 | #include <vector> |
| 33 | |
| 34 | using namespace android; |
| 35 | using namespace android::hardware::camera; |
| 36 | |
| 37 | // Empty service listener. |
| 38 | class TestCameraServiceListener : public hardware::BnCameraServiceListener { |
| 39 | public: |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 40 | virtual ~TestCameraServiceListener() {} |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 41 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 42 | virtual binder::Status onStatusChanged(int32_t , const std::string&, int32_t) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 43 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 44 | } |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 45 | |
| 46 | virtual binder::Status onPhysicalCameraStatusChanged(int32_t /*status*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 47 | const std::string& /*cameraId*/, const std::string& /*physicalCameraId*/, |
| 48 | int32_t /*deviceId*/) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 49 | // No op |
| 50 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 51 | } |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 52 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 53 | virtual binder::Status onTorchStatusChanged(int32_t /*status*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 54 | const std::string& /*cameraId*/, int32_t /*deviceId*/) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 55 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 56 | } |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 57 | |
| 58 | virtual binder::Status onCameraAccessPrioritiesChanged() { |
| 59 | // No op |
| 60 | return binder::Status::ok(); |
| 61 | } |
| 62 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 63 | virtual binder::Status onCameraOpened(const std::string& /*cameraId*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 64 | const std::string& /*clientPackageName*/, int32_t /*deviceId*/) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 65 | // No op |
| 66 | return binder::Status::ok(); |
| 67 | } |
| 68 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 69 | virtual binder::Status onCameraClosed(const std::string& /*cameraId*/, int32_t /*deviceId*/) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 70 | // No op |
| 71 | return binder::Status::ok(); |
| 72 | } |
| 73 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 74 | virtual binder::Status onTorchStrengthLevelChanged(const std::string& /*cameraId*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 75 | int32_t /*torchStrength*/, int32_t /*deviceId*/) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 76 | // No op |
| 77 | return binder::Status::ok(); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | // Empty device callback. |
| 82 | class TestCameraDeviceCallbacks : public hardware::camera2::BnCameraDeviceCallbacks { |
| 83 | public: |
| 84 | TestCameraDeviceCallbacks() {} |
| 85 | |
| 86 | virtual ~TestCameraDeviceCallbacks() {} |
| 87 | |
| 88 | virtual binder::Status onDeviceError(int /*errorCode*/, |
| 89 | const CaptureResultExtras& /*resultExtras*/) { |
| 90 | return binder::Status::ok(); |
| 91 | } |
| 92 | |
| 93 | virtual binder::Status onDeviceIdle() { |
| 94 | return binder::Status::ok(); |
| 95 | } |
| 96 | |
| 97 | virtual binder::Status onCaptureStarted(const CaptureResultExtras& /*resultExtras*/, |
| 98 | int64_t /*timestamp*/) { |
| 99 | return binder::Status::ok(); |
| 100 | } |
| 101 | |
| 102 | virtual binder::Status onResultReceived(const CameraMetadata& /*metadata*/, |
| 103 | const CaptureResultExtras& /*resultExtras*/, |
| 104 | const std::vector<PhysicalCaptureResultInfo>& /*physicalResultInfos*/) { |
| 105 | return binder::Status::ok(); |
| 106 | } |
| 107 | |
| 108 | virtual binder::Status onPrepared(int /*streamId*/) { |
| 109 | return binder::Status::ok(); |
| 110 | } |
| 111 | |
| 112 | virtual binder::Status onRepeatingRequestError( |
| 113 | int64_t /*lastFrameNumber*/, int32_t /*stoppedSequenceId*/) { |
| 114 | return binder::Status::ok(); |
| 115 | } |
| 116 | |
| 117 | virtual binder::Status onRequestQueueEmpty() { |
| 118 | return binder::Status::ok(); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | // Override isCameraDisabled from the CameraServiceProxy with a flag. |
| 123 | class CameraServiceProxyOverride : public ::android::hardware::BnCameraServiceProxy { |
| 124 | public: |
| 125 | CameraServiceProxyOverride() : |
| 126 | mCameraServiceProxy(CameraServiceProxyWrapper::getDefaultCameraServiceProxy()), |
Greg Kaiser | f18ea7c | 2022-06-06 09:39:00 -0700 | [diff] [blame] | 127 | mCameraDisabled(false), mOverrideCameraDisabled(false) |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 128 | { } |
| 129 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 130 | virtual binder::Status getRotateAndCropOverride(const std::string& packageName, int lensFacing, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 131 | int userId, int *ret) override { |
| 132 | return mCameraServiceProxy->getRotateAndCropOverride(packageName, lensFacing, |
| 133 | userId, ret); |
| 134 | } |
| 135 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 136 | virtual binder::Status getAutoframingOverride(const std::string& packageName, int *ret) override { |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 137 | return mCameraServiceProxy->getAutoframingOverride(packageName, ret); |
| 138 | } |
| 139 | |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 140 | virtual binder::Status pingForUserUpdate() override { |
| 141 | return mCameraServiceProxy->pingForUserUpdate(); |
| 142 | } |
| 143 | |
| 144 | virtual binder::Status notifyCameraState( |
| 145 | const hardware::CameraSessionStats& cameraSessionStats) override { |
| 146 | return mCameraServiceProxy->notifyCameraState(cameraSessionStats); |
| 147 | } |
| 148 | |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 149 | virtual binder::Status isCameraDisabled(int userId, bool *ret) override { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 150 | if (mOverrideCameraDisabled) { |
| 151 | *ret = mCameraDisabled; |
| 152 | return binder::Status::ok(); |
| 153 | } |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 154 | return mCameraServiceProxy->isCameraDisabled(userId, ret); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void setCameraDisabled(bool cameraDisabled) { |
| 158 | mCameraDisabled = cameraDisabled; |
| 159 | } |
| 160 | |
| 161 | void setOverrideCameraDisabled(bool overrideCameraDisabled) { |
| 162 | mOverrideCameraDisabled = overrideCameraDisabled; |
| 163 | } |
| 164 | |
| 165 | protected: |
| 166 | sp<hardware::ICameraServiceProxy> mCameraServiceProxy; |
| 167 | bool mCameraDisabled; |
| 168 | bool mOverrideCameraDisabled; |
| 169 | }; |
| 170 | |
| 171 | class AutoDisconnectDevice { |
| 172 | public: |
| 173 | AutoDisconnectDevice(sp<hardware::camera2::ICameraDeviceUser> device) : |
| 174 | mDevice(device) |
| 175 | { } |
| 176 | |
| 177 | ~AutoDisconnectDevice() { |
Austin Borger | 9bfa0a7 | 2022-08-03 17:50:40 -0700 | [diff] [blame] | 178 | if (mDevice != nullptr) { |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 179 | mDevice->disconnect(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | sp<hardware::camera2::ICameraDeviceUser> mDevice; |
| 185 | }; |
| 186 | |
| 187 | class CameraPermissionsTest : public ::testing::Test { |
| 188 | protected: |
| 189 | static sp<CameraService> sCameraService; |
| 190 | static sp<CameraServiceProxyOverride> sCameraServiceProxy; |
| 191 | static std::shared_ptr<CameraServiceProxyWrapper> sCameraServiceProxyWrapper; |
| 192 | static uid_t sOldUid; |
| 193 | |
| 194 | static void SetUpTestSuite() { |
| 195 | sOldUid = getuid(); |
| 196 | setuid(AID_CAMERASERVER); |
| 197 | sCameraServiceProxy = new CameraServiceProxyOverride(); |
| 198 | sCameraServiceProxyWrapper = |
| 199 | std::make_shared<CameraServiceProxyWrapper>(sCameraServiceProxy); |
| 200 | sCameraService = new CameraService(sCameraServiceProxyWrapper); |
| 201 | sCameraService->clearCachedVariables(); |
| 202 | } |
| 203 | |
| 204 | static void TearDownTestSuite() { |
| 205 | sCameraServiceProxyWrapper = nullptr; |
| 206 | sCameraServiceProxy = nullptr; |
| 207 | sCameraService = nullptr; |
| 208 | setuid(sOldUid); |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | sp<CameraService> CameraPermissionsTest::sCameraService = nullptr; |
| 213 | sp<CameraServiceProxyOverride> CameraPermissionsTest::sCameraServiceProxy = nullptr; |
| 214 | std::shared_ptr<CameraServiceProxyWrapper> |
| 215 | CameraPermissionsTest::sCameraServiceProxyWrapper = nullptr; |
| 216 | uid_t CameraPermissionsTest::sOldUid = 0; |
| 217 | |
| 218 | // Test that camera connections fail with ERROR_DISABLED when the camera is disabled via device |
| 219 | // policy, and succeed when it isn't. |
| 220 | TEST_F(CameraPermissionsTest, TestCameraDisabled) { |
| 221 | std::vector<hardware::CameraStatus> statuses; |
| 222 | sp<TestCameraServiceListener> serviceListener = new TestCameraServiceListener(); |
| 223 | sCameraService->addListenerTest(serviceListener, &statuses); |
| 224 | sCameraServiceProxy->setOverrideCameraDisabled(true); |
| 225 | |
| 226 | sCameraServiceProxy->setCameraDisabled(true); |
| 227 | for (auto s : statuses) { |
| 228 | sp<TestCameraDeviceCallbacks> callbacks = new TestCameraDeviceCallbacks(); |
| 229 | sp<hardware::camera2::ICameraDeviceUser> device; |
| 230 | binder::Status status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 231 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 232 | android::CameraService::USE_CALLING_UID, 0/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 233 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 234 | kDefaultDeviceId, /*devicePolicy*/0, &device); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 235 | AutoDisconnectDevice autoDisconnect(device); |
| 236 | ASSERT_TRUE(!status.isOk()) << "connectDevice returned OK status"; |
| 237 | ASSERT_EQ(status.serviceSpecificErrorCode(), hardware::ICameraService::ERROR_DISABLED) |
| 238 | << "connectDevice returned exception code " << status.exceptionCode(); |
| 239 | } |
| 240 | |
| 241 | sCameraServiceProxy->setCameraDisabled(false); |
| 242 | for (auto s : statuses) { |
| 243 | sp<TestCameraDeviceCallbacks> callbacks = new TestCameraDeviceCallbacks(); |
| 244 | sp<hardware::camera2::ICameraDeviceUser> device; |
| 245 | binder::Status status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 246 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 247 | android::CameraService::USE_CALLING_UID, 0/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 248 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 249 | kDefaultDeviceId, /*devicePolicy*/0, &device); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 250 | AutoDisconnectDevice autoDisconnect(device); |
| 251 | ASSERT_TRUE(status.isOk()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Test that consecutive camera connections succeed. |
| 256 | TEST_F(CameraPermissionsTest, TestConsecutiveConnections) { |
| 257 | std::vector<hardware::CameraStatus> statuses; |
| 258 | sp<TestCameraServiceListener> serviceListener = new TestCameraServiceListener(); |
| 259 | sCameraService->addListenerTest(serviceListener, &statuses); |
| 260 | sCameraServiceProxy->setOverrideCameraDisabled(false); |
| 261 | |
| 262 | for (auto s : statuses) { |
| 263 | sp<TestCameraDeviceCallbacks> callbacks = new TestCameraDeviceCallbacks(); |
| 264 | sp<hardware::camera2::ICameraDeviceUser> deviceA, deviceB; |
| 265 | binder::Status status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 266 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 267 | android::CameraService::USE_CALLING_UID, 0/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 268 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 269 | kDefaultDeviceId, /*devicePolicy*/0, &deviceA); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 270 | AutoDisconnectDevice autoDisconnectA(deviceA); |
| 271 | ASSERT_TRUE(status.isOk()) << "Exception code " << status.exceptionCode() << |
| 272 | " service specific error code " << status.serviceSpecificErrorCode(); |
| 273 | status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 274 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 275 | android::CameraService::USE_CALLING_UID, 0/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 276 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 277 | kDefaultDeviceId, /*devicePolicy*/0, &deviceB); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 278 | AutoDisconnectDevice autoDisconnectB(deviceB); |
| 279 | ASSERT_TRUE(status.isOk()) << "Exception code " << status.exceptionCode() << |
| 280 | " service specific error code " << status.serviceSpecificErrorCode(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Test that consecutive camera connections succeed even when a nonzero oomScoreOffset is provided |
| 285 | // in the second call. |
| 286 | TEST_F(CameraPermissionsTest, TestConflictingOomScoreOffset) { |
| 287 | std::vector<hardware::CameraStatus> statuses; |
| 288 | sp<TestCameraServiceListener> serviceListener = new TestCameraServiceListener(); |
| 289 | sCameraService->addListenerTest(serviceListener, &statuses); |
| 290 | sCameraServiceProxy->setOverrideCameraDisabled(false); |
| 291 | |
| 292 | for (auto s : statuses) { |
| 293 | sp<TestCameraDeviceCallbacks> callbacks = new TestCameraDeviceCallbacks(); |
| 294 | sp<hardware::camera2::ICameraDeviceUser> deviceA, deviceB; |
| 295 | binder::Status status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 296 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 297 | android::CameraService::USE_CALLING_UID, 0/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 298 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 299 | kDefaultDeviceId, /*devicePolicy*/0, &deviceA); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 300 | AutoDisconnectDevice autoDisconnectA(deviceA); |
| 301 | ASSERT_TRUE(status.isOk()) << "Exception code " << status.exceptionCode() << |
| 302 | " service specific error code " << status.serviceSpecificErrorCode(); |
| 303 | status = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 304 | sCameraService->connectDevice(callbacks, s.cameraId, std::string(), {}, |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 305 | android::CameraService::USE_CALLING_UID, 1/*oomScoreDiff*/, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame^] | 306 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 307 | kDefaultDeviceId, /*devicePolicy*/0, &deviceB); |
Austin Borger | 86a588e | 2022-05-23 12:41:58 -0700 | [diff] [blame] | 308 | AutoDisconnectDevice autoDisconnectB(deviceB); |
| 309 | ASSERT_TRUE(status.isOk()) << "Exception code " << status.exceptionCode() << |
| 310 | " service specific error code " << status.serviceSpecificErrorCode(); |
| 311 | } |
| 312 | } |