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