1 /*
2 * Copyright (C) 2018 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 <bufferpool/ClientManager.h>
24 #include <hidl/HidlSupport.h>
25 #include <hidl/HidlTransportSupport.h>
26 #include <hidl/LegacySupport.h>
27 #include <hidl/Status.h>
28 #include <unistd.h>
29 #include <iostream>
30 #include <memory>
31 #include <vector>
32 #include "allocator.h"
33
34 using android::hardware::hidl_handle;
35 using android::hardware::media::bufferpool::V1_0::ResultStatus;
36 using android::hardware::media::bufferpool::V1_0::implementation::BufferId;
37 using android::hardware::media::bufferpool::V1_0::implementation::ClientManager;
38 using android::hardware::media::bufferpool::V1_0::implementation::ConnectionId;
39 using android::hardware::media::bufferpool::V1_0::implementation::TransactionId;
40 using android::hardware::media::bufferpool::BufferPoolData;
41
42 namespace {
43
44 // Number of iteration for buffer allocation test.
45 constexpr static int kNumAllocationTest = 3;
46
47 // Number of iteration for buffer recycling test.
48 constexpr static int kNumRecycleTest = 3;
49
50 // media.bufferpool test setup
51 class BufferpoolSingleTest : public ::testing::Test {
52 public:
SetUp()53 virtual void SetUp() override {
54 ResultStatus status;
55 mConnectionValid = false;
56
57 mManager = ClientManager::getInstance();
58 ASSERT_NE(mManager, nullptr);
59
60 mAllocator = std::make_shared<TestBufferPoolAllocator>();
61 ASSERT_TRUE((bool)mAllocator);
62
63 status = mManager->create(mAllocator, &mConnectionId);
64 ASSERT_TRUE(status == ResultStatus::OK);
65
66 mConnectionValid = true;
67
68 status = mManager->registerSender(mManager, mConnectionId, &mReceiverId);
69 ASSERT_TRUE(status == ResultStatus::ALREADY_EXISTS &&
70 mReceiverId == mConnectionId);
71 }
72
TearDown()73 virtual void TearDown() override {
74 if (mConnectionValid) {
75 mManager->close(mConnectionId);
76 }
77 }
78
79 protected:
description(const std::string & description)80 static void description(const std::string& description) {
81 RecordProperty("description", description);
82 }
83
84 android::sp<ClientManager> mManager;
85 std::shared_ptr<BufferPoolAllocator> mAllocator;
86 bool mConnectionValid;
87 ConnectionId mConnectionId;
88 ConnectionId mReceiverId;
89
90 };
91
92 // Buffer allocation test.
93 // Check whether each buffer allocation is done successfully with
94 // unique buffer id.
TEST_F(BufferpoolSingleTest,AllocateBuffer)95 TEST_F(BufferpoolSingleTest, AllocateBuffer) {
96 ResultStatus status;
97 std::vector<uint8_t> vecParams;
98 getTestAllocatorParams(&vecParams);
99
100 std::shared_ptr<BufferPoolData> buffer[kNumAllocationTest];
101 native_handle_t *allocHandle = nullptr;
102 for (int i = 0; i < kNumAllocationTest; ++i) {
103 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &buffer[i]);
104 ASSERT_TRUE(status == ResultStatus::OK);
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 ResultStatus 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 }
129 for (int i = 1; i < kNumRecycleTest; ++i) {
130 ASSERT_TRUE(bid[i - 1] == bid[i]);
131 }
132 EXPECT_TRUE(kNumRecycleTest > 1);
133 }
134
135 // Buffer transfer test.
136 // Check whether buffer is transferred to another client successfully.
TEST_F(BufferpoolSingleTest,TransferBuffer)137 TEST_F(BufferpoolSingleTest, TransferBuffer) {
138 ResultStatus status;
139 std::vector<uint8_t> vecParams;
140 getTestAllocatorParams(&vecParams);
141 std::shared_ptr<BufferPoolData> sbuffer, rbuffer;
142 native_handle_t *allocHandle = nullptr;
143 native_handle_t *recvHandle = nullptr;
144
145 TransactionId transactionId;
146 int64_t postUs;
147
148 status = mManager->allocate(mConnectionId, vecParams, &allocHandle, &sbuffer);
149 ASSERT_TRUE(status == ResultStatus::OK);
150 ASSERT_TRUE(TestBufferPoolAllocator::Fill(allocHandle, 0x77));
151 status = mManager->postSend(mReceiverId, sbuffer, &transactionId, &postUs);
152 ASSERT_TRUE(status == ResultStatus::OK);
153 status = mManager->receive(mReceiverId, transactionId, sbuffer->mId, postUs,
154 &recvHandle, &rbuffer);
155 EXPECT_TRUE(status == ResultStatus::OK);
156 ASSERT_TRUE(TestBufferPoolAllocator::Verify(recvHandle, 0x77));
157 }
158
159 } // anonymous namespace
160
main(int argc,char ** argv)161 int main(int argc, char** argv) {
162 ::testing::InitGoogleTest(&argc, argv);
163 int status = RUN_ALL_TESTS();
164 LOG(INFO) << "Test result = " << status;
165 return status;
166 }
167