summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/parse_test_file.h
blob: 772b7f62326c52017d76eeee84ba46c163302d78 (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
72
73
74
75
76
77
78
79
80
81
82
83
/*	$OpenBSD: parse_test_file.h,v 1.1 2024/12/26 00:04:24 tb Exp $ */

/*
 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef PARSE_TEST_FILE_H
#define PARSE_TEST_FILE_H

#include <stdint.h>
#include <stdio.h>

#include "bytestring.h"

#if defined(__cplusplus)
extern "C" {
#endif

struct parse;

enum line {
	LINE_STRING_MATCH,	/* Checks if string after label matches. */
	LINE_HEX,		/* Parses hex into cbb from type2cbb. */
};

struct line_spec {
	int		 state;
	enum line	 type;
	const char	*name;
	const char	*label; /* followed by ": " or " = " */
	const char	*match;	/* only for LINE_STRING_MATCH */
};

struct test_parse {
	const struct line_spec *states;
	size_t num_states;

	const struct line_spec *instructions;
	size_t num_instructions;

	int (*init)(void *ctx, void *parse_ctx);
	void (*finish)(void *ctx);

	int (*run_test_case)(void *ctx);
};

int parse_test_file(const char *fn, const struct test_parse *lctx, void *ctx);

int parse_get_int(struct parse *p, size_t idx, int *out);
int parse_get_cbs(struct parse *p, size_t idx, CBS *out);

int parse_instruction_get_int(struct parse *p, size_t idx, int *out);
int parse_instruction_get_cbs(struct parse *p, size_t idx, CBS *out);

int parse_length_equal(struct parse *p, const char *descr, size_t want, size_t got);
int parse_data_equal(struct parse *p, const char *descr, CBS *want,
    const uint8_t *got, size_t len);

void parse_info(struct parse *ctx, const char *fmt, ...)
    __attribute__((__format__ (printf, 2, 3)))
    __attribute__((__nonnull__ (2)));
void parse_errx(struct parse *ctx, const char *fmt, ...)
    __attribute__((__format__ (printf, 2, 3)))
    __attribute__((__nonnull__ (2)))
    __attribute__((__noreturn__));

#ifdef __cplusplus
}
#endif

#endif /* PARSE_TEST_FILE_H */