summaryrefslogtreecommitdiff
path: root/tests/check_path.c
blob: c3ae15d4f6d0fc25b8239f52d288af6bac265d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * libfdt - Flat Device Tree manipulation
 *	Testcase for node existence
 * Copyright (C) 2016 Konsulko Inc.
 */

#include <stdio.h>

#include <libfdt.h>

#include "tests.h"

#define CHECK(code) \
	{ \
		int err = (code); \
		if (err) \
			FAIL(#code ": %s", fdt_strerror(err)); \
	}

/* 4k ought to be enough for anybody */
#define FDT_COPY_SIZE	(4 * 1024)

static void *open_dt(char *path)
{
	void *dt, *copy;

	dt = load_blob(path);
	copy = xmalloc(FDT_COPY_SIZE);

	/*
	 * Resize our DTs to 4k so that we have room to operate on
	 */
	CHECK(fdt_open_into(dt, copy, FDT_COPY_SIZE));
	free(dt);

	return copy;
}

int main(int argc, char *argv[])
{
	void *fdt_base;
	int fail_config, exists, check_exists;

	test_init(argc, argv);
	fail_config = 0;

	if (argc != 4)
		fail_config = 1;

	if (!fail_config) {
		if (!strcmp(argv[2], "exists"))
			check_exists = 1;
		else if (!strcmp(argv[2], "not-exists"))
			check_exists = 0;
		else
			fail_config = 1;
	}

	if (fail_config)
		CONFIG("Usage: %s <base dtb> <[exists|not-exists]> <node-path>", argv[0]);

	fdt_base = open_dt(argv[1]);

	exists = fdt_path_offset(fdt_base, argv[3]) >= 0;

	if (exists == check_exists)
		PASS();
	else
		FAIL();
}