test+test.ha (1745B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use memio; 5 use net::ip; 6 use strings; 7 8 def HOSTS_FILE = ` 9 127.0.0.1 localhost 10 11 # The following lines are desirable for IPv6 capable hosts 12 ::1 ip6-localhost ip6-loopback 13 14 10.10.10.10 other.localdomain 15 10.10.20.20 other.localdomain 16 `; 17 18 @test fn next() void = { 19 const buf = memio::fixed(strings::toutf8(HOSTS_FILE)); 20 const rd = read(&buf); 21 defer finish(&rd); 22 23 const h = next(&rd) as host; 24 assert(ip::equal(h.addr, ip::LOCAL_V4)); 25 assert(len(h.names) == 1); 26 assert(h.names[0] == "localhost"); 27 28 const h = next(&rd) as host; 29 assert(ip::equal(h.addr, ip::LOCAL_V6)); 30 assert(len(h.names) == 2); 31 assert(h.names[0] == "ip6-localhost"); 32 assert(h.names[1] == "ip6-loopback"); 33 34 const h = next(&rd) as host; 35 assert(ip::equal(h.addr, [10, 10, 10, 10]: ip::addr4)); 36 assert(len(h.names) == 1); 37 assert(h.names[0] == "other.localdomain"); 38 39 const h = next(&rd) as host; 40 assert(ip::equal(h.addr, [10, 10, 20, 20]: ip::addr4)); 41 assert(len(h.names) == 1); 42 assert(h.names[0] == "other.localdomain"); 43 44 const h = next(&rd); 45 assert(h is done); 46 const h = next(&rd); 47 assert(h is done); 48 }; 49 50 @test fn errors() void = { 51 const s = "127\n"; 52 assert(next(&read(&memio::fixed(strings::toutf8(s)))) 53 is ip::invalid); 54 const s = "127.0.0.1\n"; 55 assert(next(&read(&memio::fixed(strings::toutf8(s)))) 56 is invalid); 57 }; 58 59 @test fn lookup() void = { 60 const buf = memio::fixed(strings::toutf8(HOSTS_FILE)); 61 const rd = read(&buf); 62 defer finish(&rd); 63 64 const addrs = _lookup(&rd, "other.localdomain") as []ip::addr; 65 defer free(addrs); 66 assert(len(addrs) == 2); 67 assert(ip::equal(addrs[0], [10, 10, 10, 10]: ip::addr4)); 68 assert(ip::equal(addrs[1], [10, 10, 20, 20]: ip::addr4)); 69 };