blob: babbcd4e350f5028631e79faa8b2d73b9fb39e8a [file] [log] [blame]
chaviw8ffc7b82020-08-18 11:25:37 -07001/*
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 Luo05539a12022-02-23 10:29:40 -080021#include <android/gui/ISurfaceComposer.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000022#include <gtest/gtest.h>
Huihong Luo3bdef862022-03-03 11:57:19 -080023#include <gui/AidlStatusUtil.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000024#include <gui/Surface.h>
25#include <gui/SurfaceComposerClient.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000026#include <private/android_filesystem_config.h>
Huihong Luo05539a12022-02-23 10:29:40 -080027#include <private/gui/ComposerServiceAIDL.h>
Marin Shalamanova7fe3042021-01-29 21:02:08 +010028#include <ui/DisplayMode.h>
Patrick Williamsf65da8a2024-07-24 17:11:19 +000029#include <ui/DisplayState.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010030#include <ui/DynamicDisplayInfo.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000031#include <utils/String8.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080032#include <functional>
chaviw8ffc7b82020-08-18 11:25:37 -070033#include "utils/ScreenshotUtils.h"
Chavi Weingartenc78f53c2023-04-14 18:50:53 +000034#include "utils/WindowInfosListenerUtils.h"
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080035
Ana Krulec13be8ad2018-08-21 02:43:56 +000036namespace android {
37
38using Transaction = SurfaceComposerClient::Transaction;
Huihong Luo3bdef862022-03-03 11:57:19 -080039using gui::aidl_utils::statusTFromBinderStatus;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -080040using ui::ColorMode;
Ana Krulec13be8ad2018-08-21 02:43:56 +000041
42namespace {
Alan Dingd53801c2024-05-08 16:45:29 -070043const std::string kDisplayName("Credentials Display Test");
44const String8 kSurfaceName("Test Surface Name");
Ana Krulec13be8ad2018-08-21 02:43:56 +000045} // 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 Abrahamb0dbdaa2020-01-06 16:19:42 -080052
53// TODO(b/129481165): remove the #pragma below and fix conversion issues
54#pragma clang diagnostic push
55#pragma clang diagnostic ignored "-Wconversion"
Ana Krulec13be8ad2018-08-21 02:43:56 +000056class CredentialsTest : public ::testing::Test {
57protected:
Chavi Weingartenc73be482022-08-31 16:55:07 +000058 void SetUp() override { ASSERT_NO_FATAL_FAILURE(initClient()); }
Ana Krulec13be8ad2018-08-21 02:43:56 +000059
60 void TearDown() override {
61 mComposerClient->dispose();
62 mBGSurfaceControl.clear();
63 mComposerClient.clear();
Ana Krulec13be8ad2018-08-21 02:43:56 +000064 }
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 Abrahamd11bade2022-08-01 16:18:03 -070073 mComposerClient = sp<SurfaceComposerClient>::make();
Ana Krulec13be8ad2018-08-21 02:43:56 +000074 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
75 }
76
Huihong Luo31b5ac22022-08-15 20:38:10 -070077 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 Qi6bb12822022-10-05 11:42:30 -070086 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 Krulec13be8ad2018-08-21 02:43:56 +000095 void setupBackgroundSurface() {
Huihong Luo31b5ac22022-08-15 20:38:10 -070096 mDisplay = getFirstDisplayToken();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080097 ASSERT_FALSE(mDisplay == nullptr);
98
Marin Shalamanova7fe3042021-01-29 21:02:08 +010099 ui::DisplayMode mode;
100 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveDisplayMode(mDisplay, &mode));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000101
102 // Background surface
Alan Dingd53801c2024-05-08 16:45:29 -0700103 mBGSurfaceControl = mComposerClient->createSurface(kSurfaceName, mode.resolution.getWidth(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100104 mode.resolution.getHeight(),
105 PIXEL_FORMAT_RGBA_8888, 0);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000106 ASSERT_TRUE(mBGSurfaceControl != nullptr);
107 ASSERT_TRUE(mBGSurfaceControl->isValid());
108
109 Transaction t;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700110 t.setDisplayLayerStack(mDisplay, ui::DEFAULT_LAYER_STACK);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000111 ASSERT_EQ(NO_ERROR,
112 t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply());
113 }
114
Ana Krulec13be8ad2018-08-21 02:43:56 +0000115 /**
Ana Krulec13be8ad2018-08-21 02:43:56 +0000116 * 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 Weingartenc73be482022-08-31 16:55:07 +0000123 {
124 UIDFaker f(AID_SYSTEM);
125 ASSERT_EQ(privilegedValue, condition());
126 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000127
128 // Check as a Graphics user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000129 {
130 UIDFaker f(AID_GRAPHICS);
131 ASSERT_EQ(privilegedValue, condition());
132 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000133
134 // Check as a system user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000135 {
136 UIDFaker f(AID_SYSTEM);
137 ASSERT_EQ(privilegedValue, condition());
138 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000139
140 // Check as a non-supported user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000141 {
142 UIDFaker f(AID_BIN);
143 ASSERT_EQ(unprivilegedValue, condition());
144 }
chaviwd4a61642020-09-01 14:53:46 -0700145
146 // Check as shell since shell has some additional permissions
Chavi Weingartenc73be482022-08-31 16:55:07 +0000147 {
148 UIDFaker f(AID_SHELL);
149 ASSERT_EQ(privilegedValue, condition());
150 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000151 }
152};
153
154TEST_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 Weingartenc73be482022-08-31 16:55:07 +0000159 {
160 UIDFaker f(AID_GRAPHICS);
161 ASSERT_NO_FATAL_FAILURE(initClient());
162 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000163
164 // System can init the client.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000165 {
166 UIDFaker f(AID_SYSTEM);
167 ASSERT_NO_FATAL_FAILURE(initClient());
168 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000169
Robert Carrb89ea9d2018-12-10 13:01:14 -0800170 // Anyone else can init the client.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000171 {
172 UIDFaker f(AID_BIN);
173 mComposerClient = sp<SurfaceComposerClient>::make();
174 ASSERT_NO_FATAL_FAILURE(initClient());
175 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000176}
177
178TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700179 std::function<bool()> condition = [] { return getFirstDisplayToken() != nullptr; };
Ana Krulec13be8ad2018-08-21 02:43:56 +0000180 // Anyone can access display information.
Sally Qi6bb12822022-10-05 11:42:30 -0700181 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000182}
183
184TEST_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 Weingartenc73be482022-08-31 16:55:07 +0000187 UIDFaker f(AID_BIN);
Sally Qi6bb12822022-10-05 11:42:30 -0700188 const auto id = getFirstDisplayId();
189 ASSERT_TRUE(id);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100190 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -0700191 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info));
Ana Krulec13be8ad2018-08-21 02:43:56 +0000192}
193
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100194TEST_F(CredentialsTest, GetDynamicDisplayInfoTest) {
Sally Qi6bb12822022-10-05 11:42:30 -0700195 const auto id = getFirstDisplayId();
196 ASSERT_TRUE(id);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000197 std::function<status_t()> condition = [=]() {
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100198 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -0700199 return SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000200 };
201 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
202}
203
Daniel Solomon42d04562019-01-20 21:03:19 -0800204TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700205 const auto display = getFirstDisplayToken();
Daniel Solomon42d04562019-01-20 21:03:19 -0800206 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 Thomasa87ed452020-01-03 16:10:05 -0800213TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700214 const auto display = getFirstDisplayToken();
Ady Abraham285f8c12022-10-11 17:12:14 -0700215 gui::DisplayModeSpecs specs;
216 status_t res = SurfaceComposerClient::getDesiredDisplayModeSpecs(display, &specs);
Steven Thomasa87ed452020-01-03 16:10:05 -0800217 ASSERT_EQ(res, NO_ERROR);
Ady Abraham285f8c12022-10-11 17:12:14 -0700218 gui::DisplayModeSpecs setSpecs;
Ana Krulec13be8ad2018-08-21 02:43:56 +0000219 std::function<status_t()> condition = [=]() {
Ady Abraham285f8c12022-10-11 17:12:14 -0700220 return SurfaceComposerClient::setDesiredDisplayModeSpecs(display, specs);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000221 };
222 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
223}
224
225TEST_F(CredentialsTest, SetActiveColorModeTest) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700226 const auto display = getFirstDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000227 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 Krulec13be8ad2018-08-21 02:43:56 +0000233TEST_F(CredentialsTest, CreateDisplayTest) {
chaviwd4a61642020-09-01 14:53:46 -0700234 // Only graphics and system processes can create a secure display.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000235 std::function<bool()> condition = [=]() {
Alan Dingd53801c2024-05-08 16:45:29 -0700236 sp<IBinder> testDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName, true);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000237 return testDisplay.get() != nullptr;
238 };
chaviwd4a61642020-09-01 14:53:46 -0700239
240 // Check with root.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000241 {
242 UIDFaker f(AID_ROOT);
Patrick Williamse58a92b2024-02-29 14:52:25 -0600243 ASSERT_TRUE(condition());
Chavi Weingartenc73be482022-08-31 16:55:07 +0000244 }
chaviwd4a61642020-09-01 14:53:46 -0700245
246 // Check as a Graphics user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000247 {
248 UIDFaker f(AID_GRAPHICS);
249 ASSERT_TRUE(condition());
250 }
chaviwd4a61642020-09-01 14:53:46 -0700251
252 // Check as a system user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000253 {
254 UIDFaker f(AID_SYSTEM);
255 ASSERT_TRUE(condition());
256 }
chaviwd4a61642020-09-01 14:53:46 -0700257
258 // Check as a non-supported user.
Chavi Weingartenc73be482022-08-31 16:55:07 +0000259 {
260 UIDFaker f(AID_BIN);
261 ASSERT_FALSE(condition());
262 }
chaviwd4a61642020-09-01 14:53:46 -0700263
264 // Check as shell since shell has some additional permissions
Chavi Weingartenc73be482022-08-31 16:55:07 +0000265 {
266 UIDFaker f(AID_SHELL);
267 ASSERT_FALSE(condition());
268 }
Ana Krulec13be8ad2018-08-21 02:43:56 +0000269
270 condition = [=]() {
Alan Dingd53801c2024-05-08 16:45:29 -0700271 sp<IBinder> testDisplay = SurfaceComposerClient::createVirtualDisplay(kDisplayName, false);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000272 return testDisplay.get() != nullptr;
273 };
274 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
275}
276
Ana Krulec13be8ad2018-08-21 02:43:56 +0000277TEST_F(CredentialsTest, CaptureLayersTest) {
278 setupBackgroundSurface();
279 sp<GraphicBuffer> outBuffer;
Patrick Williamsf65da8a2024-07-24 17:11:19 +0000280 std::function<status_t()> condition = [=, this]() {
chaviw26c52482020-07-28 16:25:52 -0700281 LayerCaptureArgs captureArgs;
282 captureArgs.layerHandle = mBGSurfaceControl->getHandle();
Alec Mouribae67862024-08-06 14:53:46 +0000283 captureArgs.captureArgs.sourceCrop = gui::aidl_utils::toARect(0, 0, 1, 1);
chaviw26c52482020-07-28 16:25:52 -0700284
285 ScreenCaptureResults captureResults;
chaviw8ffc7b82020-08-18 11:25:37 -0700286 return ScreenCapture::captureLayers(captureArgs, captureResults);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000287 };
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 Lin4f3fddf2019-01-24 17:21:24 -0800294
295TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700296 const auto display = getFirstDisplayToken();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800297 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800298 bool result = false;
299 status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result);
300 ASSERT_EQ(NO_ERROR, error);
301 bool hasWideColorMode = false;
Sally Qi6bb12822022-10-05 11:42:30 -0700302 const auto id = getFirstDisplayId();
303 ASSERT_TRUE(id);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100304 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -0700305 SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100306 const auto& colorModes = info.supportedColorModes;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800307 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
321TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) {
Huihong Luo31b5ac22022-08-15 20:38:10 -0700322 const auto display = getFirstDisplayToken();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800323 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800324 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 Lind1fedb42019-03-11 17:48:41 -0700331TEST_F(CredentialsTest, GetActiveColorModeBasicCorrectness) {
Sally Qi6bb12822022-10-05 11:42:30 -0700332 const auto id = getFirstDisplayId();
333 ASSERT_TRUE(id);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100334 ui::DynamicDisplayInfo info;
Sally Qi6bb12822022-10-05 11:42:30 -0700335 SurfaceComposerClient::getDynamicDisplayInfoFromId(*id, &info);
Marin Shalamanov228f46b2021-01-28 21:11:45 +0100336 ColorMode colorMode = info.activeColorMode;
Peiyong Lind1fedb42019-03-11 17:48:41 -0700337 ASSERT_NE(static_cast<ColorMode>(BAD_VALUE), colorMode);
338}
339
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000340TEST_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 Williams04e41762024-04-23 19:05:38 -0500362 // 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 Weingartenc78f53c2023-04-14 18:50:53 +0000369 {
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 Williams04e41762024-04-23 19:05:38 -0500379 ASSERT_TRUE(
380 windowInfosListenerUtils.waitForWindowInfosPredicate(windowIsPresentAndNotTrusted));
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000381 }
382
Patrick Williams04e41762024-04-23 19:05:38 -0500383 // Verify that privileged processes are able to set trusted overlays.
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000384 {
385 UIDFaker f(AID_SYSTEM);
Patrick Williams04e41762024-04-23 19:05:38 -0500386 Transaction().setTrustedOverlay(surfaceControl, true).apply(/*synchronous=*/true);
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000387 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 Williams04e41762024-04-23 19:05:38 -0500395 ASSERT_TRUE(
396 windowInfosListenerUtils.waitForWindowInfosPredicate(windowIsPresentAndTrusted));
Chavi Weingartenc78f53c2023-04-14 18:50:53 +0000397 }
398}
399
Patrick Williamsf65da8a2024-07-24 17:11:19 +0000400TEST_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 Krulec13be8ad2018-08-21 02:43:56 +0000450} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800451
452// TODO(b/129481165): remove the #pragma below and fix conversion issues
453#pragma clang diagnostic pop // ignored "-Wconversion"