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