test_query_module.c 2.54 KB
Newer Older
1
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    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 <http://www.gnu.org/licenses/>.
 */

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

21
#include "libknot/libknot.h"
22 23
#include "knot/nameserver/query_module.h"
#include "libknot/packet/pkt.h"
24
#include "contrib/mempattern.h"
25
#include "contrib/ucw/mempool.h"
26 27

/* Universal processing stage. */
28 29
unsigned state_visit(unsigned state, knot_pkt_t *pkt, knotd_qdata_t *qdata,
                     knotd_mod_t *mod)
30 31
{
	/* Visit current state */
32
	bool *state_map = (bool *)mod;
33 34 35 36 37 38 39
	state_map[state] = true;

	return state + 1;
}

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

	/* Create processing context. */
43
	knot_mm_t mm;
44
	mm_ctx_mempool(&mm, MM_DEFAULT_BLKSIZE);
45 46

	/* Create a map of expected steps. */
47
	bool state_map[KNOTD_STAGES] = { false };
48 49 50 51 52 53 54

	/* Prepare query plan. */
	struct query_plan *plan = query_plan_create(&mm);
	ok(plan != NULL, "query_plan: create");

	/* Register all stage visits. */
	int ret = KNOT_EOK;
55
	for (unsigned stage = KNOTD_STAGE_BEGIN; stage < KNOTD_STAGES; ++stage) {
56 57 58 59 60
		ret = query_plan_step(plan, stage, state_visit, state_map);
		if (ret != KNOT_EOK) {
			break;
		}
	}
61
	is_int(KNOT_EOK, ret, "query_plan: planned all steps");
62 63 64

	/* Execute the plan. */
	int state = 0, next_state = 0;
65
	for (unsigned stage = KNOTD_STAGE_BEGIN; stage < KNOTD_STAGES; ++stage) {
66 67 68 69 70 71 72 73 74
		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;
		}
	}
75
	ok(state == KNOTD_STAGES, "query_plan: executed all steps");
76 77

	/* Verify if all steps executed their callback. */
78
	for (state = 0; state < KNOTD_STAGES; ++state) {
79 80 81 82
		if (state_map[state] == false) {
			break;
		}
	}
83
	ok(state == KNOTD_STAGES, "query_plan: executed all callbacks");
84 85 86 87 88 89 90 91 92

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

	/* Cleanup. */
	mp_delete((struct mempool *)mm.ctx);

	return 0;
}