summaryrefslogtreecommitdiff
path: root/test/parallel/test-buffer-indexof.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2016-05-02 05:31:48 +0200
committerAnna Henningsen <anna@addaleax.net>2016-05-04 22:13:30 +0200
commit5defa0cbaa9c6d72420df32f6a67fe55edc8160a (patch)
tree74af16bea4c450b1cf42e78a4a4c533667546c6e /test/parallel/test-buffer-indexof.js
parentfc66e55396f9584cda2fde255bf9204926bd2cf2 (diff)
downloadandroid-node-v8-5defa0cbaa9c6d72420df32f6a67fe55edc8160a.tar.gz
android-node-v8-5defa0cbaa9c6d72420df32f6a67fe55edc8160a.tar.bz2
android-node-v8-5defa0cbaa9c6d72420df32f6a67fe55edc8160a.zip
buffer: fix needle length misestimation for UCS2
Use `StringBytes::Size` to determine the needle string length instead of assuming latin-1 or UTF-8. Previously, `Buffer.indexOf` could fail with an assertion failure when the needle's byte length, but not its character count, exceeded the haystack's byte length. PR-URL: https://github.com/nodejs/node/pull/6511 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'test/parallel/test-buffer-indexof.js')
-rw-r--r--test/parallel/test-buffer-indexof.js6
1 files changed, 6 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js
index 0a20d2ce9a..0aeb066f21 100644
--- a/test/parallel/test-buffer-indexof.js
+++ b/test/parallel/test-buffer-indexof.js
@@ -222,6 +222,12 @@ var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
+// Needle is longer than haystack, but only because it's encoded as UTF-16
+assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
+
+assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
+assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1);
+
{
// Find substrings in Utf8.
const lengths = [1, 3, 15]; // Single char, simple and complex.