| 1 |
#include <raft.h> |
| 2 |
|
| 3 |
#include "fault.h" |
| 4 |
#include "raft_heap.h" |
| 5 |
|
| 6 |
struct heapFault |
| 7 |
{ |
| 8 |
struct test_fault fault; |
| 9 |
const struct raft_heap *orig_heap; |
| 10 |
}; |
| 11 |
|
| 12 |
static struct heapFault faulty; |
| 13 |
|
| 14 |
static void *faultyMalloc(void *data, size_t size) |
| 15 |
{ |
| 16 |
(void)data; |
| 17 |
if (test_fault_tick(&faulty.fault)) { |
| 18 |
return NULL; |
| 19 |
} else { |
| 20 |
return faulty.orig_heap->malloc(faulty.orig_heap->data, size); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
static void faultyFree(void *data, void *ptr) |
| 25 |
{ |
| 26 |
(void)data; |
| 27 |
faulty.orig_heap->free(faulty.orig_heap->data, ptr); |
| 28 |
} |
| 29 |
|
| 30 |
static void *faultyCalloc(void *data, size_t nmemb, size_t size) |
| 31 |
{ |
| 32 |
(void)data; |
| 33 |
if (test_fault_tick(&faulty.fault)) { |
| 34 |
return NULL; |
| 35 |
} else { |
| 36 |
return faulty.orig_heap->calloc(faulty.orig_heap->data, nmemb, |
| 37 |
size); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
static void *faultyRealloc(void *data, void *ptr, size_t size) |
| 42 |
{ |
| 43 |
(void)data; |
| 44 |
if (test_fault_tick(&faulty.fault)) { |
| 45 |
return NULL; |
| 46 |
} else { |
| 47 |
return faulty.orig_heap->realloc(faulty.orig_heap->data, ptr, |
| 48 |
size); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void *faultyAlignedAlloc(void *data, size_t alignment, size_t size) |
| 53 |
{ |
| 54 |
(void)data; |
| 55 |
if (test_fault_tick(&faulty.fault)) { |
| 56 |
return NULL; |
| 57 |
} else { |
| 58 |
return faulty.orig_heap->aligned_alloc(faulty.orig_heap->data, |
| 59 |
alignment, size); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void faultyAlignedFree(void *data, size_t alignment, void *ptr) |
| 64 |
{ |
| 65 |
(void)data; |
| 66 |
(void)alignment; |
| 67 |
faulty.orig_heap->aligned_free(faulty.orig_heap->data, alignment, ptr); |
| 68 |
} |
| 69 |
|
| 70 |
void test_raft_heap_setup(const MunitParameter params[], void *user_data) |
| 71 |
{ |
| 72 |
(void)params; |
| 73 |
(void)user_data; |
| 74 |
struct raft_heap *heap = munit_malloc(sizeof(*heap)); |
| 75 |
test_fault_init(&faulty.fault); |
| 76 |
faulty.orig_heap = raft_heap_get(); |
| 77 |
heap->data = NULL; |
| 78 |
heap->malloc = faultyMalloc; |
| 79 |
heap->free = faultyFree; |
| 80 |
heap->calloc = faultyCalloc; |
| 81 |
heap->realloc = faultyRealloc; |
| 82 |
heap->aligned_alloc = faultyAlignedAlloc; |
| 83 |
heap->aligned_free = faultyAlignedFree; |
| 84 |
raft_heap_set(heap); |
| 85 |
} |
| 86 |
|
| 87 |
void test_raft_heap_tear_down(void *data) |
| 88 |
{ |
| 89 |
struct raft_heap *heap = (struct raft_heap *)raft_heap_get(); |
| 90 |
(void)data; |
| 91 |
raft_heap_set((struct raft_heap *)faulty.orig_heap); |
| 92 |
faulty.orig_heap = NULL; |
| 93 |
free(heap); |
| 94 |
} |
| 95 |
|
| 96 |
void test_raft_heap_fault_config(int delay, int repeat) |
| 97 |
{ |
| 98 |
test_fault_config(&faulty.fault, delay, repeat); |
| 99 |
} |
| 100 |
|
| 101 |
void test_raft_heap_fault_enable(void) |
| 102 |
{ |
| 103 |
test_fault_enable(&faulty.fault); |
| 104 |
} |