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 
8 #include <common.h>
9 #include <efi_loader.h>
10 #include <test/lib.h>
11 #include <test/test.h>
12 #include <test/ut.h>
13 
lib_test_efi_dp_check_length(struct unit_test_state * uts)14 static int lib_test_efi_dp_check_length(struct unit_test_state *uts)
15 {
16 	/* end of device path */
17 	u8 d1[] __aligned(2) = {
18 		0x7f, 0xff, 0x04, 0x00 };
19 	/* device path node with length less then 4 */
20 	u8 d2[] __aligned(2) = {
21 		0x01, 0x02, 0x02, 0x00, 0x04, 0x00, 0x7f, 0xff, 0x04, 0x00 };
22 	/* well formed device path */
23 	u8 d3[] __aligned(2) = {
24 		0x03, 0x02, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00,
25 		0x7f, 0xff, 0x04, 0x00 };
26 
27 	struct efi_device_path *p1 = (struct efi_device_path *)d1;
28 	struct efi_device_path *p2 = (struct efi_device_path *)d2;
29 	struct efi_device_path *p3 = (struct efi_device_path *)d3;
30 
31 	ut_asserteq((ssize_t)-EINVAL, efi_dp_check_length(p1, SIZE_MAX));
32 	ut_asserteq((ssize_t)sizeof(d1), efi_dp_check_length(p1, sizeof(d1)));
33 	ut_asserteq((ssize_t)sizeof(d1),
34 		    efi_dp_check_length(p1, sizeof(d1) + 4));
35 	ut_asserteq((ssize_t)-1, efi_dp_check_length(p1, sizeof(d1) - 1));
36 
37 	ut_asserteq((ssize_t)-1, efi_dp_check_length(p2, sizeof(d2)));
38 
39 	ut_asserteq((ssize_t)-1, efi_dp_check_length(p3, sizeof(d3) - 1));
40 	ut_asserteq((ssize_t)sizeof(d3), efi_dp_check_length(p3, sizeof(d3)));
41 	ut_asserteq((ssize_t)sizeof(d3), efi_dp_check_length(p3, SSIZE_MAX));
42 	ut_asserteq((ssize_t)-EINVAL,
43 		    efi_dp_check_length(p3, (size_t)SSIZE_MAX + 1));
44 	ut_asserteq((ssize_t)sizeof(d3),
45 		    efi_dp_check_length(p3, sizeof(d3) + 4));
46 
47 	return 0;
48 }
49 
50 LIB_TEST(lib_test_efi_dp_check_length, 0);
51