| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <binder/Binder.h> | |
| #include <gtest/gtest.h> | |
| #include <gui/LayerState.h> | |
| #include <gui/OoprTransaction.h> | |
| #include <ui/GraphicBuffer.h> | |
| namespace android { | |
| namespace { | |
| gui::OoprTransaction makeNonEmptyTransaction() { | |
| gui::OoprTransaction transaction; | |
| transaction.renderResourceToken = sp<BBinder>::make(); | |
| transaction.bitmapBindings.push_back( | |
| gui::OoprTransaction::HardwareBitmapBinding{/*id=*/42u, | |
| /*buffer=*/sp<GraphicBuffer>::make()}); | |
| return transaction; | |
| } | |
| } // namespace | |
| TEST(OoprTransactionTest, SanitizeStripsTransactionWithoutPermission) { | |
| gui::OoprTransaction transaction = makeNonEmptyTransaction(); | |
| ASSERT_FALSE(transaction.empty()); | |
| transaction.sanitize(0 /* permissions */); | |
| EXPECT_TRUE(transaction.empty()); | |
| EXPECT_EQ(nullptr, transaction.renderResourceToken); | |
| EXPECT_TRUE(transaction.bitmapBindings.empty()); | |
| } | |
| TEST(OoprTransactionTest, SanitizeStripsTransactionWithUnrelatedPermission) { | |
| gui::OoprTransaction transaction = makeNonEmptyTransaction(); | |
| // A caller holding other SurfaceFlinger permissions but not OOPR is still denied. | |
| transaction.sanitize(layer_state_t::Permission::ACCESS_SURFACE_FLINGER | | |
| layer_state_t::Permission::ROTATE_SURFACE_FLINGER | | |
| layer_state_t::Permission::READ_FRAME_BUFFER); | |
| EXPECT_TRUE(transaction.empty()); | |
| } | |
| TEST(OoprTransactionTest, SanitizePreservesTransactionWithPermission) { | |
| gui::OoprTransaction transaction = makeNonEmptyTransaction(); | |
| const sp<IBinder> token = transaction.renderResourceToken; | |
| transaction.sanitize(layer_state_t::Permission::ACCESS_OUT_OF_PROCESS_RENDERING); | |
| ASSERT_FALSE(transaction.empty()); | |
| EXPECT_EQ(token, transaction.renderResourceToken); | |
| ASSERT_EQ(1u, transaction.bitmapBindings.size()); | |
| EXPECT_EQ(42u, transaction.bitmapBindings[0].id); | |
| } | |
| TEST(OoprTransactionTest, SanitizeEmptyTransactionIsNoop) { | |
| gui::OoprTransaction transaction; | |
| ASSERT_TRUE(transaction.empty()); | |
| transaction.sanitize(0 /* permissions */); | |
| EXPECT_TRUE(transaction.empty()); | |
| transaction.sanitize(layer_state_t::Permission::ACCESS_OUT_OF_PROCESS_RENDERING); | |
| EXPECT_TRUE(transaction.empty()); | |
| } | |
| } // namespace android |