blob: 3ca49dc4a2eb959c29dbbe333ea29763148226a9 [file] [log] [blame]
Heinrich Schuchardte1eb3192020-07-01 20:01:53 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
4 */
5
Heinrich Schuchardte1eb3192020-07-01 20:01:53 +02006#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
13static 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}
68
69LIB_TEST(lib_test_efi_image_region_add, 0);
70
71static int lib_test_efi_image_region_sort(struct unit_test_state *uts)
72{
73 struct efi_image_regions *regs;
74
75 regs = calloc(sizeof(*regs) +
76 sizeof(struct image_region) * UT_REG_CAPACITY, 1);
77 ut_assert(regs);
78
79 regs->max = UT_REG_CAPACITY;
80
81 ut_asserteq(0, regs->num);
82 ut_asserteq_64(EFI_INVALID_PARAMETER,
83 efi_image_region_add(regs, (void *)0x4000,
84 (void *)0x3000, 0));
85 ut_asserteq(0, regs->num);
86 ut_asserteq_64(EFI_SUCCESS,
87 efi_image_region_add(regs, (void *)0x3100,
88 (void *)0x4000, 0));
89 ut_asserteq(1, regs->num);
90 ut_asserteq_64(EFI_SUCCESS,
91 efi_image_region_add(regs, (void *)0x2000,
92 (void *)0x3100, 0));
93 ut_asserteq(2, regs->num);
94 ut_asserteq_64(EFI_SUCCESS,
95 efi_image_region_add(regs, (void *)0x1000,
96 (void *)0x1f00, 0));
97 ut_asserteq(3, regs->num);
98 ut_asserteq_64(EFI_SUCCESS,
99 efi_image_region_add(regs, (void *)0x4000,
100 (void *)0x4e00, 0));
101 ut_asserteq(4, regs->num);
102 ut_asserteq_64(EFI_INVALID_PARAMETER,
103 efi_image_region_add(regs, (void *)0x1f00,
104 (void *)0x2001, 0));
105 ut_asserteq(4, regs->num);
106 ut_asserteq_64(EFI_INVALID_PARAMETER,
107 efi_image_region_add(regs, (void *)0x10ff,
108 (void *)0x11ff, 0));
109 ut_asserteq(4, regs->num);
110 ut_asserteq_64(EFI_INVALID_PARAMETER,
111 efi_image_region_add(regs, (void *)0x0000,
112 (void *)0x6000, 0));
113 ut_asserteq(4, regs->num);
114 ut_asserteq_64(EFI_INVALID_PARAMETER,
115 efi_image_region_add(regs, (void *)0x3100,
116 (void *)0x0e00, 0));
117 ut_asserteq(4, regs->num);
118 ut_asserteq_64(EFI_INVALID_PARAMETER,
119 efi_image_region_add(regs, (void *)0x3200,
120 (void *)0x0e00, 0));
121 ut_asserteq(4, regs->num);
122 ut_asserteq_64(EFI_INVALID_PARAMETER,
123 efi_image_region_add(regs, (void *)0x3200,
124 (void *)0x0d00, 0));
125 ut_asserteq(4, regs->num);
126 ut_asserteq_64(EFI_SUCCESS,
127 efi_image_region_add(regs, (void *)0x1f00,
128 (void *)0x2000, 0));
129 ut_asserteq(5, regs->num);
130 ut_asserteq_64(EFI_SUCCESS,
131 efi_image_region_add(regs, (void *)0x4000,
132 (void *)0x4000, 0));
133 ut_asserteq(6, regs->num);
134 ut_asserteq_64(EFI_OUT_OF_RESOURCES,
135 efi_image_region_add(regs, (void *)0x6000,
136 (void *)0x0100, 0));
137 ut_asserteq(6, regs->num);
138
139 ut_asserteq_ptr((void *)0x1000, regs->reg[0].data);
140 ut_asserteq(0x0f00, regs->reg[0].size);
141
142 ut_asserteq_ptr((void *)0x1f00, regs->reg[1].data);
143 ut_asserteq(0x0100, regs->reg[1].size);
144
145 ut_asserteq_ptr((void *)0x2000, regs->reg[2].data);
146 ut_asserteq(0x1100, regs->reg[2].size);
147
148 ut_asserteq_ptr((void *)0x3100, regs->reg[3].data);
149 ut_asserteq(0x0f00, regs->reg[3].size);
150
151 ut_asserteq_ptr((void *)0x4000, regs->reg[4].data);
152 ut_asserteq(0x0000, regs->reg[4].size);
153
154 ut_asserteq_ptr((void *)0x4000, regs->reg[5].data);
155 ut_asserteq(0x0e00, regs->reg[5].size);
156
157 free(regs);
158
159 return 0;
160}
161
162LIB_TEST(lib_test_efi_image_region_sort, 0);