summaryrefslogtreecommitdiff
path: root/src/unit/test_page_allocator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit/test_page_allocator.c')
-rw-r--r--src/unit/test_page_allocator.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/unit/test_page_allocator.c b/src/unit/test_page_allocator.c
new file mode 100644
index 0000000..ea90355
--- /dev/null
+++ b/src/unit/test_page_allocator.c
@@ -0,0 +1,59 @@
+#include "../core/allocator.h"
+#include "../core/page_allocator.h"
+#include "./allocator_utils.h"
+
+#include "greatest.h"
+#include <unistd.h>
+
+TEST larger_than_page_size(void) {
+ SandAllocator *a = sand_get_page_allocator();
+
+ long page_size = sysconf(_SC_PAGESIZE);
+ if (page_size < 0) {
+ FAILm("Failed to get page size!?");
+ }
+
+ size_t size = page_size + 128;
+ unsigned char *ptr = sand_allocate(a, size);
+
+ for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned.
+ ptr[i] = 0xFE;
+ }
+
+ sand_deallocate(a, ptr, size);
+ PASS();
+}
+
+TEST five_page_allocation(void) {
+ SandAllocator *a = sand_get_page_allocator();
+
+ long page_size = sysconf(_SC_PAGESIZE);
+ if (page_size < 0) {
+ FAILm("Failed to get page size!?");
+ }
+
+ size_t size = 5 * page_size;
+ unsigned char *ptr = sand_allocate(a, size);
+ ASSERT_NEQm("Allocating 5 pqgs should not fail", ptr, NULL);
+
+ for (size_t i = 0; i < size; ++i) { // This will crash under ASAN if invalid memory is returned.
+ ptr[i] = 0xFE;
+ }
+
+ sand_deallocate(a, ptr, size);
+ PASS();
+}
+
+SUITE(page_allocator) {
+#define RUN_WITH_PAGE_ALLOC(test) \
+ do { \
+ SandAllocator *a = sand_get_page_allocator(); \
+ RUN_TEST1(test, a); \
+ } while (0);
+
+ RUN_ALLOCATOR_TESTS(RUN_WITH_PAGE_ALLOC);
+#undef X
+
+ RUN_TEST(larger_than_page_size);
+ RUN_TEST(five_page_allocation);
+}