Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-03-06 06:25:12 +0300
committerItalo A. Casas <me@italoacasas.com>2017-03-13 18:19:27 +0300
commit1af0fa4b84824e3b04c7668923365d23353c3453 (patch)
tree1a9bd6c5e5ea9b881814b964facf3c4f0dc3ccb1 /test
parent1e52ba3b3defeb73637d87b4c49b7039b95b0f9b (diff)
test: test buffer behavior when zeroFill undefined
When ArrayBufferAllocator has an undefined zeroFill property, Buffer.allocUnsafe() should zero fill. Refs: https://github.com/nodejs/node/commit/27e84ddd4e1#commitcomment-19182129 PR-URL: https://github.com/nodejs/node/pull/11706 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-buffer-bindingobj-no-zerofill.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-bindingobj-no-zerofill.js b/test/parallel/test-buffer-bindingobj-no-zerofill.js
new file mode 100644
index 00000000000..a9168713584
--- /dev/null
+++ b/test/parallel/test-buffer-bindingobj-no-zerofill.js
@@ -0,0 +1,53 @@
+'use strict';
+
+// Flags: --expose-internals
+
+// Confirm that if a custom ArrayBufferAllocator does not define a zeroFill
+// property, that the buffer module will zero-fill when allocUnsafe() is called.
+
+require('../common');
+
+const assert = require('assert');
+const buffer = require('buffer');
+
+// Monkey-patch setupBufferJS() to have an undefined zeroFill.
+const process = require('process');
+const originalBinding = process.binding;
+
+const binding = originalBinding('buffer');
+const originalSetup = binding.setupBufferJS;
+
+binding.setupBufferJS = (proto, obj) => {
+ originalSetup(proto, obj);
+ assert.strictEqual(obj.zeroFill[0], 1);
+ delete obj.zeroFill;
+};
+
+const bindingObj = {};
+
+binding.setupBufferJS(Buffer.prototype, bindingObj);
+assert.strictEqual(bindingObj.zeroFill, undefined);
+
+process.binding = (bindee) => {
+ if (bindee === 'buffer')
+ return binding;
+ return originalBinding(bindee);
+};
+
+// Load from file system because internal buffer is already loaded and we're
+// testing code that runs on first load only.
+// Do not move this require() to top of file. It is important that
+// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs.
+const monkeyPatchedBuffer = require('../../lib/buffer');
+
+// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
+// possible that a segment of memory is already zeroed out, so try again and
+// again until we succeed or we time out.
+let uninitialized = buffer.Buffer.allocUnsafe(1024);
+while (uninitialized.some((val) => val !== 0))
+ uninitialized = buffer.Buffer.allocUnsafe(1024);
+
+// On monkeypatched buffer, zeroFill property is undefined. allocUnsafe() should
+// zero-fill in that case.
+const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
+assert(zeroFilled.every((val) => val === 0));