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