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 <signal.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 #include <iostream>
33 #include <memory>
34 #include <vector>
35
36 #include "allocator.h"
37
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;
45
46 namespace {
47
48 const std::string testInstance = std::string() + ClientManager::descriptor + "/multitest";
49
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:
SetUp()74 virtual void SetUp() override {
75 BufferPoolStatus status;
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 }
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
TearDown()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:
description(const std::string & description)115 static void description(const std::string& description) {
116 RecordProperty("description", description);
117 }
118
119 std::shared_ptr<ClientManager> mManager;
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
sendMessage(int * pipes,const PipeMessage & message)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
receiveMessage(int * pipes,PipeMessage * message)132 bool receiveMessage(int *pipes, PipeMessage *message) {
133 int ret = read(pipes[0], message->array, sizeof(PipeMessage));
134 return ret == sizeof(PipeMessage);
135 }
136
doReceiver()137 void doReceiver() {
138 ABinderProcess_setThreadPoolMaxThreadCount(1);
139 ABinderProcess_startThreadPool();
140 PipeMessage message;
141 mManager = ClientManager::getInstance();
142 if (!mManager) {
143 message.data.command = PipeCommand::INIT_ERROR;
144 sendMessage(mResultPipeFds, message);
145 return;
146 }
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);
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;
164 BufferPoolStatus status = mManager->receive(
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.
TEST_F(BufferpoolMultiTest,TransferBuffer)189 TEST_F(BufferpoolMultiTest, TransferBuffer) {
190 BufferPoolStatus status;
191 PipeMessage message;
192
193 ASSERT_TRUE(receiveMessage(mResultPipeFds, &message));
194 ABinderProcess_setThreadPoolMaxThreadCount(1);
195 ABinderProcess_startThreadPool();
196
197 std::shared_ptr<IClientManager> receiver = IClientManager::fromBinder(ndk::SpAIBinder(
198 AServiceManager_waitForService(testInstance.c_str())));
199 ASSERT_NE(receiver, nullptr);
200 ConnectionId receiverId;
201
202 bool isNew = true;
203 status = mManager->registerSender(receiver, mConnectionId, &receiverId, &isNew);
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
main(int argc,char ** argv)238 int main(int argc, char** argv) {
239 ::testing::InitGoogleTest(&argc, argv);
240 int status = RUN_ALL_TESTS();
241 LOG(INFO) << "Test result = " << status;
242 return status;
243 }
244