test_conf.c 5.92 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

    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>

19
#include "knot/conf/conf.c"
20 21
#include "test_conf.h"

22 23 24 25 26
#define ZONE_ARPA	"0/25.2.0.192.in-addr.arpa."
#define ZONE_ROOT	"."
#define ZONE_1LABEL	"x."
#define ZONE_3LABEL	"abc.ab.a."
#define ZONE_UNKNOWN	"unknown."
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

static void check_name(const char *zone, const char *name, const char *ref)
{
	knot_dname_t *z = knot_dname_from_str_alloc(zone);

	char *file = get_filename(NULL, NULL, z, name);
	ok(file != NULL, "Get zonefile path for %s", zone);
	if (file != NULL) {
		ok(strcmp(file, ref) == 0, "Zonefile path compare %s", name);
		free(file);
	}

	knot_dname_free(&z, NULL);
}

static void check_name_err(const char *zone, const char *name)
{
	knot_dname_t *z = knot_dname_from_str_alloc(zone);

	ok(get_filename(NULL, NULL, z, name) == NULL, "Invalid name %s", name);

	knot_dname_free(&z, NULL);
}

static void test_get_filename(void)
{
53 54 55 56 57 58 59
	// Name formatter.
	char *zone = "abc";
	check_name(zone, "/%s", "/abc");

	zone = ".";
	check_name(zone, "/%s", "/");

60
	// Char formatter.
61
	zone = "abc.def.g";
62 63
	check_name(zone, "/%c[0]", "/a");
	check_name(zone, "/%c[3]", "/.");
64 65
	check_name(zone, "/%c[8]", "/g");
	check_name(zone, "/%c[9]", "/.");
66 67
	check_name(zone, "/%c[10]", "/");
	check_name(zone, "/%c[255]", "/");
68 69 70 71
	check_name(zone, "/%c[0-1]", "/ab");
	check_name(zone, "/%c[1-1]", "/b");
	check_name(zone, "/%c[1-3]", "/bc.");
	check_name(zone, "/%c[1-4]", "/bc.d");
72
	check_name(zone, "/%c[254-255]", "/");
73 74 75 76 77
	check_name_err(zone, "/%c");
	check_name_err(zone, "/%cx");
	check_name_err(zone, "/%c[a]");
	check_name_err(zone, "/%c[:]");
	check_name_err(zone, "/%c[/]");
78 79
	check_name_err(zone, "/%c[-1]");
	check_name_err(zone, "/%c[256]");
80 81 82 83 84
	check_name_err(zone, "/%c[");
	check_name_err(zone, "/%c[1");
	check_name_err(zone, "/%c[1-");
	check_name_err(zone, "/%c[1-2");
	check_name_err(zone, "/%c[1-b]");
85
	check_name_err(zone, "/%c[8-0]");
86 87

	zone = "abcd";
88
	check_name(zone, "/%c[2-9]", "/cd.");
89
	check_name(zone, "/%c[3]", "/d");
90
	check_name(zone, "/%c[4]", "/.");
91 92

	zone = ".";
93
	check_name(zone, "/%c[0]", "/.");
94 95 96 97 98 99 100 101
	check_name(zone, "/%c[1]", "/");

	// Label formatter.
	zone = "abc.def.gh";
	check_name(zone, "/%l[0]", "/gh");
	check_name(zone, "/%l[1]", "/def");
	check_name(zone, "/%l[2]", "/abc");
	check_name(zone, "/%l[3]", "/");
102
	check_name(zone, "/%l[255]", "/");
103 104
	check_name(zone, "/%l[0]-%l[1]-%l[2]", "/gh-def-abc");
	check_name_err(zone, "/%l[0-1]");
105 106
	check_name_err(zone, "/%l[-1]");
	check_name_err(zone, "/%l[256]");
107 108

	zone = ".";
109
	check_name(zone, "/%l[0]", "/");
110 111
	check_name(zone, "/%l[1]", "/");
}
112 113 114 115 116 117

