libhidlcache_test.cpp 5.4 KB

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