efi_device_path.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test device path functions
  4. *
  5. * Copyright (c) 2020 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <efi_loader.h>
  9. #include <test/lib.h>
  10. #include <test/test.h>
  11. #include <test/ut.h>
  12. static int lib_test_efi_dp_check_length(struct unit_test_state *uts)
  13. {
  14. /* end of device path */
  15. u8 d1[] __aligned(2) = {
  16. 0x7f, 0xff, 0x04, 0x00 };
  17. /* device path node with length less then 4 */
  18. u8 d2[] __aligned(2) = {
  19. 0x01, 0x02, 0x02, 0x00, 0x04, 0x00, 0x7f, 0xff, 0x04, 0x00 };
  20. /* well formed device path */
  21. u8 d3[] __aligned(2) = {
  22. 0x03, 0x02, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00,
  23. 0x7f, 0xff, 0x04, 0x00 };
  24. struct efi_device_path *p1 = (struct efi_device_path *)d1;
  25. struct efi_device_path *p2 = (struct efi_device_path *)d2;
  26. struct efi_device_path *p3 = (struct efi_device_path *)d3;
  27. ut_asserteq((ssize_t)-EINVAL, efi_dp_check_length(p1, SIZE_MAX));
  28. ut_asserteq((ssize_t)sizeof(d1), efi_dp_check_length(p1, sizeof(d1)));
  29. ut_asserteq((ssize_t)sizeof(d1),
  30. efi_dp_check_length(p1, sizeof(d1) + 4));
  31. ut_asserteq((ssize_t)-1, efi_dp_check_length(p1, sizeof(d1) - 1));
  32. ut_asserteq((ssize_t)-1, efi_dp_check_length(p2, sizeof(d2)));
  33. ut_asserteq((ssize_t)-1, efi_dp_check_length(p3, sizeof(d3) - 1));
  34. ut_asserteq((ssize_t)sizeof(d3), efi_dp_check_length(p3, sizeof(d3)));
  35. ut_asserteq((ssize_t)sizeof(d3), efi_dp_check_length(p3, SSIZE_MAX));
  36. ut_asserteq((ssize_t)-EINVAL,
  37. efi_dp_check_length(p3, (size_t)SSIZE_MAX + 1));
  38. ut_asserteq((ssize_t)sizeof(d3),
  39. efi_dp_check_length(p3, sizeof(d3) + 4));
  40. return 0;
  41. }
  42. LIB_TEST(lib_test_efi_dp_check_length, 0);