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 #include <android-base/logging.h>
18 #include <android/hidl/allocator/1.0/IAllocator.h>
19 #include <android/hidl/memory/1.0/IMemory.h>
20 #include <gtest/gtest.h>
21 #include <hidl/GtestPrinter.h>
22 #include <hidl/ServiceManagement.h>
23 #include <hidlmemory/mapping.h>
24
25 using ::android::sp;
26 using ::android::hardware::hidl_memory;
27 using ::android::hardware::hidl_vec;
28 using ::android::hardware::Return;
29 using ::android::hardware::Void;
30 using ::android::hidl::allocator::V1_0::IAllocator;
31 using ::android::hidl::memory::V1_0::IMemory;
32
33 #define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
34 #define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())
35
36 class AllocatorHidlTest : public ::testing::TestWithParam<std::string> {
37 public:
SetUp()38 virtual void SetUp() override {
39 allocator = IAllocator::getService(GetParam());
40 ASSERT_NE(allocator, nullptr);
41 }
42
expectAllocateSuccess(size_t size)43 sp<IMemory> expectAllocateSuccess(size_t size) {
44 sp<IMemory> memory;
45 EXPECT_OK(allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
46 ASSERT_TRUE(success) << "Allocate failed for size: " << size;
47 EXPECT_EQ(mem.size(), size)
48 << "Allocated " << size << " but got hidl_memory with size " << mem.size();
49 memory = mapMemory(mem);
50 }));
51 EXPECT_NE(nullptr, memory.get());
52 EXPECT_EQ(memory->getSize(), size)
53 << "Allocated " << size << " but got IMemory with size " << memory->getSize();
54 return memory;
55 }
56
expectBatchAllocateSuccess(size_t size,size_t count)57 std::vector<sp<IMemory>> expectBatchAllocateSuccess(size_t size, size_t count) {
58 std::vector<sp<IMemory>> memories;
59 memories.reserve(count);
60 EXPECT_OK(allocator->batchAllocate(
61 size, count, [&](bool success, const hidl_vec<hidl_memory>& mems) {
62 EXPECT_EQ(count, mems.size());
63
64 for (const hidl_memory& mem : mems) {
65 ASSERT_TRUE(success) << "Allocate failed for size: " << size;
66 EXPECT_EQ(mem.size(), size)
67 << "Allocated " << size << " but got hidl_memory with size " << mem.size();
68 memories.push_back(mapMemory(mem));
69 }
70 }));
71 for (const sp<IMemory>& memory : memories) {
72 EXPECT_NE(nullptr, memory.get());
73 EXPECT_EQ(memory->getSize(), size)
74 << "Allocated " << size << " but got IMemory with size " << memory->getSize();
75 }
76 return memories;
77 }
78
79 sp<IAllocator> allocator;
80 };
81
TEST_P(AllocatorHidlTest,TestAllocateSizes)82 TEST_P(AllocatorHidlTest, TestAllocateSizes) {
83 for (size_t size : {1, 1023, 1024, 1025, 4096}) {
84 expectAllocateSuccess(size);
85 }
86 }
87
TEST_P(AllocatorHidlTest,TestBatchAllocateSizes)88 TEST_P(AllocatorHidlTest, TestBatchAllocateSizes) {
89 for (size_t count : {1, 1, 2, 3, 10}) {
90 for (size_t size : {1, 1023, 1024, 1025, 4096}) {
91 expectBatchAllocateSuccess(size, count);
92 }
93 }
94 }
95
TEST_P(AllocatorHidlTest,TestCommit)96 TEST_P(AllocatorHidlTest, TestCommit) {
97 constexpr size_t kSize = 1337;
98
99 sp<IMemory> memory = expectAllocateSuccess(kSize);
100 for (int i = 0; i < 100; i++) {
101 EXPECT_OK(memory->read());
102 EXPECT_OK(memory->update());
103 EXPECT_OK(memory->commit());
104
105 EXPECT_OK(memory->read());
106 EXPECT_OK(memory->commit());
107
108 EXPECT_OK(memory->update());
109 EXPECT_OK(memory->commit());
110 }
111
112 for (int i = 0; i < kSize; i++) {
113 EXPECT_OK(memory->readRange(i, kSize));
114 EXPECT_OK(memory->updateRange(i, kSize));
115 EXPECT_OK(memory->commit());
116
117 EXPECT_OK(memory->readRange(i, kSize));
118 EXPECT_OK(memory->commit());
119
120 EXPECT_OK(memory->updateRange(i, kSize));
121 EXPECT_OK(memory->commit());
122 }
123 }
124
125 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AllocatorHidlTest);
126 INSTANTIATE_TEST_SUITE_P(
127 PerInstance, AllocatorHidlTest,
128 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IAllocator::descriptor)),
129 android::hardware::PrintInstanceNameToString);
130