]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-gpt.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-gpt.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "architecture.h"
4 #include "glyph-util.h"
5 #include "gpt.h"
6 #include "log.h"
7 #include "pretty-print.h"
8 #include "strv.h"
9 #include "terminal-util.h"
10 #include "tests.h"
11
12 TEST(gpt_types_against_architectures) {
13 int r;
14
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. */
17
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;
23
24 joined = strjoin(prefix, architecture_to_string(a), suffix);
25 if (!joined)
26 return (void) log_oom();
27
28 r = gpt_partition_type_from_string(joined, &type);
29 if (r < 0) {
30 printf("%s %s\n", RED_CROSS_MARK(), joined);
31 continue;
32 }
33
34 printf("%s %s\n", GREEN_CHECK_MARK(), joined);
35
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);
44
45 ASSERT_EQ(type.arch, a);
46 }
47 }
48
49 TEST(verity_mappings) {
50 for (PartitionDesignator p = 0; p < _PARTITION_DESIGNATOR_MAX; p++) {
51 PartitionDesignator q;
52
53 q = partition_verity_of(p);
54 assert_se(q < 0 || partition_verity_to_data(q) == p);
55
56 q = partition_verity_sig_of(p);
57 assert_se(q < 0 || partition_verity_sig_to_data(q) == p);
58
59 q = partition_verity_to_data(p);
60 assert_se(q < 0 || partition_verity_of(q) == p);
61
62 q = partition_verity_sig_to_data(p);
63 assert_se(q < 0 || partition_verity_sig_of(q) == p);
64 }
65 }
66
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 */
70
71 for (const GptPartitionType *t = gpt_partition_type_table; t->name; t++) {
72 GptPartitionType x, y;
73
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 */
76
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);
81 }
82 }
83
84 TEST(override_architecture) {
85 GptPartitionType x, y;
86
87 ASSERT_GE(gpt_partition_type_from_string("root-x86-64", &x), 0);
88 ASSERT_EQ(x.arch, ARCHITECTURE_X86_64);
89
90 ASSERT_GE(gpt_partition_type_from_string("root-arm64", &y), 0);
91 ASSERT_EQ(y.arch, ARCHITECTURE_ARM64);
92
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);
98
99 /* If the partition type does not have an architecture, nothing should change. */
100
101 ASSERT_GE(gpt_partition_type_from_string("esp", &x), 0);
102 y = x;
103
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);
109 }
110
111 DEFINE_TEST_MAIN(LOG_INFO);