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