1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23 24require('../common'); 25const assert = require('assert'); 26 27assert.strictEqual(Buffer.from('hello', 'utf8').slice(0, 0).length, 0); 28assert.strictEqual(Buffer('hello', 'utf8').slice(0, 0).length, 0); 29 30const buf = Buffer.from('0123456789', 'utf8'); 31const expectedSameBufs = [ 32 [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], 33 [buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')], 34 [buf.slice(-20, -10), Buffer.from('', 'utf8')], 35 [buf.slice(), Buffer.from('0123456789', 'utf8')], 36 [buf.slice(0), Buffer.from('0123456789', 'utf8')], 37 [buf.slice(0, 0), Buffer.from('', 'utf8')], 38 [buf.slice(undefined), Buffer.from('0123456789', 'utf8')], 39 [buf.slice('foobar'), Buffer.from('0123456789', 'utf8')], 40 [buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')], 41 [buf.slice(2), Buffer.from('23456789', 'utf8')], 42 [buf.slice(5), Buffer.from('56789', 'utf8')], 43 [buf.slice(10), Buffer.from('', 'utf8')], 44 [buf.slice(5, 8), Buffer.from('567', 'utf8')], 45 [buf.slice(8, -1), Buffer.from('8', 'utf8')], 46 [buf.slice(-10), Buffer.from('0123456789', 'utf8')], 47 [buf.slice(0, -9), Buffer.from('0', 'utf8')], 48 [buf.slice(0, -10), Buffer.from('', 'utf8')], 49 [buf.slice(0, -1), Buffer.from('012345678', 'utf8')], 50 [buf.slice(2, -2), Buffer.from('234567', 'utf8')], 51 [buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')], 52 [buf.slice(65536, 0), Buffer.from('', 'utf8')], 53 [buf.slice(-5, -8), Buffer.from('', 'utf8')], 54 [buf.slice(-5, -3), Buffer.from('56', 'utf8')], 55 [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')], 56 [buf.slice('0', '1'), Buffer.from('0', 'utf8')], 57 [buf.slice('-5', '10'), Buffer.from('56789', 'utf8')], 58 [buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')], 59 [buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')], 60 [buf.slice('-10', '-0'), Buffer.from('', 'utf8')], 61 [buf.slice('111'), Buffer.from('', 'utf8')], 62 [buf.slice('0', '-111'), Buffer.from('', 'utf8')], 63]; 64 65for (let i = 0, s = buf.toString(); i < buf.length; ++i) { 66 expectedSameBufs.push( 67 [buf.slice(i), Buffer.from(s.slice(i))], 68 [buf.slice(0, i), Buffer.from(s.slice(0, i))], 69 [buf.slice(-i), Buffer.from(s.slice(-i))], 70 [buf.slice(0, -i), Buffer.from(s.slice(0, -i))] 71 ); 72} 73 74expectedSameBufs.forEach(([buf1, buf2]) => { 75 assert.strictEqual(Buffer.compare(buf1, buf2), 0); 76}); 77 78const utf16Buf = Buffer.from('0123456789', 'utf16le'); 79assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le')); 80// Try to slice a zero length Buffer. 81// See https://github.com/joyent/node/issues/5881 82assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); 83 84{ 85 // Single argument slice 86 assert.strictEqual(Buffer.from('abcde', 'utf8').slice(1).toString('utf8'), 87 'bcde'); 88} 89 90// slice(0,0).length === 0 91assert.strictEqual(Buffer.from('hello', 'utf8').slice(0, 0).length, 0); 92 93{ 94 // Regression tests for https://github.com/nodejs/node/issues/9096 95 const buf = Buffer.from('abcd', 'utf8'); 96 assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd'); 97 assert.strictEqual( 98 buf.slice(buf.length / 3, buf.length).toString(), 99 'bcd' 100 ); 101} 102 103{ 104 const buf = Buffer.from('abcdefg', 'utf8'); 105 assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'), 106 buf.toString('utf8')); 107} 108 109{ 110 const buf = Buffer.from('abc', 'utf8'); 111 assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8')); 112} 113 114{ 115 const buf = Buffer.from([ 116 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, 117 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0, 118 ]); 119 const chunk1 = Buffer.from([ 120 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0, 121 ]); 122 const chunk2 = Buffer.from([ 123 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0, 124 ]); 125 const middle = buf.length / 2; 126 127 assert.deepStrictEqual(buf.slice(0, middle), chunk1); 128 assert.deepStrictEqual(buf.slice(middle), chunk2); 129} 130