summaryrefslogtreecommitdiffstats
path: root/tests/knot/test_query_module.c
blob: 4ab14d287f3d7881d639416de2a0b255ddbe906b (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
84
85
86
87
/*  Copyright (C) 2019 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>
#include <string.h>
#include <stdlib.h>

#include "libknot/libknot.h"
#include "knot/nameserver/query_module.h"
#include "libknot/packet/pkt.h"

/* Universal processing stage. */
unsigned state_visit(unsigned state, knot_pkt_t *pkt, knotd_qdata_t *qdata,
                     knotd_mod_t *mod)
{
	/* Visit current state */
	bool *state_map = (bool *)mod;
	state_map[state] = true;

	return state + 1;
}

int main(int argc, char *argv[])
{
	plan_lazy();

	/* Create a map of expected steps. */
	bool state_map[KNOTD_STAGES] = { false };

	/* Prepare query plan. */
	struct query_plan *plan = query_plan_create();
	ok(plan != NULL, "query_plan: create");
	if (plan == NULL) {
		goto fatal;
	}

	/* Register all stage visits. */
	int ret = KNOT_EOK;
	for (unsigned stage = KNOTD_STAGE_BEGIN; stage < KNOTD_STAGES; ++stage) {
		ret = query_plan_step(plan, stage, state_visit, state_map);
		if (ret != KNOT_EOK) {
			break;
		}
	}
	is_int(KNOT_EOK, ret, "query_plan: planned all steps");

	/* Execute the plan. */
	int state = 0, next_state = 0;
	for (unsigned stage = KNOTD_STAGE_BEGIN; stage < KNOTD_STAGES; ++stage) {
		struct query_step *step = NULL;
		WALK_LIST(step, plan->stage[stage]) {
			next_state = step->process(state, NULL, NULL, step->ctx);
			if (next_state != state + 1) {
				break;
			}
			state = next_state;
		}
	}
	ok(state == KNOTD_STAGES, "query_plan: executed all steps");

	/* Verify if all steps executed their callback. */
	for (state = 0; state < KNOTD_STAGES; ++state) {
		if (state_map[state] == false) {
			break;
		}
	}
	ok(state == KNOTD_STAGES, "query_plan: executed all callbacks");

fatal:
	/* Free the query plan. */
	query_plan_free(plan);

	return 0;
}