Skip to content

Commit d401e55

Browse files
addaleaxevanlucas
authored andcommitted
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: #7535 PR-URL: #8039 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 1f9fbad commit d401e55

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<node.h>
2+
#include<node_buffer.h>
3+
#include<assert.h>
4+
#include<zlib.h>
5+
6+
namespace{
7+
8+
inlinevoidCompressBytes(const v8::FunctionCallbackInfo<v8::Value>& info){
9+
assert(info[0]->IsArrayBufferView());
10+
auto view = info[0].As<v8::ArrayBufferView>();
11+
auto byte_offset = view->ByteOffset();
12+
auto byte_length = view->ByteLength();
13+
assert(view->HasBuffer());
14+
auto buffer = view->Buffer();
15+
auto contents = buffer->GetContents();
16+
auto data = static_cast<unsignedchar*>(contents.Data()) + byte_offset;
17+
18+
Bytef buf[1024];
19+
20+
z_stream stream;
21+
stream.zalloc = nullptr;
22+
stream.zfree = nullptr;
23+
24+
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
25+
-15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
26+
assert(err == Z_OK);
27+
28+
stream.avail_in = byte_length;
29+
stream.next_in = data;
30+
stream.avail_out = sizeof(buf);
31+
stream.next_out = buf;
32+
err = deflate(&stream, Z_FINISH);
33+
assert(err == Z_STREAM_END);
34+
35+
auto result = node::Buffer::Copy(info.GetIsolate(),
36+
reinterpret_cast<constchar*>(buf),
37+
sizeof(buf) - stream.avail_out);
38+
39+
deflateEnd(&stream);
40+
41+
info.GetReturnValue().Set(result.ToLocalChecked());
42+
}
43+
44+
inlinevoidInitialize(v8::Local<v8::Object> exports,
45+
v8::Local<v8::Value> module,
46+
v8::Local<v8::Context> context){
47+
auto isolate = context->GetIsolate();
48+
auto key = v8::String::NewFromUtf8(isolate, "compressBytes");
49+
auto value = v8::FunctionTemplate::New(isolate, CompressBytes)->GetFunction();
50+
assert(exports->Set(context, key, value).IsJust());
51+
}
52+
53+
} // anonymous namespace
54+
55+
NODE_MODULE_CONTEXT_AWARE(binding, Initialize)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'sources': ['binding.cc'],
6+
'include_dirs': ['../../../deps/zlib'],
7+
},
8+
]
9+
}

‎test/addons/zlib-binding/test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
require('../../common');
4+
constassert=require('assert');
5+
constzlib=require('zlib');
6+
constbinding=require('./build/Release/binding');
7+
8+
constinput=Buffer.from('Hello, World!');
9+
10+
constoutput=zlib.inflateRawSync(binding.compressBytes(input));
11+
assert.deepStrictEqual(input,output);

0 commit comments

Comments
(0)