diff options
| author | Colin Cross <ccross@android.com> | 2016-03-02 17:52:56 -0800 |
|---|---|---|
| committer | Colin Cross <ccross@android.com> | 2016-03-07 15:52:39 -0800 |
| commit | 54a1610404986c998216a41b4c61c8b7aea4126c (patch) | |
| tree | 08dbb1699493cc8027e0e543ace9896034a51685 /libmemunreachable/tests | |
| parent | a6680f78d4665b2a936f76311e6f60eb930992c8 (diff) | |
| download | system_core-54a1610404986c998216a41b4c61c8b7aea4126c.tar.gz system_core-54a1610404986c998216a41b4c61c8b7aea4126c.tar.bz2 system_core-54a1610404986c998216a41b4c61c8b7aea4126c.zip | |
Compile some tests for the host
Compile some tests for the host to ease debugging with valgrind or gdb.
Bug: 27208635
Change-Id: Ib46fcfa333ceb721f26efca00b2fa60b9fba44e6
(cherry picked from commit b8e20f557f315d2ab5e21f7bf70657bb16d5d42d)
Diffstat (limited to 'libmemunreachable/tests')
| -rw-r--r-- | libmemunreachable/tests/Allocator_test.cpp | 99 | ||||
| -rw-r--r-- | libmemunreachable/tests/DisableMalloc_test.cpp | 116 | ||||
| -rw-r--r-- | libmemunreachable/tests/HeapWalker_test.cpp | 8 | ||||
| -rw-r--r-- | libmemunreachable/tests/HostMallocStub.cpp | 23 |
4 files changed, 143 insertions, 103 deletions
diff --git a/libmemunreachable/tests/Allocator_test.cpp b/libmemunreachable/tests/Allocator_test.cpp index d8e473eba..fa76ae034 100644 --- a/libmemunreachable/tests/Allocator_test.cpp +++ b/libmemunreachable/tests/Allocator_test.cpp @@ -15,12 +15,6 @@ */ #include <Allocator.h> -#include <sys/time.h> - -#include <chrono> -#include <functional> -#include <list> -#include <vector> #include <gtest/gtest.h> #include <ScopedDisableMalloc.h> @@ -28,8 +22,6 @@ std::function<void()> ScopedAlarm::func_; -using namespace std::chrono_literals; - class AllocatorTest : public testing::Test { protected: AllocatorTest() : heap(), disable_malloc_() {} @@ -180,94 +172,3 @@ TEST_F(AllocatorTest, unique) { ASSERT_NE(ptr, nullptr); } - -class DisableMallocTest : public ::testing::Test { - protected: - void alarm(std::chrono::microseconds us) { - std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us); - itimerval t = itimerval(); - t.it_value.tv_sec = s.count(); - t.it_value.tv_usec = (us - s).count(); - setitimer(ITIMER_REAL, &t, NULL); - } -}; - -TEST_F(DisableMallocTest, reenable) { - ASSERT_EXIT({ - alarm(100ms); - void *ptr1 = malloc(128); - ASSERT_NE(ptr1, nullptr); - free(ptr1); - { - ScopedDisableMalloc disable_malloc; - } - void *ptr2 = malloc(128); - ASSERT_NE(ptr2, nullptr); - free(ptr2); - _exit(1); - }, ::testing::ExitedWithCode(1), ""); -} - -TEST_F(DisableMallocTest, deadlock_allocate) { - ASSERT_DEATH({ - void *ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - free(ptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - void* ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - free(ptr); - } - }, ""); -} - -TEST_F(DisableMallocTest, deadlock_new) { - ASSERT_DEATH({ - char* ptr = new(char); - ASSERT_NE(ptr, nullptr); - delete(ptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - char* ptr = new(char); - ASSERT_NE(ptr, nullptr); - delete(ptr); - } - }, ""); -} - -TEST_F(DisableMallocTest, deadlock_delete) { - ASSERT_DEATH({ - char* ptr = new(char); - ASSERT_NE(ptr, nullptr); - { - alarm(250ms); - ScopedDisableMalloc disable_malloc; - delete(ptr); - } - }, ""); -} - -TEST_F(DisableMallocTest, deadlock_free) { - ASSERT_DEATH({ - void *ptr = malloc(128); - ASSERT_NE(ptr, nullptr); - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - free(ptr); - } - }, ""); -} - -TEST_F(DisableMallocTest, deadlock_fork) { - ASSERT_DEATH({ - { - alarm(100ms); - ScopedDisableMalloc disable_malloc; - fork(); - } - }, ""); -} diff --git a/libmemunreachable/tests/DisableMalloc_test.cpp b/libmemunreachable/tests/DisableMalloc_test.cpp new file mode 100644 index 000000000..ea5c22c88 --- /dev/null +++ b/libmemunreachable/tests/DisableMalloc_test.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/time.h> + +#include <chrono> +#include <functional> + +#include <gtest/gtest.h> +#include <ScopedDisableMalloc.h> + +using namespace std::chrono_literals; + +class DisableMallocTest : public ::testing::Test { + protected: + void alarm(std::chrono::microseconds us) { + std::chrono::seconds s = std::chrono::duration_cast<std::chrono::seconds>(us); + itimerval t = itimerval(); + t.it_value.tv_sec = s.count(); + t.it_value.tv_usec = (us - s).count(); + setitimer(ITIMER_REAL, &t, NULL); + } +}; + +TEST_F(DisableMallocTest, reenable) { + ASSERT_EXIT({ + alarm(100ms); + void *ptr1 = malloc(128); + ASSERT_NE(ptr1, nullptr); + free(ptr1); + { + ScopedDisableMalloc disable_malloc; + } + void *ptr2 = malloc(128); + ASSERT_NE(ptr2, nullptr); + free(ptr2); + _exit(1); + }, ::testing::ExitedWithCode(1), ""); +} + +TEST_F(DisableMallocTest, deadlock_allocate) { + ASSERT_DEATH({ + void *ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + free(ptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + void* ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + free(ptr); + } + }, ""); +} + +TEST_F(DisableMallocTest, deadlock_new) { + ASSERT_DEATH({ + char* ptr = new(char); + ASSERT_NE(ptr, nullptr); + delete(ptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + char* ptr = new(char); + ASSERT_NE(ptr, nullptr); + delete(ptr); + } + }, ""); +} + +TEST_F(DisableMallocTest, deadlock_delete) { + ASSERT_DEATH({ + char* ptr = new(char); + ASSERT_NE(ptr, nullptr); + { + alarm(250ms); + ScopedDisableMalloc disable_malloc; + delete(ptr); + } + }, ""); +} + +TEST_F(DisableMallocTest, deadlock_free) { + ASSERT_DEATH({ + void *ptr = malloc(128); + ASSERT_NE(ptr, nullptr); + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + free(ptr); + } + }, ""); +} + +TEST_F(DisableMallocTest, deadlock_fork) { + ASSERT_DEATH({ + { + alarm(100ms); + ScopedDisableMalloc disable_malloc; + fork(); + } + }, ""); +} diff --git a/libmemunreachable/tests/HeapWalker_test.cpp b/libmemunreachable/tests/HeapWalker_test.cpp index 9921eb65e..ccdd156c4 100644 --- a/libmemunreachable/tests/HeapWalker_test.cpp +++ b/libmemunreachable/tests/HeapWalker_test.cpp @@ -107,8 +107,8 @@ TEST_F(HeapWalkerTest, live) { heap_walker.Root(buffer_begin(buffer1), buffer_end(buffer1)); allocator::vector<Range> leaked(heap_); - size_t num_leaks = SIZE_T_MAX; - size_t leaked_bytes = SIZE_T_MAX; + size_t num_leaks = SIZE_MAX; + size_t leaked_bytes = SIZE_MAX; ASSERT_EQ(true, heap_walker.Leaked(leaked, 100, &num_leaks, &leaked_bytes)); EXPECT_EQ(0U, num_leaks); @@ -133,8 +133,8 @@ TEST_F(HeapWalkerTest, unaligned) { heap_walker.Root(buffer_begin(buffer1) + i, buffer_end(buffer1) - j); allocator::vector<Range> leaked(heap_); - size_t num_leaks = SIZE_T_MAX; - size_t leaked_bytes = SIZE_T_MAX; + size_t num_leaks = SIZE_MAX; + size_t leaked_bytes = SIZE_MAX; ASSERT_EQ(true, heap_walker.Leaked(leaked, 100, &num_leaks, &leaked_bytes)); EXPECT_EQ(0U, num_leaks); diff --git a/libmemunreachable/tests/HostMallocStub.cpp b/libmemunreachable/tests/HostMallocStub.cpp new file mode 100644 index 000000000..a7e3f07d3 --- /dev/null +++ b/libmemunreachable/tests/HostMallocStub.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bionic.h" + +void malloc_disable() { +} + +void malloc_enable() { +} |
