David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Testcase for fdt_get_name() |
| 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public License |
| 8 | * as published by the Free Software Foundation; either version 2.1 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <fdt.h> |
| 26 | #include <libfdt.h> |
| 27 | |
| 28 | #include "tests.h" |
| 29 | #include "testdata.h" |
| 30 | |
| 31 | void check_name(void *fdt, const char *path) |
| 32 | { |
| 33 | int offset; |
| 34 | const char *getname, *getname2, *checkname; |
| 35 | int len; |
| 36 | |
| 37 | checkname = strrchr(path, '/'); |
| 38 | if (!checkname) |
| 39 | TEST_BUG(); |
| 40 | checkname += 1; |
| 41 | |
| 42 | offset = fdt_path_offset(fdt, path); |
| 43 | if (offset < 0) |
| 44 | FAIL("Couldn't find %s", path); |
| 45 | |
| 46 | getname = fdt_get_name(fdt, offset, &len); |
| 47 | verbose_printf("fdt_get_name(%d) returns \"%s\" (len=%d)\n", |
| 48 | offset, getname, len); |
| 49 | if (!getname) |
| 50 | FAIL("fdt_get_name(%d): %s", offset, fdt_strerror(len)); |
| 51 | |
| 52 | if (strcmp(getname, checkname) != 0) |
| 53 | FAIL("fdt_get_name(%s) returned \"%s\" instead of \"%s\"", |
| 54 | path, getname, checkname); |
| 55 | |
| 56 | if (len != strlen(getname)) |
Jon Loeliger | afa8c2e | 2007-10-18 09:49:45 -0500 | [diff] [blame] | 57 | FAIL("fdt_get_name(%s) returned length %d instead of %zd", |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 58 | path, len, strlen(getname)); |
| 59 | |
| 60 | /* Now check that it doesn't break if we omit len */ |
| 61 | getname2 = fdt_get_name(fdt, offset, NULL); |
| 62 | if (!getname2) |
| 63 | FAIL("fdt_get_name(%d, NULL) failed", offset); |
| 64 | if (strcmp(getname2, getname) != 0) |
| 65 | FAIL("fdt_get_name(%d, NULL) returned \"%s\" instead of \"%s\"", |
| 66 | offset, getname2, getname); |
| 67 | } |
| 68 | |
| 69 | int main(int argc, char *argv[]) |
| 70 | { |
| 71 | void *fdt; |
| 72 | |
| 73 | test_init(argc, argv); |
| 74 | fdt = load_blob_arg(argc, argv); |
| 75 | |
| 76 | check_name(fdt, "/"); |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 77 | check_name(fdt, "/subnode@1"); |
| 78 | check_name(fdt, "/subnode@2"); |
| 79 | check_name(fdt, "/subnode@1/subsubnode"); |
| 80 | check_name(fdt, "/subnode@2/subsubnode@0"); |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 81 | |
| 82 | PASS(); |
| 83 | } |