blob: 11eecba1fd15478f5987aea00430e82c830b682f [file] [log] [blame]
Ana Krulec13be8ad2018-08-21 02:43:56 +00001#include <algorithm>
2#include <functional>
3#include <limits>
4#include <ostream>
5
6#include <gtest/gtest.h>
7
8#include <gui/ISurfaceComposer.h>
9#include <gui/LayerDebugInfo.h>
10#include <gui/Surface.h>
11#include <gui/SurfaceComposerClient.h>
12
13#include <private/android_filesystem_config.h>
14#include <private/gui/ComposerService.h>
Ana Krulec13be8ad2018-08-21 02:43:56 +000015#include <ui/DisplayInfo.h>
16#include <utils/String8.h>
17
18namespace android {
19
20using Transaction = SurfaceComposerClient::Transaction;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -080021using ui::ColorMode;
Ana Krulec13be8ad2018-08-21 02:43:56 +000022
23namespace {
24const String8 DISPLAY_NAME("Credentials Display Test");
25const String8 SURFACE_NAME("Test Surface Name");
Ana Krulec13be8ad2018-08-21 02:43:56 +000026const uint32_t ROTATION = 0;
27const float FRAME_SCALE = 1.0f;
28} // namespace
29
30/**
31 * This class tests the CheckCredentials method in SurfaceFlinger.
32 * Methods like EnableVsyncInjections and InjectVsync are not tested since they do not
33 * return anything meaningful.
34 */
35class CredentialsTest : public ::testing::Test {
36protected:
37 void SetUp() override {
38 // Start the tests as root.
39 seteuid(AID_ROOT);
40
41 ASSERT_NO_FATAL_FAILURE(initClient());
42 }
43
44 void TearDown() override {
45 mComposerClient->dispose();
46 mBGSurfaceControl.clear();
47 mComposerClient.clear();
48 // Finish the tests as root.
49 seteuid(AID_ROOT);
50 }
51
52 sp<IBinder> mDisplay;
53 sp<IBinder> mVirtualDisplay;
54 sp<SurfaceComposerClient> mComposerClient;
55 sp<SurfaceControl> mBGSurfaceControl;
56 sp<SurfaceControl> mVirtualSurfaceControl;
57
58 void initClient() {
59 mComposerClient = new SurfaceComposerClient;
60 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
61 }
62
63 void setupBackgroundSurface() {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080064 mDisplay = SurfaceComposerClient::getInternalDisplayToken();
65 ASSERT_FALSE(mDisplay == nullptr);
66
Ana Krulec13be8ad2018-08-21 02:43:56 +000067 DisplayInfo info;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080068 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info));
Ana Krulec13be8ad2018-08-21 02:43:56 +000069 const ssize_t displayWidth = info.w;
70 const ssize_t displayHeight = info.h;
71
72 // Background surface
73 mBGSurfaceControl =
74 mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight,
75 PIXEL_FORMAT_RGBA_8888, 0);
76 ASSERT_TRUE(mBGSurfaceControl != nullptr);
77 ASSERT_TRUE(mBGSurfaceControl->isValid());
78
79 Transaction t;
80 t.setDisplayLayerStack(mDisplay, 0);
81 ASSERT_EQ(NO_ERROR,
82 t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply());
83 }
84
85 void setupVirtualDisplay() {
86 mVirtualDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
87 const ssize_t displayWidth = 100;
88 const ssize_t displayHeight = 100;
89
90 // Background surface
91 mVirtualSurfaceControl =
92 mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight,
93 PIXEL_FORMAT_RGBA_8888, 0);
94 ASSERT_TRUE(mVirtualSurfaceControl != nullptr);
95 ASSERT_TRUE(mVirtualSurfaceControl->isValid());
96
97 Transaction t;
98 t.setDisplayLayerStack(mVirtualDisplay, 0);
99 ASSERT_EQ(NO_ERROR,
100 t.setLayer(mVirtualSurfaceControl, INT_MAX - 3)
101 .show(mVirtualSurfaceControl)
102 .apply());
103 }
104
105 /**
106 * Sets UID to imitate Graphic's process.
107 */
108 void setGraphicsUID() {
109 seteuid(AID_ROOT);
110 seteuid(AID_GRAPHICS);
111 }
112
113 /**
114 * Sets UID to imitate System's process.
115 */
116 void setSystemUID() {
117 seteuid(AID_ROOT);
118 seteuid(AID_SYSTEM);
119 }
120
121 /**
122 * Sets UID to imitate a process that doesn't have any special privileges in
123 * our code.
124 */
125 void setBinUID() {
126 seteuid(AID_ROOT);
127 seteuid(AID_BIN);
128 }
129
130 /**
131 * Template function the check a condition for different types of users: root
132 * graphics, system, and non-supported user. Root, graphics, and system should
133 * always equal privilegedValue, and non-supported user should equal unprivilegedValue.
134 */
135 template <typename T>
136 void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) {
137 // Check with root.
138 seteuid(AID_ROOT);
139 ASSERT_EQ(privilegedValue, condition());
140
141 // Check as a Graphics user.
142 setGraphicsUID();
143 ASSERT_EQ(privilegedValue, condition());
144
145 // Check as a system user.
146 setSystemUID();
147 ASSERT_EQ(privilegedValue, condition());
148
149 // Check as a non-supported user.
150 setBinUID();
151 ASSERT_EQ(unprivilegedValue, condition());
152 }
153};
154
155TEST_F(CredentialsTest, ClientInitTest) {
156 // Root can init can init the client.
157 ASSERT_NO_FATAL_FAILURE(initClient());
158
159 // Graphics can init the client.
160 setGraphicsUID();
161 ASSERT_NO_FATAL_FAILURE(initClient());
162
163 // System can init the client.
164 setSystemUID();
165 ASSERT_NO_FATAL_FAILURE(initClient());
166
Robert Carrb89ea9d2018-12-10 13:01:14 -0800167 // Anyone else can init the client.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000168 setBinUID();
169 mComposerClient = new SurfaceComposerClient;
Robert Carrb89ea9d2018-12-10 13:01:14 -0800170 ASSERT_NO_FATAL_FAILURE(initClient());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000171}
172
173TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800174 std::function<bool()> condition = [] {
175 return SurfaceComposerClient::getInternalDisplayToken() != nullptr;
Ana Krulec13be8ad2018-08-21 02:43:56 +0000176 };
177 // Anyone can access display information.
178 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
179}
180
181TEST_F(CredentialsTest, AllowedGetterMethodsTest) {
182 // The following methods are tested with a UID that is not root, graphics,
183 // or system, to show that anyone can access them.
184 setBinUID();
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800185 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000186 ASSERT_TRUE(display != nullptr);
187
188 DisplayInfo info;
189 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
190
191 Vector<DisplayInfo> configs;
192 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
193
194 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveConfig(display));
195
196 ASSERT_NE(static_cast<ui::ColorMode>(BAD_VALUE),
197 SurfaceComposerClient::getActiveColorMode(display));
198}
199
200TEST_F(CredentialsTest, GetDisplayColorModesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800201 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000202 std::function<status_t()> condition = [=]() {
203 Vector<ui::ColorMode> outColorModes;
204 return SurfaceComposerClient::getDisplayColorModes(display, &outColorModes);
205 };
206 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
207}
208
Daniel Solomon42d04562019-01-20 21:03:19 -0800209TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800210 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Daniel Solomon42d04562019-01-20 21:03:19 -0800211 std::function<status_t()> condition = [=]() {
212 ui::DisplayPrimaries primaries;
213 return SurfaceComposerClient::getDisplayNativePrimaries(display, primaries);
214 };
215 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
216}
217
Steven Thomasa87ed452020-01-03 16:10:05 -0800218TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800219 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Steven Thomasa87ed452020-01-03 16:10:05 -0800220 int32_t defaultConfig;
221 float minFps;
222 float maxFps;
223 status_t res = SurfaceComposerClient::getDesiredDisplayConfigSpecs(display, &defaultConfig,
224 &minFps, &maxFps);
225 ASSERT_EQ(res, NO_ERROR);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000226 std::function<status_t()> condition = [=]() {
Steven Thomasa87ed452020-01-03 16:10:05 -0800227 return SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, defaultConfig, minFps,
228 maxFps);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000229 };
230 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
231}
232
233TEST_F(CredentialsTest, SetActiveColorModeTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800234 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000235 std::function<status_t()> condition = [=]() {
236 return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE);
237 };
238 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
239}
240
Ana Krulec13be8ad2018-08-21 02:43:56 +0000241TEST_F(CredentialsTest, CreateDisplayTest) {
242 std::function<bool()> condition = [=]() {
243 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
244 return testDisplay.get() != nullptr;
245 };
246 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
247
248 condition = [=]() {
249 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false);
250 return testDisplay.get() != nullptr;
251 };
252 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
253}
254
255TEST_F(CredentialsTest, DISABLED_DestroyDisplayTest) {
256 setupVirtualDisplay();
257
258 DisplayInfo info;
259 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info));
260 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
261 // This test currently fails. TODO(b/112002626): Find a way to properly create
262 // a display in the test environment, so that destroy display can remove it.
263 ASSERT_EQ(NAME_NOT_FOUND, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info));
264}
265
266TEST_F(CredentialsTest, CaptureTest) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800267 const auto display = SurfaceComposerClient::getInternalDisplayToken();
Ana Krulec13be8ad2018-08-21 02:43:56 +0000268 std::function<status_t()> condition = [=]() {
269 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700270 return ScreenshotClient::capture(display, ui::Dataspace::V0_SRGB,
271 ui::PixelFormat::RGBA_8888, Rect(), 0 /*reqWidth*/,
272 0 /*reqHeight*/, false, ROTATION, &outBuffer);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000273 };
274 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
275}
276
277TEST_F(CredentialsTest, CaptureLayersTest) {
278 setupBackgroundSurface();
279 sp<GraphicBuffer> outBuffer;
280 std::function<status_t()> condition = [=]() {
281 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700282 return ScreenshotClient::captureLayers(mBGSurfaceControl->getHandle(),
283 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888,
Vishnu Nairefc42e22019-12-03 17:36:12 -0800284 Rect(0, 0, 1, 1), FRAME_SCALE, &outBuffer);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000285 };
286 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
287}
288
289/**
290 * The following tests are for methods accessible directly through SurfaceFlinger.
291 */
292
293/**
294 * An app can pass a buffer queue to the media server and ask the media server to decode a DRM video
295 * to that buffer queue. The media server is the buffer producer in this case. Because the app may create
296 * its own buffer queue and act as the buffer consumer, the media server wants to be careful to avoid
297 * sending decoded video frames to the app. This is where authenticateSurfaceTexture call comes in, to check
298 * the consumer of a buffer queue is SurfaceFlinger.
299 */
300TEST_F(CredentialsTest, AuthenticateSurfaceTextureTest) {
301 setupBackgroundSurface();
302 sp<IGraphicBufferProducer> producer =
303 mBGSurfaceControl->getSurface()->getIGraphicBufferProducer();
304 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
305
306 std::function<bool()> condition = [=]() { return sf->authenticateSurfaceTexture(producer); };
307 // Anyone should be able to check if the consumer of the buffer queue is SF.
308 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
309}
310
311TEST_F(CredentialsTest, GetLayerDebugInfo) {
312 setupBackgroundSurface();
313 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
314
315 // Historically, only root and shell can access the getLayerDebugInfo which
316 // is called when we call dumpsys. I don't see a reason why we should change this.
317 std::vector<LayerDebugInfo> outLayers;
318 // Check with root.
319 seteuid(AID_ROOT);
320 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
321
322 // Check as a shell.
323 seteuid(AID_SHELL);
324 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
325
326 // Check as anyone else.
327 seteuid(AID_ROOT);
328 seteuid(AID_BIN);
329 ASSERT_EQ(PERMISSION_DENIED, sf->getLayerDebugInfo(&outLayers));
330}
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800331
332TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800333 const auto display = SurfaceComposerClient::getInternalDisplayToken();
334 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800335 bool result = false;
336 status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result);
337 ASSERT_EQ(NO_ERROR, error);
338 bool hasWideColorMode = false;
339 Vector<ColorMode> colorModes;
340 SurfaceComposerClient::getDisplayColorModes(display, &colorModes);
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
355TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800356 const auto display = SurfaceComposerClient::getInternalDisplayToken();
357 ASSERT_FALSE(display == nullptr);
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800358 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 Lind1fedb42019-03-11 17:48:41 -0700365TEST_F(CredentialsTest, GetActiveColorModeBasicCorrectness) {
366 const auto display = SurfaceComposerClient::getInternalDisplayToken();
367 ASSERT_FALSE(display == nullptr);
368 ColorMode colorMode = SurfaceComposerClient::getActiveColorMode(display);
369 ASSERT_NE(static_cast<ColorMode>(BAD_VALUE), colorMode);
370}
371
Ana Krulec13be8ad2018-08-21 02:43:56 +0000372} // namespace android