1/*
2 * libfdt - Flat Device Tree manipulation
3 *	Tests that fdt_next_subnode() works as expected
4 *
5 * Copyright (C) 2013 Google, Inc
6 *
7 * Copyright (C) 2007 David Gibson, IBM Corporation.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <stdint.h>
28
29#include <libfdt.h>
30
31#include "tests.h"
32#include "testdata.h"
33
34static void test_node(void *fdt, int parent_offset)
35{
36	fdt32_t properties;
37	const fdt32_t *prop;
38	int offset, property;
39	int count;
40	int len;
41
42	/*
43	 * This property indicates the number of properties in our
44	 * test node to expect
45	 */
46	prop = fdt_getprop(fdt, parent_offset, "test-properties", &len);
47	if (!prop || len != sizeof(fdt32_t)) {
48		FAIL("Missing/invalid test-properties property at '%s'",
49		     fdt_get_name(fdt, parent_offset, NULL));
50	}
51	properties = cpu_to_fdt32(*prop);
52
53	count = 0;
54	offset = fdt_first_subnode(fdt, parent_offset);
55	if (offset < 0)
56		FAIL("Missing test node\n");
57
58	fdt_for_each_property_offset(property, fdt, offset)
59		count++;
60
61	if (count != properties) {
62		FAIL("Node '%s': Expected %d properties, got %d\n",
63		     fdt_get_name(fdt, parent_offset, NULL), properties,
64		     count);
65	}
66}
67
68static void check_fdt_next_subnode(void *fdt)
69{
70	int offset;
71	int count = 0;
72
73	fdt_for_each_subnode(offset, fdt, 0) {
74		test_node(fdt, offset);
75		count++;
76	}
77
78	if (count != 2)
79		FAIL("Expected %d tests, got %d\n", 2, count);
80}
81
82int main(int argc, char *argv[])
83{
84	void *fdt;
85
86	test_init(argc, argv);
87	if (argc != 2)
88		CONFIG("Usage: %s <dtb file>", argv[0]);
89
90	fdt = load_blob(argv[1]);
91	if (!fdt)
92		FAIL("No device tree available");
93
94	check_fdt_next_subnode(fdt);
95
96	PASS();
97}
98