aboutgitcodelistschat:MatrixIRC
path: root/tests/unit/test_errors.c
blob: d00d42ebd72bb44440074b435041c9ebdd419aae (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/syscall.h>

#include <check.h>

#include "operations.h"
#include "common/common.h"
#include "common/gluten.h"
#include "testutil.h"

static void setup_error_check()
{
	at = mmap(NULL, sizeof(struct args_target), PROT_READ | PROT_WRITE,
		  MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	at->check_fd = false;
	at->nr = __NR_getpid;
	at->install_filter = install_notification_filter;
	setup();
}

struct gluten_offset test_max_size_data[] = {
	{ OFFSET_DATA, DATA_SIZE },
	{ OFFSET_RO_DATA, RO_DATA_SIZE },
	{ OFFSET_SECCOMP_DATA, 6 },
	{ OFFSET_INSTRUCTION, INST_SIZE },
};

START_TEST(test_bound_check)
{
	struct op ops[] = {
		{ OP_RETURN, { { 0 } } },
	};
	ops[0].op.ret.val.offset = test_max_size_data[_i].offset;
	ops[0].op.ret.val.type = test_max_size_data[_i].type;

	eval(&gluten, ops, &req, notifyfd);
}

START_TEST(test_write_op_return)
{
	struct op ops[] = {
		{ OP_CALL,
		  { .call = { .nr = __NR_getpid,
			      .has_ret = true,
			      .ret = { OFFSET_DATA, DATA_SIZE - 1 } } } },
	};

	eval(&gluten, ops, &req, notifyfd);
}

START_TEST(test_write_op_load)
{
	char a[30];
	struct op ops[] = {
		{ OP_LOAD,
		  { .load = { { OFFSET_SECCOMP_DATA, 1 },
			      { OFFSET_DATA, DATA_SIZE - 1 },
			      sizeof(a) } } },
	};

	eval(&gluten, ops, &req, notifyfd);
}

START_TEST(test_read_op_return)
{
	struct op ops[] = {
		{ OP_RETURN, { { 0 } } },
	};
	ops[0].op.ret.val.offset = test_max_size_data[_i].offset - 1;
	ops[0].op.ret.val.type = test_max_size_data[_i].type;

	eval(&gluten, ops, &req, notifyfd);
}

Suite *error_suite(void)
{
	Suite *s;
	TCase *bounds, *gwrite, *gread;

	s = suite_create("Error handling");

	bounds = tcase_create("bound checks");
	tcase_add_loop_exit_test(bounds, test_bound_check, EXIT_FAILURE, 0,
				 sizeof(test_max_size_data) /
					 sizeof(test_max_size_data[0]));
	suite_add_tcase(s, bounds);

	gwrite = tcase_create("write gluten");
	tcase_add_checked_fixture(gwrite, setup_error_check, teardown);
	tcase_add_exit_test(gwrite, test_write_op_return, EXIT_FAILURE);
	tcase_add_exit_test(gwrite, test_write_op_load, EXIT_FAILURE);
	suite_add_tcase(s, gwrite);

	gread = tcase_create("read gluten");
	tcase_add_loop_exit_test(gread, test_read_op_return, EXIT_FAILURE, 0,
				 sizeof(test_max_size_data) /
					 sizeof(test_max_size_data[0]));
	suite_add_tcase(s, gread);

	return s;
}

int main(void)
{
	int no_failed = 0;
	Suite *s;
	SRunner *runner;

	s = error_suite();
	runner = srunner_create(s);

	srunner_run_all(runner, CK_VERBOSE);
	no_failed = srunner_ntests_failed(runner);
	srunner_free(runner);
	return (no_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}