Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 1 | #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 Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 15 | #include <ui/DisplayInfo.h> |
| 16 | #include <utils/String8.h> |
| 17 | |
| 18 | namespace android { |
| 19 | |
| 20 | using Transaction = SurfaceComposerClient::Transaction; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 21 | using ui::ColorMode; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | const String8 DISPLAY_NAME("Credentials Display Test"); |
| 25 | const String8 SURFACE_NAME("Test Surface Name"); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 26 | const uint32_t ROTATION = 0; |
| 27 | const 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 | */ |
| 35 | class CredentialsTest : public ::testing::Test { |
| 36 | protected: |
| 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 Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 64 | mDisplay = SurfaceComposerClient::getInternalDisplayToken(); |
| 65 | ASSERT_FALSE(mDisplay == nullptr); |
| 66 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 67 | DisplayInfo info; |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 68 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mDisplay, &info)); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 69 | 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 | |
| 155 | TEST_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 Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 167 | // Anyone else can init the client. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 168 | setBinUID(); |
| 169 | mComposerClient = new SurfaceComposerClient; |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 170 | ASSERT_NO_FATAL_FAILURE(initClient()); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | TEST_F(CredentialsTest, GetBuiltInDisplayAccessTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 174 | std::function<bool()> condition = [] { |
| 175 | return SurfaceComposerClient::getInternalDisplayToken() != nullptr; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 176 | }; |
| 177 | // Anyone can access display information. |
| 178 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true)); |
| 179 | } |
| 180 | |
| 181 | TEST_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 Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 185 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 186 | 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 | |
| 200 | TEST_F(CredentialsTest, GetDisplayColorModesTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 201 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 202 | 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 Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 209 | TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 210 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 211 | 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 Thomas | a87ed45 | 2020-01-03 16:10:05 -0800 | [diff] [blame^] | 218 | TEST_F(CredentialsTest, SetDesiredDisplayConfigsTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 219 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Steven Thomas | a87ed45 | 2020-01-03 16:10:05 -0800 | [diff] [blame^] | 220 | 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 Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 226 | std::function<status_t()> condition = [=]() { |
Steven Thomas | a87ed45 | 2020-01-03 16:10:05 -0800 | [diff] [blame^] | 227 | return SurfaceComposerClient::setDesiredDisplayConfigSpecs(display, defaultConfig, minFps, |
| 228 | maxFps); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 229 | }; |
| 230 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 231 | } |
| 232 | |
| 233 | TEST_F(CredentialsTest, SetActiveColorModeTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 234 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 235 | 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 Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 241 | TEST_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 | |
| 255 | TEST_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 | |
| 266 | TEST_F(CredentialsTest, CaptureTest) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 267 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 268 | std::function<status_t()> condition = [=]() { |
| 269 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 270 | return ScreenshotClient::capture(display, ui::Dataspace::V0_SRGB, |
| 271 | ui::PixelFormat::RGBA_8888, Rect(), 0 /*reqWidth*/, |
| 272 | 0 /*reqHeight*/, false, ROTATION, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 273 | }; |
| 274 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 275 | } |
| 276 | |
| 277 | TEST_F(CredentialsTest, CaptureLayersTest) { |
| 278 | setupBackgroundSurface(); |
| 279 | sp<GraphicBuffer> outBuffer; |
| 280 | std::function<status_t()> condition = [=]() { |
| 281 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 282 | return ScreenshotClient::captureLayers(mBGSurfaceControl->getHandle(), |
| 283 | ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, |
Vishnu Nair | efc42e2 | 2019-12-03 17:36:12 -0800 | [diff] [blame] | 284 | Rect(0, 0, 1, 1), FRAME_SCALE, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 285 | }; |
| 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 | */ |
| 300 | TEST_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 | |
| 311 | TEST_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 Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 331 | |
| 332 | TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 333 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
| 334 | ASSERT_FALSE(display == nullptr); |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 335 | 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 | |
| 355 | TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 356 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
| 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) { |
| 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 Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 372 | } // namespace android |