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