Skip to content

Commit

Permalink
updates tests + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Feb 11, 2025
1 parent 070fa0b commit f08c06b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 39 deletions.
38 changes: 0 additions & 38 deletions src/bun.js/node/node_net_binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,6 @@ pub fn setDefaultAutoSelectFamilyAttemptTimeout(global: *JSC.JSGlobalObject) JSC
}).setter, 1, .{});
}

// FIXME: c-headers-for-zig casts AF_* and PF_* to `c_int` when it should be `comptime_int`
const AF = struct {
pub const INET: C.sa_family_t = @intCast(C.AF_INET);
pub const INET6: C.sa_family_t = @intCast(C.AF_INET6);
};

/// ## Notes
/// - Linux broke compat between `sockaddr_in` and `sockaddr_in6` in v2.4.
/// They're no longer the same size.
/// - This replaces `sockaddr_storage` because it's huge. This is 28 bytes,
/// while `sockaddr_storage` is 128 bytes.
const sockaddr_in = extern union {
sin: C.sockaddr_in,
sin6: C.sockaddr_in6,

pub const @"127.0.0.1": sockaddr_in = .{
.sin = .{
.sin_family = AF.INET,
.sin_port = 0,
.sin_addr = .{ .s_addr = C.INADDR_LOOPBACK },
},
};
pub const @"::1": sockaddr_in = .{ .sin6 = .{
.sin6_family = AF.INET6,
.sin6_port = 0,
.sin6_flowinfo = 0,
.sin6_addr = C.inaddr6_loopback,
} };
};

// The same types are defined in a bunch of different places. We should probably unify them.
comptime {
for (.{ std.posix.socklen_t, C.socklen_t }) |other_socklen| {
if (@sizeOf(socklen) != @sizeOf(other_socklen)) @compileError("socklen_t size mismatch");
if (@alignOf(socklen) != @alignOf(other_socklen)) @compileError("socklen_t alignment mismatch");
}
}

pub fn createBinding(global: *JSC.JSGlobalObject) JSC.JSValue {
const SocketAddress = bun.JSC.GeneratedClassesList.SocketAddress;
const net = JSC.JSValue.createEmptyObjectWithNullPrototype(global);
Expand Down
2 changes: 1 addition & 1 deletion test/js/bun/udp/udp_socket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("udpSocket()", () => {
expect(socket.port).toBe(socket.port); // test that property is cached
expect(socket.hostname).toBeString();
expect(socket.hostname).toBe(socket.hostname); // test that property is cached
expect(socket.address).toEqual({
expect(socket.address).toMatchObject({
address: socket.hostname,
family: socket.hostname === "::" ? "IPv6" : "IPv4",
port: socket.port,
Expand Down
13 changes: 13 additions & 0 deletions test/js/node/net/socketaddress.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ describe("SocketAddress.parse", () => {
])("(%s) == %o", (input, expected) => {
expect(SocketAddress.parse(input)).toMatchObject(expected);
});

it.each([
"",
"invalid",
"1.2.3.4.5.6",
"0.0.0.9999",
"1.2.3.4:-1",
"1.2.3.4:null",
"1.2.3.4:65536",
"[1:0:::::::]", // line break
])("(%s) == undefined", invalidInput => {
expect(SocketAddress.parse(invalidInput)).toBeUndefined();
});
});

describe("SocketAddress.prototype.address", () => {
Expand Down

0 comments on commit f08c06b

Please sign in to comment.