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> |
| 15 | |
| 16 | #include <ui/DisplayInfo.h> |
| 17 | #include <utils/String8.h> |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | using Transaction = SurfaceComposerClient::Transaction; |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 22 | using ui::ColorMode; |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | const String8 DISPLAY_NAME("Credentials Display Test"); |
| 26 | const String8 SURFACE_NAME("Test Surface Name"); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 27 | const uint32_t ROTATION = 0; |
| 28 | const 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 | */ |
| 36 | class CredentialsTest : public ::testing::Test { |
| 37 | protected: |
| 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 | |
| 154 | TEST_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 Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 166 | // Anyone else can init the client. |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 167 | setBinUID(); |
| 168 | mComposerClient = new SurfaceComposerClient; |
Robert Carr | b89ea9d | 2018-12-10 13:01:14 -0800 | [diff] [blame] | 169 | ASSERT_NO_FATAL_FAILURE(initClient()); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_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 | |
| 182 | TEST_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 | |
| 201 | TEST_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 | |
Daniel Solomon | 42d0456 | 2019-01-20 21:03:19 -0800 | [diff] [blame] | 210 | TEST_F(CredentialsTest, GetDisplayNativePrimariesTest) { |
| 211 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 212 | std::function<status_t()> condition = [=]() { |
| 213 | ui::DisplayPrimaries primaries; |
| 214 | return SurfaceComposerClient::getDisplayNativePrimaries(display, primaries); |
| 215 | }; |
| 216 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 217 | } |
| 218 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 219 | TEST_F(CredentialsTest, SetActiveConfigTest) { |
| 220 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 221 | std::function<status_t()> condition = [=]() { |
| 222 | return SurfaceComposerClient::setActiveConfig(display, 0); |
| 223 | }; |
| 224 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 225 | } |
| 226 | |
| 227 | TEST_F(CredentialsTest, SetActiveColorModeTest) { |
| 228 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 229 | std::function<status_t()> condition = [=]() { |
| 230 | return SurfaceComposerClient::setActiveColorMode(display, ui::ColorMode::NATIVE); |
| 231 | }; |
| 232 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 233 | } |
| 234 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 235 | TEST_F(CredentialsTest, CreateDisplayTest) { |
| 236 | std::function<bool()> condition = [=]() { |
| 237 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true); |
| 238 | return testDisplay.get() != nullptr; |
| 239 | }; |
| 240 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 241 | |
| 242 | condition = [=]() { |
| 243 | sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, false); |
| 244 | return testDisplay.get() != nullptr; |
| 245 | }; |
| 246 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, false)); |
| 247 | } |
| 248 | |
| 249 | TEST_F(CredentialsTest, DISABLED_DestroyDisplayTest) { |
| 250 | setupVirtualDisplay(); |
| 251 | |
| 252 | DisplayInfo info; |
| 253 | ASSERT_EQ(NO_ERROR, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info)); |
| 254 | SurfaceComposerClient::destroyDisplay(mVirtualDisplay); |
| 255 | // This test currently fails. TODO(b/112002626): Find a way to properly create |
| 256 | // a display in the test environment, so that destroy display can remove it. |
| 257 | ASSERT_EQ(NAME_NOT_FOUND, SurfaceComposerClient::getDisplayInfo(mVirtualDisplay, &info)); |
| 258 | } |
| 259 | |
| 260 | TEST_F(CredentialsTest, CaptureTest) { |
| 261 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 262 | std::function<status_t()> condition = [=]() { |
| 263 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 264 | return ScreenshotClient::capture(display, ui::Dataspace::V0_SRGB, |
| 265 | ui::PixelFormat::RGBA_8888, Rect(), 0 /*reqWidth*/, |
| 266 | 0 /*reqHeight*/, false, ROTATION, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 267 | }; |
| 268 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 269 | } |
| 270 | |
| 271 | TEST_F(CredentialsTest, CaptureLayersTest) { |
| 272 | setupBackgroundSurface(); |
| 273 | sp<GraphicBuffer> outBuffer; |
| 274 | std::function<status_t()> condition = [=]() { |
| 275 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 0e003c9 | 2018-09-17 11:09:51 -0700 | [diff] [blame] | 276 | return ScreenshotClient::captureLayers(mBGSurfaceControl->getHandle(), |
| 277 | ui::Dataspace::V0_SRGB, ui::PixelFormat::RGBA_8888, |
| 278 | Rect(), FRAME_SCALE, &outBuffer); |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 279 | }; |
| 280 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, PERMISSION_DENIED)); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * The following tests are for methods accessible directly through SurfaceFlinger. |
| 285 | */ |
| 286 | |
| 287 | /** |
| 288 | * An app can pass a buffer queue to the media server and ask the media server to decode a DRM video |
| 289 | * to that buffer queue. The media server is the buffer producer in this case. Because the app may create |
| 290 | * its own buffer queue and act as the buffer consumer, the media server wants to be careful to avoid |
| 291 | * sending decoded video frames to the app. This is where authenticateSurfaceTexture call comes in, to check |
| 292 | * the consumer of a buffer queue is SurfaceFlinger. |
| 293 | */ |
| 294 | TEST_F(CredentialsTest, AuthenticateSurfaceTextureTest) { |
| 295 | setupBackgroundSurface(); |
| 296 | sp<IGraphicBufferProducer> producer = |
| 297 | mBGSurfaceControl->getSurface()->getIGraphicBufferProducer(); |
| 298 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 299 | |
| 300 | std::function<bool()> condition = [=]() { return sf->authenticateSurfaceTexture(producer); }; |
| 301 | // Anyone should be able to check if the consumer of the buffer queue is SF. |
| 302 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges(condition, true, true)); |
| 303 | } |
| 304 | |
| 305 | TEST_F(CredentialsTest, GetLayerDebugInfo) { |
| 306 | setupBackgroundSurface(); |
| 307 | sp<ISurfaceComposer> sf(ComposerService::getComposerService()); |
| 308 | |
| 309 | // Historically, only root and shell can access the getLayerDebugInfo which |
| 310 | // is called when we call dumpsys. I don't see a reason why we should change this. |
| 311 | std::vector<LayerDebugInfo> outLayers; |
| 312 | // Check with root. |
| 313 | seteuid(AID_ROOT); |
| 314 | ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers)); |
| 315 | |
| 316 | // Check as a shell. |
| 317 | seteuid(AID_SHELL); |
| 318 | ASSERT_EQ(NO_ERROR, sf->getLayerDebugInfo(&outLayers)); |
| 319 | |
| 320 | // Check as anyone else. |
| 321 | seteuid(AID_ROOT); |
| 322 | seteuid(AID_BIN); |
| 323 | ASSERT_EQ(PERMISSION_DENIED, sf->getLayerDebugInfo(&outLayers)); |
| 324 | } |
Peiyong Lin | 4f3fddf | 2019-01-24 17:21:24 -0800 | [diff] [blame] | 325 | |
| 326 | TEST_F(CredentialsTest, IsWideColorDisplayBasicCorrectness) { |
| 327 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 328 | bool result = false; |
| 329 | status_t error = SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 330 | ASSERT_EQ(NO_ERROR, error); |
| 331 | bool hasWideColorMode = false; |
| 332 | Vector<ColorMode> colorModes; |
| 333 | SurfaceComposerClient::getDisplayColorModes(display, &colorModes); |
| 334 | for (ColorMode colorMode : colorModes) { |
| 335 | switch (colorMode) { |
| 336 | case ColorMode::DISPLAY_P3: |
| 337 | case ColorMode::ADOBE_RGB: |
| 338 | case ColorMode::DCI_P3: |
| 339 | hasWideColorMode = true; |
| 340 | break; |
| 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | ASSERT_EQ(hasWideColorMode, result); |
| 346 | } |
| 347 | |
| 348 | TEST_F(CredentialsTest, IsWideColorDisplayWithPrivileges) { |
| 349 | sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain)); |
| 350 | std::function<status_t()> condition = [=]() { |
| 351 | bool result = false; |
| 352 | return SurfaceComposerClient::isWideColorDisplay(display, &result); |
| 353 | }; |
| 354 | ASSERT_NO_FATAL_FAILURE(checkWithPrivileges<status_t>(condition, NO_ERROR, NO_ERROR)); |
| 355 | } |
| 356 | |
Ana Krulec | 13be8ad | 2018-08-21 02:43:56 +0000 | [diff] [blame] | 357 | } // namespace android |