xref: /netbsd-src/external/gpl2/dtc/dist/tests/get_path.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: get_path.c,v 1.1.1.2 2017/06/08 15:59:26 skrll Exp $	*/
2 
3 /*
4  * libfdt - Flat Device Tree manipulation
5  *	Testcase for fdt_get_path()
6  * Copyright (C) 2006 David Gibson, IBM Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdint.h>
26 
27 #include <libfdt.h>
28 
29 #include "tests.h"
30 #include "testdata.h"
31 
32 #define POISON	('\xff')
33 
34 static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen)
35 {
36 	int offset;
37 	char buf[buflen+1];
38 	int len;
39 
40 	offset = fdt_path_offset(fdt, path);
41 	if (offset < 0)
42 		FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset));
43 
44 	memset(buf, POISON, sizeof(buf)); /* poison the buffer */
45 
46 	len = fdt_get_path(fdt, offset, buf, buflen);
47 	verbose_printf("get_path() %s -> %d -> %s\n", path, offset, buf);
48 
49 	if (buflen <= pathlen) {
50 		if (len != -FDT_ERR_NOSPACE)
51 			FAIL("fdt_get_path([%d bytes]) returns %d with "
52 			     "insufficient buffer space", buflen, len);
53 	} else {
54 		if (len < 0)
55 			FAIL("fdt_get_path([%d bytes]): %s", buflen,
56 			     fdt_strerror(len));
57 		if (len != 0)
58 			FAIL("fdt_get_path([%d bytes]) returns %d "
59 			     "instead of 0", buflen, len);
60 		if (strcmp(buf, path) != 0)
61 			FAIL("fdt_get_path([%d bytes]) returns \"%s\" "
62 			     "instead of \"%s\"", buflen, buf, path);
63 	}
64 
65 	if (buf[buflen] != POISON)
66 		FAIL("fdt_get_path([%d bytes]) overran buffer", buflen);
67 }
68 
69 static void check_path(void *fdt, const char *path)
70 {
71 	int pathlen = strlen(path);
72 
73 	check_path_buf(fdt, path, pathlen, 1024);
74 	check_path_buf(fdt, path, pathlen, pathlen+1);
75 	check_path_buf(fdt, path, pathlen, pathlen);
76 	check_path_buf(fdt, path, pathlen, 0);
77 	check_path_buf(fdt, path, pathlen, 2);
78 }
79 
80 int main(int argc, char *argv[])
81 {
82 	void *fdt;
83 
84 	test_init(argc, argv);
85 	fdt = load_blob_arg(argc, argv);
86 
87 	check_path(fdt, "/");
88 	check_path(fdt, "/subnode@1");
89 	check_path(fdt, "/subnode@2");
90 	check_path(fdt, "/subnode@1/subsubnode");
91 	check_path(fdt, "/subnode@2/subsubnode@0");
92 
93 	PASS();
94 }
95