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