Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2021 Steffen Jaeckel |
| 4 | * |
| 5 | * Unit tests for autoboot functionality |
| 6 | */ |
| 7 | |
| 8 | #include <autoboot.h> |
Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 9 | #include <test/common.h> |
| 10 | #include <test/test.h> |
| 11 | #include <test/ut.h> |
| 12 | |
| 13 | #include <crypt.h> |
| 14 | |
| 15 | static int check_for_input(struct unit_test_state *uts, const char *in, |
| 16 | bool correct) |
| 17 | { |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 18 | bool old_val; |
Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 19 | /* The bootdelay is set to 1 second in test_autoboot() */ |
| 20 | const char *autoboot_prompt = |
| 21 | "Enter password \"a\" in 1 seconds to stop autoboot"; |
| 22 | |
| 23 | console_record_reset_enable(); |
| 24 | console_in_puts(in); |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 25 | |
| 26 | /* turn on keyed autoboot for the test, if possible */ |
| 27 | old_val = autoboot_set_keyed(true); |
Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 28 | autoboot_command("echo Autoboot password unlock not successful"); |
Simon Glass | d8c6017 | 2021-07-24 15:14:39 -0600 | [diff] [blame] | 29 | old_val = autoboot_set_keyed(old_val); |
| 30 | |
Steffen Jaeckel | e1788f9 | 2021-07-08 15:57:40 +0200 | [diff] [blame] | 31 | ut_assert_nextline(autoboot_prompt); |
| 32 | if (!correct) |
| 33 | ut_assert_nextline("Autoboot password unlock not successful"); |
| 34 | ut_assert_console_end(); |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * test_autoboot() - unit test for autoboot |
| 40 | * |
| 41 | * @uts: unit test state |
| 42 | * Return: 0 = success, 1 = failure |
| 43 | */ |
| 44 | static int test_autoboot(struct unit_test_state *uts) |
| 45 | { |
| 46 | /* make sure that the bootdelay is set to something, |
| 47 | * otherwise the called functions will time out |
| 48 | */ |
| 49 | ut_assertok(env_set("bootdelay", "1")); |
| 50 | bootdelay_process(); |
| 51 | |
| 52 | /* unset all relevant environment variables */ |
| 53 | env_set("bootstopusesha256", NULL); |
| 54 | env_set("bootstopkeycrypt", NULL); |
| 55 | env_set("bootstopkeysha256", NULL); |
| 56 | |
| 57 | if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) { |
| 58 | /* test the default password from CONFIG_AUTOBOOT_STOP_STR_CRYPT */ |
| 59 | ut_assertok(check_for_input(uts, "a\n", true)); |
| 60 | /* test a password from the `bootstopkeycrypt` environment variable */ |
| 61 | ut_assertok(env_set( |
| 62 | "bootstopkeycrypt", |
| 63 | "$5$rounds=640000$ycgRgpnRq4lmu.eb$aZ6YJWdklvyLML13w7mEHMHJnJOux6aptnp6VlsR5a9")); |
| 64 | |
| 65 | ut_assertok(check_for_input(uts, "test\n", true)); |
| 66 | |
| 67 | /* verify that the `bootstopusesha256` variable is treated correctly */ |
| 68 | ut_assertok(env_set("bootstopusesha256", "false")); |
| 69 | ut_assertok(check_for_input(uts, "test\n", true)); |
| 70 | } |
| 71 | |
| 72 | if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) { |
| 73 | /* test the `bootstopusesha256` and `bootstopkeysha256` features */ |
| 74 | ut_assertok(env_set("bootstopusesha256", "true")); |
| 75 | ut_assertok(env_set( |
| 76 | "bootstopkeysha256", |
| 77 | "edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb")); |
| 78 | |
| 79 | ut_assertok(check_for_input(uts, "abc\n", true)); |
| 80 | |
| 81 | ut_assertok(env_set( |
| 82 | "bootstopkeysha256", |
| 83 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")); |
| 84 | |
| 85 | ut_assertok(check_for_input(uts, "abc", true)); |
| 86 | |
| 87 | ut_assertok(check_for_input(uts, "abc\n", true)); |
| 88 | |
| 89 | ut_assertok(check_for_input(uts, "abd", false)); |
| 90 | } |
| 91 | |
| 92 | return CMD_RET_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | COMMON_TEST(test_autoboot, 0); |