1 /* $NetBSD: stringlist.c,v 1.1.1.2 2017/06/08 15:59:27 skrll Exp $ */ 2 3 /* 4 * libfdt - Flat Device Tree manipulation 5 * Testcase for string handling 6 * Copyright (C) 2015 NVIDIA Corporation 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public License 10 * as published by the Free Software Foundation; either version 2.1 of 11 * the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23 #include <stdlib.h> 24 #include <stdio.h> 25 #include <string.h> 26 #include <stdint.h> 27 28 #include <libfdt.h> 29 30 #include "tests.h" 31 #include "testdata.h" 32 33 static void check_expected_failure(const void *fdt, const char *path, 34 const char *property) 35 { 36 int offset, err; 37 38 offset = fdt_path_offset(fdt, "/"); 39 if (offset < 0) 40 FAIL("Couldn't find path %s", path); 41 42 err = fdt_stringlist_count(fdt, offset, "#address-cells"); 43 if (err != -FDT_ERR_BADVALUE) 44 FAIL("unexpectedly succeeded in parsing #address-cells\n"); 45 46 err = fdt_stringlist_search(fdt, offset, "#address-cells", "foo"); 47 if (err != -FDT_ERR_BADVALUE) 48 FAIL("found string in #address-cells: %d\n", err); 49 50 /* 51 * Note that the #address-cells property contains a small 32-bit 52 * unsigned integer, hence some bytes will be zero, and searching for 53 * the empty string will succeed. 54 * 55 * The reason for this oddity is that the function will exit when the 56 * first occurrence of the string is found, but in order to determine 57 * that the property does not contain a valid string list it would 58 * need to process the whole value. 59 */ 60 err = fdt_stringlist_search(fdt, offset, "#address-cells", ""); 61 if (err != 0) 62 FAIL("empty string not found in #address-cells: %d\n", err); 63 64 /* 65 * fdt_get_string() can successfully extract strings from non-string 66 * properties. This is because it doesn't necessarily parse the whole 67 * property value, which would be necessary for it to determine if a 68 * valid string or string list is present. 69 */ 70 } 71 72 static void check_string_count(const void *fdt, const char *path, 73 const char *property, int count) 74 { 75 int offset, err; 76 77 offset = fdt_path_offset(fdt, path); 78 if (offset < 0) 79 FAIL("Couldn't find path %s", path); 80 81 err = fdt_stringlist_count(fdt, offset, property); 82 if (err < 0) 83 FAIL("Couldn't count strings in property %s of node %s: %d\n", 84 property, path, err); 85 86 if (err != count) 87 FAIL("String count for property %s of node %s is %d instead of %d\n", 88 path, property, err, count); 89 } 90 91 static void check_string_index(const void *fdt, const char *path, 92 const char *property, const char *string, 93 int idx) 94 { 95 int offset, err; 96 97 offset = fdt_path_offset(fdt, path); 98 if (offset < 0) 99 FAIL("Couldn't find path %s", path); 100 101 err = fdt_stringlist_search(fdt, offset, property, string); 102 103 if (err != idx) 104 FAIL("Index of %s in property %s of node %s is %d, expected %d\n", 105 string, property, path, err, idx); 106 } 107 108 static void check_string(const void *fdt, const char *path, 109 const char *property, int idx, 110 const char *string) 111 { 112 const char *result; 113 int offset, len; 114 115 offset = fdt_path_offset(fdt, path); 116 if (offset < 0) 117 FAIL("Couldn't find path %s", path); 118 119 result = fdt_stringlist_get(fdt, offset, property, idx, &len); 120 if (!result) 121 FAIL("Couldn't extract string %d from property %s of node %s: %d\n", 122 idx, property, path, len); 123 124 if (strcmp(string, result) != 0) 125 FAIL("String %d in property %s of node %s is %s, expected %s\n", 126 idx, property, path, result, string); 127 } 128 129 int main(int argc, char *argv[]) 130 { 131 void *fdt; 132 133 if (argc != 2) 134 CONFIG("Usage: %s <dtb file>\n", argv[0]); 135 136 test_init(argc, argv); 137 fdt = load_blob(argv[1]); 138 139 check_expected_failure(fdt, "/", "#address-cells"); 140 check_expected_failure(fdt, "/", "#size-cells"); 141 142 check_string_count(fdt, "/", "compatible", 1); 143 check_string_count(fdt, "/device", "compatible", 2); 144 check_string_count(fdt, "/device", "big-endian", 0); 145 146 check_string_index(fdt, "/", "compatible", "test-strings", 0); 147 check_string_index(fdt, "/device", "compatible", "foo", 0); 148 check_string_index(fdt, "/device", "compatible", "bar", 1); 149 check_string_index(fdt, "/device", "big-endian", "baz", -1); 150 151 check_string(fdt, "/", "compatible", 0, "test-strings"); 152 check_string(fdt, "/device", "compatible", 0, "foo"); 153 check_string(fdt, "/device", "compatible", 1, "bar"); 154 155 PASS(); 156 } 157