1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 
5 #include "fs-util.h"
6 #include "log.h"
7 #include "os-util.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "tests.h"
11 
TEST(path_is_os_tree)12 TEST(path_is_os_tree) {
13         assert_se(path_is_os_tree("/") > 0);
14         assert_se(path_is_os_tree("/etc") == 0);
15         assert_se(path_is_os_tree("/idontexist") == -ENOENT);
16 }
17 
TEST(parse_os_release)18 TEST(parse_os_release) {
19         /* Let's assume that we're running in a valid system, so os-release is available */
20         _cleanup_free_ char *id = NULL, *id2 = NULL, *name = NULL, *foobar = NULL;
21         assert_se(parse_os_release(NULL, "ID", &id) == 0);
22         log_info("ID: %s", id);
23 
24         assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0);
25         assert_se(parse_os_release(NULL, "ID", &id2) == 0);
26         log_info("ID: %s", strnull(id2));
27 
28         _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX";
29         assert_se(write_tmpfile(tmpfile,
30                                 "ID=the-id  \n"
31                                 "NAME=the-name") == 0);
32 
33         assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0);
34         assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 0);
35         log_info("ID: %s NAME: %s", id, name);
36         assert_se(streq(id, "the-id"));
37         assert_se(streq(name, "the-name"));
38 
39         _cleanup_(unlink_tempfilep) char tmpfile2[] = "/tmp/test-os-util.XXXXXX";
40         assert_se(write_tmpfile(tmpfile2,
41                                 "ID=\"ignored\"  \n"
42                                 "ID=\"the-id\"  \n"
43                                 "NAME='the-name'") == 0);
44 
45         assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile2, 1) == 0);
46         assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 0);
47         log_info("ID: %s NAME: %s", id, name);
48         assert_se(streq(id, "the-id"));
49         assert_se(streq(name, "the-name"));
50 
51         assert_se(parse_os_release(NULL, "FOOBAR", &foobar) == 0);
52         log_info("FOOBAR: %s", strnull(foobar));
53         assert_se(foobar == NULL);
54 
55         assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
56 }
57 
TEST(load_os_release_pairs)58 TEST(load_os_release_pairs) {
59         _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX";
60         assert_se(write_tmpfile(tmpfile,
61                                 "ID=\"ignored\"  \n"
62                                 "ID=\"the-id\"  \n"
63                                 "NAME='the-name'") == 0);
64 
65         assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0);
66 
67         _cleanup_strv_free_ char **pairs = NULL;
68         assert_se(load_os_release_pairs(NULL, &pairs) == 0);
69         assert_se(strv_equal(pairs, STRV_MAKE("ID", "the-id",
70                                               "NAME", "the-name")));
71 
72         assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
73 }
74 
75 DEFINE_TEST_MAIN(LOG_DEBUG);
76