blob: e3050d02c605b5df7f3962abae052d88cb980029 [file] [log] [blame]
Steffen Jaeckele1788f92021-07-08 15:57:40 +02001// 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 Jaeckele1788f92021-07-08 15:57:40 +02009#include <test/common.h>
10#include <test/test.h>
11#include <test/ut.h>
12
13#include <crypt.h>
14
15static int check_for_input(struct unit_test_state *uts, const char *in,
16 bool correct)
17{
Simon Glassd8c60172021-07-24 15:14:39 -060018 bool old_val;
Steffen Jaeckele1788f92021-07-08 15:57:40 +020019 /* 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
Steffen Jaeckele1788f92021-07-08 15:57:40 +020023 console_in_puts(in);
Simon Glassd8c60172021-07-24 15:14:39 -060024
25 /* turn on keyed autoboot for the test, if possible */
26 old_val = autoboot_set_keyed(true);
Steffen Jaeckele1788f92021-07-08 15:57:40 +020027 autoboot_command("echo Autoboot password unlock not successful");
Simon Glassd8c60172021-07-24 15:14:39 -060028 old_val = autoboot_set_keyed(old_val);
29
Steffen Jaeckele1788f92021-07-08 15:57:40 +020030 ut_assert_nextline(autoboot_prompt);
31 if (!correct)
32 ut_assert_nextline("Autoboot password unlock not successful");
33 ut_assert_console_end();
34 return 0;
35}
36
37/**
38 * test_autoboot() - unit test for autoboot
39 *
40 * @uts: unit test state
41 * Return: 0 = success, 1 = failure
42 */
43static int test_autoboot(struct unit_test_state *uts)
44{
45 /* make sure that the bootdelay is set to something,
46 * otherwise the called functions will time out
47 */
48 ut_assertok(env_set("bootdelay", "1"));
49 bootdelay_process();
50
51 /* unset all relevant environment variables */
52 env_set("bootstopusesha256", NULL);
53 env_set("bootstopkeycrypt", NULL);
54 env_set("bootstopkeysha256", NULL);
55
56 if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
57 /* test the default password from CONFIG_AUTOBOOT_STOP_STR_CRYPT */
58 ut_assertok(check_for_input(uts, "a\n", true));
59 /* test a password from the `bootstopkeycrypt` environment variable */
60 ut_assertok(env_set(
61 "bootstopkeycrypt",
62 "$5$rounds=640000$ycgRgpnRq4lmu.eb$aZ6YJWdklvyLML13w7mEHMHJnJOux6aptnp6VlsR5a9"));
63
64 ut_assertok(check_for_input(uts, "test\n", true));
65
66 /* verify that the `bootstopusesha256` variable is treated correctly */
67 ut_assertok(env_set("bootstopusesha256", "false"));
68 ut_assertok(check_for_input(uts, "test\n", true));
69 }
70
71 if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
72 /* test the `bootstopusesha256` and `bootstopkeysha256` features */
73 ut_assertok(env_set("bootstopusesha256", "true"));
74 ut_assertok(env_set(
75 "bootstopkeysha256",
76 "edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb"));
77
78 ut_assertok(check_for_input(uts, "abc\n", true));
79
80 ut_assertok(env_set(
81 "bootstopkeysha256",
82 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
83
84 ut_assertok(check_for_input(uts, "abc", true));
85
86 ut_assertok(check_for_input(uts, "abc\n", true));
87
88 ut_assertok(check_for_input(uts, "abd", false));
89 }
90
91 return CMD_RET_SUCCESS;
92}
Simon Glasscbb230e2024-08-22 07:58:02 -060093COMMON_TEST(test_autoboot, UTF_CONSOLE);