xref: /aosp_15_r20/external/dtc/tests/notfound.c (revision cd60bc56d4bea3af4ec04523e4d71c2b272c8aff)
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * libfdt - Flat Device Tree manipulation
4  *	Testcase for behaviour on searching for a non-existent node
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_error(const char * s,int err)17 static void check_error(const char *s, int err)
18 {
19 	if (err != -FDT_ERR_NOTFOUND)
20 		FAIL("%s return error %s instead of -FDT_ERR_NOTFOUND", s,
21 		     fdt_strerror(err));
22 }
23 
main(int argc,char * argv[])24 int main(int argc, char *argv[])
25 {
26 	void *fdt;
27 	int offset;
28 	int subnode1_offset;
29 	int lenerr;
30 
31 	test_init(argc, argv);
32 	fdt = load_blob_arg(argc, argv);
33 
34 	fdt_get_property(fdt, 0, "nonexistant-property", &lenerr);
35 	check_error("fdt_get_property(\"nonexistant-property\")", lenerr);
36 
37 	fdt_getprop(fdt, 0, "nonexistant-property", &lenerr);
38 	check_error("fdt_getprop(\"nonexistant-property\"", lenerr);
39 
40 	subnode1_offset = fdt_subnode_offset(fdt, 0, "subnode@1");
41 	if (subnode1_offset < 0)
42 		FAIL("Couldn't find subnode1: %s", fdt_strerror(subnode1_offset));
43 
44 	fdt_getprop(fdt, subnode1_offset, "prop-str", &lenerr);
45 	check_error("fdt_getprop(\"prop-str\")", lenerr);
46 
47 	offset = fdt_subnode_offset(fdt, 0, "nonexistant-subnode");
48 	check_error("fdt_subnode_offset(\"nonexistant-subnode\")", offset);
49 
50 	offset = fdt_subnode_offset(fdt, 0, "subsubnode");
51 	check_error("fdt_subnode_offset(\"subsubnode\")", offset);
52 
53 	offset = fdt_path_offset(fdt, "/nonexistant-subnode");
54 	check_error("fdt_path_offset(\"/nonexistant-subnode\")", offset);
55 
56 	PASS();
57 }
58