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/Surface.h> |
| 25 | #include <gui/SurfaceComposerClient.h> |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Huihong Luo | 05539a1 | 2022-02-23 10:29:40 -0800 | [diff] [blame] | 27 | #include <private/gui/ComposerServiceAIDL.h> |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 28 | #include <ui/DisplayMode.h> |
Patrick Williams | f65da8a | 2024-07-24 17:11:19 +0000 | [diff] [blame] | 29 | #include <ui/DisplayState.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" |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 34 | #include "utils/WindowInfosListenerUtils.h" |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 35 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | using Transaction = SurfaceComposerClient::Transaction; |
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 { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 43 | const std::string kDisplayName("Credentials Display Test"); |
| 44 | const String8 kSurfaceName("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 |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 103 | mBGSurfaceControl = mComposerClient->createSurface(kSurfaceName, mode.resolution.getWidth(), |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 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 = [=]() { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 236 | sp<IBinder> testDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName, true); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 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); |
Patrick Williams | e58a92b | 2024-02-29 14:52:25 -0600 | [diff] [blame] | 243 | ASSERT_TRUE(condition()); |
Chavi Weingarten | c73be48 | 2022-08-31 16:55:07 +0000 | [diff] [blame] | 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 = [=]() { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame] | 271 | sp<IBinder> testDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName, false); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 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, CaptureLayersTest) { |
| 278 | setupBackgroundSurface(); |
| 279 | sp<GraphicBuffer> outBuffer; |
Patrick Williams | f65da8a | 2024-07-24 17:11:19 +0000 | [diff] [blame] | 280 | std::function<status_t()> condition = [=, this]() { |
chaviw | 26c5248 | 2020-07-28 16:25:52 -0700 | [diff] [blame] | 281 | LayerCaptureArgs captureArgs; |
| 282 | captureArgs.layerHandle = mBGSurfaceControl->getHandle(); |
Alec Mouri | bae6786 | 2024-08-06 14:53:46 +0000 | [diff] [blame^] | 283 | captureArgs.captureArgs.sourceCrop = gui::aidl_utils::toARect(0, 0, 1, 1); |
chaviw | 26c5248 | 2020-07-28 16:25:52 -0700 | [diff] [blame] | 284 | |
| 285 | ScreenCaptureResults captureResults; |
chaviw | 8ffc7b8 | 2020-08-18 11:25:37 -0700 | [diff] [blame] | 286 | return ScreenCapture::captureLayers(captureArgs, captureResults); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 287 | }; |
| 288 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * The following tests are for methods accessible directly through SurfaceFlinger. |
| 293 | */ |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 294 | |
| 295 | TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 296 | const auto display = getFirstDisplayToken(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 297 | ASSERT_FALSE(display == nullptr); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 298 | bool result = false; |
| 299 | status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 300 | ASSERT_EQ(NO_ERROR, error); |
| 301 | bool hasWideColorMode = false; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 302 | const auto id = getFirstDisplayId(); |
| 303 | ASSERT_TRUE(id); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 304 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 305 | SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 306 | const auto& colorModes = info.supportedColorModes; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 307 | for (ColorMode colorMode : colorModes) { |
| 308 | switch (colorMode) { |
| 309 | case ColorMode::DISPLAY_P3: |
| 310 | case ColorMode::ADOBE_RGB: |
| 311 | case ColorMode::DCI_P3: |
| 312 | hasWideColorMode = true; |
| 313 | break; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | ASSERT_EQ(hasWideColorMode, result); |
| 319 | } |
| 320 | |
| 321 | TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) { |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 322 | const auto display = getFirstDisplayToken(); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 323 | ASSERT_FALSE(display == nullptr); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 324 | std::function<status_t()> condition = [=]() { |
| 325 | bool result = false; |
| 326 | return SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 327 | }; |
| 328 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 329 | } |
| 330 | |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 331 | TEST_F(CredentialsTest, GetActiveColorModeBasicCorrectness) { |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 332 | const auto id = getFirstDisplayId(); |
| 333 | ASSERT_TRUE(id); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 334 | ui::DynamicDisplayInfo info; |
Sally Qi | 6bb1282 | 2022-10-05 11:42:30 -0700 | [diff] [blame] | 335 | SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info); |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 336 | ColorMode colorMode = info.activeColorMode; |
Peiyong Lin | d1fedb4 | 2019-03-11 17:48:41 -0700 | [diff] [blame] | 337 | ASSERT_NE(static_cast<ColorMode>(BAD_VALUE), colorMode); |
| 338 | } |
| 339 | |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 340 | TEST_F(CredentialsTest, TransactionPermissionTest) { |
| 341 | WindowInfosListenerUtils windowInfosListenerUtils; |
| 342 | std::string name = "Test Layer"; |
| 343 | sp<IBinder> token = sp<BBinder>::make(); |
| 344 | WindowInfo windowInfo; |
| 345 | windowInfo.name = name; |
| 346 | windowInfo.token = token; |
| 347 | sp<SurfaceControl> surfaceControl = |
| 348 | mComposerClient->createSurface(String8(name.c_str()), 100, 100, PIXEL_FORMAT_RGBA_8888, |
| 349 | ISurfaceComposerClient::eFXSurfaceBufferState); |
| 350 | const Rect crop(0, 0, 100, 100); |
| 351 | { |
| 352 | UIDFaker f(AID_SYSTEM); |
| 353 | Transaction() |
| 354 | .setLayerStack(surfaceControl, ui::DEFAULT_LAYER_STACK) |
| 355 | .show(surfaceControl) |
| 356 | .setLayer(surfaceControl, INT32_MAX - 1) |
| 357 | .setCrop(surfaceControl, crop) |
| 358 | .setInputWindowInfo(surfaceControl, windowInfo) |
| 359 | .apply(); |
| 360 | } |
| 361 | |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 362 | // Attempt to set a trusted overlay from a non-privileged process. This should fail silently. |
| 363 | { |
| 364 | UIDFaker f{AID_BIN}; |
| 365 | Transaction().setTrustedOverlay(surfaceControl, true).apply(/*synchronous=*/true); |
| 366 | } |
| 367 | |
| 368 | // Verify that the layer was not made a trusted overlay. |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 369 | { |
| 370 | UIDFaker f(AID_SYSTEM); |
| 371 | auto windowIsPresentAndNotTrusted = [&](const std::vector<WindowInfo>& windowInfos) { |
| 372 | auto foundWindowInfo = |
| 373 | WindowInfosListenerUtils::findMatchingWindowInfo(windowInfo, windowInfos); |
| 374 | if (!foundWindowInfo) { |
| 375 | return false; |
| 376 | } |
| 377 | return !foundWindowInfo->inputConfig.test(WindowInfo::InputConfig::TRUSTED_OVERLAY); |
| 378 | }; |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 379 | ASSERT_TRUE( |
| 380 | windowInfosListenerUtils.waitForWindowInfosPredicate(windowIsPresentAndNotTrusted)); |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 383 | // Verify that privileged processes are able to set trusted overlays. |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 384 | { |
| 385 | UIDFaker f(AID_SYSTEM); |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 386 | Transaction().setTrustedOverlay(surfaceControl, true).apply(/*synchronous=*/true); |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 387 | auto windowIsPresentAndTrusted = [&](const std::vector<WindowInfo>& windowInfos) { |
| 388 | auto foundWindowInfo = |
| 389 | WindowInfosListenerUtils::findMatchingWindowInfo(windowInfo, windowInfos); |
| 390 | if (!foundWindowInfo) { |
| 391 | return false; |
| 392 | } |
| 393 | return foundWindowInfo->inputConfig.test(WindowInfo::InputConfig::TRUSTED_OVERLAY); |
| 394 | }; |
Patrick Williams | 04e4176 | 2024-04-23 19:05:38 -0500 | [diff] [blame] | 395 | ASSERT_TRUE( |
| 396 | windowInfosListenerUtils.waitForWindowInfosPredicate(windowIsPresentAndTrusted)); |
Chavi Weingarten | c78f53c | 2023-04-14 18:50:53 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Patrick Williams | f65da8a | 2024-07-24 17:11:19 +0000 | [diff] [blame] | 400 | TEST_F(CredentialsTest, DisplayTransactionPermissionTest) { |
| 401 | const auto display = getFirstDisplayToken(); |
| 402 | |
| 403 | ui::DisplayState displayState; |
| 404 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(display, &displayState)); |
| 405 | const ui::Rotation initialOrientation = displayState.orientation; |
| 406 | |
| 407 | // Set display orientation from an untrusted process. This should fail silently. |
| 408 | { |
| 409 | UIDFaker f{AID_BIN}; |
| 410 | Transaction transaction; |
| 411 | Rect layerStackRect; |
| 412 | Rect displayRect; |
| 413 | transaction.setDisplayProjection(display, initialOrientation + ui::ROTATION_90, |
| 414 | layerStackRect, displayRect); |
| 415 | transaction.apply(/*synchronous=*/true); |
| 416 | } |
| 417 | |
| 418 | // Verify that the display orientation did not change. |
| 419 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(display, &displayState)); |
| 420 | ASSERT_EQ(initialOrientation, displayState.orientation); |
| 421 | |
| 422 | // Set display orientation from a trusted process. |
| 423 | { |
| 424 | UIDFaker f{AID_SYSTEM}; |
| 425 | Transaction transaction; |
| 426 | Rect layerStackRect; |
| 427 | Rect displayRect; |
| 428 | transaction.setDisplayProjection(display, initialOrientation + ui::ROTATION_90, |
| 429 | layerStackRect, displayRect); |
| 430 | transaction.apply(/*synchronous=*/true); |
| 431 | } |
| 432 | |
| 433 | // Verify that the display orientation did change. |
| 434 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(display, &displayState)); |
| 435 | ASSERT_EQ(initialOrientation + ui::ROTATION_90, displayState.orientation); |
| 436 | |
| 437 | // Reset orientation |
| 438 | { |
| 439 | UIDFaker f{AID_SYSTEM}; |
| 440 | Transaction transaction; |
| 441 | Rect layerStackRect; |
| 442 | Rect displayRect; |
| 443 | transaction.setDisplayProjection(display, initialOrientation, layerStackRect, displayRect); |
| 444 | transaction.apply(/*synchronous=*/true); |
| 445 | } |
| 446 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayState(display, &displayState)); |
| 447 | ASSERT_EQ(initialOrientation, displayState.orientation); |
| 448 | } |
| 449 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 450 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 451 | |
| 452 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 453 | #pragma clang diagnostic pop // ignored "-Wconversion" |