blob: 48b2b807b1f334ae271ee4e928b3f862a628a737 [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>
15
16#include <ui/DisplayInfo.h>
17#include <utils/String8.h>
18
19namespace android {
20
21using Transaction = SurfaceComposerClient::Transaction;
Peiyong Lin4f3fddf2019-01-24 17:21:24 -080022using ui::ColorMode;
Ana Krulec13be8ad2018-08-21 02:43:56 +000023
24namespace {
25const String8 DISPLAY_NAME("Credentials Display Test");
26const String8 SURFACE_NAME("Test Surface Name");
Ana Krulec13be8ad2018-08-21 02:43:56 +000027const uint32_t ROTATION = 0;
28const float FRAME_SCALE = 1.0f;
29} // namespace
30
31/**
32 * This class tests the CheckCredentials method in SurfaceFlinger.
33 * Methods like EnableVsyncInjections and InjectVsync are not tested since they do not
34 * return anything meaningful.
35 */
36class CredentialsTest : public ::testing::Test {
37protected:
38 void SetUp() override {
39 // Start the tests as root.
40 seteuid(AID_ROOT);
41
42 ASSERT_NO_FATAL_FAILURE(initClient());
43 }
44
45 void TearDown() override {
46 mComposerClient->dispose();
47 mBGSurfaceControl.clear();
48 mComposerClient.clear();
49 // Finish the tests as root.
50 seteuid(AID_ROOT);
51 }
52
53 sp<IBinder> mDisplay;
54 sp<IBinder> mVirtualDisplay;
55 sp<SurfaceComposerClient> mComposerClient;
56 sp<SurfaceControl> mBGSurfaceControl;
57 sp<SurfaceControl> mVirtualSurfaceControl;
58
59 void initClient() {
60 mComposerClient = new SurfaceComposerClient;
61 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
62 }
63
64 void setupBackgroundSurface() {
65 mDisplay = SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
66 DisplayInfo info;
67 SurfaceComposerClient::getDisplayInfo(mDisplay, &info);
68 const ssize_t displayWidth = info.w;
69 const ssize_t displayHeight = info.h;
70
71 // Background surface
72 mBGSurfaceControl =
73 mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight,
74 PIXEL_FORMAT_RGBA_8888, 0);
75 ASSERT_TRUE(mBGSurfaceControl != nullptr);
76 ASSERT_TRUE(mBGSurfaceControl->isValid());
77
78 Transaction t;
79 t.setDisplayLayerStack(mDisplay, 0);
80 ASSERT_EQ(NO_ERROR,
81 t.setLayer(mBGSurfaceControl, INT_MAX - 3).show(mBGSurfaceControl).apply());
82 }
83
84 void setupVirtualDisplay() {
85 mVirtualDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
86 const ssize_t displayWidth = 100;
87 const ssize_t displayHeight = 100;
88
89 // Background surface
90 mVirtualSurfaceControl =
91 mComposerClient->createSurface(SURFACE_NAME, displayWidth, displayHeight,
92 PIXEL_FORMAT_RGBA_8888, 0);
93 ASSERT_TRUE(mVirtualSurfaceControl != nullptr);
94 ASSERT_TRUE(mVirtualSurfaceControl->isValid());
95
96 Transaction t;
97 t.setDisplayLayerStack(mVirtualDisplay, 0);
98 ASSERT_EQ(NO_ERROR,
99 t.setLayer(mVirtualSurfaceControl, INT_MAX - 3)
100 .show(mVirtualSurfaceControl)
101 .apply());
102 }
103
104 /**
105 * Sets UID to imitate Graphic's process.
106 */
107 void setGraphicsUID() {
108 seteuid(AID_ROOT);
109 seteuid(AID_GRAPHICS);
110 }
111
112 /**
113 * Sets UID to imitate System's process.
114 */
115 void setSystemUID() {
116 seteuid(AID_ROOT);
117 seteuid(AID_SYSTEM);
118 }
119
120 /**
121 * Sets UID to imitate a process that doesn't have any special privileges in
122 * our code.
123 */
124 void setBinUID() {
125 seteuid(AID_ROOT);
126 seteuid(AID_BIN);
127 }
128
129 /**
130 * Template function the check a condition for different types of users: root
131 * graphics, system, and non-supported user. Root, graphics, and system should
132 * always equal privilegedValue, and non-supported user should equal unprivilegedValue.
133 */
134 template <typename T>
135 void checkWithPrivileges(std::function<T()> condition, T privilegedValue, T unprivilegedValue) {
136 // Check with root.
137 seteuid(AID_ROOT);
138 ASSERT_EQ(privilegedValue, condition());
139
140 // Check as a Graphics user.
141 setGraphicsUID();
142 ASSERT_EQ(privilegedValue, condition());
143
144 // Check as a system user.
145 setSystemUID();
146 ASSERT_EQ(privilegedValue, condition());
147
148 // Check as a non-supported user.
149 setBinUID();
150 ASSERT_EQ(unprivilegedValue, condition());
151 }
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.
159 setGraphicsUID();
160 ASSERT_NO_FATAL_FAILURE(initClient());
161
162 // System can init the client.
163 setSystemUID();
164 ASSERT_NO_FATAL_FAILURE(initClient());
165
Robert Carrb89ea9d2018-12-10 13:01:14 -0800166 // Anyone else can init the client.
Ana Krulec13be8ad2018-08-21 02:43:56 +0000167 setBinUID();
168 mComposerClient = new SurfaceComposerClient;
Robert Carrb89ea9d2018-12-10 13:01:14 -0800169 ASSERT_NO_FATAL_FAILURE(initClient());
Ana Krulec13be8ad2018-08-21 02:43:56 +0000170}
171
172TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) {
173 std::function<bool()> condition = [=]() {
174 sp<IBinder> display(
175 SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
176 return (display != nullptr);
177 };
178 // Anyone can access display information.
179 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
180}
181
182TEST_F(CredentialsTest, AllowedGetterMethodsTest) {
183 // The following methods are tested with a UID that is not root, graphics,
184 // or system, to show that anyone can access them.
185 setBinUID();
186 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
187 ASSERT_TRUE(display != nullptr);
188
189 DisplayInfo info;
190 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(display, &info));
191
192 Vector<DisplayInfo> configs;
193 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayConfigs(display, &configs));
194
195 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getActiveConfig(display));
196
197 ASSERT_NE(static_cast<ui::ColorMode>(BAD_VALUE),
198 SurfaceComposerClient::getActiveColorMode(display));
199}
200
201TEST_F(CredentialsTest, GetDisplayColorModesTest) {
202 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
203 std::function<status_t()> condition = [=]() {
204 Vector<ui::ColorMode> outColorModes;
205 return SurfaceComposerClient::getDisplayColorModes(display, &outColorModes);
206 };
207 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
208}
209
210TEST_F(CredentialsTest, SetActiveConfigTest) {
211 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
212 std::function<status_t()> condition = [=]() {
213 return SurfaceComposerClient::setActiveConfig(display, 0);
214 };
215 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
216}
217
218TEST_F(CredentialsTest, SetActiveColorModeTest) {
219 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
220 std::function<status_t()> condition = [=]() {
221 return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE);
222 };
223 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
224}
225
Ana Krulec13be8ad2018-08-21 02:43:56 +0000226TEST_F(CredentialsTest, CreateDisplayTest) {
227 std::function<bool()> condition = [=]() {
228 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
229 return testDisplay.get() != nullptr;
230 };
231 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
232
233 condition = [=]() {
234 sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false);
235 return testDisplay.get() != nullptr;
236 };
237 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false));
238}
239
240TEST_F(CredentialsTest, DISABLED_DestroyDisplayTest) {
241 setupVirtualDisplay();
242
243 DisplayInfo info;
244 ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info));
245 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
246 // This test currently fails. TODO(b/112002626): Find a way to properly create
247 // a display in the test environment, so that destroy display can remove it.
248 ASSERT_EQ(NAME_NOT_FOUND, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info));
249}
250
251TEST_F(CredentialsTest, CaptureTest) {
252 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
253 std::function<status_t()> condition = [=]() {
254 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700255 return ScreenshotClient::capture(display, ui::Dataspace::V0_SRGB,
256 ui::PixelFormat::RGBA_8888, Rect(), 0 /*reqWidth*/,
257 0 /*reqHeight*/, false, ROTATION, &outBuffer);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000258 };
259 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
260}
261
262TEST_F(CredentialsTest, CaptureLayersTest) {
263 setupBackgroundSurface();
264 sp<GraphicBuffer> outBuffer;
265 std::function<status_t()> condition = [=]() {
266 sp<GraphicBuffer> outBuffer;
Peiyong Lin0e003c92018-09-17 11:09:51 -0700267 return ScreenshotClient::captureLayers(mBGSurfaceControl->getHandle(),
268 ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888,
269 Rect(), FRAME_SCALE, &outBuffer);
Ana Krulec13be8ad2018-08-21 02:43:56 +0000270 };
271 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED));
272}
273
274/**
275 * The following tests are for methods accessible directly through SurfaceFlinger.
276 */
277
278/**
279 * An app can pass a buffer queue to the media server and ask the media server to decode a DRM video
280 * to that buffer queue. The media server is the buffer producer in this case. Because the app may create
281 * its own buffer queue and act as the buffer consumer, the media server wants to be careful to avoid
282 * sending decoded video frames to the app. This is where authenticateSurfaceTexture call comes in, to check
283 * the consumer of a buffer queue is SurfaceFlinger.
284 */
285TEST_F(CredentialsTest, AuthenticateSurfaceTextureTest) {
286 setupBackgroundSurface();
287 sp<IGraphicBufferProducer> producer =
288 mBGSurfaceControl->getSurface()->getIGraphicBufferProducer();
289 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
290
291 std::function<bool()> condition = [=]() { return sf->authenticateSurfaceTexture(producer); };
292 // Anyone should be able to check if the consumer of the buffer queue is SF.
293 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true));
294}
295
296TEST_F(CredentialsTest, GetLayerDebugInfo) {
297 setupBackgroundSurface();
298 sp<ISurfaceComposer> sf(ComposerService::getComposerService());
299
300 // Historically, only root and shell can access the getLayerDebugInfo which
301 // is called when we call dumpsys. I don't see a reason why we should change this.
302 std::vector<LayerDebugInfo> outLayers;
303 // Check with root.
304 seteuid(AID_ROOT);
305 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
306
307 // Check as a shell.
308 seteuid(AID_SHELL);
309 ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers));
310
311 // Check as anyone else.
312 seteuid(AID_ROOT);
313 seteuid(AID_BIN);
314 ASSERT_EQ(PERMISSION_DENIED, sf->getLayerDebugInfo(&outLayers));
315}
Peiyong Lin4f3fddf2019-01-24 17:21:24 -0800316
317TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) {
318 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
319 bool result = false;
320 status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result);
321 ASSERT_EQ(NO_ERROR, error);
322 bool hasWideColorMode = false;
323 Vector<ColorMode> colorModes;
324 SurfaceComposerClient::getDisplayColorModes(display, &colorModes);
325 for (ColorMode colorMode : colorModes) {
326 switch (colorMode) {
327 case ColorMode::DISPLAY_P3:
328 case ColorMode::ADOBE_RGB:
329 case ColorMode::DCI_P3:
330 hasWideColorMode = true;
331 break;
332 default:
333 break;
334 }
335 }
336 ASSERT_EQ(hasWideColorMode, result);
337}
338
339TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) {
340 sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
341 std::function<status_t()> condition = [=]() {
342 bool result = false;
343 return SurfaceComposerClient::isWideColorDisplay(display, &result);
344 };
345 ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR));
346}
347
Ana Krulec13be8ad2018-08-21 02:43:56 +0000348} // namespace android