1 /*
2  * Copyright (C) 2019 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 
21 #include <binder/Binder.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include <gui/ISurfaceComposer.h>
26 #include <gui/SurfaceComposerClient.h>
27 #include <private/gui/ComposerService.h>
28 #include <ui/Rect.h>
29 #include "utils/ScreenshotUtils.h"
30 
31 namespace android {
32 namespace {
33 
34 class NotALayer : public BBinder {};
35 
36 /**
37  * For all of these tests we make a SurfaceControl with an invalid layer handle
38  * and verify we aren't able to trick SurfaceFlinger.
39  */
40 class InvalidHandleTest : public ::testing::Test {
41 protected:
42     sp<SurfaceComposerClient> mScc;
43     sp<SurfaceControl> mNotSc;
SetUp()44     void SetUp() override {
45         mScc = sp<SurfaceComposerClient>::make();
46         ASSERT_EQ(NO_ERROR, mScc->initCheck());
47         mNotSc = makeNotSurfaceControl();
48     }
49 
makeNotSurfaceControl()50     sp<SurfaceControl> makeNotSurfaceControl() {
51         return sp<SurfaceControl>::make(mScc, sp<NotALayer>::make(), 1, "#1");
52     }
53 };
54 
TEST_F(InvalidHandleTest,captureLayersInvalidHandle)55 TEST_F(InvalidHandleTest, captureLayersInvalidHandle) {
56     LayerCaptureArgs args;
57     args.layerHandle = mNotSc->getHandle();
58 
59     ScreenCaptureResults captureResults;
60     ASSERT_EQ(NAME_NOT_FOUND, ScreenCapture::captureLayers(args, captureResults));
61 }
62 
63 } // namespace
64 } // namespace android
65 
66 // TODO(b/129481165): remove the #pragma below and fix conversion issues
67 #pragma clang diagnostic pop // ignored "-Wconversion"
68