From 517bc28da85f2f79bdb54b16a217855f0ccc00ae Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 13 Dec 2024 21:51:02 -0800 Subject: [PATCH] node: fix test-zlib-from-gzip-with-trailing-garbage.js (#15757) --- src/bun.js/node/node_zlib_binding.zig | 12 +++- ...st-zlib-from-gzip-with-trailing-garbage.js | 66 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/js/node/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js diff --git a/src/bun.js/node/node_zlib_binding.zig b/src/bun.js/node/node_zlib_binding.zig index 8531c209d5..19f5e7d556 100644 --- a/src/bun.js/node/node_zlib_binding.zig +++ b/src/bun.js/node/node_zlib_binding.zig @@ -497,7 +497,17 @@ const ZlibContext = struct { return .{ .msg = message, .err = @intFromEnum(this.err), - .code = @tagName(this.err), + .code = switch (this.err) { + .Ok => "Z_OK", + .StreamEnd => "Z_STREAM_END", + .NeedDict => "Z_NEED_DICT", + .ErrNo => "Z_ERRNO", + .StreamError => "Z_STREAM_ERROR", + .DataError => "Z_DATA_ERROR", + .MemError => "Z_MEM_ERROR", + .BufError => "Z_BUF_ERROR", + .VersionError => "Z_VERSION_ERROR", + }, }; } diff --git a/test/js/node/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js b/test/js/node/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js new file mode 100644 index 0000000000..477a6c544f --- /dev/null +++ b/test/js/node/test/parallel/test-zlib-from-gzip-with-trailing-garbage.js @@ -0,0 +1,66 @@ +'use strict'; +// Test unzipping a gzip file that has trailing garbage + +const common = require('../common'); +const assert = require('assert'); +const zlib = require('zlib'); + +// Should ignore trailing null-bytes +let data = Buffer.concat([ + zlib.gzipSync('abc'), + zlib.gzipSync('def'), + Buffer.alloc(10), +]); + +assert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef'); + +zlib.gunzip(data, common.mustSucceed((result) => { + assert.strictEqual( + result.toString(), + 'abcdef', + `result '${result.toString()}' should match original string` + ); +})); + +// If the trailing garbage happens to look like a gzip header, it should +// throw an error. +data = Buffer.concat([ + zlib.gzipSync('abc'), + zlib.gzipSync('def'), + Buffer.from([0x1f, 0x8b, 0xff, 0xff]), + Buffer.alloc(10), +]); + +assert.throws( + () => zlib.gunzipSync(data), + /^Error: unknown compression method$/ +); + +zlib.gunzip(data, common.mustCall((err, result) => { + common.expectsError({ + code: 'Z_DATA_ERROR', + name: 'Error', + message: 'unknown compression method' + })(err); + assert.strictEqual(result, undefined); +})); + +// In this case the trailing junk is too short to be a gzip segment +// So we ignore it and decompression succeeds. +data = Buffer.concat([ + zlib.gzipSync('abc'), + zlib.gzipSync('def'), + Buffer.from([0x1f, 0x8b, 0xff, 0xff]), +]); + +assert.throws( + () => zlib.gunzipSync(data), + /^Error: unknown compression method$/ +); + +zlib.gunzip(data, common.mustCall((err, result) => { + assert(err instanceof Error); + assert.strictEqual(err.code, 'Z_DATA_ERROR'); + assert.strictEqual(err.message, 'unknown compression method'); + assert.strictEqual(result, undefined); +}));