blob: 6d469ce02c2935c7d27e0488f0ce86bcfb86c4ca [file] [log] [blame]
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00001/*
Sungtak Lee76937c62022-12-07 11:42:03 +00002 * Copyright (C) 2022 The Android Open Source Project
Sungtak Lee8fc3ca42022-12-07 07:45:45 +00003 *
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 Lee76937c62022-12-07 11:42:03 +000021#include <android/binder_manager.h>
22#include <android/binder_process.h>
23#include <android/binder_stability.h>
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000024#include <android-base/logging.h>
Sungtak Lee76937c62022-12-07 11:42:03 +000025#include <bufferpool2/ClientManager.h>
26
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000027#include <errno.h>
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000028#include <signal.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
Sungtak Lee76937c62022-12-07 11:42:03 +000032
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000033#include <iostream>
34#include <memory>
35#include <vector>
Sungtak Lee76937c62022-12-07 11:42:03 +000036
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000037#include "allocator.h"
38
Sungtak Lee76937c62022-12-07 11:42:03 +000039using aidl::android::hardware::media::bufferpool2::IClientManager;
40using aidl::android::hardware::media::bufferpool2::ResultStatus;
41using aidl::android::hardware::media::bufferpool2::implementation::BufferId;
42using aidl::android::hardware::media::bufferpool2::implementation::ClientManager;
43using aidl::android::hardware::media::bufferpool2::implementation::ConnectionId;
44using aidl::android::hardware::media::bufferpool2::implementation::TransactionId;
45using aidl::android::hardware::media::bufferpool2::BufferPoolData;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000046
47namespace {
48
Sungtak Lee76937c62022-12-07 11:42:03 +000049const std::string testInstance = std::string() + ClientManager::descriptor + "/condtest";
50
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000051// communication message types between processes.
52enum 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.
61union 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
72constexpr int kSignalInt = 200;
73
74// media.bufferpool test setup
75class BufferpoolMultiTest : public ::testing::Test {
76 public:
77 virtual void SetUp() override {
Sungtak Lee76937c62022-12-07 11:42:03 +000078 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +000079 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 Lee76937c62022-12-07 11:42:03 +0000123 std::shared_ptr<ClientManager> mManager;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000124 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 Lee76937c62022-12-07 11:42:03 +0000142 ABinderProcess_setThreadPoolMaxThreadCount(1);
143 ABinderProcess_startThreadPool();
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000144 PipeMessage message;
145 mManager = ClientManager::getInstance();
146 if (!mManager) {
147 message.data.command = PipeCommand::INIT_ERROR;
148 sendMessage(mResultPipeFds, message);
149 return;
150 }
Sungtak Lee76937c62022-12-07 11:42:03 +0000151 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 Lee8fc3ca42022-12-07 07:45:45 +0000156 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 Lee76937c62022-12-07 11:42:03 +0000171 BufferPoolStatus status = mManager->receive(
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000172 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.
209TEST_F(BufferpoolMultiTest, TransferBuffer) {
Sungtak Lee76937c62022-12-07 11:42:03 +0000210 BufferPoolStatus status;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000211 PipeMessage message;
212
213 ASSERT_TRUE(receiveMessage(mResultPipeFds, &message));
Sungtak Lee76937c62022-12-07 11:42:03 +0000214 ABinderProcess_setThreadPoolMaxThreadCount(1);
215 ABinderProcess_startThreadPool();
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000216
Sungtak Lee76937c62022-12-07 11:42:03 +0000217
218 std::shared_ptr<IClientManager> receiver =
219 IClientManager::fromBinder(
220 ndk::SpAIBinder(AServiceManager_waitForService(testInstance.c_str())));
221 ASSERT_NE(receiver, nullptr);
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000222 ConnectionId receiverId;
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000223
Sungtak Lee76937c62022-12-07 11:42:03 +0000224 bool isNew = true;
225 status = mManager->registerSender(receiver, mConnectionId, &receiverId, &isNew);
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000226 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
275int main(int argc, char** argv) {
Sungtak Lee8fc3ca42022-12-07 07:45:45 +0000276 ::testing::InitGoogleTest(&argc, argv);
277 int status = RUN_ALL_TESTS();
278 LOG(INFO) << "Test result = " << status;
279 return status;
280}