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