Commit 4b6a3c9e authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

base/allocator: Add a unittest for __wrap_strdup on Android

Adds a unittest for the change of https://crrev.com/c/2418513 .

Also removes unnecessary "#if defined" guards.  All test cases in
allocator_shim_unittest.cc depend on USE_ALLOCATOR_SHIM, so the
entire contents of the file should be guarded with it.  Currently
it's done at:
https://source.chromium.org/chromium/chromium/src/+/master:base/BUILD.gn;drc=625ee4c0eea632fed7825725d54843ef3cd6ca2c;l=3352

Bug: 1111332
Change-Id: I2204d88b676365b317e27f21baa0fb5ba8df213c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2449869
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814133}
parent a958bba3
...@@ -581,11 +581,11 @@ TEST_F(AllocatorShimTest, NewHandlerConcurrency) { ...@@ -581,11 +581,11 @@ TEST_F(AllocatorShimTest, NewHandlerConcurrency) {
ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls()); ASSERT_EQ(kNumThreads, GetNumberOfNewHandlerCalls());
} }
#if defined(OS_WIN) && BUILDFLAG(USE_ALLOCATOR_SHIM) #if defined(OS_WIN)
TEST_F(AllocatorShimTest, ShimReplacesCRTHeapWhenEnabled) { TEST_F(AllocatorShimTest, ShimReplacesCRTHeapWhenEnabled) {
ASSERT_EQ(::GetProcessHeap(), reinterpret_cast<HANDLE>(_get_heap_handle())); ASSERT_EQ(::GetProcessHeap(), reinterpret_cast<HANDLE>(_get_heap_handle()));
} }
#endif // defined(OS_WIN) && BUILDFLAG(USE_ALLOCATOR_SHIM) #endif // defined(OS_WIN)
#if defined(OS_WIN) #if defined(OS_WIN)
static size_t GetAllocatedSize(void* ptr) { static size_t GetAllocatedSize(void* ptr) {
...@@ -603,7 +603,7 @@ static size_t GetAllocatedSize(void* ptr) { ...@@ -603,7 +603,7 @@ static size_t GetAllocatedSize(void* ptr) {
#define NO_MALLOC_SIZE #define NO_MALLOC_SIZE
#endif #endif
#if !defined(NO_MALLOC_SIZE) && BUILDFLAG(USE_ALLOCATOR_SHIM) #if !defined(NO_MALLOC_SIZE)
TEST_F(AllocatorShimTest, ShimReplacesMallocSizeWhenEnabled) { TEST_F(AllocatorShimTest, ShimReplacesMallocSizeWhenEnabled) {
InsertAllocatorDispatch(&g_mock_dispatch); InsertAllocatorDispatch(&g_mock_dispatch);
EXPECT_EQ(GetAllocatedSize(kTestSizeEstimateAddress), kTestSizeEstimate); EXPECT_EQ(GetAllocatedSize(kTestSizeEstimateAddress), kTestSizeEstimate);
...@@ -621,7 +621,39 @@ TEST_F(AllocatorShimTest, ShimDoesntChangeMallocSizeWhenEnabled) { ...@@ -621,7 +621,39 @@ TEST_F(AllocatorShimTest, ShimDoesntChangeMallocSizeWhenEnabled) {
free(alloc); free(alloc);
} }
#endif // !defined(NO_MALLOC_SIZE) && BUILDFLAG(USE_ALLOCATOR_SHIM) #endif // !defined(NO_MALLOC_SIZE)
#if defined(OS_ANDROID)
TEST_F(AllocatorShimTest, InterceptCLibraryFunctions) {
auto total_counts = [](const std::vector<size_t>& counts) {
size_t total = 0;
for (const auto count : counts)
total += count;
return total;
};
size_t counts_before;
size_t counts_after = total_counts(allocs_intercepted_by_size);
void* ptr;
InsertAllocatorDispatch(&g_mock_dispatch);
counts_before = counts_after;
ptr = strdup("hello, world");
EXPECT_NE(nullptr, ptr);
free(ptr);
counts_after = total_counts(allocs_intercepted_by_size);
EXPECT_GT(counts_after, counts_before);
counts_before = counts_after;
ptr = strndup("hello, world", 5);
EXPECT_NE(nullptr, ptr);
free(ptr);
counts_after = total_counts(allocs_intercepted_by_size);
EXPECT_GT(counts_after, counts_before);
RemoveAllocatorDispatchForTesting(&g_mock_dispatch);
}
#endif // defined(OS_ANDROID)
} // namespace } // namespace
} // namespace allocator } // namespace allocator
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment