chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 21 | #include <android/gui/ISurfaceComposer.h> |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 23 | #include <gui/AidlStatusUtil.h> |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 24 | #include <gui/LayerDebugInfo.h> |
| 25 | #include <gui/Surface.h> |
| 26 | #include <gui/SurfaceComposerClient.h> |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 28 | #include <private/gui/ComposerServiceAIDL.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 29 | #include <ui/DisplayMode.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 30 | #include <ui/DynamicDisplayInfo.h> |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 31 | #include <utils/String8.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 32 | #include <functional> |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 33 | #include "utils/ScreenshotUtils.h" |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 34 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 35 | namespace android { |
| 36 | |
| 37 | using Transaction = SurfaceComposerClient::Transaction; |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 38 | using gui::LayerDebugInfo; |
Huihong Luo | 3bdef86 | 2022-03-03 11:57:19 -0800 | [diff] [blame] | 39 | using gui::aidl_utils::statusTFromBinderStatus; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 40 | using ui::ColorMode; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | const String8 DISPLAY_NAME("Credentials Display Test"); |
| 44 | const String8 SURFACE_NAME("Test Surface Name"); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | /** |
| 48 | * This class tests the CheckCredentials method in SurfaceFlinger. |
| 49 | * Methods like EnableVsyncInjections and InjectVsync are not tested since they do not |
| 50 | * return anything meaningful. |
| 51 | */ |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 52 | |
| 53 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 54 | #pragma clang diagnostic push |
| 55 | #pragma clang diagnostic ignored "-Wconversion" |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 56 | class CredentialsTest : public ::testing::Test { |
| 57 | protected: |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 58 | void SetUp() override { ASSERT_NO_FATAL_FAILURE(initClient()); } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 59 | |
| 60 | void TearDown() override { |
| 61 | mComposerClient->dispose(); |
| 62 | mBGSurfaceControl.clear(); |
| 63 | mComposerClient.clear(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | sp<IBinder> mDisplay; |
| 67 | sp<IBinder> mVirtualDisplay; |
| 68 | sp<SurfaceComposerClient> mComposerClient; |
| 69 | sp<SurfaceControl> mBGSurfaceControl; |
| 70 | sp<SurfaceControl> mVirtualSurfaceControl; |
| 71 | |
| 72 | void initClient() { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 73 | mComposerClient = sp<SurfaceComposerClient>::make(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 74 | ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); |
| 75 | } |
| 76 | |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 77 | static sp<IBinder> getFirstDisplayToken() { |
| 78 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 79 | if (ids.empty()) { |
| 80 | return nullptr; |
| 81 | } |
| 82 | |
| 83 | return SurfaceComposerClient::getPhysicalDisplayToken(ids.front()); |
| 84 | } |
| 85 | |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 86 | static std::optional<uint64_t> getFirstDisplayId() { |
| 87 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 88 | if (ids.empty()) { |
| 89 | return std::nullopt; |
| 90 | } |
| 91 | |
| 92 | return ids.front().value; |
| 93 | } |
| 94 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 95 | void setupBackgroundSurface() { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 96 | mDisplay = getFirstDisplayToken(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 97 | ASSERT_FALSE(mDisplay == nullptr); |
| 98 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 99 | ui::DisplayMode mode; |
| 100 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplay, &mode)); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 101 | |
| 102 | // Background surface |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 103 | mBGSurfaceControl = mComposerClient->createSurface(SURFACE_NAME, mode.resolution.getWidth(), |
| 104 | mode.resolution.getHeight(), |
| 105 | PIXEL_FORMAT_RGBA_8888, 0); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 106 | ASSERT_TRUE(mBGSurfaceControl != nullptr); |
| 107 | ASSERT_TRUE(mBGSurfaceControl->isValid()); |
| 108 | |
| 109 | Transaction t; |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 110 | t.setDisplayLayerStack(mDisplay, ui::DEFAULT_LAYER_STACK); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 111 | ASSERT_EQ(NO_ERROR, |
| 112 | t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply()); |
| 113 | } |
| 114 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 115 | /** |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 116 | * Template function the check a condition for different types of users: root |
| 117 | * graphics, system, and non-supported user. Root, graphics, and system should |
| 118 | * always equal privilegedValue, and non-supported user should equal unprivilegedValue. |
| 119 | */ |
| 120 | template <typename T> |
| 121 | void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) { |
| 122 | // Check with root. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 123 | { |
| 124 | UIDFaker f(AID_SYSTEM); |
| 125 | ASSERT_EQ(privilegedValue, condition()); |
| 126 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 127 | |
| 128 | // Check as a Graphics user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 129 | { |
| 130 | UIDFaker f(AID_GRAPHICS); |
| 131 | ASSERT_EQ(privilegedValue, condition()); |
| 132 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 133 | |
| 134 | // Check as a system user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 135 | { |
| 136 | UIDFaker f(AID_SYSTEM); |
| 137 | ASSERT_EQ(privilegedValue, condition()); |
| 138 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 139 | |
| 140 | // Check as a non-supported user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 141 | { |
| 142 | UIDFaker f(AID_BIN); |
| 143 | ASSERT_EQ(unprivilegedValue, condition()); |
| 144 | } |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 145 | |
| 146 | // Check as shell since shell has some additional permissions |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 147 | { |
| 148 | UIDFaker f(AID_SHELL); |
| 149 | ASSERT_EQ(privilegedValue, condition()); |
| 150 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | |
| 154 | TEST_F(CredentialsTest, ClientInitTest) { |
| 155 | // Root can init can init the client. |
| 156 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 157 | |
| 158 | // Graphics can init the client. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 159 | { |
| 160 | UIDFaker f(AID_GRAPHICS); |
| 161 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 162 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 163 | |
| 164 | // System can init the client. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 165 | { |
| 166 | UIDFaker f(AID_SYSTEM); |
| 167 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 168 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 169 | |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 170 | // Anyone else can init the client. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 171 | { |
| 172 | UIDFaker f(AID_BIN); |
| 173 | mComposerClient = sp<SurfaceComposerClient>::make(); |
| 174 | ASSERT_NO_FATAL_FAILURE(initClient()); |
| 175 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 179 | std::function<bool()> condition = [] { return getFirstDisplayToken() != nullptr; }; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 180 | // Anyone can access display information. |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 181 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(CredentialsTest, AllowedGetterMethodsTest) { |
| 185 | // The following methods are tested with a UID that is not root, graphics, |
| 186 | // or system, to show that anyone can access them. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 187 | UIDFaker f(AID_BIN); |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 188 | const auto id = getFirstDisplayId(); |
| 189 | ASSERT_TRUE(id); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 190 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 191 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info)); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 194 | TEST_F(CredentialsTest, GetDynamicDisplayInfoTest) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 195 | const auto id = getFirstDisplayId(); |
| 196 | ASSERT_TRUE(id); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 197 | std::function<status_t()> condition = [=]() { |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 198 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 199 | return SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 200 | }; |
| 201 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 202 | } |
| 203 | |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 204 | TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 205 | const auto display = getFirstDisplayToken(); |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 206 | std::function<status_t()> condition = [=]() { |
| 207 | ui::DisplayPrimaries primaries; |
| 208 | return SurfaceComposerClient::getDisplayNativePrimaries(display, primaries); |
| 209 | }; |
| 210 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 211 | } |
| 212 | |
Steven Thomas | a87ed45 | 2020-01-03 16:10:05 -0800 | [diff] [blame] | 213 | TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 214 | const auto display = getFirstDisplayToken(); |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 215 | gui::DisplayModeSpecs specs; |
| 216 | status_t res = SurfaceComposerClient::getDesiredDisplayModeSpecs(display, &specs); |
Steven Thomas | a87ed45 | 2020-01-03 16:10:05 -0800 | [diff] [blame] | 217 | ASSERT_EQ(res, NO_ERROR); |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 218 | gui::DisplayModeSpecs setSpecs; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 219 | std::function<status_t()> condition = [=]() { |
Ady Abraham | 285f8c1 | 2022-10-11 17:12:14 -0700 | [diff] [blame] | 220 | return SurfaceComposerClient::setDesiredDisplayModeSpecs(display, specs); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 221 | }; |
| 222 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 223 | } |
| 224 | |
| 225 | TEST_F(CredentialsTest, SetActiveColorModeTest) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 226 | const auto display = getFirstDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 227 | std::function<status_t()> condition = [=]() { |
| 228 | return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE); |
| 229 | }; |
| 230 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 231 | } |
| 232 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 233 | TEST_F(CredentialsTest, CreateDisplayTest) { |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 234 | // Only graphics and system processes can create a secure display. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 235 | std::function<bool()> condition = [=]() { |
| 236 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true); |
| 237 | return testDisplay.get() != nullptr; |
| 238 | }; |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 239 | |
| 240 | // Check with root. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 241 | { |
| 242 | UIDFaker f(AID_ROOT); |
| 243 | ASSERT_FALSE(condition()); |
| 244 | } |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 245 | |
| 246 | // Check as a Graphics user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 247 | { |
| 248 | UIDFaker f(AID_GRAPHICS); |
| 249 | ASSERT_TRUE(condition()); |
| 250 | } |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 251 | |
| 252 | // Check as a system user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 253 | { |
| 254 | UIDFaker f(AID_SYSTEM); |
| 255 | ASSERT_TRUE(condition()); |
| 256 | } |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 257 | |
| 258 | // Check as a non-supported user. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 259 | { |
| 260 | UIDFaker f(AID_BIN); |
| 261 | ASSERT_FALSE(condition()); |
| 262 | } |
chaviw | d4a6164 | 2020-09-01 14:53:46 -0700 | [diff] [blame] | 263 | |
| 264 | // Check as shell since shell has some additional permissions |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 265 | { |
| 266 | UIDFaker f(AID_SHELL); |
| 267 | ASSERT_FALSE(condition()); |
| 268 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 269 | |
| 270 | condition = [=]() { |
| 271 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false); |
| 272 | return testDisplay.get() != nullptr; |
| 273 | }; |
| 274 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 275 | } |
| 276 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 277 | TEST_F(CredentialsTest, CaptureTest) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 278 | const auto display = getFirstDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 279 | std::function<status_t()> condition = [=]() { |
| 280 | sp<GraphicBuffer> outBuffer; |
chaviw | 690db38 | 2020-07-27 16:46:46 -0700 | [diff] [blame] | 281 | DisplayCaptureArgs captureArgs; |
| 282 | captureArgs.displayToken = display; |
| 283 | ScreenCaptureResults captureResults; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 284 | return ScreenCapture::captureDisplay(captureArgs, captureResults); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 285 | }; |
| 286 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 287 | } |
| 288 | |
| 289 | TEST_F(CredentialsTest, CaptureLayersTest) { |
| 290 | setupBackgroundSurface(); |
| 291 | sp<GraphicBuffer> outBuffer; |
| 292 | std::function<status_t()> condition = [=]() { |
chaviw | 26c5248 | 2020-07-28 16:25:52 -0700 | [diff] [blame] | 293 | LayerCaptureArgs captureArgs; |
| 294 | captureArgs.layerHandle = mBGSurfaceControl->getHandle(); |
| 295 | captureArgs.sourceCrop = {0, 0, 1, 1}; |
| 296 | |
| 297 | ScreenCaptureResults captureResults; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 298 | return ScreenCapture::captureLayers(captureArgs, captureResults); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 299 | }; |
| 300 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * The following tests are for methods accessible directly through SurfaceFlinger. |
| 305 | */ |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 306 | TEST_F(CredentialsTest, GetLayerDebugInfo) { |
| 307 | setupBackgroundSurface(); |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 308 | sp<gui::ISurfaceComposer> sf(ComposerServiceAIDL::getComposerService()); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 309 | |
| 310 | // Historically, only root and shell can access the getLayerDebugInfo which |
| 311 | // is called when we call dumpsys. I don't see a reason why we should change this. |
| 312 | std::vector<LayerDebugInfo> outLayers; |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 313 | binder::Status status = binder::Status::ok(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 314 | // Check with root. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 315 | { |
| 316 | UIDFaker f(AID_ROOT); |
| 317 | status = sf->getLayerDebugInfo(&outLayers); |
| 318 | ASSERT_EQ(NO_ERROR, statusTFromBinderStatus(status)); |
| 319 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 320 | |
| 321 | // Check as a shell. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 322 | { |
| 323 | UIDFaker f(AID_SHELL); |
| 324 | status = sf->getLayerDebugInfo(&outLayers); |
| 325 | ASSERT_EQ(NO_ERROR, statusTFromBinderStatus(status)); |
| 326 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 327 | |
| 328 | // Check as anyone else. |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 329 | { |
| 330 | UIDFaker f(AID_BIN); |
| 331 | status = sf->getLayerDebugInfo(&outLayers); |
| 332 | ASSERT_EQ(PERMISSION_DENIED, statusTFromBinderStatus(status)); |
| 333 | } |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 334 | } |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 335 | |
| 336 | TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 337 | const auto display = getFirstDisplayToken(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 338 | ASSERT_FALSE(display == nullptr); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 339 | bool result = false; |
| 340 | status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 341 | ASSERT_EQ(NO_ERROR, error); |
| 342 | bool hasWideColorMode = false; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 343 | const auto id = getFirstDisplayId(); |
| 344 | ASSERT_TRUE(id); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 345 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 346 | SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 347 | const auto& colorModes = info.supportedColorModes; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 348 | for (ColorMode colorMode : colorModes) { |
| 349 | switch (colorMode) { |
| 350 | case ColorMode::DISPLAY_P3: |
| 351 | case ColorMode::ADOBE_RGB: |
| 352 | case ColorMode::DCI_P3: |
| 353 | hasWideColorMode = true; |
| 354 | break; |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | ASSERT_EQ(hasWideColorMode, result); |
| 360 | } |
| 361 | |
| 362 | TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 363 | const auto display = getFirstDisplayToken(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 364 | ASSERT_FALSE(display == nullptr); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 365 | std::function<status_t()> condition = [=]() { |
| 366 | bool result = false; |
| 367 | return SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 368 | }; |
| 369 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 370 | } |
| 371 | |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 372 | TEST_F(CredentialsTest, GetActiveColorModeBasicCorrectness) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 373 | const auto id = getFirstDisplayId(); |
| 374 | ASSERT_TRUE(id); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 375 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame^] | 376 | SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 377 | ColorMode colorMode = info.activeColorMode; |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 378 | ASSERT_NE(static_cast<ColorMode>(BAD_VALUE), colorMode); |
| 379 | } |
| 380 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 381 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 382 | |
| 383 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 384 | #pragma clang diagnostic pop // ignored "-Wconversion" |