1/*	$NetBSD: tests.h,v 1.4 2019/12/22 12:38:24 skrll Exp $	*/
2
3/* SPDX-License-Identifier: LGPL-2.1-or-later */
4#ifndef TESTS_H
5#define TESTS_H
6/*
7 * libfdt - Flat Device Tree manipulation
8 *	Testcase definitions
9 * Copyright (C) 2006 David Gibson, IBM Corporation.
10 */
11
12#define DEBUG
13
14/* Test return codes */
15#define RC_PASS 	0
16#define RC_CONFIG 	1
17#define RC_FAIL		2
18#define RC_BUG		99
19
20extern int verbose_test;
21extern char *test_name;
22void test_init(int argc, char *argv[]);
23
24#define FDTALIGN2(x, a)	(((x) + (a) - 1) & ~((a) - 1))
25#define PALIGN(p, a)	((void *)FDTALIGN2((unsigned long)(p), (a)))
26#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
27
28#define streq(s1, s2)	(strcmp((s1),(s2)) == 0)
29
30/* Each test case must define this function */
31void cleanup(void);
32
33#define verbose_printf(...) \
34	if (verbose_test) { \
35		printf(__VA_ARGS__); \
36		fflush(stdout); \
37	}
38#define ERR	"ERR: "
39#define ERROR(fmt, args...)	fprintf(stderr, ERR fmt, ## args)
40
41
42#define	PASS()						\
43	do {						\
44		cleanup();				\
45		printf("PASS\n");			\
46		exit(RC_PASS);				\
47	} while (0)
48
49#define	PASS_INCONCLUSIVE()				\
50	do {						\
51		cleanup();				\
52		printf("PASS (inconclusive)\n");	\
53		exit(RC_PASS);				\
54	} while (0)
55
56#define IRRELEVANT()					\
57	do {						\
58		cleanup();				\
59		printf("PASS (irrelevant)\n");		\
60		exit(RC_PASS);				\
61	} while (0)
62
63/* Look out, gcc extension below... */
64#define FAIL(fmt, ...)					\
65	do {						\
66		cleanup();				\
67		printf("FAIL\t" fmt "\n", ##__VA_ARGS__);	\
68		exit(RC_FAIL);				\
69	} while (0)
70
71#define CONFIG(fmt, ...)				\
72	do {						\
73		cleanup();				\
74		printf("Bad configuration: " fmt "\n", ##__VA_ARGS__);	\
75		exit(RC_CONFIG);			\
76	} while (0)
77
78#define TEST_BUG(fmt, ...)				\
79	do {						\
80		cleanup();				\
81		printf("BUG in testsuite: " fmt "\n", ##__VA_ARGS__);	\
82		exit(RC_BUG);				\
83	} while (0)
84
85void check_mem_rsv(void *fdt, int n, uint64_t addr, uint64_t size);
86
87void check_property(void *fdt, int nodeoffset, const char *name,
88		    int len, const void *val);
89#define check_property_cell(fdt, nodeoffset, name, val) \
90	({ \
91		fdt32_t x = cpu_to_fdt32(val);			      \
92		check_property(fdt, nodeoffset, name, sizeof(x), &x); \
93	})
94
95
96const void *check_getprop(void *fdt, int nodeoffset, const char *name,
97			  int len, const void *val);
98#define check_getprop_cell(fdt, nodeoffset, name, val) \
99	({ \
100		fdt32_t x = cpu_to_fdt32(val);			     \
101		check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
102	})
103#define check_getprop_64(fdt, nodeoffset, name, val) \
104	({ \
105		fdt64_t x = cpu_to_fdt64(val);			     \
106		check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
107	})
108#define check_getprop_string(fdt, nodeoffset, name, s) \
109	check_getprop((fdt), (nodeoffset), (name), strlen(s)+1, (s))
110
111/* Returns non-NULL if the property at poffset has the name in_name */
112const void *check_get_prop_offset(void *fdt, int poffset, const char *in_name,
113				  int in_len, const void *in_val);
114#define check_get_prop_offset_cell(fdt, poffset, name, val) \
115	({ \
116		fdt32_t x = cpu_to_fdt32(val);			     \
117		check_get_prop_offset(fdt, poffset, name, sizeof(x), &x); \
118	})
119
120const void *check_getprop_addrrange(void *fdt, int parent, int nodeoffset,
121				    const char *name, int num);
122
123int nodename_eq(const char *s1, const char *s2);
124void vg_prepare_blob(void *fdt, size_t bufsize);
125void *load_blob(const char *filename);
126void *load_blob_arg(int argc, char *argv[]);
127void save_blob(const char *filename, void *blob);
128void *open_blob_rw(void *blob);
129
130#include "util.h"
131
132#endif /* TESTS_H */
133