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-base/logging.h>
22 #include <binder/ProcessState.h>
23 #include <bufferpool2/ClientManager.h>
24 #include <unistd.h>
25 #include <iostream>
26 #include <memory>
27 #include <vector>
28 #include "allocator.h"
29
30 using aidl::android::hardware::media::bufferpool2::implementation::BufferId;
31 using aidl::android::hardware::media::bufferpool2::implementation::BufferPoolStatus;
32 using aidl::android::hardware::media::bufferpool2::implementation::ClientManager;
33 using aidl::android::hardware::media::bufferpool2::implementation::ConnectionId;
34 using aidl::android::hardware::media::bufferpool2::implementation::TransactionId;
35 using aidl::android::hardware::media::bufferpool2::BufferPoolData;
36
37 namespace {
38
39 // Number of iteration for buffer allocation test.
40 constexpr static int kNumAllocationTest = 3;
41
42 // Number of iteration for buffer recycling test.
43 constexpr static int kNumRecycleTest = 3;
44
45 // media.bufferpool test setup
46 class BufferpoolSingleTest : public ::testing::Test {
47 public:
SetUp()48 virtual void SetUp() override {
49 BufferPoolStatus status;
50 mConnectionValid = false;
51
52 mManager = ClientManager::getInstance();
53 ASSERT_NE(mManager, nullptr);
54
55 mAllocator = std::make_shared<TestBufferPoolAllocator>();
56 ASSERT_TRUE((bool)mAllocator);
57
58 status = mManager->create(mAllocator, &mConnectionId);
59 ASSERT_TRUE(status == ResultStatus::OK);
60
61 mConnectionValid = true;
62
63 bool isNew = true;
64 status = mManager->registerSender(mManager, mConnectionId, &mReceiverId, &isNew);
65 ASSERT_TRUE(status == ResultStatus::OK && isNew == false &&
66 mReceiverId == mConnectionId);
67 }
68
TearDown()69 virtual void TearDown() override {
70 if (mConnectionValid) {
71 mManager->close(mConnectionId);
72 }
73 }
74
75 protected:
description(const std::string & description)76 static void description(const std::string& description) {
77 RecordProperty("description", description);
78 }
79
80 std::shared_ptr<ClientManager> mManager;
81 std::shared_ptr<BufferPoolAllocator> mAllocator;
82 bool mConnectionValid;
83 ConnectionId mConnectionId;
84 ConnectionId mReceiverId;
85
86 };
87
88 // Buffer allocation test.
89 // Check whether each buffer allocation is done successfully with
90 // unique buffer id.
TEST_F(BufferpoolSingleTest,AllocateBuffer)91 TEST_F(BufferpoolSingleTest, AllocateBuffer) {
92 BufferPoolStatus status;
93 std::vector<uint8_t> vecParams;
94 getTestAllocatorParams(&vecParams);
95
96 std::shared_ptr<BufferPoolData> buffer[kNumAllocationTest];
97 native_handle_t *allocHandle = nullptr;
98 for (int i = 0; i < kNumAllocationTest; ++i) {
99 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer[i]);
100 ASSERT_TRUE(status == ResultStatus::OK);
101 if (allocHandle) {
102 native_handle_close(allocHandle);
103 native_handle_delete(allocHandle);
104 }
105 }
106 for (int i = 0; i < kNumAllocationTest; ++i) {
107 for (int j = i + 1; j < kNumAllocationTest; ++j) {
108 ASSERT_TRUE(buffer[i]->mId != buffer[j]->mId);
109 }
110 }
111 EXPECT_TRUE(kNumAllocationTest > 1);
112 }
113
114 // Buffer recycle test.
115 // Check whether de-allocated buffers are recycled.
TEST_F(BufferpoolSingleTest,RecycleBuffer)116 TEST_F(BufferpoolSingleTest, RecycleBuffer) {
117 BufferPoolStatus status;
118 std::vector<uint8_t> vecParams;
119 getTestAllocatorParams(&vecParams);
120
121 BufferId bid[kNumRecycleTest];
122 for (int i = 0; i < kNumRecycleTest; ++i) {
123 std::shared_ptr<BufferPoolData> buffer;
124 native_handle_t *allocHandle = nullptr;
125 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer);
126 ASSERT_TRUE(status == ResultStatus::OK);
127 bid[i] = buffer->mId;
128 if (allocHandle) {
129 native_handle_close(allocHandle);
130 native_handle_delete(allocHandle);
131 }
132 }
133 for (int i = 1; i < kNumRecycleTest; ++i) {
134 ASSERT_TRUE(bid[i - 1] == bid[i]);
135 }
136 EXPECT_TRUE(kNumRecycleTest > 1);
137 }
138
139 // Buffer transfer test.
140 // Check whether buffer is transferred to another client successfully.
TEST_F(BufferpoolSingleTest,TransferBuffer)141 TEST_F(BufferpoolSingleTest, TransferBuffer) {
142 BufferPoolStatus status;
143 std::vector<uint8_t> vecParams;
144 getTestAllocatorParams(&vecParams);
145 std::shared_ptr<BufferPoolData> sbuffer, rbuffer;
146 native_handle_t *allocHandle = nullptr;
147 native_handle_t *recvHandle = nullptr;
148
149 TransactionId transactionId;
150 int64_t postMs;
151
152 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &sbuffer);
153 ASSERT_TRUE(status == ResultStatus::OK);
154 ASSERT_TRUE(TestBufferPoolAllocator::Fill(allocHandle, 0x77));
155 status = mManager->postSend(mReceiverId, sbuffer, &transactionId, &postMs);
156 ASSERT_TRUE(status == ResultStatus::OK);
157 status = mManager->receive(mReceiverId, transactionId, sbuffer->mId, postMs,
158 &recvHandle, &rbuffer);
159 EXPECT_TRUE(status == ResultStatus::OK);
160 ASSERT_TRUE(TestBufferPoolAllocator::Verify(recvHandle, 0x77));
161
162 if (allocHandle) {
163 native_handle_close(allocHandle);
164 native_handle_delete(allocHandle);
165 }
166 if (recvHandle) {
167 native_handle_close(recvHandle);
168 native_handle_delete(recvHandle);
169 }
170 }
171
172 } // anonymous namespace
173
main(int argc,char ** argv)174 int main(int argc, char** argv) {
175 ::testing::InitGoogleTest(&argc, argv);
176 int status = RUN_ALL_TESTS();
177 LOG(INFO) << "Test result = " << status;
178 return status;
179 }
180