test_autoboot.c 2.7 KB

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