1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "architecture.h"
4 #include "glyph-util.h"
7 #include "pretty-print.h"
9 #include "terminal-util.h"
12 TEST(gpt_types_against_architectures
) {
15 /* Dumps a table indicating for which architectures we know we have matching GPT partition
16 * types. Also validates whether we can properly categorize the entries. */
18 FOREACH_STRING(prefix
, "root-", "usr-")
19 for (Architecture a
= 0; a
< _ARCHITECTURE_MAX
; a
++)
20 FOREACH_STRING(suffix
, "", "-verity", "-verity-sig") {
21 _cleanup_free_
char *joined
= NULL
;
22 GptPartitionType type
;
24 joined
= strjoin(prefix
, architecture_to_string(a
), suffix
);
26 return (void) log_oom();
28 r
= gpt_partition_type_from_string(joined
, &type
);
30 printf("%s %s\n", RED_CROSS_MARK(), joined
);
34 printf("%s %s\n", GREEN_CHECK_MARK(), joined
);
36 if (streq(prefix
, "root-") && streq(suffix
, ""))
37 ASSERT_EQ(type
.designator
, PARTITION_ROOT
);
38 if (streq(prefix
, "root-") && streq(suffix
, "-verity"))
39 ASSERT_EQ(type
.designator
, PARTITION_ROOT_VERITY
);
40 if (streq(prefix
, "usr-") && streq(suffix
, ""))
41 ASSERT_EQ(type
.designator
, PARTITION_USR
);
42 if (streq(prefix
, "usr-") && streq(suffix
, "-verity"))
43 ASSERT_EQ(type
.designator
, PARTITION_USR_VERITY
);
45 ASSERT_EQ(type
.arch
, a
);
49 TEST(verity_mappings
) {
50 for (PartitionDesignator p
= 0; p
< _PARTITION_DESIGNATOR_MAX
; p
++) {
51 PartitionDesignator q
;
53 q
= partition_verity_of(p
);
54 assert_se(q
< 0 || partition_verity_to_data(q
) == p
);
56 q
= partition_verity_sig_of(p
);
57 assert_se(q
< 0 || partition_verity_sig_to_data(q
) == p
);
59 q
= partition_verity_to_data(p
);
60 assert_se(q
< 0 || partition_verity_of(q
) == p
);
62 q
= partition_verity_sig_to_data(p
);
63 assert_se(q
< 0 || partition_verity_sig_of(q
) == p
);
67 TEST(type_alias_same
) {
68 /* Check that the partition type table is consistent, i.e. all aliases of the same partition type
69 * carry the same metadata */
71 for (const GptPartitionType
*t
= gpt_partition_type_table
; t
->name
; t
++) {
72 GptPartitionType x
, y
;
74 x
= gpt_partition_type_from_uuid(t
->uuid
); /* search first by uuid */
75 ASSERT_GE(gpt_partition_type_from_string(t
->name
, &y
), 0); /* search first by name */
77 ASSERT_EQ(t
->arch
, x
.arch
);
78 ASSERT_EQ(t
->arch
, y
.arch
);
79 ASSERT_EQ(t
->designator
, x
.designator
);
80 ASSERT_EQ(t
->designator
, y
.designator
);
84 TEST(override_architecture
) {
85 GptPartitionType x
, y
;
87 ASSERT_GE(gpt_partition_type_from_string("root-x86-64", &x
), 0);
88 ASSERT_EQ(x
.arch
, ARCHITECTURE_X86_64
);
90 ASSERT_GE(gpt_partition_type_from_string("root-arm64", &y
), 0);
91 ASSERT_EQ(y
.arch
, ARCHITECTURE_ARM64
);
93 x
= gpt_partition_type_override_architecture(x
, ARCHITECTURE_ARM64
);
94 ASSERT_EQ(x
.arch
, y
.arch
);
95 ASSERT_EQ(x
.designator
, y
.designator
);
96 assert_se(sd_id128_equal(x
.uuid
, y
.uuid
));
97 ASSERT_STREQ(x
.name
, y
.name
);
99 /* If the partition type does not have an architecture, nothing should change. */
101 ASSERT_GE(gpt_partition_type_from_string("esp", &x
), 0);
104 x
= gpt_partition_type_override_architecture(x
, ARCHITECTURE_ARM64
);
105 ASSERT_EQ(x
.arch
, y
.arch
);
106 ASSERT_EQ(x
.designator
, y
.designator
);
107 assert_se(sd_id128_equal(x
.uuid
, y
.uuid
));
108 ASSERT_STREQ(x
.name
, y
.name
);
111 DEFINE_TEST_MAIN(LOG_INFO
);