hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit fb661e20b1fa3cb8d5a6d7d5e8fd39d2d7cfd588
parent 9183c7d83c98a2742c168889575c5faa5bbc7dbd
Author: Pierre Curto <pierre.curto@gmail.com>
Date:   Wed, 31 Aug 2022 11:56:41 +0200

unix: use buffered I/O

Add tests for hosts, resolv.conf and passwd files.

Fixes https://todo.sr.ht/~sircmpwn/hare/472

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

Diffstat:
Munix/hosts/lookup.ha | 25++++++++++++++++++++++++-
Munix/passwd/group.ha | 11+++++++++--
Munix/passwd/passwd.ha | 9++++++++-
Munix/resolvconf/load.ha | 22+++++++++++++++++++++-
4 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/unix/hosts/lookup.ha b/unix/hosts/lookup.ha @@ -15,10 +15,16 @@ export fn lookup(name: str) []ip::addr = { // XXX: Would be cool to have meaningful error handling(?) const file = os::open(PATH)!; defer io::close(file)!; + return lookupio(name, file); +}; + +fn lookupio(name: str, src: io::handle) []ip::addr = { + let buffer: [os::BUFSIZ]u8 = [0...]; + const buf = bufio::buffered(src, buffer, []); let addrs: []ip::addr = []; for (true) { - const line = match (bufio::scanline(file)) { + const line = match (bufio::scanline(&buf)) { case io::EOF => break; case let line: []u8 => @@ -55,3 +61,20 @@ export fn lookup(name: str) []ip::addr = { }; return addrs; }; + +@test fn lookup() void = { + let buf = bufio::fixed(strings::toutf8( + "127.0.0.1 localhost\n" + "\n" + "# The following lines are desirable for IPv6 capable hosts\n" + "::1 ip6-localhost ip6-loopback\n" + "fe00::0 ip6-localnet\n" + "ff00::0 ip6-mcastprefix\n" + "ff02::1 ip6-allnodes\n" + "ff02::2 ip6-allrouters\n"), io::mode::READ); + const ips = lookupio("localhost", &buf); + assert(len(ips) == 1); + const got = ips[0]; + const want: ip::addr4 = [127, 0, 0, 1]; + assert(ip::equal(got, want)); +}; diff --git a/unix/passwd/group.ha b/unix/passwd/group.ha @@ -77,9 +77,11 @@ export fn getgroup(name: str) (grent | void) = { abort("Unable to open /etc/group"); }; defer io::close(file)!; - + let buffer: [os::BUFSIZ]u8 = [0...]; + const buf = bufio::buffered(file, buffer, []); + for (true) { - let ent = match (nextgr(file)) { + let ent = match (nextgr(&buf)) { case let e: grent => yield e; case io::EOF => @@ -127,6 +129,11 @@ export fn getgid(gid: uint) (grent | void) = { }; }; +@test fn getgroup() void = { + const g = getgroup("root") as grent; + assert(g.name == "root"); +}; + @test fn nextgr() void = { let buf = bufio::fixed(strings::toutf8( "root:x:0:root\n" diff --git a/unix/passwd/passwd.ha b/unix/passwd/passwd.ha @@ -96,9 +96,11 @@ export fn getuser(username: str) (pwent | void) = { abort("Can't open /etc/passwd"); }; defer io::close(file)!; + let buffer: [os::BUFSIZ]u8 = [0...]; + const buf = bufio::buffered(file, buffer, []); for (true) { - let ent = match (nextpw(file)) { + let ent = match (nextpw(&buf)) { case let e: pwent => yield e; case io::EOF => @@ -149,6 +151,11 @@ export fn getuid(uid: uint) (pwent | void) = { }; }; +@test fn getuser() void = { + const u = getuser("root") as pwent; + assert(u.username == "root"); +}; + @test fn nextpw() void = { let buf = bufio::fixed(strings::toutf8( "sircmpwn:x:1000:1000:sircmpwn's comment:/home/sircmpwn:/bin/mrsh\n" diff --git a/unix/resolvconf/load.ha b/unix/resolvconf/load.ha @@ -25,9 +25,15 @@ export fn load() []ip::addr = { const file = os::open(PATH)!; defer io::close(file)!; + return loadio(file); +}; + +fn loadio(src: io::handle) []ip::addr = { + let buffer: [os::BUFSIZ]u8 = [0...]; + const buf = bufio::buffered(src, buffer, []); for (true) { - const line = match (bufio::scanline(file)) { + const line = match (bufio::scanline(&buf)) { case io::EOF => break; case let line: []u8 => @@ -62,3 +68,17 @@ export fn load() []ip::addr = { return cache; }; + +@test fn load() void = { + let buf = bufio::fixed(strings::toutf8( + "# comment\n" + "nameserver 127.0.0.1\n" + "options edns0 trust-ad\n" + "search home"), io::mode::READ); + + const ips = loadio(&buf); + assert(len(ips) == 1); + const got = ips[0]; + const want: ip::addr4 = [127, 0, 0, 1]; + assert(ip::equal(got, want)); +};