blob: ce21c6f38a973d80fcfba4894ed14060a9e55929 [file] [log] [blame]
Howard Chen801e2612018-01-09 17:31:18 +08001/*
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/mapping.h>
24#include <hidlmemory/HidlMemoryToken.h>
25#include <hidlmemory/mapping.h>
26#include "HidlMemoryCache.h"
27
28#define EXPECT_OK(__ret__) EXPECT_TRUE(isOk(__ret__))
29
30template <typename T>
31static inline ::testing::AssertionResult isOk(const ::android::hardware::Return<T>& ret) {
32 return ret.isOk() ? (::testing::AssertionSuccess() << ret.description())
33 : (::testing::AssertionFailure() << ret.description());
34}
35
36namespace android {
37
38namespace hardware {
39void HidlCacheWhiteBoxTest() {
40 using ::android::hardware::HidlMemoryCache;
41 using ::android::hardware::HidlMemoryToken;
42 using ::android::hidl::allocator::V1_0::IAllocator;
43 using ::android::hidl::memory::V1_0::IMemory;
44 using ::android::hidl::memory::token::V1_0::IMemoryToken;
45 using ::android::hidl::memory::block::V1_0::MemoryBlock;
46
47 sp<IAllocator> ashmemAllocator;
48
49 ashmemAllocator = IAllocator::getService("ashmem");
50 ASSERT_NE(nullptr, ashmemAllocator.get());
51 ASSERT_TRUE(ashmemAllocator->isRemote()); // allocator is always remote
52
53 sp<HidlMemory> mem;
54 EXPECT_OK(ashmemAllocator->allocate(1024, [&](bool success, const hidl_memory& _mem) {
55 ASSERT_TRUE(success);
56 mem = HidlMemory::getInstance(_mem);
57 }));
58
59 sp<IMemoryToken> token = new HidlMemoryToken(mem);
60
61 MemoryBlock blk = {token, 0x200 /* size */, 0x100 /* offset */};
62 sp<IMemoryToken> mtoken = blk.token;
63 mtoken->get([&](const hidl_memory& mem) { sp<IMemory> memory = mapMemory(mem); });
64
65 sp<HidlMemoryCache> cache = HidlMemoryCache::getInstance();
66 EXPECT_FALSE(cache->cached(token));
67
68 MemoryBlock blk2 = {token, 0x200 /* size */, 0x300 /* offset */};
69
70 EXPECT_FALSE(cache->cached(token));
71
72 {
73 sp<IMemory> mem1 = cache->fetch(token);
74 EXPECT_TRUE(cache->cached(token));
75 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
76 sp<IMemory> mem2 = cache->fetch(token);
77 EXPECT_TRUE(cache->cached(token));
78 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
79 }
80 EXPECT_FALSE(cache->cached(token));
81 {
82 sp<IMemory> mem1 = mapMemory(blk);
83 EXPECT_TRUE(cache->cached(token));
84 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
85 uint8_t* data = static_cast<uint8_t*>(static_cast<void*>(mem1->getPointer()));
86 EXPECT_NE(nullptr, data);
87 }
88 {
89 sp<IMemory> mem2 = mapMemory(blk);
90 EXPECT_TRUE(cache->cached(token));
91 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
92 }
93 EXPECT_FALSE(cache->cached(token));
94 EXPECT_TRUE(cache->lock(token));
95 EXPECT_TRUE(cache->cached(token));
96 EXPECT_NE(nullptr, cache->getCachedLocked(token).get());
97 EXPECT_TRUE(cache->unlock(token));
98 EXPECT_FALSE(cache->cached(token));
99}
100} // namespace hardware
101
102class HidlCacheTest : public ::testing::Test {};
103
104TEST_F(HidlCacheTest, TestAll) {
105 hardware::HidlCacheWhiteBoxTest();
106}
107
108int main(int argc, char** argv) {
109 ::testing::InitGoogleTest(&argc, argv);
110 return RUN_ALL_TESTS();
111}
112
113} // namespace android