xref: /aosp_15_r20/external/dtc/tests/get_name.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for fdt_get_name()
5  * Copyright (C) 2006 David Gibson, IBM Corporation.
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdint.h>
11 
12 #include <libfdt.h>
13 
14 #include "tests.h"
15 #include "testdata.h"
16 
check_name(void * fdt,const char * path)17 static void check_name(void *fdt, const char *path)
18 {
19 	int offset;
20 	const char *getname, *getname2, *checkname;
21 	int len;
22 
23 	checkname = strrchr(path, '/');
24 	if (!checkname)
25 		TEST_BUG();
26 	checkname += 1;
27 
28 	offset = fdt_path_offset(fdt, path);
29 	if (offset < 0)
30 		FAIL("Couldn't find %s", path);
31 
32 	getname = fdt_get_name(fdt, offset, &len);
33 	verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
34 		       offset, getname, len);
35 	if (!getname)
36 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len));
37 	if (len < 0)
38 		FAIL("negative name length (%d) for returned node name\n", len);
39 
40 	if (strcmp(getname, checkname) != 0)
41 		FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"",
42 		     path, getname, checkname);
43 
44 	if ((unsigned)len != strlen(getname))
45 		FAIL("fdt_get_name(%s) returned length %d instead of %zd",
46 		     path, len, strlen(getname));
47 
48 	/* Now check that it doesn't break if we omit len */
49 	getname2 = fdt_get_name(fdt, offset, NULL);
50 	if (!getname2)
51 		FAIL("fdt_get_name(%d, NULL) failed", offset);
52 	if (strcmp(getname2, getname) != 0)
53 		FAIL("fdt_get_name(%d, NULL) returned \"%s\" instead of \"%s\"",
54 		     offset, getname2, getname);
55 }
56 
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59 	void *fdt;
60 
61 	test_init(argc, argv);
62 	fdt = load_blob_arg(argc, argv);
63 
64 	check_name(fdt, "/");
65 	check_name(fdt, "/subnode@1");
66 	check_name(fdt, "/subnode@2");
67 	check_name(fdt, "/subnode@1/subsubnode");
68 	check_name(fdt, "/subnode@2/subsubnode@0");
69 
70 	PASS();
71 }
72