Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 1 | /* |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 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 | #define LOG_TAG "buffferpool_unit_test" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
| 23 | #include <android/binder_stability.h> |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 25 | #include <bufferpool2/ClientManager.h> |
| 26 | |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 31 | |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 32 | #include <iostream> |
| 33 | #include <memory> |
| 34 | #include <vector> |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 35 | |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 36 | #include "allocator.h" |
| 37 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 38 | using aidl::android::hardware::media::bufferpool2::IClientManager; |
| 39 | using aidl::android::hardware::media::bufferpool2::ResultStatus; |
| 40 | using aidl::android::hardware::media::bufferpool2::implementation::BufferId; |
| 41 | using aidl::android::hardware::media::bufferpool2::implementation::ClientManager; |
| 42 | using aidl::android::hardware::media::bufferpool2::implementation::ConnectionId; |
| 43 | using aidl::android::hardware::media::bufferpool2::implementation::TransactionId; |
| 44 | using aidl::android::hardware::media::bufferpool2::BufferPoolData; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 48 | const std::string testInstance = std::string() + ClientManager::descriptor + "/multitest"; |
| 49 | |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 50 | // communication message types between processes. |
| 51 | enum PipeCommand : int32_t { |
| 52 | INIT_OK = 0, |
| 53 | INIT_ERROR, |
| 54 | SEND, |
| 55 | RECEIVE_OK, |
| 56 | RECEIVE_ERROR, |
| 57 | }; |
| 58 | |
| 59 | // communication message between processes. |
| 60 | union PipeMessage { |
| 61 | struct { |
| 62 | int32_t command; |
| 63 | BufferId bufferId; |
| 64 | ConnectionId connectionId; |
| 65 | TransactionId transactionId; |
| 66 | int64_t timestampUs; |
| 67 | } data; |
| 68 | char array[0]; |
| 69 | }; |
| 70 | |
| 71 | // media.bufferpool test setup |
| 72 | class BufferpoolMultiTest : public ::testing::Test { |
| 73 | public: |
| 74 | virtual void SetUp() override { |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 75 | BufferPoolStatus status; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 76 | mReceiverPid = -1; |
| 77 | mConnectionValid = false; |
| 78 | |
| 79 | ASSERT_TRUE(pipe(mCommandPipeFds) == 0); |
| 80 | ASSERT_TRUE(pipe(mResultPipeFds) == 0); |
| 81 | |
| 82 | mReceiverPid = fork(); |
| 83 | ASSERT_TRUE(mReceiverPid >= 0); |
| 84 | |
| 85 | if (mReceiverPid == 0) { |
| 86 | doReceiver(); |
| 87 | // In order to ignore gtest behaviour, wait for being killed from |
| 88 | // tearDown |
| 89 | pause(); |
| 90 | } |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 91 | mManager = ClientManager::getInstance(); |
| 92 | ASSERT_NE(mManager, nullptr); |
| 93 | |
| 94 | mAllocator = std::make_shared<TestBufferPoolAllocator>(); |
| 95 | ASSERT_TRUE((bool)mAllocator); |
| 96 | |
| 97 | status = mManager->create(mAllocator, &mConnectionId); |
| 98 | ASSERT_TRUE(status == ResultStatus::OK); |
| 99 | mConnectionValid = true; |
| 100 | } |
| 101 | |
| 102 | virtual void TearDown() override { |
| 103 | if (mReceiverPid > 0) { |
| 104 | kill(mReceiverPid, SIGKILL); |
| 105 | int wstatus; |
| 106 | wait(&wstatus); |
| 107 | } |
| 108 | |
| 109 | if (mConnectionValid) { |
| 110 | mManager->close(mConnectionId); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | protected: |
| 115 | static void description(const std::string& description) { |
| 116 | RecordProperty("description", description); |
| 117 | } |
| 118 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 119 | std::shared_ptr<ClientManager> mManager; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 120 | std::shared_ptr<BufferPoolAllocator> mAllocator; |
| 121 | bool mConnectionValid; |
| 122 | ConnectionId mConnectionId; |
| 123 | pid_t mReceiverPid; |
| 124 | int mCommandPipeFds[2]; |
| 125 | int mResultPipeFds[2]; |
| 126 | |
| 127 | bool sendMessage(int *pipes, const PipeMessage &message) { |
| 128 | int ret = write(pipes[1], message.array, sizeof(PipeMessage)); |
| 129 | return ret == sizeof(PipeMessage); |
| 130 | } |
| 131 | |
| 132 | bool receiveMessage(int *pipes, PipeMessage *message) { |
| 133 | int ret = read(pipes[0], message->array, sizeof(PipeMessage)); |
| 134 | return ret == sizeof(PipeMessage); |
| 135 | } |
| 136 | |
| 137 | void doReceiver() { |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 138 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 139 | ABinderProcess_startThreadPool(); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 140 | PipeMessage message; |
| 141 | mManager = ClientManager::getInstance(); |
| 142 | if (!mManager) { |
| 143 | message.data.command = PipeCommand::INIT_ERROR; |
| 144 | sendMessage(mResultPipeFds, message); |
| 145 | return; |
| 146 | } |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 147 | auto binder = mManager->asBinder(); |
| 148 | AIBinder_forceDowngradeToSystemStability(binder.get()); |
| 149 | binder_status_t status = |
| 150 | AServiceManager_addService(binder.get(), testInstance.c_str()); |
| 151 | CHECK_EQ(status, STATUS_OK); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 152 | if (status != android::OK) { |
| 153 | message.data.command = PipeCommand::INIT_ERROR; |
| 154 | sendMessage(mResultPipeFds, message); |
| 155 | return; |
| 156 | } |
| 157 | message.data.command = PipeCommand::INIT_OK; |
| 158 | sendMessage(mResultPipeFds, message); |
| 159 | |
| 160 | receiveMessage(mCommandPipeFds, &message); |
| 161 | { |
| 162 | native_handle_t *rhandle = nullptr; |
| 163 | std::shared_ptr<BufferPoolData> rbuffer; |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 164 | BufferPoolStatus status = mManager->receive( |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 165 | message.data.connectionId, message.data.transactionId, |
| 166 | message.data.bufferId, message.data.timestampUs, &rhandle, &rbuffer); |
| 167 | mManager->close(message.data.connectionId); |
| 168 | if (status != ResultStatus::OK) { |
| 169 | message.data.command = PipeCommand::RECEIVE_ERROR; |
| 170 | sendMessage(mResultPipeFds, message); |
| 171 | return; |
| 172 | } |
| 173 | if (!TestBufferPoolAllocator::Verify(rhandle, 0x77)) { |
| 174 | message.data.command = PipeCommand::RECEIVE_ERROR; |
| 175 | sendMessage(mResultPipeFds, message); |
| 176 | return; |
| 177 | } |
| 178 | if (rhandle) { |
| 179 | native_handle_close(rhandle); |
| 180 | native_handle_delete(rhandle); |
| 181 | } |
| 182 | } |
| 183 | message.data.command = PipeCommand::RECEIVE_OK; |
| 184 | sendMessage(mResultPipeFds, message); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | // Buffer transfer test between processes. |
| 189 | TEST_F(BufferpoolMultiTest, TransferBuffer) { |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 190 | BufferPoolStatus status; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 191 | PipeMessage message; |
| 192 | |
| 193 | ASSERT_TRUE(receiveMessage(mResultPipeFds, &message)); |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 194 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 195 | ABinderProcess_startThreadPool(); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 196 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 197 | std::shared_ptr<IClientManager> receiver = IClientManager::fromBinder(ndk::SpAIBinder( |
| 198 | AServiceManager_waitForService(testInstance.c_str()))); |
| 199 | ASSERT_NE(receiver, nullptr); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 200 | ConnectionId receiverId; |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 201 | |
Sungtak Lee | 76937c6 | 2022-12-07 11:42:03 +0000 | [diff] [blame] | 202 | bool isNew = true; |
| 203 | status = mManager->registerSender(receiver, mConnectionId, &receiverId, &isNew); |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 204 | ASSERT_TRUE(status == ResultStatus::OK); |
| 205 | { |
| 206 | native_handle_t *shandle = nullptr; |
| 207 | std::shared_ptr<BufferPoolData> sbuffer; |
| 208 | TransactionId transactionId; |
| 209 | int64_t postUs; |
| 210 | std::vector<uint8_t> vecParams; |
| 211 | |
| 212 | getTestAllocatorParams(&vecParams); |
| 213 | status = mManager->allocate(mConnectionId, vecParams, &shandle, &sbuffer); |
| 214 | ASSERT_TRUE(status == ResultStatus::OK); |
| 215 | |
| 216 | ASSERT_TRUE(TestBufferPoolAllocator::Fill(shandle, 0x77)); |
| 217 | if (shandle) { |
| 218 | native_handle_close(shandle); |
| 219 | native_handle_delete(shandle); |
| 220 | } |
| 221 | |
| 222 | status = mManager->postSend(receiverId, sbuffer, &transactionId, &postUs); |
| 223 | ASSERT_TRUE(status == ResultStatus::OK); |
| 224 | |
| 225 | message.data.command = PipeCommand::SEND; |
| 226 | message.data.bufferId = sbuffer->mId; |
| 227 | message.data.connectionId = receiverId; |
| 228 | message.data.transactionId = transactionId; |
| 229 | message.data.timestampUs = postUs; |
| 230 | sendMessage(mCommandPipeFds, message); |
| 231 | } |
| 232 | EXPECT_TRUE(receiveMessage(mResultPipeFds, &message)); |
| 233 | EXPECT_TRUE(message.data.command == PipeCommand::RECEIVE_OK); |
| 234 | } |
| 235 | |
| 236 | } // anonymous namespace |
| 237 | |
| 238 | int main(int argc, char** argv) { |
Sungtak Lee | 8fc3ca4 | 2022-12-07 07:45:45 +0000 | [diff] [blame] | 239 | ::testing::InitGoogleTest(&argc, argv); |
| 240 | int status = RUN_ALL_TESTS(); |
| 241 | LOG(INFO) << "Test result = " << status; |
| 242 | return status; |
| 243 | } |