test_cookies-client.c 8.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*  Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
 */

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

Jan Včelák's avatar
Jan Včelák committed
22
#include "contrib/sockaddr.h"
23 24 25 26 27 28 29
#include "libknot/consts.h"
#include "libknot/cookies/alg-fnv64.h"
#include "libknot/cookies/client.h"
#include "libknot/errcode.h"

int main(int argc, char *argv[])
{
Jan Včelák's avatar
Jan Včelák committed
30
	plan_lazy();
31 32 33

	int ret;

Jan Včelák's avatar
Jan Včelák committed
34
	const uint8_t secret[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
35

Jan Včelák's avatar
Jan Včelák committed
36
	uint8_t hash[32] = { 0 };
37 38 39 40
	uint16_t hash_len;

	struct knot_cc_input cc_in = { 0 };

Jan Včelák's avatar
Jan Včelák committed
41 42 43 44 45 46
	struct sockaddr_storage unspec_sa = { 0 };

	struct sockaddr_storage c4_sa = { 0 };
	struct sockaddr_storage s4_sa = { 0 };
	sockaddr_set(&c4_sa, AF_INET, "127.0.0.1", 0);
	sockaddr_set(&s4_sa, AF_INET, "10.0.0.1", 0);
47

Jan Včelák's avatar
Jan Včelák committed
48 49 50 51
	struct sockaddr_storage c6_sa = { 0 };
	struct sockaddr_storage s6_sa = { 0 };
	sockaddr_set(&c6_sa, AF_INET6, "2001:db8:8714:3a90::12", 0);
	sockaddr_set(&s6_sa, AF_INET6, "::1", 0);
52 53 54

	/* Client cookie hash algorithm. */

Jan Včelák's avatar
Jan Včelák committed
55
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
56 57
	hash_len = knot_cc_alg_fnv64.gen_func(NULL, hash, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie no input");
58 59 60 61 62 63

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = NULL;
	cc_in.srvr_sockaddr = NULL;
	cc_in.secret_data = NULL;
	cc_in.secret_len = 0;
Jan Včelák's avatar
Jan Včelák committed
64
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
65 66
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie input no data");
67 68 69 70 71 72

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = NULL;
	cc_in.secret_len = 0;
Jan Včelák's avatar
Jan Včelák committed
73
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
74 75
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie input no secret");
76 77 78 79 80

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = NULL;
	cc_in.srvr_sockaddr = NULL;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
81 82
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
83 84
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie input no socket");
85 86 87 88 89

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
90
	cc_in.secret_len = sizeof(secret);
Karel Slaný's avatar
Karel Slaný committed
91 92 93
	hash_len = 0;
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, NULL, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie output no hash");
94 95 96 97 98

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
99
	cc_in.secret_len = sizeof(secret);
100
	hash_len = 1;
Karel Slaný's avatar
Karel Slaný committed
101 102
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len == 0, "cookies: FNV64 client cookie hash no space");
103 104 105 106 107

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
108 109
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
110 111
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
112
	{
113
		uint8_t expected[] = { 0xb1, 0x15, 0xef, 0x03, 0xa8, 0xf9, 0x31, 0x74 };
Jan Včelák's avatar
Jan Včelák committed
114
		ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
115 116 117 118 119 120
	}

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = NULL;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
121 122
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
123 124
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
125
	{
126
		uint8_t expected[] = { 0xe7, 0x09, 0xdd, 0x43, 0xd2, 0x25, 0x62, 0x7c };
Jan Včelák's avatar
Jan Včelák committed
127
		ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
128 129
	}

130 131 132 133
	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&unspec_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s4_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
134 135
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
136 137
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
138
	{
139
		uint8_t expected[] = { 0xe7, 0x09, 0xdd, 0x43, 0xd2, 0x25, 0x62, 0x7c };
Jan Včelák's avatar
Jan Včelák committed
140
		ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
141 142
	}

143 144 145 146
	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = NULL;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
147 148
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
149 150
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
151
	{
152
		uint8_t expected[] = { 0xaa, 0xe0, 0x98, 0x1b, 0x08, 0xd1, 0xa9, 0x05 };
Jan Včelák's avatar
Jan Včelák committed
153
		ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
154 155
	}

156 157 158 159
	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c4_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&unspec_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
160 161
	cc_in.secret_len = sizeof(secret);
	hash_len = sizeof(hash);
Karel Slaný's avatar
Karel Slaný committed
162 163
	hash_len = knot_cc_alg_fnv64.gen_func(&cc_in, hash, hash_len);
	ok(hash_len != 0 && hash_len == knot_cc_alg_fnv64.cc_size, "cookies: FNV64 client cookie output");
164
	{
165
		uint8_t expected[] = { 0xaa, 0xe0, 0x98, 0x1b, 0x08, 0xd1, 0xa9, 0x05 };
Jan Včelák's avatar
Jan Včelák committed
166
		ok(sizeof(expected) == hash_len && 0 == memcmp(expected, hash, hash_len), "cookies: FNV64 client cookie content");
167 168
	}

169 170 171 172 173 174
	/* Client cookie check. */

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c6_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s6_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
175
	cc_in.secret_len = sizeof(secret);
176 177
	{
		ret = knot_cc_check(NULL, 0, &cc_in, &knot_cc_alg_fnv64);
178
		is_int(KNOT_EINVAL, ret, "cookies: FNV64 client cookie check no cookie");
179 180 181
	}

	{
182
		uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
Jan Včelák's avatar
Jan Včelák committed
183
		ret = knot_cc_check(cookie, sizeof(cookie), NULL, &knot_cc_alg_fnv64);
184
		is_int(KNOT_EINVAL, ret, "cookies: FNV64 client cookie check no input");
185 186 187 188 189 190
	}

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c6_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s6_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
191
	cc_in.secret_len = sizeof(secret);
192
	{
193
		uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
Jan Včelák's avatar
Jan Včelák committed
194
		ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, NULL);
195
		is_int(KNOT_EINVAL, ret, "cookies: FNV64 client cookie check no algorithm");
196 197 198 199 200 201
	}

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c6_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s6_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
202
	cc_in.secret_len = sizeof(secret);
203
	{
204
		uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
Jan Včelák's avatar
Jan Včelák committed
205
		ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, &knot_cc_alg_fnv64);
206
		is_int(KNOT_EOK, ret, "cookies: FNV64 client good cookie check");
207 208 209 210 211 212
	}

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c6_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s6_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
213
	cc_in.secret_len = sizeof(secret);
214
	{
215
		uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x33 };
Jan Včelák's avatar
Jan Včelák committed
216
		ret = knot_cc_check(cookie, sizeof(cookie) - 1, &cc_in, &knot_cc_alg_fnv64);
217
		is_int(KNOT_EINVAL, ret, "cookies: FNV64 client cookie check invalid length");
218 219 220 221 222 223
	}

	memset(&cc_in, 0, sizeof(cc_in));
	cc_in.clnt_sockaddr = (struct sockaddr *)&c6_sa;
	cc_in.srvr_sockaddr = (struct sockaddr *)&s6_sa;
	cc_in.secret_data = secret;
Jan Včelák's avatar
Jan Včelák committed
224
	cc_in.secret_len = sizeof(secret);
225
	{
226
		uint8_t cookie[] = { 0xaf, 0xe5, 0x17, 0x94, 0x80, 0xa6, 0x0c, 0x32 };
Jan Včelák's avatar
Jan Včelák committed
227
		ret = knot_cc_check(cookie, sizeof(cookie), &cc_in, &knot_cc_alg_fnv64);
228
		is_int(KNOT_EINVAL, ret, "cookies: FNV64 client cookie check invalid cookie");
229 230
	}
}