Skip to content
Browse files

Test cases for #394, graciously stolen from Koichi Kobayash.

Assert that the trailing '\0' is not swallowed in UTF-8 input
to the Buffer constructor.
  • Loading branch information...
1 parent 22fabd4 commit 6c46304b1bd6f5c7f6d0a0fc7547d6b3b9bab045 @bnoordhuis committed
Showing with 26 additions and 0 deletions.
  1. +26 −0 test/simple/test-buffer.js
View
26 test/simple/test-buffer.js
@@ -594,3 +594,29 @@ assert.equal(0xef, b[3]);
assert.throws(function() {
new Buffer('"pong"', 0, 6, 8031, '127.0.0.1')
});
+
+// #1210 Test UTF-8 string includes null character
+var buf = new Buffer('\0');
+assert.equal(buf.length, 1);
+buf = new Buffer('\0\0');
+assert.equal(buf.length, 2);
+
+buf = new Buffer(2);
+var written = buf.write(''); // 0byte
+assert.equal(written, 0);
+written = buf.write('\0'); // 1byte (v8 adds null terminator)
+assert.equal(written, 1);
+written = buf.write('a\0'); // 1byte * 2
+assert.equal(written, 2);
+written = buf.write(''); // 3bytes
+assert.equal(written, 0);
+written = buf.write('\0'); // 1byte + 3bytes
+assert.equal(written, 1);
+written = buf.write('\0\0'); // 1byte * 2 + 3bytes
+assert.equal(written, 2);
+
+buf = new Buffer(10);
+written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
+assert.equal(written, 9);
+written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
+assert.equal(written, 10);

0 comments on commit 6c46304

Please sign in to comment.
Something went wrong with that request. Please try again.