Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright The Transfer List Library Contributors |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT OR GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include "test.h" |
| 12 | #include "transfer_list.h" |
| 13 | #include "unity.h" |
| 14 | |
| 15 | void *buffer = NULL; |
| 16 | |
| 17 | void test_init() |
| 18 | { |
| 19 | struct transfer_list_header *tl = buffer; |
| 20 | |
| 21 | /* Attempt to init TL with 0 size */ |
| 22 | TEST_ASSERT_NULL(transfer_list_init(tl, 0)); |
| 23 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_NON); |
| 24 | |
| 25 | /* Init TL with an invalid memory address */ |
| 26 | TEST_ASSERT_NULL(transfer_list_init((void *)1, TL_SIZE)); |
| 27 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_NON); |
| 28 | |
| 29 | /* Valid memory address and large enough TL */ |
| 30 | TEST_ASSERT(transfer_list_init(tl, TL_SIZE)); |
| 31 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_ALL); |
| 32 | TEST_ASSERT_NULL(transfer_list_next(tl, NULL)); |
| 33 | } |
| 34 | |
| 35 | void test_init_alignment() |
| 36 | { |
| 37 | struct transfer_list_header *tl = buffer; |
| 38 | memset(tl, 0, TL_SIZE); |
| 39 | |
| 40 | TEST_ASSERT_NULL(transfer_list_init((void *)tl + 0xff, TL_SIZE)); |
| 41 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_NON); |
| 42 | } |
| 43 | |
| 44 | void test_relocate() |
| 45 | { |
| 46 | struct transfer_list_header *tl, *new_tl; |
| 47 | struct transfer_list_entry *te; |
| 48 | |
| 49 | void *new_buf = malloc(TL_SIZE * 2); |
| 50 | unsigned int test_tag = 0x1; |
| 51 | int test_payload = 0xdeadbeef; |
| 52 | |
| 53 | tl = buffer; |
| 54 | memset(tl, 0, TL_SIZE); |
| 55 | |
| 56 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_NON); |
| 57 | |
| 58 | tl = transfer_list_init(tl, TL_SIZE / 2); |
| 59 | |
| 60 | // Add TE's until we run out of space |
| 61 | while ((te = transfer_list_add(tl, test_tag, tl->max_size / 8, |
| 62 | &test_payload))) { |
| 63 | TEST_ASSERT(te = transfer_list_find(tl, test_tag++)); |
| 64 | TEST_ASSERT(*(int *)transfer_list_entry_data(te) == |
| 65 | test_payload++); |
| 66 | } |
| 67 | |
| 68 | // Relocate the TL and make sure all the information we put in is still there. |
| 69 | TEST_ASSERT( |
| 70 | new_tl = transfer_list_relocate(tl, new_buf, tl->max_size * 2)); |
| 71 | TEST_ASSERT(transfer_list_check_header(new_tl) == TL_OPS_ALL); |
| 72 | |
| 73 | unsigned int i = test_tag; |
| 74 | while ((te = transfer_list_find(tl, --i))) { |
| 75 | int *data = transfer_list_entry_data(te); |
| 76 | TEST_ASSERT_NOT_NULL(data); |
| 77 | TEST_ASSERT(*data == --test_payload); |
| 78 | } |
| 79 | |
| 80 | // Add the last TE we failed to add into the relocated TL. |
| 81 | TEST_ASSERT(te = transfer_list_add(new_tl, test_tag, TL_SIZE / 8, |
| 82 | &test_payload)); |
| 83 | TEST_ASSERT(*(int *)transfer_list_find(new_tl, test_tag) = |
| 84 | ++test_payload); |
| 85 | } |
| 86 | |
| 87 | void test_bad_reloc() |
| 88 | { |
| 89 | struct transfer_list_header *tl = transfer_list_init(buffer, TL_SIZE); |
| 90 | void *new_buf = malloc(TL_SIZE); |
| 91 | |
| 92 | /* Relocate to invalid memory address. */ |
| 93 | TEST_ASSERT_NULL(transfer_list_relocate(tl, 0, tl->max_size)); |
| 94 | |
| 95 | /* |
| 96 | * Try relocate to area with insufficent memory, check at the end that we |
| 97 | * still have a valid TL in the original location. |
| 98 | */ |
| 99 | TEST_ASSERT_NULL(transfer_list_relocate(tl, new_buf, 0)); |
| 100 | TEST_ASSERT(transfer_list_check_header(tl) == TL_OPS_ALL); |
| 101 | } |
| 102 | |
| 103 | void setUp(void) |
| 104 | { |
| 105 | buffer = malloc(TL_MAX_SIZE); |
| 106 | } |
| 107 | |
| 108 | void tearDown(void) |
| 109 | { |
| 110 | free(buffer); |
| 111 | buffer = NULL; |
| 112 | } |
| 113 | |
| 114 | int main(void) |
| 115 | { |
| 116 | UNITY_BEGIN(); |
| 117 | RUN_TEST(test_bad_reloc); |
| 118 | RUN_TEST(test_init_alignment); |
| 119 | RUN_TEST(test_init); |
| 120 | RUN_TEST(test_relocate); |
| 121 | return UNITY_END(); |
| 122 | } |