From d401e5575ab49a08752473fe4c9b25142f165d0d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 10 Aug 2016 00:37:55 +0200 Subject: [PATCH] test: add an zlib binding addon test Add a test addon that makes use of the zlib implementation bundled with node, checking that a compression/decompression round-trip works. This is largely based on the already-existing OpenSSL addon. Fixes: https://github.com/nodejs/node/issues/7535 PR-URL: https://github.com/nodejs/node/pull/8039 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig --- test/addons/zlib-binding/binding.cc | 55 ++++++++++++++++++++++++++++ test/addons/zlib-binding/binding.gyp | 9 +++++ test/addons/zlib-binding/test.js | 11 ++++++ 3 files changed, 75 insertions(+) create mode 100644 test/addons/zlib-binding/binding.cc create mode 100644 test/addons/zlib-binding/binding.gyp create mode 100644 test/addons/zlib-binding/test.js diff --git a/test/addons/zlib-binding/binding.cc b/test/addons/zlib-binding/binding.cc new file mode 100644 index 0000000000..262f90bf02 --- /dev/null +++ b/test/addons/zlib-binding/binding.cc @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +namespace { + +inline void CompressBytes(const v8::FunctionCallbackInfo& info) { + assert(info[0]->IsArrayBufferView()); + auto view = info[0].As(); + auto byte_offset = view->ByteOffset(); + auto byte_length = view->ByteLength(); + assert(view->HasBuffer()); + auto buffer = view->Buffer(); + auto contents = buffer->GetContents(); + auto data = static_cast(contents.Data()) + byte_offset; + + Bytef buf[1024]; + + z_stream stream; + stream.zalloc = nullptr; + stream.zfree = nullptr; + + int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, + -15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); + assert(err == Z_OK); + + stream.avail_in = byte_length; + stream.next_in = data; + stream.avail_out = sizeof(buf); + stream.next_out = buf; + err = deflate(&stream, Z_FINISH); + assert(err == Z_STREAM_END); + + auto result = node::Buffer::Copy(info.GetIsolate(), + reinterpret_cast(buf), + sizeof(buf) - stream.avail_out); + + deflateEnd(&stream); + + info.GetReturnValue().Set(result.ToLocalChecked()); +} + +inline void Initialize(v8::Local exports, + v8::Local module, + v8::Local context) { + auto isolate = context->GetIsolate(); + auto key = v8::String::NewFromUtf8(isolate, "compressBytes"); + auto value = v8::FunctionTemplate::New(isolate, CompressBytes)->GetFunction(); + assert(exports->Set(context, key, value).IsJust()); +} + +} // anonymous namespace + +NODE_MODULE_CONTEXT_AWARE(binding, Initialize) diff --git a/test/addons/zlib-binding/binding.gyp b/test/addons/zlib-binding/binding.gyp new file mode 100644 index 0000000000..60a9bb8266 --- /dev/null +++ b/test/addons/zlib-binding/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'sources': ['binding.cc'], + 'include_dirs': ['../../../deps/zlib'], + }, + ] +} diff --git a/test/addons/zlib-binding/test.js b/test/addons/zlib-binding/test.js new file mode 100644 index 0000000000..1bd014507b --- /dev/null +++ b/test/addons/zlib-binding/test.js @@ -0,0 +1,11 @@ +'use strict'; + +require('../../common'); +const assert = require('assert'); +const zlib = require('zlib'); +const binding = require('./build/Release/binding'); + +const input = Buffer.from('Hello, World!'); + +const output = zlib.inflateRawSync(binding.compressBytes(input)); +assert.deepStrictEqual(input, output);