summaryrefslogtreecommitdiffstats
path: root/test/ioctl/util.c
blob: 09c6e7f9a06b0ae7a7c845585f54168a5334861d (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
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "util.h"

#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void hexdump(const uint8_t *buf, size_t len)
{
	size_t i = 0;

	if (!len)
		return;

	for (;;) {
		fprintf(stderr, "%02X", buf[i++]);
		if (i >= len)
			break;

		fputc(i % 16 > 0 ? ' ' : '\n', stderr);
	}
	fputc('\n', stderr);
}

void fail(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
	fputc('\n', stderr);
	abort();
}

void cmp(const void *actual, const void *expected, size_t len, const char *msg)
{
	if (memcmp(actual, expected, len) == 0)
		return;

	fputs(msg, stderr);
	fputs("\nactual:\n", stderr);
	hexdump(actual, len);
	fputs("expected:\n", stderr);
	hexdump(expected, len);
	abort();
}

void arbitrary(void *buf_, size_t len)
{
	uint8_t *buf = buf_;

	while (len--)
		*(buf++) = rand();
}

size_t arbitrary_range(size_t max)
{
	size_t value;
	arbitrary(&value, sizeof(value));
	return value % max;
}