Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 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 <signal.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
| 32 | #include <iostream> |
| 33 | #include <memory> |
| 34 | #include <vector> |
| 35 | #include "allocator.h" |
| 36 | |
| 37 | using android::hardware::configureRpcThreadpool; |
| 38 | using android::hardware::hidl_handle; |
| 39 | using android::hardware::media::bufferpool::V2_0::IClientManager; |
| 40 | using android::hardware::media::bufferpool::V2_0::ResultStatus; |
| 41 | using android::hardware::media::bufferpool::V2_0::implementation::BufferId; |
| 42 | using android::hardware::media::bufferpool::V2_0::implementation::ClientManager; |
| 43 | using android::hardware::media::bufferpool::V2_0::implementation::ConnectionId; |
| 44 | using android::hardware::media::bufferpool::V2_0::implementation::TransactionId; |
| 45 | using android::hardware::media::bufferpool::BufferPoolData; |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | // communication message types between processes. |
| 50 | enum PipeCommand : int32_t { |
| 51 | INIT_OK = 0, |
| 52 | INIT_ERROR, |
| 53 | SEND, |
| 54 | RECEIVE_OK, |
| 55 | RECEIVE_ERROR, |
| 56 | }; |
| 57 | |
| 58 | // communication message between processes. |
| 59 | union PipeMessage { |
| 60 | struct { |
| 61 | int32_t command; |
| 62 | BufferId bufferId; |
| 63 | ConnectionId connectionId; |
| 64 | TransactionId transactionId; |
| 65 | int64_t timestampUs; |
| 66 | } data; |
| 67 | char array[0]; |
| 68 | }; |
| 69 | |
| 70 | // media.bufferpool test setup |
| 71 | class BufferpoolMultiTest : public ::testing::Test { |
| 72 | public: |
| 73 | virtual void SetUp() override { |
| 74 | ResultStatus status; |
| 75 | mReceiverPid = -1; |
| 76 | mConnectionValid = false; |
| 77 | |
| 78 | ASSERT_TRUE(pipe(mCommandPipeFds) == 0); |
| 79 | ASSERT_TRUE(pipe(mResultPipeFds) == 0); |
| 80 | |
| 81 | mReceiverPid = fork(); |
| 82 | ASSERT_TRUE(mReceiverPid >= 0); |
| 83 | |
| 84 | if (mReceiverPid == 0) { |
| 85 | doReceiver(); |
| 86 | // In order to ignore gtest behaviour, wait for being killed from |
| 87 | // tearDown |
| 88 | pause(); |
| 89 | } |
| 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 | |
| 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: |
| 115 | static void description(const std::string& description) { |
| 116 | RecordProperty("description", description); |
| 117 | } |
| 118 | |
| 119 | android::sp<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 | |
| 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 | |
| 132 | bool receiveMessage(int *pipes, PipeMessage *message) { |
| 133 | int ret = read(pipes[0], message->array, sizeof(PipeMessage)); |
| 134 | return ret == sizeof(PipeMessage); |
| 135 | } |
| 136 | |
| 137 | void doReceiver() { |
| 138 | configureRpcThreadpool(1, false); |
| 139 | PipeMessage message; |
| 140 | mManager = ClientManager::getInstance(); |
| 141 | if (!mManager) { |
| 142 | message.data.command = PipeCommand::INIT_ERROR; |
| 143 | sendMessage(mResultPipeFds, message); |
| 144 | return; |
| 145 | } |
| 146 | android::status_t status = mManager->registerAsService(); |
| 147 | if (status != android::OK) { |
| 148 | message.data.command = PipeCommand::INIT_ERROR; |
| 149 | sendMessage(mResultPipeFds, message); |
| 150 | return; |
| 151 | } |
| 152 | message.data.command = PipeCommand::INIT_OK; |
| 153 | sendMessage(mResultPipeFds, message); |
| 154 | |
| 155 | receiveMessage(mCommandPipeFds, &message); |
| 156 | { |
| 157 | native_handle_t *rhandle = nullptr; |
| 158 | std::shared_ptr<BufferPoolData> rbuffer; |
| 159 | ResultStatus status = mManager->receive( |
| 160 | message.data.connectionId, message.data.transactionId, |
| 161 | message.data.bufferId, message.data.timestampUs, &rhandle, &rbuffer); |
| 162 | mManager->close(message.data.connectionId); |
| 163 | if (status != ResultStatus::OK) { |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 164 | message.data.command = PipeCommand::RECEIVE_ERROR; |
| 165 | sendMessage(mResultPipeFds, message); |
| 166 | return; |
| 167 | } |
| 168 | if (!TestBufferPoolAllocator::Verify(rhandle, 0x77)) { |
| 169 | message.data.command = PipeCommand::RECEIVE_ERROR; |
| 170 | sendMessage(mResultPipeFds, message); |
| 171 | return; |
| 172 | } |
| 173 | if (rhandle) { |
| 174 | native_handle_close(rhandle); |
| 175 | native_handle_delete(rhandle); |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | message.data.command = PipeCommand::RECEIVE_OK; |
| 179 | sendMessage(mResultPipeFds, message); |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | // Buffer transfer test between processes. |
| 184 | TEST_F(BufferpoolMultiTest, TransferBuffer) { |
| 185 | ResultStatus status; |
| 186 | PipeMessage message; |
| 187 | |
| 188 | ASSERT_TRUE(receiveMessage(mResultPipeFds, &message)); |
| 189 | |
| 190 | android::sp<IClientManager> receiver = IClientManager::getService(); |
| 191 | ConnectionId receiverId; |
| 192 | ASSERT_TRUE((bool)receiver); |
| 193 | |
| 194 | status = mManager->registerSender(receiver, mConnectionId, &receiverId); |
| 195 | ASSERT_TRUE(status == ResultStatus::OK); |
| 196 | { |
| 197 | native_handle_t *shandle = nullptr; |
| 198 | std::shared_ptr<BufferPoolData> sbuffer; |
| 199 | TransactionId transactionId; |
| 200 | int64_t postUs; |
| 201 | std::vector<uint8_t> vecParams; |
| 202 | |
| 203 | getTestAllocatorParams(&vecParams); |
| 204 | status = mManager->allocate(mConnectionId, vecParams, &shandle, &sbuffer); |
| 205 | ASSERT_TRUE(status == ResultStatus::OK); |
| 206 | |
Sungtak Lee | c34b9b7 | 2018-09-06 14:48:21 -0700 | [diff] [blame] | 207 | ASSERT_TRUE(TestBufferPoolAllocator::Fill(shandle, 0x77)); |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 208 | if (shandle) { |
| 209 | native_handle_close(shandle); |
| 210 | native_handle_delete(shandle); |
| 211 | } |
Sungtak Lee | c34b9b7 | 2018-09-06 14:48:21 -0700 | [diff] [blame] | 212 | |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 213 | status = mManager->postSend(receiverId, sbuffer, &transactionId, &postUs); |
| 214 | ASSERT_TRUE(status == ResultStatus::OK); |
| 215 | |
| 216 | message.data.command = PipeCommand::SEND; |
| 217 | message.data.bufferId = sbuffer->mId; |
| 218 | message.data.connectionId = receiverId; |
| 219 | message.data.transactionId = transactionId; |
| 220 | message.data.timestampUs = postUs; |
| 221 | sendMessage(mCommandPipeFds, message); |
| 222 | } |
| 223 | EXPECT_TRUE(receiveMessage(mResultPipeFds, &message)); |
Sungtak Lee | ec638f5 | 2020-10-26 23:54:43 -0700 | [diff] [blame] | 224 | EXPECT_TRUE(message.data.command == PipeCommand::RECEIVE_OK); |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | } // anonymous namespace |
| 228 | |
| 229 | int main(int argc, char** argv) { |
Steven Moreland | 42e294c | 2020-07-08 18:46:16 +0000 | [diff] [blame] | 230 | android::hardware::details::setTrebleTestingOverride(true); |
Sungtak Lee | 9ec3356 | 2018-09-04 15:22:46 -0700 | [diff] [blame] | 231 | ::testing::InitGoogleTest(&argc, argv); |
| 232 | int status = RUN_ALL_TESTS(); |
| 233 | LOG(INFO) << "Test result = " << status; |
| 234 | return status; |
| 235 | } |