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