xref: /netbsd-src/external/gpl2/dtc/dist/tests/get_name.c (revision cc7d2833ecf67da5a5ddc470841931eb9f6723e4)
1 /*	$NetBSD: get_name.c,v 1.1.1.3 2019/12/22 12:34:07 skrll Exp $	*/
2 
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 /*
5  * libfdt - Flat Device Tree manipulation
6  *	Testcase for fdt_get_name()
7  * Copyright (C) 2006 David Gibson, IBM Corporation.
8  */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdint.h>
13 
14 #include <libfdt.h>
15 
16 #include "tests.h"
17 #include "testdata.h"
18 
check_name(void * fdt,const char * path)19 static void check_name(void *fdt, const char *path)
20 {
21 	int offset;
22 	const char *getname, *getname2, *checkname;
23 	int len;
24 
25 	checkname = strrchr(path, '/');
26 	if (!checkname)
27 		TEST_BUG();
28 	checkname += 1;
29 
30 	offset = fdt_path_offset(fdt, path);
31 	if (offset < 0)
32 		FAIL("Couldn't find %s", path);
33 
34 	getname = fdt_get_name(fdt, offset, &len);
35 	verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n",
36 		       offset, getname, len);
37 	if (!getname)
38 		FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(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 (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