Compare commits

..

1 Commits

Author SHA1 Message Date
Claude Bot
bcded111d2 fix(test): show empty string keys in object diffs
`Identifier::isEmpty()` returns true for both null identifiers and
empty string `""` identifiers. This caused `forEachPropertyOrdered` and
`forEachPropertyImpl` to skip properties with empty string keys when
iterating over object properties for display.

Replace `property.isEmpty()` with `property.isNull()` to only skip
null identifiers while preserving legitimate empty string property keys.
Also remove redundant `key.len == 0` check in `forEachPropertyImpl`.

Closes #18028

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 10:11:21 +00:00
4 changed files with 70 additions and 125 deletions

View File

@@ -5138,7 +5138,7 @@ restart:
RETURN_IF_EXCEPTION(scope, void());
for (auto& property : properties) {
if (property.isEmpty() || property.isNull()) [[unlikely]]
if (property.isNull()) [[unlikely]]
continue;
// ignore constructor
@@ -5169,9 +5169,6 @@ restart:
ZigString key = toZigString(property.isSymbol() && !property.isPrivateName() ? property.impl() : property.string());
if (key.len == 0)
continue;
JSC::JSValue propertyValue = jsUndefined();
if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) {
@@ -5312,7 +5309,7 @@ extern "C" [[ZIG_EXPORT(nothrow)]] bool JSC__isBigIntInInt64Range(JSC::EncodedJS
auto clientData = WebCore::clientData(vm);
for (auto property : vector) {
if (property.isEmpty() || property.isNull()) [[unlikely]]
if (property.isNull()) [[unlikely]]
continue;
// ignore constructor

View File

@@ -253,9 +253,7 @@ const SocketHandlers: SocketHandler = {
const { checkServerIdentity } = self[bunTLSConnectOptions];
if (!verifyError && typeof checkServerIdentity === "function" && self.servername) {
const cert = self.getPeerCertificate(true);
if (cert) {
verifyError = checkServerIdentity(self.servername, cert);
}
verifyError = checkServerIdentity(self.servername, cert);
}
if (self._requestCert || self._rejectUnauthorized) {
if (verifyError) {
@@ -565,9 +563,7 @@ const SocketHandlers2: SocketHandler<NonNullable<import("node:net").Socket["_han
const { checkServerIdentity } = self[bunTLSConnectOptions];
if (!verifyError && typeof checkServerIdentity === "function" && self.servername) {
const cert = self.getPeerCertificate(true);
if (cert) {
verifyError = checkServerIdentity(self.servername, cert);
}
verifyError = checkServerIdentity(self.servername, cert);
}
if (self._requestCert || self._rejectUnauthorized) {
if (verifyError) {

View File

@@ -0,0 +1,66 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
// https://github.com/oven-sh/bun/issues/18028
// Object diff in bun test should display empty string keys correctly.
test("toStrictEqual diff shows empty string keys", () => {
expect({
val: { "": "value" },
}).toStrictEqual({
val: { "": "value" },
});
});
test("toEqual diff shows empty string keys", () => {
expect({ "": "hello" }).toEqual({ "": "hello" });
});
test("empty string key with various value types", () => {
expect({ "": 0 }).toEqual({ "": 0 });
expect({ "": null }).toEqual({ "": null });
expect({ "": "" }).toEqual({ "": "" });
expect({ "": false }).toEqual({ "": false });
expect({ "": undefined }).toEqual({ "": undefined });
});
test("empty string key mixed with other keys", () => {
expect({ foo: "bar", "": "value" }).toEqual({ foo: "bar", "": "value" });
});
test("toStrictEqual fails and shows diff with empty string key", async () => {
using dir = tempDir("issue-18028", {
"test.test.ts": `
import { test, expect } from "bun:test";
test("diff", () => {
expect({ val: {} }).toStrictEqual({ val: { "": "value" } });
});
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test", "test.test.ts"],
cwd: String(dir),
env: { ...bunEnv, FORCE_COLOR: "0" },
stdout: "pipe",
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
// The diff section should show non-zero changed lines
expect(stderr).toContain("- Expected - 3");
expect(stderr).toContain("+ Received + 1");
// The diff should include the empty string key
expect(stderr).toContain('"": "value"');
expect(exitCode).toBe(1);
});
test("console.log shows empty string keys", () => {
const result = Bun.spawnSync({
cmd: [bunExe(), "-e", 'console.log({ "": "value", foo: "bar" })'],
env: { ...bunEnv, NO_COLOR: "1" },
});
const stdout = result.stdout.toString();
expect(stdout).toContain('"": "value"');
});

View File

@@ -1,114 +0,0 @@
import { expect, test } from "bun:test";
import { tls as COMMON_CERT } from "harness";
import tls from "tls";
// Regression test for https://github.com/oven-sh/bun/issues/23556
// checkServerIdentity should not be called with null/undefined cert
// when getPeerCertificate returns undefined during TLS handshake.
test("checkServerIdentity receives a valid cert object", async () => {
const { promise: serverListening, resolve: resolveListening } = Promise.withResolvers<void>();
const { promise: done, resolve: resolveDone, reject: rejectDone } = Promise.withResolvers<void>();
const server = tls.createServer(COMMON_CERT, socket => {
socket.end();
});
server.listen(0, "127.0.0.1", () => {
resolveListening();
});
await serverListening;
const address = server.address() as { port: number };
const socket = tls.connect(
{
port: address.port,
host: "127.0.0.1",
servername: "localhost",
rejectUnauthorized: false,
checkServerIdentity: (hostname: string, cert: any) => {
// Before the fix, cert could be undefined which would crash
// with: "Cannot destructure property 'subject' from null or undefined"
expect(cert).toBeDefined();
expect(cert).not.toBeNull();
expect(cert.subject).toBeDefined();
return undefined;
},
},
() => {
socket.end();
server.close();
resolveDone();
},
);
socket.on("error", err => {
server.close();
rejectDone(err);
});
await done;
});
test("no crash when getPeerCertificate returns undefined during handshake", async () => {
const { promise: serverListening, resolve: resolveListening } = Promise.withResolvers<void>();
const { promise: done, resolve: resolveDone, reject: rejectDone } = Promise.withResolvers<void>();
const server = tls.createServer(COMMON_CERT, socket => {
socket.end();
});
server.listen(0, "127.0.0.1", () => {
resolveListening();
});
await serverListening;
const address = server.address() as { port: number };
let checkCalledWithUndefined = false;
const socket = tls.connect(
{
port: address.port,
host: "127.0.0.1",
servername: "localhost",
rejectUnauthorized: false,
checkServerIdentity: (hostname: string, cert: any) => {
if (cert === undefined || cert === null) {
checkCalledWithUndefined = true;
}
return undefined;
},
},
() => {
// Monkey-patch getPeerCertificate BEFORE the handshake completes wouldn't
// work because the callback fires after handshake. But the guard in net.ts
// ensures checkServerIdentity is never called with undefined cert.
// This test verifies the connection succeeds without errors.
expect(checkCalledWithUndefined).toBe(false);
socket.end();
server.close();
resolveDone();
},
);
socket.on("error", err => {
server.close();
rejectDone(err);
});
await done;
});
test("tls.checkServerIdentity with null cert throws TypeError", () => {
// The default checkServerIdentity should throw a clear error when
// called with null/undefined cert, not a cryptic destructuring error.
expect(() => {
tls.checkServerIdentity("example.com", null as any);
}).toThrow();
expect(() => {
tls.checkServerIdentity("example.com", undefined as any);
}).toThrow();
});