Heinrich Schuchardt | e1eb319 | 2020-07-01 20:01:53 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 4 | */ |
| 5 | |
Heinrich Schuchardt | e1eb319 | 2020-07-01 20:01:53 +0200 | [diff] [blame] | 6 | #include <efi_loader.h> |
| 7 | #include <test/lib.h> |
| 8 | #include <test/test.h> |
| 9 | #include <test/ut.h> |
| 10 | |
| 11 | #define UT_REG_CAPACITY 6 |
| 12 | |
| 13 | static int lib_test_efi_image_region_add(struct unit_test_state *uts) |
| 14 | { |
| 15 | struct efi_image_regions *regs; |
| 16 | |
| 17 | regs = calloc(sizeof(*regs) + |
| 18 | sizeof(struct image_region) * UT_REG_CAPACITY, 1); |
| 19 | ut_assert(regs); |
| 20 | |
| 21 | regs->max = UT_REG_CAPACITY; |
| 22 | |
| 23 | ut_asserteq(0, regs->num); |
| 24 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 25 | efi_image_region_add(regs, (void *)0x4000, |
| 26 | (void *)0x3000, 1)); |
| 27 | ut_asserteq(0, regs->num); |
| 28 | ut_asserteq_64(EFI_SUCCESS, |
| 29 | efi_image_region_add(regs, (void *)0x3100, |
| 30 | (void *)0x4000, 1)); |
| 31 | ut_asserteq(1, regs->num); |
| 32 | ut_asserteq_64(EFI_SUCCESS, |
| 33 | efi_image_region_add(regs, (void *)0x2000, |
| 34 | (void *)0x3100, 1)); |
| 35 | ut_asserteq(2, regs->num); |
| 36 | ut_asserteq_64(EFI_SUCCESS, |
| 37 | efi_image_region_add(regs, (void *)0x1000, |
| 38 | (void *)0x1f00, 1)); |
| 39 | ut_asserteq(3, regs->num); |
| 40 | ut_asserteq_64(EFI_SUCCESS, |
| 41 | efi_image_region_add(regs, (void *)0x4000, |
| 42 | (void *)0x4e00, 1)); |
| 43 | ut_asserteq(4, regs->num); |
| 44 | ut_asserteq_64(EFI_SUCCESS, |
| 45 | efi_image_region_add(regs, (void *)0x1f00, |
| 46 | (void *)0x2001, 1)); |
| 47 | ut_asserteq(5, regs->num); |
| 48 | |
| 49 | ut_asserteq_ptr((void *)0x3100, regs->reg[0].data); |
| 50 | ut_asserteq(0x0f00, regs->reg[0].size); |
| 51 | |
| 52 | ut_asserteq_ptr((void *)0x2000, regs->reg[1].data); |
| 53 | ut_asserteq(0x1100, regs->reg[1].size); |
| 54 | |
| 55 | ut_asserteq_ptr((void *)0x1000, regs->reg[2].data); |
| 56 | ut_asserteq(0x0f00, regs->reg[2].size); |
| 57 | |
| 58 | ut_asserteq_ptr((void *)0x4000, regs->reg[3].data); |
| 59 | ut_asserteq(0x0e00, regs->reg[3].size); |
| 60 | |
| 61 | ut_asserteq_ptr((void *)0x1f00, regs->reg[4].data); |
| 62 | ut_asserteq(0x0101, regs->reg[4].size); |
| 63 | |
| 64 | free(regs); |
| 65 | |
| 66 | return 0; |
| 67 | } |
Heinrich Schuchardt | e1eb319 | 2020-07-01 20:01:53 +0200 | [diff] [blame] | 68 | LIB_TEST(lib_test_efi_image_region_add, 0); |
| 69 | |
| 70 | static int lib_test_efi_image_region_sort(struct unit_test_state *uts) |
| 71 | { |
| 72 | struct efi_image_regions *regs; |
| 73 | |
| 74 | regs = calloc(sizeof(*regs) + |
| 75 | sizeof(struct image_region) * UT_REG_CAPACITY, 1); |
| 76 | ut_assert(regs); |
| 77 | |
| 78 | regs->max = UT_REG_CAPACITY; |
| 79 | |
| 80 | ut_asserteq(0, regs->num); |
| 81 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 82 | efi_image_region_add(regs, (void *)0x4000, |
| 83 | (void *)0x3000, 0)); |
| 84 | ut_asserteq(0, regs->num); |
| 85 | ut_asserteq_64(EFI_SUCCESS, |
| 86 | efi_image_region_add(regs, (void *)0x3100, |
| 87 | (void *)0x4000, 0)); |
| 88 | ut_asserteq(1, regs->num); |
| 89 | ut_asserteq_64(EFI_SUCCESS, |
| 90 | efi_image_region_add(regs, (void *)0x2000, |
| 91 | (void *)0x3100, 0)); |
| 92 | ut_asserteq(2, regs->num); |
| 93 | ut_asserteq_64(EFI_SUCCESS, |
| 94 | efi_image_region_add(regs, (void *)0x1000, |
| 95 | (void *)0x1f00, 0)); |
| 96 | ut_asserteq(3, regs->num); |
| 97 | ut_asserteq_64(EFI_SUCCESS, |
| 98 | efi_image_region_add(regs, (void *)0x4000, |
| 99 | (void *)0x4e00, 0)); |
| 100 | ut_asserteq(4, regs->num); |
| 101 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 102 | efi_image_region_add(regs, (void *)0x1f00, |
| 103 | (void *)0x2001, 0)); |
| 104 | ut_asserteq(4, regs->num); |
| 105 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 106 | efi_image_region_add(regs, (void *)0x10ff, |
| 107 | (void *)0x11ff, 0)); |
| 108 | ut_asserteq(4, regs->num); |
| 109 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 110 | efi_image_region_add(regs, (void *)0x0000, |
| 111 | (void *)0x6000, 0)); |
| 112 | ut_asserteq(4, regs->num); |
| 113 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 114 | efi_image_region_add(regs, (void *)0x3100, |
| 115 | (void *)0x0e00, 0)); |
| 116 | ut_asserteq(4, regs->num); |
| 117 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 118 | efi_image_region_add(regs, (void *)0x3200, |
| 119 | (void *)0x0e00, 0)); |
| 120 | ut_asserteq(4, regs->num); |
| 121 | ut_asserteq_64(EFI_INVALID_PARAMETER, |
| 122 | efi_image_region_add(regs, (void *)0x3200, |
| 123 | (void *)0x0d00, 0)); |
| 124 | ut_asserteq(4, regs->num); |
| 125 | ut_asserteq_64(EFI_SUCCESS, |
| 126 | efi_image_region_add(regs, (void *)0x1f00, |
| 127 | (void *)0x2000, 0)); |
| 128 | ut_asserteq(5, regs->num); |
| 129 | ut_asserteq_64(EFI_SUCCESS, |
| 130 | efi_image_region_add(regs, (void *)0x4000, |
| 131 | (void *)0x4000, 0)); |
| 132 | ut_asserteq(6, regs->num); |
| 133 | ut_asserteq_64(EFI_OUT_OF_RESOURCES, |
| 134 | efi_image_region_add(regs, (void *)0x6000, |
| 135 | (void *)0x0100, 0)); |
| 136 | ut_asserteq(6, regs->num); |
| 137 | |
| 138 | ut_asserteq_ptr((void *)0x1000, regs->reg[0].data); |
| 139 | ut_asserteq(0x0f00, regs->reg[0].size); |
| 140 | |
| 141 | ut_asserteq_ptr((void *)0x1f00, regs->reg[1].data); |
| 142 | ut_asserteq(0x0100, regs->reg[1].size); |
| 143 | |
| 144 | ut_asserteq_ptr((void *)0x2000, regs->reg[2].data); |
| 145 | ut_asserteq(0x1100, regs->reg[2].size); |
| 146 | |
| 147 | ut_asserteq_ptr((void *)0x3100, regs->reg[3].data); |
| 148 | ut_asserteq(0x0f00, regs->reg[3].size); |
| 149 | |
| 150 | ut_asserteq_ptr((void *)0x4000, regs->reg[4].data); |
| 151 | ut_asserteq(0x0000, regs->reg[4].size); |
| 152 | |
| 153 | ut_asserteq_ptr((void *)0x4000, regs->reg[5].data); |
| 154 | ut_asserteq(0x0e00, regs->reg[5].size); |
| 155 | |
| 156 | free(regs); |
| 157 | |
| 158 | return 0; |
| 159 | } |
Heinrich Schuchardt | e1eb319 | 2020-07-01 20:01:53 +0200 | [diff] [blame] | 160 | LIB_TEST(lib_test_efi_image_region_sort, 0); |