1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/android/library_loader/library_prefetcher.h"
9 #include "base/debug/proc_maps_linux.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 const uint8 kRead
= base::debug::MappedMemoryRegion::READ
;
17 const uint8 kReadPrivate
= base::debug::MappedMemoryRegion::READ
|
18 base::debug::MappedMemoryRegion::PRIVATE
;
19 const uint8 kExecutePrivate
= base::debug::MappedMemoryRegion::EXECUTE
|
20 base::debug::MappedMemoryRegion::PRIVATE
;
23 TEST(NativeLibraryPrefetcherTest
, TestIsGoodToPrefetchNoRange
) {
24 const base::debug::MappedMemoryRegion regions
[4] = {
25 base::debug::MappedMemoryRegion
{0x4000, 0x5000, 10, kReadPrivate
, ""},
26 base::debug::MappedMemoryRegion
{0x4000, 0x5000, 10, kReadPrivate
, "foo"},
27 base::debug::MappedMemoryRegion
{
28 0x4000, 0x5000, 10, kReadPrivate
, "foobar.apk"},
29 base::debug::MappedMemoryRegion
{
30 0x4000, 0x5000, 10, kReadPrivate
, "libchromium.so"}};
31 for (int i
= 0; i
< 4; ++i
) {
32 ASSERT_FALSE(NativeLibraryPrefetcher::IsGoodToPrefetch(regions
[i
]));
36 TEST(NativeLibraryPrefetcherTest
, TestIsGoodToPrefetchUnreadableRange
) {
37 const base::debug::MappedMemoryRegion region
= {
38 0x4000, 0x5000, 10, kExecutePrivate
, "base.apk"};
39 ASSERT_FALSE(NativeLibraryPrefetcher::IsGoodToPrefetch(region
));
42 TEST(NativeLibraryPrefetcherTest
, TestIsGoodToPrefetchSkipSharedRange
) {
43 const base::debug::MappedMemoryRegion region
= {
44 0x4000, 0x5000, 10, kRead
, "base.apk"};
45 ASSERT_FALSE(NativeLibraryPrefetcher::IsGoodToPrefetch(region
));
48 TEST(NativeLibraryPrefetcherTest
, TestIsGoodToPrefetchLibchromeRange
) {
49 const base::debug::MappedMemoryRegion region
= {
50 0x4000, 0x5000, 10, kReadPrivate
, "libchrome.so"};
51 ASSERT_TRUE(NativeLibraryPrefetcher::IsGoodToPrefetch(region
));
54 TEST(NativeLibraryPrefetcherTest
, TestIsGoodToPrefetchBaseApkRange
) {
55 const base::debug::MappedMemoryRegion region
= {
56 0x4000, 0x5000, 10, kReadPrivate
, "base.apk"};
57 ASSERT_TRUE(NativeLibraryPrefetcher::IsGoodToPrefetch(region
));
60 TEST(NativeLibraryPrefetcherTest
,
61 TestFilterLibchromeRangesOnlyIfPossibleNoLibchrome
) {
62 std::vector
<base::debug::MappedMemoryRegion
> regions
;
64 base::debug::MappedMemoryRegion
{0x1, 0x2, 0, kReadPrivate
, "base.apk"});
66 base::debug::MappedMemoryRegion
{0x3, 0x4, 0, kReadPrivate
, "base.apk"});
67 std::vector
<NativeLibraryPrefetcher::AddressRange
> ranges
;
68 NativeLibraryPrefetcher::FilterLibchromeRangesOnlyIfPossible(regions
,
70 EXPECT_EQ(ranges
.size(), 2U);
71 EXPECT_EQ(ranges
[0].first
, 0x1U
);
72 EXPECT_EQ(ranges
[0].second
, 0x2U
);
73 EXPECT_EQ(ranges
[1].first
, 0x3U
);
74 EXPECT_EQ(ranges
[1].second
, 0x4U
);
77 TEST(NativeLibraryPrefetcherTest
,
78 TestFilterLibchromeRangesOnlyIfPossibleHasLibchrome
) {
79 std::vector
<base::debug::MappedMemoryRegion
> regions
;
81 base::debug::MappedMemoryRegion
{0x1, 0x2, 0, kReadPrivate
, "base.apk"});
82 regions
.push_back(base::debug::MappedMemoryRegion
{
83 0x6, 0x7, 0, kReadPrivate
, "libchrome.so"});
85 base::debug::MappedMemoryRegion
{0x3, 0x4, 0, kReadPrivate
, "base.apk"});
86 std::vector
<NativeLibraryPrefetcher::AddressRange
> ranges
;
87 NativeLibraryPrefetcher::FilterLibchromeRangesOnlyIfPossible(regions
,
89 EXPECT_EQ(ranges
.size(), 1U);
90 EXPECT_EQ(ranges
[0].first
, 0x6U
);
91 EXPECT_EQ(ranges
[0].second
, 0x7U
);
94 } // namespace android