1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Testcase for fdt_node_check_compatible()
5 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12
13 #include <libfdt.h>
14
15 #include "tests.h"
16 #include "testdata.h"
17
check_compatible(const void * fdt,const char * path,const char * compat)18 static void check_compatible(const void *fdt, const char *path,
19 const char *compat)
20 {
21 int offset, err;
22
23 offset = fdt_path_offset(fdt, path);
24 if (offset < 0)
25 FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(offset));
26
27 err = fdt_node_check_compatible(fdt, offset, compat);
28 if (err < 0)
29 FAIL("fdt_node_check_compatible(%s): %s", path,
30 fdt_strerror(err));
31 if (err != 0)
32 FAIL("%s is not compatible with \"%s\"", path, compat);
33 }
34
check_not_compatible(const void * fdt,const char * path,const char * compat)35 static void check_not_compatible(const void *fdt, const char *path,
36 const char *compat)
37 {
38 int offset, err;
39
40 offset = fdt_path_offset(fdt, path);
41 if (offset < 0)
42 FAIL("fdt_path_offset(%s): %s", path, fdt_strerror(offset));
43
44 err = fdt_node_check_compatible(fdt, offset, compat);
45 if (err < 0)
46 FAIL("fdt_node_check_compatible(%s): %s", path,
47 fdt_strerror(err));
48 if (err == 0)
49 FAIL("%s is incorrectly compatible with \"%s\"", path, compat);
50 }
51
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54 void *fdt;
55
56 test_init(argc, argv);
57 fdt = load_blob_arg(argc, argv);
58
59 check_compatible(fdt, "/", "test_tree1");
60 check_compatible(fdt, "/subnode@1/subsubnode", "subsubnode1");
61 check_compatible(fdt, "/subnode@1/subsubnode", "subsubnode");
62 check_not_compatible(fdt, "/subnode@1/subsubnode", "subsubnode2");
63 check_compatible(fdt, "/subnode@2/subsubnode", "subsubnode2");
64 check_compatible(fdt, "/subnode@2/subsubnode", "subsubnode");
65 check_not_compatible(fdt, "/subnode@2/subsubnode", "subsubnode1");
66
67 PASS();
68 }
69