File: test%2Btest.ha

package info (click to toggle)
hare 0.24.2-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,756 kB
  • sloc: asm: 1,180; sh: 119; makefile: 116; lisp: 99
file content (69 lines) | stat: -rw-r--r-- 1,741 bytes parent folder | download
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
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use memio;
use net::ip;
use strings;

def HOSTS_FILE = `
127.0.0.1	localhost

# The following lines are desirable for IPv6 capable hosts
::1		ip6-localhost ip6-loopback

10.10.10.10	other.localdomain
10.10.20.20	other.localdomain
`;

@test fn next() void = {
	const buf = memio::fixed(strings::toutf8(HOSTS_FILE));
	const rd = read(&buf);
	defer finish(&rd);

	const h = next(&rd) as host;
	assert(ip::equal(h.addr, ip::LOCAL_V4));
	assert(len(h.names) == 1);
	assert(h.names[0] == "localhost");

	const h = next(&rd) as host;
	assert(ip::equal(h.addr, ip::LOCAL_V6));
	assert(len(h.names) == 2);
	assert(h.names[0] == "ip6-localhost");
	assert(h.names[1] == "ip6-loopback");

	const h = next(&rd) as host;
	assert(ip::equal(h.addr, [10, 10, 10, 10]: ip::addr4));
	assert(len(h.names) == 1);
	assert(h.names[0] == "other.localdomain");

	const h = next(&rd) as host;
	assert(ip::equal(h.addr, [10, 10, 20, 20]: ip::addr4));
	assert(len(h.names) == 1);
	assert(h.names[0] == "other.localdomain");

	const h = next(&rd);
	assert(h is done);
	const h = next(&rd);
	assert(h is done);
};

@test fn errors() void = {
	const s = "127";
	assert(next(&read(&memio::fixed(strings::toutf8(s))))
		is ip::invalid);
	const s = "127.0.0.1";
	assert(next(&read(&memio::fixed(strings::toutf8(s))))
		is invalid);
};

@test fn lookup() void = {
	const buf = memio::fixed(strings::toutf8(HOSTS_FILE));
	const rd = read(&buf);
	defer finish(&rd);

	const addrs = _lookup(&rd, "other.localdomain") as []ip::addr;
	defer free(addrs);
	assert(len(addrs) == 2);
	assert(ip::equal(addrs[0], [10, 10, 10, 10]: ip::addr4));
	assert(ip::equal(addrs[1], [10, 10, 20, 20]: ip::addr4));
};