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