From 9ae6806e30e1d83dfa697ed26bac39db2dbad912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristo=CC=81fer=20R?= Date: Mon, 4 Nov 2024 17:25:00 -0500 Subject: [PATCH] os/hostname: test bufPrintHostnameFromFileUri Note that this includes some failing tests because I want to make the uri handling better and more specific. It's a little bit too general right now so I want to lock it down to: 1. macOS only; and 2. valid mac address values because that's how the macOS private Wi-Fi address thing works; randomizes your mac address and sets that as your hostname. --- src/os/hostname.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/os/hostname.zig b/src/os/hostname.zig index dcc802b65..31738a90f 100644 --- a/src/os/hostname.zig +++ b/src/os/hostname.zig @@ -43,6 +43,50 @@ pub fn isLocalHostname(hostname: []const u8) !bool { return std.mem.eql(u8, hostname, ourHostname); } +test "bufPrintHostnameFromFileUri succeeds with ascii hostname" { + const uri = try std.Uri.parse("file://localhost/"); + + var buf: [posix.HOST_NAME_MAX]u8 = undefined; + const actual = try bufPrintHostnameFromFileUri(&buf, uri); + + try std.testing.expectEqualStrings("localhost", actual); +} + +test "bufPrintHostnameFromFileUri succeeds with hostname as mac address" { + const uri = try std.Uri.parse("file://12:34:56:78:90:12"); + + var buf: [posix.HOST_NAME_MAX]u8 = undefined; + const actual = try bufPrintHostnameFromFileUri(&buf, uri); + + try std.testing.expectEqualStrings("12:34:56:78:90:12", actual); +} + +test "bufPrintHostnameFromFileUri returns only hostname when there is a port component in the URI" { + // First: try with a non-2-digit port, to test general port handling. + const four_port_uri = try std.Uri.parse("file://has-a-port:1234"); + + var four_port_buf: [posix.HOST_NAME_MAX]u8 = undefined; + const four_port_actual = try bufPrintHostnameFromFileUri(&four_port_buf, four_port_uri); + + try std.testing.expectEqualStrings("has-a-port", four_port_actual); + + // Second: try with a 2-digit port to test mac-address handling. + const two_port_uri = try std.Uri.parse("file://has-a-port:12"); + + var two_port_buf: [posix.HOST_NAME_MAX]u8 = undefined; + const two_port_actual = try bufPrintHostnameFromFileUri(&two_port_buf, two_port_uri); + + try std.testing.expectEqualStrings("has-a-port", two_port_actual); + + // Third: try with a mac-address that has a port-component added to it to test mac-address handling. + const mac_with_port_uri = try std.Uri.parse("file://12:34:56:78:90:12:1234"); + + var mac_with_port_buf: [posix.HOST_NAME_MAX]u8 = undefined; + const mac_with_port_actual = try bufPrintHostnameFromFileUri(&mac_with_port_buf, mac_with_port_uri); + + try std.testing.expectEqualStrings("12:34:56:78:90:12", mac_with_port_actual); +} + test "isLocalHostname returns true when provided hostname is localhost" { try std.testing.expect(try isLocalHostname("localhost")); }