]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-bootspec.c
efi: use CMP() more
[thirdparty/systemd.git] / src / test / test-bootspec.c
CommitLineData
1fe368e5
LP
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include "bootspec.h"
4#include "fileio.h"
5#include "path-util.h"
6#include "rm-rf.h"
7#include "tests.h"
8#include "tmpfile-util.h"
9
10TEST_RET(bootspec_sort) {
11
12 static const struct {
13 const char *fname;
14 const char *contents;
15 } entries[] = {
16 {
17 .fname = "a-10.conf",
18 .contents =
19 "title A\n"
20 "version 10\n"
21 "machine-id dd235d00696545768f6f693bfd23b15f\n",
22 },
23 {
24 .fname = "a-5.conf",
25 .contents =
26 "title A\n"
27 "version 5\n"
28 "machine-id dd235d00696545768f6f693bfd23b15f\n",
29 },
30 {
31 .fname = "b.conf",
32 .contents =
33 "title B\n"
34 "version 3\n"
35 "machine-id b75451ad92f94feeab50b0b442768dbd\n",
36 },
37 {
38 .fname = "c.conf",
39 .contents =
40 "title C\n"
41 "sort-key xxxx\n"
42 "version 5\n"
43 "machine-id 309de666fd5044268a9a26541ac93176\n",
44 },
45 {
46 .fname = "cx.conf",
47 .contents =
48 "title C\n"
49 "sort-key xxxx\n"
50 "version 10\n"
51 "machine-id 309de666fd5044268a9a26541ac93176\n",
52 },
53 {
54 .fname = "d.conf",
55 .contents =
56 "title D\n"
57 "sort-key kkkk\n"
58 "version 100\n"
59 "machine-id 81c6e3147cf544c19006af023e22b292\n",
60 },
61 };
62
63 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
64 _cleanup_(boot_config_free) BootConfig config = {};
65
66 assert_se(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d) >= 0);
67
68 for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
69 _cleanup_free_ char *j = NULL;
70
71 j = path_join(d, "/loader/entries/", entries[i].fname);
72 assert_se(j);
73
74 assert_se(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
75 }
76
77 assert_se(boot_entries_load_config(d, NULL, &config) >= 0);
78
79 assert_se(config.n_entries == 6);
80
81 /* First, because has sort key, and its the lowest one */
82 assert_se(streq(config.entries[0].id, "d.conf"));
83
84 /* These two have a sort key, and newest must be first */
85 assert_se(streq(config.entries[1].id, "cx.conf"));
86 assert_se(streq(config.entries[2].id, "c.conf"));
87
88 /* The following ones have no sort key, hence order by version compared ids, lowest first */
89 assert_se(streq(config.entries[3].id, "a-5.conf"));
90 assert_se(streq(config.entries[4].id, "a-10.conf"));
91 assert_se(streq(config.entries[5].id, "b.conf"));
92
93 return 0;
94}
95
96DEFINE_TEST_MAIN(LOG_INFO);