1 /*
2 * Copyright (C) 2022 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 #define LOG_TAG "buffferpool_unit_test"
18
19 #include <gtest/gtest.h>
20
21 #include <android/binder_manager.h>
22 #include <android/binder_process.h>
23 #include <android/binder_stability.h>
24 #include <android-base/logging.h>
25 #include <bufferpool2/ClientManager.h>
26
27 #include <errno.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include <iostream>
34 #include <memory>
35 #include <vector>
36
37 #include "allocator.h"
38
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;
46
47 namespace {
48
49 const std::string testInstance = std::string() + ClientManager::descriptor + "/condtest";
50
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:
SetUp()77 virtual void SetUp() override {
78 BufferPoolStatus status;
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
TearDown()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:
description(const std::string & description)119 static void description(const std::string& description) {
120 RecordProperty("description", description);
121 }
122
123 std::shared_ptr<ClientManager> mManager;
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
sendMessage(int * pipes,const PipeMessage & message)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
receiveMessage(int * pipes,PipeMessage * message)136 bool receiveMessage(int *pipes, PipeMessage *message) {
137 int ret = read(pipes[0], message->array, sizeof(PipeMessage));
138 return ret == sizeof(PipeMessage);
139 }
140
doReceiver()141 void doReceiver() {
142 ABinderProcess_setThreadPoolMaxThreadCount(1);
143 ABinderProcess_startThreadPool();
144 PipeMessage message;
145 mManager = ClientManager::getInstance();
146 if (!mManager) {
147 message.data.command = PipeCommand::INIT_ERROR;
148 sendMessage(mResultPipeFds, message);
149 return;
150 }
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);
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;
171 BufferPoolStatus status = mManager->receive(
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.
TEST_F(BufferpoolMultiTest,TransferBuffer)209 TEST_F(BufferpoolMultiTest, TransferBuffer) {
210 BufferPoolStatus status;
211 PipeMessage message;
212
213 ASSERT_TRUE(receiveMessage(mResultPipeFds, &message));
214 ABinderProcess_setThreadPoolMaxThreadCount(1);
215 ABinderProcess_startThreadPool();
216
217
218 std::shared_ptr<IClientManager> receiver =
219 IClientManager::fromBinder(
220 ndk::SpAIBinder(AServiceManager_waitForService(testInstance.c_str())));
221 ASSERT_NE(receiver, nullptr);
222 ConnectionId receiverId;
223
224 bool isNew = true;
225 status = mManager->registerSender(receiver, mConnectionId, &receiverId, &isNew);
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
main(int argc,char ** argv)275 int main(int argc, char** argv) {
276 ::testing::InitGoogleTest(&argc, argv);
277 int status = RUN_ALL_TESTS();
278 LOG(INFO) << "Test result = " << status;
279 return status;
280 }
281