Rob Herring | c4ffc05 | 2019-06-20 15:19:41 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 2 | /* |
| 3 | * libfdt - Flat Device Tree manipulation |
| 4 | * Testcase for fdt_node_check_compatible() |
| 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | #include <stdint.h> |
| 12 | |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 13 | #include <libfdt.h> |
| 14 | |
| 15 | #include "tests.h" |
| 16 | #include "testdata.h" |
| 17 | |
David Gibson | 01a2d8a | 2008-08-04 15:30:13 +1000 | [diff] [blame] | 18 | static void check_compatible(const void *fdt, const char *path, |
| 19 | const char *compat) |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 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 | |
David Gibson | cc392f0 | 2017-10-29 22:56:54 +0100 | [diff] [blame] | 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 | |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 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"); |
David Gibson | cc392f0 | 2017-10-29 22:56:54 +0100 | [diff] [blame] | 62 | check_not_compatible(fdt, "/subnode@1/subsubnode", "subsubnode2"); |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 63 | check_compatible(fdt, "/subnode@2/subsubnode", "subsubnode2"); |
| 64 | check_compatible(fdt, "/subnode@2/subsubnode", "subsubnode"); |
David Gibson | cc392f0 | 2017-10-29 22:56:54 +0100 | [diff] [blame] | 65 | check_not_compatible(fdt, "/subnode@2/subsubnode", "subsubnode1"); |
David Gibson | 32b6bd3 | 2007-10-17 11:23:57 +1000 | [diff] [blame] | 66 | |
| 67 | PASS(); |
| 68 | } |