summaryrefslogtreecommitdiffstats
path: root/tests/test.h
blob: 79167d2a85108e4983cb9df4be2511370e627b28 (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
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
119
120
121
122
123
/*  Copyright (C) 2014-2017 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/>.
 */

#pragma once

#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <cmocka.h>

/* Silence clang/GCC warnings when using cmocka 1.0 */
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#include "lib/defines.h"
#include "lib/utils.h"
#include <libknot/descriptor.h>
#include <libknot/rrset.h>

/* Helpers */
static inline void *mm_test_malloc(void *ctx, size_t n)
{ return test_malloc(n); }
static inline void mm_test_free(void *p)
{ if (p) test_free(p); }

/** Memory context using CMocka allocator. */
static inline void test_mm_ctx_init(knot_mm_t *mm)
{
	mm->alloc = &mm_test_malloc;
	mm->free = &mm_test_free;
}

/** Recursively delete directory. */
static inline int test_tmpdir_remove(const char *path)
{
	char buf[512];
	struct dirent *ent = NULL;
	DIR *dir = opendir(path);
	if (dir == NULL) {
		return kr_error(errno);
	}
	while ((ent = readdir(dir)) != NULL) {
		/* Skip special dirs (this presumes no files begin with '.') */
		if (ent->d_name[0] == '.') {
			continue;
		}
		sprintf(buf, "%s/%s", path, ent->d_name);
		remove(buf);
	}
	remove(path);
	closedir(dir);
	return 0;
}

/** Create temporary directory. */
static inline const char* test_tmpdir_create(void)
{
	static char env_path[64];
	strcpy(env_path, "./tmpXXXXXX");
	return mkdtemp(env_path);
}

/** Generate random string with given length. */
static inline void test_randstr(char* dst, size_t len)
{
	if (len == 0) {
		return;
	}

	for (int i = 0; i < len - 1; ++i) {
		dst[i] = '0' + (int) (('Z'-'0') * (rand() / (RAND_MAX + 1.0)));
	}
	dst[len - 1] = '\0';
	return;
}

/** Init RRSet with type TXT, random owner and random payload.
 *  @note Static memory reused, copy it if you need persistence.
 */
static inline void test_random_rr(knot_rrset_t *rr, uint32_t ttl)
{
	static uint8_t owner_buf[KNOT_DNAME_MAXLEN];
	static uint8_t rdata_buf[65535];
	knot_rdata_t *rdata = (knot_rdata_t *)rdata_buf;

	uint16_t num = rand() % (sizeof(owner_buf) - 2);
	uint8_t tmp_buf[KNOT_DNAME_MAXLEN];

	/* Create random label. */
	memset(owner_buf, 0, sizeof(owner_buf));
	uint8_t label_len = num % KNOT_DNAME_MAXLABELLEN;
	owner_buf[0] = label_len;
	test_randstr((char *)(owner_buf + 1), label_len);

	/* Create payload */
	tmp_buf[0] = num;
	test_randstr((char *)(tmp_buf + 1), tmp_buf[0] + 1);
	knot_rdata_init(rdata, num + 1, tmp_buf);

	/* Assign static buffers. */
	knot_rrset_init(rr, owner_buf, KNOT_RRTYPE_TXT, KNOT_CLASS_IN, ttl);
	rr->rrs.count = 1;
	rr->rrs.rdata = rdata;
}