test_autoboot.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* The bootdelay is set to 1 second in test_autoboot() */
  17. const char *autoboot_prompt =
  18. "Enter password \"a\" in 1 seconds to stop autoboot";
  19. console_record_reset_enable();
  20. console_in_puts(in);
  21. autoboot_command("echo Autoboot password unlock not successful");
  22. ut_assert_nextline(autoboot_prompt);
  23. if (!correct)
  24. ut_assert_nextline("Autoboot password unlock not successful");
  25. ut_assert_console_end();
  26. return 0;
  27. }
  28. /**
  29. * test_autoboot() - unit test for autoboot
  30. *
  31. * @uts: unit test state
  32. * Return: 0 = success, 1 = failure
  33. */
  34. static int test_autoboot(struct unit_test_state *uts)
  35. {
  36. /* make sure that the bootdelay is set to something,
  37. * otherwise the called functions will time out
  38. */
  39. ut_assertok(env_set("bootdelay", "1"));
  40. bootdelay_process();
  41. /* unset all relevant environment variables */
  42. env_set("bootstopusesha256", NULL);
  43. env_set("bootstopkeycrypt", NULL);
  44. env_set("bootstopkeysha256", NULL);
  45. if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
  46. /* test the default password from CONFIG_AUTOBOOT_STOP_STR_CRYPT */
  47. ut_assertok(check_for_input(uts, "a\n", true));
  48. /* test a password from the `bootstopkeycrypt` environment variable */
  49. ut_assertok(env_set(
  50. "bootstopkeycrypt",
  51. "$5$rounds=640000$ycgRgpnRq4lmu.eb$aZ6YJWdklvyLML13w7mEHMHJnJOux6aptnp6VlsR5a9"));
  52. ut_assertok(check_for_input(uts, "test\n", true));
  53. /* verify that the `bootstopusesha256` variable is treated correctly */
  54. ut_assertok(env_set("bootstopusesha256", "false"));
  55. ut_assertok(check_for_input(uts, "test\n", true));
  56. }
  57. if (IS_ENABLED(CONFIG_AUTOBOOT_ENCRYPTION)) {
  58. /* test the `bootstopusesha256` and `bootstopkeysha256` features */
  59. ut_assertok(env_set("bootstopusesha256", "true"));
  60. ut_assertok(env_set(
  61. "bootstopkeysha256",
  62. "edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb"));
  63. ut_assertok(check_for_input(uts, "abc\n", true));
  64. ut_assertok(env_set(
  65. "bootstopkeysha256",
  66. "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
  67. ut_assertok(check_for_input(uts, "abc", true));
  68. ut_assertok(check_for_input(uts, "abc\n", true));
  69. ut_assertok(check_for_input(uts, "abd", false));
  70. }
  71. return CMD_RET_SUCCESS;
  72. }
  73. COMMON_TEST(test_autoboot, 0);