| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "efivars.h" |
| 6 |
#include "errno-util.h" |
| 7 |
#include "log.h" |
| 8 |
#include "sleep-config.h" |
| 9 |
#include "strv.h" |
| 10 |
#include "tests.h" |
| 11 |
|
| 12 |
TEST(parse_sleep_config) { |
| 13 |
_cleanup_(sleep_config_freep) SleepConfig *sleep_config = NULL; |
| 14 |
|
| 15 |
assert_se(parse_sleep_config(&sleep_config) == 0); |
| 16 |
|
| 17 |
_cleanup_free_ char *sum = NULL, *sus = NULL, *him = NULL, *his = NULL, *hym = NULL, *hys = NULL; |
| 18 |
|
| 19 |
sum = strv_join(sleep_config->modes[SLEEP_SUSPEND], ", "); |
| 20 |
sus = strv_join(sleep_config->states[SLEEP_SUSPEND], ", "); |
| 21 |
him = strv_join(sleep_config->modes[SLEEP_HIBERNATE], ", "); |
| 22 |
his = strv_join(sleep_config->states[SLEEP_HIBERNATE], ", "); |
| 23 |
hym = strv_join(sleep_config->modes[SLEEP_HYBRID_SLEEP], ", "); |
| 24 |
hys = strv_join(sleep_config->states[SLEEP_HYBRID_SLEEP], ", "); |
| 25 |
log_debug(" allow_suspend: %s", yes_no(sleep_config->allow[SLEEP_SUSPEND])); |
| 26 |
log_debug(" allow_hibernate: %s", yes_no(sleep_config->allow[SLEEP_HIBERNATE])); |
| 27 |
log_debug(" allow_s2h: %s", yes_no(sleep_config->allow[SLEEP_SUSPEND_THEN_HIBERNATE])); |
| 28 |
log_debug(" allow_hybrid_sleep: %s", yes_no(sleep_config->allow[SLEEP_HYBRID_SLEEP])); |
| 29 |
log_debug(" suspend modes: %s", sum); |
| 30 |
log_debug(" states: %s", sus); |
| 31 |
log_debug(" hibernate modes: %s", him); |
| 32 |
log_debug(" states: %s", his); |
| 33 |
log_debug(" hybrid modes: %s", hym); |
| 34 |
log_debug(" states: %s", hys); |
| 35 |
} |
| 36 |
|
| 37 |
TEST(sleep_supported) { |
| 38 |
_cleanup_strv_free_ char |
| 39 |
**standby = strv_new("standby"), |
| 40 |
**mem = strv_new("mem"), |
| 41 |
**disk = strv_new("disk"), |
| 42 |
**suspend = strv_new("suspend"), |
| 43 |
**reboot = strv_new("reboot"), |
| 44 |
**platform = strv_new("platform"), |
| 45 |
**shutdown = strv_new("shutdown"), |
| 46 |
**freeze = strv_new("freeze"); |
| 47 |
int r; |
| 48 |
|
| 49 |
printf("Secure boot: %sd\n", enable_disable(is_efi_secure_boot())); |
| 50 |
|
| 51 |
log_info("/= individual sleep modes =/"); |
| 52 |
log_info("Standby configured: %s", yes_no(sleep_state_supported(standby) > 0)); |
| 53 |
log_info("Suspend configured: %s", yes_no(sleep_state_supported(mem) > 0)); |
| 54 |
log_info("Hibernate configured: %s", yes_no(sleep_state_supported(disk) > 0)); |
| 55 |
log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", suspend) > 0)); |
| 56 |
log_info("Hibernate+Reboot configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", reboot) > 0)); |
| 57 |
log_info("Hibernate+Platform configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", platform) > 0)); |
| 58 |
log_info("Hibernate+Shutdown configured: %s", yes_no(sleep_mode_supported("/sys/power/disk", shutdown) > 0)); |
| 59 |
log_info("Freeze configured: %s", yes_no(sleep_state_supported(freeze) > 0)); |
| 60 |
|
| 61 |
log_info("/= high-level sleep verbs =/"); |
| 62 |
r = sleep_supported(SLEEP_SUSPEND); |
| 63 |
log_info("Suspend configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r)); |
| 64 |
r = sleep_supported(SLEEP_HIBERNATE); |
| 65 |
log_info("Hibernation configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r)); |
| 66 |
r = sleep_supported(SLEEP_HYBRID_SLEEP); |
| 67 |
log_info("Hybrid-sleep configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r)); |
| 68 |
r = sleep_supported(SLEEP_SUSPEND_THEN_HIBERNATE); |
| 69 |
log_info("Suspend-then-Hibernate configured and possible: %s", r >= 0 ? yes_no(r) : STRERROR(r)); |
| 70 |
} |
| 71 |
|
| 72 |
static int intro(void) { |
| 73 |
if (getuid() != 0) |
| 74 |
log_warning("This program is unlikely to work for unprivileged users"); |
| 75 |
|
| 76 |
return EXIT_SUCCESS; |
| 77 |
} |
| 78 |
|
| 79 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |