|
| 1 | +/* unit-store-sbrk.c |
| 2 | + * |
| 3 | + * Unit tests for store allocator helper. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <check.h> |
| 7 | +#include <stdint.h> |
| 8 | + |
| 9 | +#include "../../src/store_sbrk.h" |
| 10 | + |
| 11 | +START_TEST(test_sbrk_first_call_advances_heap) |
| 12 | +{ |
| 13 | + uint8_t heap_buf[32]; |
| 14 | + uint8_t *heap = NULL; |
| 15 | + void *ret; |
| 16 | + |
| 17 | + ret = wolfboot_store_sbrk(5, &heap, heap_buf, sizeof(heap_buf)); |
| 18 | + |
| 19 | + ck_assert_ptr_eq(ret, heap_buf); |
| 20 | + ck_assert_ptr_eq(heap, heap_buf + 8); |
| 21 | +} |
| 22 | +END_TEST |
| 23 | + |
| 24 | +START_TEST(test_sbrk_rejects_overflow) |
| 25 | +{ |
| 26 | + uint8_t heap_buf[16]; |
| 27 | + uint8_t *heap = NULL; |
| 28 | + void *ret; |
| 29 | + |
| 30 | + ret = wolfboot_store_sbrk(8, &heap, heap_buf, sizeof(heap_buf)); |
| 31 | + ck_assert_ptr_eq(ret, heap_buf); |
| 32 | + |
| 33 | + ret = wolfboot_store_sbrk(16, &heap, heap_buf, sizeof(heap_buf)); |
| 34 | + ck_assert_ptr_eq(ret, (void *)-1); |
| 35 | + ck_assert_ptr_eq(heap, heap_buf + 8); |
| 36 | +} |
| 37 | +END_TEST |
| 38 | + |
| 39 | +Suite *wolfboot_suite(void) |
| 40 | +{ |
| 41 | + Suite *s = suite_create("store-sbrk"); |
| 42 | + TCase *tcase = tcase_create("store_sbrk"); |
| 43 | + |
| 44 | + tcase_add_test(tcase, test_sbrk_first_call_advances_heap); |
| 45 | + tcase_add_test(tcase, test_sbrk_rejects_overflow); |
| 46 | + suite_add_tcase(s, tcase); |
| 47 | + return s; |
| 48 | +} |
| 49 | + |
| 50 | +int main(void) |
| 51 | +{ |
| 52 | + int fails; |
| 53 | + Suite *s = wolfboot_suite(); |
| 54 | + SRunner *sr = srunner_create(s); |
| 55 | + |
| 56 | + srunner_run_all(sr, CK_NORMAL); |
| 57 | + fails = srunner_ntests_failed(sr); |
| 58 | + srunner_free(sr); |
| 59 | + return fails; |
| 60 | +} |
0 commit comments