Tests: move remaining gtests into their own module folders
And make them part of the blender_test runner. The one exception is blenlib performance tests, which we don't want to run by default. They remain in their own executable. Differential Revision: https://developer.blender.org/D8498
This commit is contained in:
@@ -78,3 +78,19 @@ if(WITH_CXX_GUARDEDALLOC)
|
||||
)
|
||||
blender_add_lib(bf_intern_guardedalloc_cpp "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
|
||||
endif()
|
||||
|
||||
if(WITH_GTESTS)
|
||||
set(TEST_SRC
|
||||
tests/guardedalloc_alignment_test.cc
|
||||
tests/guardedalloc_overflow_test.cc
|
||||
)
|
||||
set(TEST_INC
|
||||
../../source/blender/blenlib
|
||||
)
|
||||
set(TEST_LIB
|
||||
bf_intern_guardedalloc
|
||||
bf_blenlib
|
||||
)
|
||||
include(GTestTesting)
|
||||
blender_add_test_lib(bf_intern_guardedalloc_tests "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB};${TEST_LIB}")
|
||||
endif()
|
||||
|
||||
121
intern/guardedalloc/tests/guardedalloc_alignment_test.cc
Normal file
121
intern/guardedalloc/tests/guardedalloc_alignment_test.cc
Normal file
@@ -0,0 +1,121 @@
|
||||
/* Apache License, Version 2.0 */
|
||||
|
||||
#include "testing/testing.h"
|
||||
|
||||
#include "BLI_utildefines.h"
|
||||
|
||||
#include "MEM_guardedalloc.h"
|
||||
|
||||
#define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ((size_t)ptr % align, 0)
|
||||
|
||||
namespace {
|
||||
|
||||
void DoBasicAlignmentChecks(const int alignment)
|
||||
{
|
||||
int *foo, *bar;
|
||||
|
||||
foo = (int *)MEM_mallocN_aligned(sizeof(int) * 10, alignment, "test");
|
||||
CHECK_ALIGNMENT(foo, alignment);
|
||||
|
||||
bar = (int *)MEM_dupallocN(foo);
|
||||
CHECK_ALIGNMENT(bar, alignment);
|
||||
MEM_freeN(bar);
|
||||
|
||||
foo = (int *)MEM_reallocN(foo, sizeof(int) * 5);
|
||||
CHECK_ALIGNMENT(foo, alignment);
|
||||
|
||||
foo = (int *)MEM_recallocN(foo, sizeof(int) * 5);
|
||||
CHECK_ALIGNMENT(foo, alignment);
|
||||
|
||||
MEM_freeN(foo);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc1)
|
||||
{
|
||||
DoBasicAlignmentChecks(1);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc1)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(1);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc2)
|
||||
{
|
||||
DoBasicAlignmentChecks(2);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc2)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(2);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc4)
|
||||
{
|
||||
DoBasicAlignmentChecks(4);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc4)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(4);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc8)
|
||||
{
|
||||
DoBasicAlignmentChecks(8);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc8)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(8);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc16)
|
||||
{
|
||||
DoBasicAlignmentChecks(16);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc16)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(16);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc32)
|
||||
{
|
||||
DoBasicAlignmentChecks(32);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc32)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(32);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc256)
|
||||
{
|
||||
DoBasicAlignmentChecks(256);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc256)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(256);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, LockfreeAlignedAlloc512)
|
||||
{
|
||||
DoBasicAlignmentChecks(512);
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedAlignedAlloc512)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
DoBasicAlignmentChecks(512);
|
||||
}
|
||||
60
intern/guardedalloc/tests/guardedalloc_overflow_test.cc
Normal file
60
intern/guardedalloc/tests/guardedalloc_overflow_test.cc
Normal file
@@ -0,0 +1,60 @@
|
||||
/* Apache License, Version 2.0 */
|
||||
|
||||
#include "testing/testing.h"
|
||||
|
||||
#include "MEM_guardedalloc.h"
|
||||
|
||||
/* We expect to abort on integer overflow, to prevent possible exploits. */
|
||||
#ifdef _WIN32
|
||||
# define ABORT_PREDICATE ::testing::ExitedWithCode(3)
|
||||
#else
|
||||
# define ABORT_PREDICATE ::testing::KilledBySignal(SIGABRT)
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
void MallocArray(size_t len, size_t size)
|
||||
{
|
||||
void *mem = MEM_malloc_arrayN(len, size, "MallocArray");
|
||||
if (mem) {
|
||||
MEM_freeN(mem);
|
||||
}
|
||||
}
|
||||
|
||||
void CallocArray(size_t len, size_t size)
|
||||
{
|
||||
void *mem = MEM_calloc_arrayN(len, size, "CallocArray");
|
||||
if (mem) {
|
||||
MEM_freeN(mem);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(guardedalloc, LockfreeIntegerOverflow)
|
||||
{
|
||||
MallocArray(1, SIZE_MAX);
|
||||
CallocArray(SIZE_MAX, 1);
|
||||
MallocArray(SIZE_MAX / 2, 2);
|
||||
CallocArray(SIZE_MAX / 1234567, 1234567);
|
||||
|
||||
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
|
||||
}
|
||||
|
||||
TEST(guardedalloc, GuardedIntegerOverflow)
|
||||
{
|
||||
MEM_use_guarded_allocator();
|
||||
|
||||
MallocArray(1, SIZE_MAX);
|
||||
CallocArray(SIZE_MAX, 1);
|
||||
MallocArray(SIZE_MAX / 2, 2);
|
||||
CallocArray(SIZE_MAX / 1234567, 1234567);
|
||||
|
||||
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
|
||||
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
|
||||
}
|
||||
Reference in New Issue
Block a user