1*0c814320Stb /*	$OpenBSD: parse_test_file.h,v 1.1 2024/12/26 00:04:24 tb Exp $ */
2*0c814320Stb 
3*0c814320Stb /*
4*0c814320Stb  * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
5*0c814320Stb  *
6*0c814320Stb  * Permission to use, copy, modify, and distribute this software for any
7*0c814320Stb  * purpose with or without fee is hereby granted, provided that the above
8*0c814320Stb  * copyright notice and this permission notice appear in all copies.
9*0c814320Stb  *
10*0c814320Stb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*0c814320Stb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*0c814320Stb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*0c814320Stb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*0c814320Stb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*0c814320Stb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*0c814320Stb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*0c814320Stb  */
18*0c814320Stb 
19*0c814320Stb #ifndef PARSE_TEST_FILE_H
20*0c814320Stb #define PARSE_TEST_FILE_H
21*0c814320Stb 
22*0c814320Stb #include <stdint.h>
23*0c814320Stb #include <stdio.h>
24*0c814320Stb 
25*0c814320Stb #include "bytestring.h"
26*0c814320Stb 
27*0c814320Stb #if defined(__cplusplus)
28*0c814320Stb extern "C" {
29*0c814320Stb #endif
30*0c814320Stb 
31*0c814320Stb struct parse;
32*0c814320Stb 
33*0c814320Stb enum line {
34*0c814320Stb 	LINE_STRING_MATCH,	/* Checks if string after label matches. */
35*0c814320Stb 	LINE_HEX,		/* Parses hex into cbb from type2cbb. */
36*0c814320Stb };
37*0c814320Stb 
38*0c814320Stb struct line_spec {
39*0c814320Stb 	int		 state;
40*0c814320Stb 	enum line	 type;
41*0c814320Stb 	const char	*name;
42*0c814320Stb 	const char	*label; /* followed by ": " or " = " */
43*0c814320Stb 	const char	*match;	/* only for LINE_STRING_MATCH */
44*0c814320Stb };
45*0c814320Stb 
46*0c814320Stb struct test_parse {
47*0c814320Stb 	const struct line_spec *states;
48*0c814320Stb 	size_t num_states;
49*0c814320Stb 
50*0c814320Stb 	const struct line_spec *instructions;
51*0c814320Stb 	size_t num_instructions;
52*0c814320Stb 
53*0c814320Stb 	int (*init)(void *ctx, void *parse_ctx);
54*0c814320Stb 	void (*finish)(void *ctx);
55*0c814320Stb 
56*0c814320Stb 	int (*run_test_case)(void *ctx);
57*0c814320Stb };
58*0c814320Stb 
59*0c814320Stb int parse_test_file(const char *fn, const struct test_parse *lctx, void *ctx);
60*0c814320Stb 
61*0c814320Stb int parse_get_int(struct parse *p, size_t idx, int *out);
62*0c814320Stb int parse_get_cbs(struct parse *p, size_t idx, CBS *out);
63*0c814320Stb 
64*0c814320Stb int parse_instruction_get_int(struct parse *p, size_t idx, int *out);
65*0c814320Stb int parse_instruction_get_cbs(struct parse *p, size_t idx, CBS *out);
66*0c814320Stb 
67*0c814320Stb int parse_length_equal(struct parse *p, const char *descr, size_t want, size_t got);
68*0c814320Stb int parse_data_equal(struct parse *p, const char *descr, CBS *want,
69*0c814320Stb     const uint8_t *got, size_t len);
70*0c814320Stb 
71*0c814320Stb void parse_info(struct parse *ctx, const char *fmt, ...)
72*0c814320Stb     __attribute__((__format__ (printf, 2, 3)))
73*0c814320Stb     __attribute__((__nonnull__ (2)));
74*0c814320Stb void parse_errx(struct parse *ctx, const char *fmt, ...)
75*0c814320Stb     __attribute__((__format__ (printf, 2, 3)))
76*0c814320Stb     __attribute__((__nonnull__ (2)))
77*0c814320Stb     __attribute__((__noreturn__));
78*0c814320Stb 
79*0c814320Stb #ifdef __cplusplus
80*0c814320Stb }
81*0c814320Stb #endif
82*0c814320Stb 
83*0c814320Stb #endif /* PARSE_TEST_FILE_H */
84