1 /*
2 * Copyright (C) 2016 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 "hidl-cache-test"
18
19 #include <android/hidl/allocator/1.0/IAllocator.h>
20 #include <android/hidl/memory/1.0/IMemory.h>
21 #include <android/hidl/memory/token/1.0/IMemoryToken.h>
22 #include <gtest/gtest.h>
23 #include <hidlcache/MemoryDealer.h>
24 #include <hidlcache/mapping.h>
25 #include <hidlmemory/HidlMemoryToken.h>
26 #include <hidlmemory/mapping.h>
27 #include "HidlMemoryCache.h"
28
29 #define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
30
31 template <typename T>
isOk(const::android::hardware::Return<T> & ret)32 static inline ::testing::AssertionResult isOk(const ::android::hardware::Return<T>& ret) {
33 return ret.isOk() ? (::testing::AssertionSuccess() << ret.description())
34 : (::testing::AssertionFailure() << ret.description());
35 }
36
37 namespace android {
38
39 namespace hardware {
HidlCacheWhiteBoxTest()40 void HidlCacheWhiteBoxTest() {
41 using ::android::hardware::HidlMemoryCache;
42 using ::android::hardware::HidlMemoryToken;
43 using ::android::hidl::allocator::V1_0::IAllocator;
44 using ::android::hidl::memory::V1_0::IMemory;
45 using ::android::hidl::memory::token::V1_0::IMemoryToken;
46 using ::android::hidl::memory::block::V1_0::MemoryBlock;
47
48 sp<IAllocator> ashmemAllocator;
49
50 ashmemAllocator = IAllocator::getService("ashmem");
51 ASSERT_NE(nullptr, ashmemAllocator.get());
52 ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote
53
54 sp<HidlMemory> mem;
55 EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& _mem) {
56 ASSERT_TRUE(success);
57 mem = HidlMemory::getInstance(_mem);
58 }));
59
60 sp<IMemoryToken> token = new HidlMemoryToken(mem);
61
62 MemoryBlock blk = {token, 0x200 /* size */, 0x100 /* offset */};
63 sp<IMemoryToken> mtoken = blk.token;
64 mtoken->get([&](const hidl_memory& mem) { sp<IMemory> memory = mapMemory(mem); });
65
66 sp<HidlMemoryCache> cache = HidlMemoryCache::getInstance();
67 EXPECT_FALSE(cache->cached(token));
68
69 MemoryBlock blk2 = {token, 0x200 /* size */, 0x300 /* offset */};
70
71 EXPECT_FALSE(cache->cached(token));
72
73 {
74 sp<IMemory> mem1 = cache->fetch(token);
75 EXPECT_TRUE(cache->cached(token));
76 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
77 sp<IMemory> mem2 = cache->fetch(token);
78 EXPECT_TRUE(cache->cached(token));
79 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
80 }
81 EXPECT_FALSE(cache->cached(token));
82 {
83 sp<IMemory> mem1 = mapMemory(blk);
84 EXPECT_TRUE(cache->cached(token));
85 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
86 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(mem1->getPointer()));
87 EXPECT_NE(nullptr, data);
88 }
89 {
90 sp<IMemory> mem2 = mapMemory(blk);
91 EXPECT_TRUE(cache->cached(token));
92 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
93 }
94 EXPECT_FALSE(cache->cached(token));
95 EXPECT_TRUE(cache->lock(token));
96 EXPECT_TRUE(cache->cached(token));
97 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
98 EXPECT_TRUE(cache->unlock(token));
99 EXPECT_FALSE(cache->cached(token));
100 }
101 } // namespace hardware
102
103 class HidlCacheTest : public ::testing::Test {};
104
TEST_F(HidlCacheTest,TestAll)105 TEST_F(HidlCacheTest, TestAll) {
106 hardware::HidlCacheWhiteBoxTest();
107 }
108
TEST_F(HidlCacheTest,MemoryDealer)109 TEST_F(HidlCacheTest, MemoryDealer) {
110 using ::android::hardware::HidlMemory;
111 using ::android::hardware::hidl_memory;
112 using ::android::hardware::HidlMemoryDealer;
113 using ::android::hidl::allocator::V1_0::IAllocator;
114 using ::android::hidl::memory::block::V1_0::MemoryBlock;
115
116 sp<IAllocator> ashmemAllocator;
117
118 ashmemAllocator = IAllocator::getService("ashmem");
119 sp<HidlMemory> m1;
120 sp<HidlMemory> m2;
121 // test MemoryDealer
122 EXPECT_OK(ashmemAllocator->allocate(2048, [&m1](bool success, const hidl_memory& mem) {
123 ASSERT_TRUE(success);
124 m1 = HidlMemory::getInstance(mem);
125 }));
126
127 EXPECT_OK(ashmemAllocator->allocate(4096, [&m2](bool success, const hidl_memory& mem) {
128 ASSERT_TRUE(success);
129 m2 = HidlMemory::getInstance(mem);
130 }));
131
132 sp<HidlMemoryDealer> dealer;
133
134 // m1 does not statisfy the alignment requirement and should fail.
135 dealer = HidlMemoryDealer::getInstance(*m1);
136 EXPECT_EQ(nullptr, dealer.get());
137
138 dealer = HidlMemoryDealer::getInstance(*m2);
139 EXPECT_NE(nullptr, dealer.get());
140
141 EXPECT_EQ(dealer->heap()->getSize(), 4096ULL);
142 MemoryBlock blk = dealer->allocate(1024);
143 EXPECT_TRUE(HidlMemoryDealer::isOk(blk));
144 MemoryBlock blk2 = dealer->allocate(2048);
145 EXPECT_TRUE(HidlMemoryDealer::isOk(blk2));
146
147 MemoryBlock blk3 = dealer->allocate(2048);
148 EXPECT_FALSE(HidlMemoryDealer::isOk(blk3));
149 dealer->deallocate(blk2.offset);
150 blk3 = dealer->allocate(2048);
151 EXPECT_TRUE(HidlMemoryDealer::isOk(blk3));
152 }
153
main(int argc,char ** argv)154 int main(int argc, char** argv) {
155 ::testing::InitGoogleTest(&argc, argv);
156 return RUN_ALL_TESTS();
157 }
158
159 } // namespace android