]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-sleep.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / test / test-sleep.c
index 014b1aa7a2f0eea88d54564cb61b9748159408c5..abaae43e7240735267bdbfaa4e21f3588f98a9f2 100644 (file)
@@ -1,4 +1,4 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <fcntl.h>
 #include <inttypes.h>
@@ -7,6 +7,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "efivars.h"
 #include "errno-util.h"
 #include "fd-util.h"
 #include "log.h"
 #include "sleep-config.h"
 #include "strv.h"
 #include "tests.h"
-#include "util.h"
 
-static void test_parse_sleep_config(void) {
+TEST(parse_sleep_config) {
         _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL;
-        log_info("/* %s */", __func__);
 
-        assert(parse_sleep_config(&sleep_config) == 0);
+        assert_se(parse_sleep_config(&sleep_config) == 0);
 
         _cleanup_free_ char *sum, *sus, *him, *his, *hym, *hys;
 
-        sum = strv_join(sleep_config->suspend_modes, ", ");
-        sus = strv_join(sleep_config->suspend_states, ", ");
-        him = strv_join(sleep_config->hibernate_modes, ", ");
-        his = strv_join(sleep_config->hibernate_states, ", ");
-        hym = strv_join(sleep_config->hybrid_modes, ", ");
-        hys = strv_join(sleep_config->hybrid_states, ", ");
-        log_debug("  allow_suspend: %u", sleep_config->allow_suspend);
-        log_debug("  allow_hibernate: %u", sleep_config->allow_hibernate);
-        log_debug("  allow_s2h: %u", sleep_config->allow_s2h);
-        log_debug("  allow_hybrid_sleep: %u", sleep_config->allow_hybrid_sleep);
+        sum = strv_join(sleep_config->modes[SLEEP_SUSPEND], ", ");
+        sus = strv_join(sleep_config->states[SLEEP_SUSPEND], ", ");
+        him = strv_join(sleep_config->modes[SLEEP_HIBERNATE], ", ");
+        his = strv_join(sleep_config->states[SLEEP_HIBERNATE], ", ");
+        hym = strv_join(sleep_config->modes[SLEEP_HYBRID_SLEEP], ", ");
+        hys = strv_join(sleep_config->states[SLEEP_HYBRID_SLEEP], ", ");
+        log_debug("  allow_suspend: %s", yes_no(sleep_config->allow[SLEEP_SUSPEND]));
+        log_debug("  allow_hibernate: %s", yes_no(sleep_config->allow[SLEEP_HIBERNATE]));
+        log_debug("  allow_s2h: %s", yes_no(sleep_config->allow[SLEEP_SUSPEND_THEN_HIBERNATE]));
+        log_debug("  allow_hybrid_sleep: %s", yes_no(sleep_config->allow[SLEEP_HYBRID_SLEEP]));
         log_debug("  suspend modes: %s", sum);
         log_debug("         states: %s", sus);
         log_debug("  hibernate modes: %s", him);
@@ -42,9 +41,9 @@ static void test_parse_sleep_config(void) {
         log_debug("        states: %s", hys);
 }
 
-static int test_fiemap(const char *path) {
+static int test_fiemap_one(const char *path) {
         _cleanup_free_ struct fiemap *fiemap = NULL;
-        _cleanup_close_ int fd = -1;
+        _cleanup_close_ int fd = -EBADF;
         int r;
 
         log_info("/* %s */", __func__);
@@ -70,7 +69,20 @@ static int test_fiemap(const char *path) {
         return 0;
 }
 
-static void test_sleep(void) {
+TEST_RET(fiemap) {
+        int r = 0;
+
+        assert_se(test_fiemap_one(saved_argv[0]) == 0);
+        for (int i = 1; i < saved_argc; i++) {
+                int k = test_fiemap_one(saved_argv[i]);
+                if (r == 0)
+                        r = k;
+        }
+
+        return r;
+}
+
+TEST(sleep) {
         _cleanup_strv_free_ char
                 **standby = strv_new("standby"),
                 **mem = strv_new("mem"),
@@ -82,9 +94,9 @@ static void test_sleep(void) {
                 **freeze = strv_new("freeze");
         int r;
 
-        log_info("/* %s */", __func__);
+        printf("Secure boot: %sd\n", enable_disable(is_efi_secure_boot()));
 
-        log_info("/= configuration =/");
+        log_info("/= individual sleep modes =/");
         log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
         log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
         log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
@@ -94,36 +106,22 @@ static void test_sleep(void) {
         log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
         log_info("Freeze configured: %s", yes_no(can_sleep_state(freeze) > 0));
 
-        log_info("/= running system =/");
-        r = can_sleep("suspend");
-        log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
-        r = can_sleep("hibernate");
-        log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
-        r = can_sleep("hybrid-sleep");
-        log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
-        r = can_sleep("suspend-then-hibernate");
-        log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : strerror_safe(r));
+        log_info("/= high-level sleep verbs =/");
+        r = can_sleep(SLEEP_SUSPEND);
+        log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r));
+        r = can_sleep(SLEEP_HIBERNATE);
+        log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r));
+        r = can_sleep(SLEEP_HYBRID_SLEEP);
+        log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r));
+        r = can_sleep(SLEEP_SUSPEND_THEN_HIBERNATE);
+        log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r));
 }
 
-int main(int argc, char* argv[]) {
-        int i, r = 0, k;
-
-        test_setup_logging(LOG_DEBUG);
-
+static int intro(void) {
         if (getuid() != 0)
                 log_warning("This program is unlikely to work for unprivileged users");
 
-        test_parse_sleep_config();
-        test_sleep();
-
-        if (argc <= 1)
-                assert_se(test_fiemap(argv[0]) == 0);
-        else
-                for (i = 1; i < argc; i++) {
-                        k = test_fiemap(argv[i]);
-                        if (r == 0)
-                                r = k;
-                }
-
-        return r;
+        return EXIT_SUCCESS;
 }
+
+DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);