1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5const b = Buffer.from('abcdef'); 6const buf_a = Buffer.from('a'); 7const buf_bc = Buffer.from('bc'); 8const buf_f = Buffer.from('f'); 9const buf_z = Buffer.from('z'); 10const buf_empty = Buffer.from(''); 11 12assert(b.includes('a')); 13assert(!b.includes('a', 1)); 14assert(!b.includes('a', -1)); 15assert(!b.includes('a', -4)); 16assert(b.includes('a', -b.length)); 17assert(b.includes('a', NaN)); 18assert(b.includes('a', -Infinity)); 19assert(!b.includes('a', Infinity)); 20assert(b.includes('bc')); 21assert(!b.includes('bc', 2)); 22assert(!b.includes('bc', -1)); 23assert(!b.includes('bc', -3)); 24assert(b.includes('bc', -5)); 25assert(b.includes('bc', NaN)); 26assert(b.includes('bc', -Infinity)); 27assert(!b.includes('bc', Infinity)); 28assert(b.includes('f'), b.length - 1); 29assert(!b.includes('z')); 30assert(b.includes('')); 31assert(b.includes('', 1)); 32assert(b.includes('', b.length + 1)); 33assert(b.includes('', Infinity)); 34assert(b.includes(buf_a)); 35assert(!b.includes(buf_a, 1)); 36assert(!b.includes(buf_a, -1)); 37assert(!b.includes(buf_a, -4)); 38assert(b.includes(buf_a, -b.length)); 39assert(b.includes(buf_a, NaN)); 40assert(b.includes(buf_a, -Infinity)); 41assert(!b.includes(buf_a, Infinity)); 42assert(b.includes(buf_bc)); 43assert(!b.includes(buf_bc, 2)); 44assert(!b.includes(buf_bc, -1)); 45assert(!b.includes(buf_bc, -3)); 46assert(b.includes(buf_bc, -5)); 47assert(b.includes(buf_bc, NaN)); 48assert(b.includes(buf_bc, -Infinity)); 49assert(!b.includes(buf_bc, Infinity)); 50assert(b.includes(buf_f), b.length - 1); 51assert(!b.includes(buf_z)); 52assert(b.includes(buf_empty)); 53assert(b.includes(buf_empty, 1)); 54assert(b.includes(buf_empty, b.length + 1)); 55assert(b.includes(buf_empty, Infinity)); 56assert(b.includes(0x61)); 57assert(!b.includes(0x61, 1)); 58assert(!b.includes(0x61, -1)); 59assert(!b.includes(0x61, -4)); 60assert(b.includes(0x61, -b.length)); 61assert(b.includes(0x61, NaN)); 62assert(b.includes(0x61, -Infinity)); 63assert(!b.includes(0x61, Infinity)); 64assert(!b.includes(0x0)); 65 66// test offsets 67assert(b.includes('d', 2)); 68assert(b.includes('f', 5)); 69assert(b.includes('f', -1)); 70assert(!b.includes('f', 6)); 71 72assert(b.includes(Buffer.from('d'), 2)); 73assert(b.includes(Buffer.from('f'), 5)); 74assert(b.includes(Buffer.from('f'), -1)); 75assert(!b.includes(Buffer.from('f'), 6)); 76 77assert(!Buffer.from('ff').includes(Buffer.from('f'), 1, 'ucs2')); 78 79// test hex encoding 80assert.strictEqual( 81 Buffer.from(b.toString('hex'), 'hex') 82 .includes('64', 0, 'hex'), 83 true 84); 85assert.strictEqual( 86 Buffer.from(b.toString('hex'), 'hex') 87 .includes(Buffer.from('64', 'hex'), 0, 'hex'), 88 true 89); 90 91// Test base64 encoding 92assert.strictEqual( 93 Buffer.from(b.toString('base64'), 'base64') 94 .includes('ZA==', 0, 'base64'), 95 true 96); 97assert.strictEqual( 98 Buffer.from(b.toString('base64'), 'base64') 99 .includes(Buffer.from('ZA==', 'base64'), 0, 'base64'), 100 true 101); 102 103// test ascii encoding 104assert.strictEqual( 105 Buffer.from(b.toString('ascii'), 'ascii') 106 .includes('d', 0, 'ascii'), 107 true 108); 109assert.strictEqual( 110 Buffer.from(b.toString('ascii'), 'ascii') 111 .includes(Buffer.from('d', 'ascii'), 0, 'ascii'), 112 true 113); 114 115// Test latin1 encoding 116assert.strictEqual( 117 Buffer.from(b.toString('latin1'), 'latin1') 118 .includes('d', 0, 'latin1'), 119 true 120); 121assert.strictEqual( 122 Buffer.from(b.toString('latin1'), 'latin1') 123 .includes(Buffer.from('d', 'latin1'), 0, 'latin1'), 124 true 125); 126 127// Test binary encoding 128assert.strictEqual( 129 Buffer.from(b.toString('binary'), 'binary') 130 .includes('d', 0, 'binary'), 131 true 132); 133assert.strictEqual( 134 Buffer.from(b.toString('binary'), 'binary') 135 .includes(Buffer.from('d', 'binary'), 0, 'binary'), 136 true 137); 138 139 140// test ucs2 encoding 141let twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); 142 143assert(twoByteString.includes('\u0395', 4, 'ucs2')); 144assert(twoByteString.includes('\u03a3', -4, 'ucs2')); 145assert(twoByteString.includes('\u03a3', -6, 'ucs2')); 146assert(twoByteString.includes( 147 Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2')); 148assert(!twoByteString.includes('\u03a3', -2, 'ucs2')); 149 150const mixedByteStringUcs2 = 151 Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2'); 152assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2')); 153assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2')); 154assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2')); 155 156assert.ok( 157 mixedByteStringUcs2.includes(Buffer.from('bc', 'ucs2'), 0, 'ucs2')); 158assert.ok( 159 mixedByteStringUcs2.includes(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2')); 160assert.ok( 161 !mixedByteStringUcs2.includes(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2')); 162 163twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); 164 165// Test single char pattern 166assert(twoByteString.includes('\u039a', 0, 'ucs2')); 167assert(twoByteString.includes('\u0391', 0, 'ucs2'), 'Alpha'); 168assert(twoByteString.includes('\u03a3', 0, 'ucs2'), 'First Sigma'); 169assert(twoByteString.includes('\u03a3', 6, 'ucs2'), 'Second Sigma'); 170assert(twoByteString.includes('\u0395', 0, 'ucs2'), 'Epsilon'); 171assert(!twoByteString.includes('\u0392', 0, 'ucs2'), 'Not beta'); 172 173// Test multi-char pattern 174assert(twoByteString.includes('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha'); 175assert(twoByteString.includes('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma'); 176assert(twoByteString.includes('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma'); 177assert(twoByteString.includes('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon'); 178 179const mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395'); 180assert(mixedByteStringUtf8.includes('bc')); 181assert(mixedByteStringUtf8.includes('bc', 5)); 182assert(mixedByteStringUtf8.includes('bc', -8)); 183assert(mixedByteStringUtf8.includes('\u03a3')); 184assert(!mixedByteStringUtf8.includes('\u0396')); 185 186 187// Test complex string includes algorithms. Only trigger for long strings. 188// Long string that isn't a simple repeat of a shorter string. 189let longString = 'A'; 190for (let i = 66; i < 76; i++) { // from 'B' to 'K' 191 longString = longString + String.fromCharCode(i) + longString; 192} 193 194const longBufferString = Buffer.from(longString); 195 196// Pattern of 15 chars, repeated every 16 chars in long 197let pattern = 'ABACABADABACABA'; 198for (let i = 0; i < longBufferString.length - pattern.length; i += 7) { 199 const includes = longBufferString.includes(pattern, i); 200 assert(includes, `Long ABACABA...-string at index ${i}`); 201} 202assert(longBufferString.includes('AJABACA'), 'Long AJABACA, First J'); 203assert(longBufferString.includes('AJABACA', 511), 'Long AJABACA, Second J'); 204 205pattern = 'JABACABADABACABA'; 206assert(longBufferString.includes(pattern), 'Long JABACABA..., First J'); 207assert(longBufferString.includes(pattern, 512), 'Long JABACABA..., Second J'); 208 209// Search for a non-ASCII string in a pure ASCII string. 210const asciiString = Buffer.from( 211 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf'); 212assert(!asciiString.includes('\x2061')); 213assert(asciiString.includes('leb', 0)); 214 215// Search in string containing many non-ASCII chars. 216const allCodePoints = []; 217for (let i = 0; i < 65534; i++) allCodePoints[i] = i; 218const allCharsString = String.fromCharCode.apply(String, allCodePoints) + 219 String.fromCharCode(65534, 65535); 220const allCharsBufferUtf8 = Buffer.from(allCharsString); 221const allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2'); 222 223// Search for string long enough to trigger complex search with ASCII pattern 224// and UC16 subject. 225assert(!allCharsBufferUtf8.includes('notfound')); 226assert(!allCharsBufferUcs2.includes('notfound')); 227 228// Find substrings in Utf8. 229let lengths = [1, 3, 15]; // Single char, simple and complex. 230let indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]; 231for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 232 for (let i = 0; i < indices.length; i++) { 233 const index = indices[i]; 234 let length = lengths[lengthIndex]; 235 236 if (index + length > 0x7F) { 237 length = 2 * length; 238 } 239 240 if (index + length > 0x7FF) { 241 length = 3 * length; 242 } 243 244 if (index + length > 0xFFFF) { 245 length = 4 * length; 246 } 247 248 const patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length); 249 assert(index, allCharsBufferUtf8.includes(patternBufferUtf8)); 250 251 const patternStringUtf8 = patternBufferUtf8.toString(); 252 assert(index, allCharsBufferUtf8.includes(patternStringUtf8)); 253 } 254} 255 256// Find substrings in Usc2. 257lengths = [2, 4, 16]; // Single char, simple and complex. 258indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]; 259for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) { 260 for (let i = 0; i < indices.length; i++) { 261 const index = indices[i] * 2; 262 const length = lengths[lengthIndex]; 263 264 const patternBufferUcs2 = 265 allCharsBufferUcs2.slice(index, index + length); 266 assert.ok( 267 allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2')); 268 269 const patternStringUcs2 = patternBufferUcs2.toString('ucs2'); 270 assert.ok( 271 allCharsBufferUcs2.includes(patternStringUcs2, 0, 'ucs2')); 272 } 273} 274 275[ 276 () => { }, 277 {}, 278 [], 279].forEach((val) => { 280 assert.throws( 281 () => b.includes(val), 282 { 283 code: 'ERR_INVALID_ARG_TYPE', 284 name: 'TypeError', 285 message: 'The "value" argument must be one of type number or string ' + 286 'or an instance of Buffer or Uint8Array.' + 287 common.invalidArgTypeHelper(val) 288 } 289 ); 290}); 291 292// Test truncation of Number arguments to uint8 293{ 294 const buf = Buffer.from('this is a test'); 295 assert.ok(buf.includes(0x6973)); 296 assert.ok(buf.includes(0x697320)); 297 assert.ok(buf.includes(0x69732069)); 298 assert.ok(buf.includes(0x697374657374)); 299 assert.ok(buf.includes(0x69737374)); 300 assert.ok(buf.includes(0x69737465)); 301 assert.ok(buf.includes(0x69737465)); 302 assert.ok(buf.includes(-140)); 303 assert.ok(buf.includes(-152)); 304 assert.ok(!buf.includes(0xff)); 305 assert.ok(!buf.includes(0xffff)); 306} 307