static void test_conf_zonefile(void)
{
	int ret;
	char *file;

118 119 120 121 122 123 124 125 126 127
	knot_dname_t *zone_arpa = knot_dname_from_str_alloc(ZONE_ARPA);
	ok(zone_arpa != NULL, "create dname "ZONE_ARPA);
	knot_dname_t *zone_root = knot_dname_from_str_alloc(ZONE_ROOT);
	ok(zone_root != NULL, "create dname "ZONE_ROOT);
	knot_dname_t *zone_1label = knot_dname_from_str_alloc(ZONE_1LABEL);
	ok(zone_1label != NULL, "create dname "ZONE_1LABEL);
	knot_dname_t *zone_3label = knot_dname_from_str_alloc(ZONE_3LABEL);
	ok(zone_3label != NULL, "create dname "ZONE_3LABEL);
	knot_dname_t *zone_unknown = knot_dname_from_str_alloc(ZONE_UNKNOWN);
	ok(zone_unknown != NULL, "create dname "ZONE_UNKNOWN);
128 129 130 131 132 133 134

	const char *conf_str =
		"template:\n"
		"  - id: default\n"
		"    storage: /tmp\n"
		"\n"
		"zone:\n"
135
		"  - domain: "ZONE_ARPA"\n"
136
		"    file: dir/a%%b/%s.suffix/%a\n"
137
		"  - domain: "ZONE_ROOT"\n"
138
		"    file: /%s\n"
139
		"  - domain: "ZONE_1LABEL"\n"
140
		"    file: /%s\n"
141
		"  - domain: "ZONE_3LABEL"\n";
142 143

	ret = test_conf(conf_str, NULL);
144
	is_int(KNOT_EOK, ret, "Prepare configuration");
145 146

	// Relative path with formatters.
147 148
	file = conf_zonefile(conf(), zone_arpa);
	ok(file != NULL, "Get zonefile path for "ZONE_ARPA);
149 150
	if (file != NULL) {
		ok(strcmp(file, "/tmp/dir/a%b/0_25.2.0.192.in-addr.arpa.suffix/") == 0,
151
		          "Zonefile path compare for "ZONE_ARPA);
152 153
		free(file);
	}
154

155
	// Absolute path without formatters - root zone.
156 157
	file = conf_zonefile(conf(), zone_root);
	ok(file != NULL, "Get zonefile path for "ZONE_ROOT);
158
	if (file != NULL) {
159 160
		ok(strcmp(file, "/") == 0,
		          "Zonefile path compare for "ZONE_ROOT);
161 162
		free(file);
	}
163

164 165 166
	// Absolute path without formatters - non-root zone.
	file = conf_zonefile(conf(), zone_1label);
	ok(file != NULL, "Get zonefile path for "ZONE_1LABEL);
167 168
	if (file != NULL) {
		ok(strcmp(file, "/x") == 0,
169
		          "Zonefile path compare for "ZONE_1LABEL);
170 171 172
		free(file);
	}

173
	// Default zonefile path.
174 175
	file = conf_zonefile(conf(), zone_3label);
	ok(file != NULL, "Get zonefile path for "ZONE_3LABEL);
176
	if (file != NULL) {
177 178
		ok(strcmp(file, "/tmp/abc.ab.a.zone") == 0,
		          "Zonefile path compare for "ZONE_3LABEL);
179 180 181
		free(file);
	}

182
	// Unknown zone zonefile path.
183 184
	file = conf_zonefile(conf(), zone_unknown);
	ok(file != NULL, "Get zonefile path for "ZONE_UNKNOWN);
185
	if (file != NULL) {
186 187
		ok(strcmp(file, "/tmp/unknown.zone") == 0,
		          "Zonefile path compare for "ZONE_UNKNOWN);
188 189 190
		free(file);
	}

191
	conf_free(conf());
192 193 194 195 196
	knot_dname_free(&zone_arpa, NULL);
	knot_dname_free(&zone_root, NULL);
	knot_dname_free(&zone_1label, NULL);
	knot_dname_free(&zone_3label, NULL);
	knot_dname_free(&zone_unknown, NULL);
197 198 199 200 201 202
}

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

203 204 205 206
	diag("get_filename");
	test_get_filename();

	diag("conf_zonefile");
207 208 209 210
	test_conf_zonefile();

	return 0;
}