11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ci// Verify that the string decoder works getting 1 byte at a time, 241cb0ef41Sopenharmony_ci// the whole buffer at once, and that both match the .toString(enc) 251cb0ef41Sopenharmony_ci// result of the entire buffer. 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_cirequire('../common'); 281cb0ef41Sopenharmony_ciconst assert = require('assert'); 291cb0ef41Sopenharmony_ciconst SD = require('string_decoder').StringDecoder; 301cb0ef41Sopenharmony_ciconst encodings = ['base64', 'base64url', 'hex', 'utf8', 'utf16le', 'ucs2']; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst bufs = [ '☃', 'asdf' ].map((b) => Buffer.from(b)); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci// Also test just arbitrary bytes from 0-15. 351cb0ef41Sopenharmony_cifor (let i = 1; i <= 16; i++) { 361cb0ef41Sopenharmony_ci const bytes = '.'.repeat(i - 1).split('.').map((_, j) => j + 0x78); 371cb0ef41Sopenharmony_ci bufs.push(Buffer.from(bytes)); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciencodings.forEach(testEncoding); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2), Buffer.of(0x61), '\uFFFDa'); 431cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2), Buffer.of(0x82), '\uFFFD\uFFFD'); 441cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2), Buffer.of(0xE2), '\uFFFD\uFFFD'); 451cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0x61), '\uFFFDa'); 461cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xAC), '\uFFFD\uFFFD'); 471cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xE2), '\uFFFD\uFFFD'); 481cb0ef41Sopenharmony_citestEnd('utf8', Buffer.of(0xE2, 0x82, 0xAC), Buffer.of(0x61), '€a'); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D), Buffer.of(0x61, 0x00), 'a'); 511cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D), Buffer.of(0xD8, 0x4D, 0xDC), '\u4DD8'); 521cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(), '\uD83D'); 531cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(0x61, 0x00), '\uD83Da'); 541cb0ef41Sopenharmony_citestEnd( 551cb0ef41Sopenharmony_ci 'utf16le', 561cb0ef41Sopenharmony_ci Buffer.of(0x3D, 0xD8), 571cb0ef41Sopenharmony_ci Buffer.of(0x4D, 0xDC), 581cb0ef41Sopenharmony_ci '\uD83D\uDC4D' 591cb0ef41Sopenharmony_ci); 601cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(), '\uD83D'); 611cb0ef41Sopenharmony_citestEnd( 621cb0ef41Sopenharmony_ci 'utf16le', 631cb0ef41Sopenharmony_ci Buffer.of(0x3D, 0xD8, 0x4D), 641cb0ef41Sopenharmony_ci Buffer.of(0x61, 0x00), 651cb0ef41Sopenharmony_ci '\uD83Da' 661cb0ef41Sopenharmony_ci); 671cb0ef41Sopenharmony_citestEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(0xDC), '\uD83D'); 681cb0ef41Sopenharmony_citestEnd( 691cb0ef41Sopenharmony_ci 'utf16le', 701cb0ef41Sopenharmony_ci Buffer.of(0x3D, 0xD8, 0x4D, 0xDC), 711cb0ef41Sopenharmony_ci Buffer.of(0x61, 0x00), 721cb0ef41Sopenharmony_ci 'a' 731cb0ef41Sopenharmony_ci); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61), Buffer.of(), 'YQ=='); 761cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61), Buffer.of(0x61), 'YQ==YQ=='); 771cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(), 'YWE='); 781cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(0x61), 'YWE=YQ=='); 791cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(), 'YWFh'); 801cb0ef41Sopenharmony_citestEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), 'YWFhYQ=='); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61), Buffer.of(), 'YQ'); 831cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61), Buffer.of(0x61), 'YQYQ'); 841cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61, 0x61), Buffer.of(), 'YWE'); 851cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61, 0x61), Buffer.of(0x61), 'YWEYQ'); 861cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61, 0x61, 0x61), Buffer.of(), 'YWFh'); 871cb0ef41Sopenharmony_citestEnd('base64url', Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), 'YWFhYQ'); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_cifunction testEncoding(encoding) { 901cb0ef41Sopenharmony_ci bufs.forEach((buf) => { 911cb0ef41Sopenharmony_ci testBuf(encoding, buf); 921cb0ef41Sopenharmony_ci }); 931cb0ef41Sopenharmony_ci} 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_cifunction testBuf(encoding, buf) { 961cb0ef41Sopenharmony_ci // Write one byte at a time. 971cb0ef41Sopenharmony_ci let s = new SD(encoding); 981cb0ef41Sopenharmony_ci let res1 = ''; 991cb0ef41Sopenharmony_ci for (let i = 0; i < buf.length; i++) { 1001cb0ef41Sopenharmony_ci res1 += s.write(buf.slice(i, i + 1)); 1011cb0ef41Sopenharmony_ci } 1021cb0ef41Sopenharmony_ci res1 += s.end(); 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci // Write the whole buffer at once. 1051cb0ef41Sopenharmony_ci let res2 = ''; 1061cb0ef41Sopenharmony_ci s = new SD(encoding); 1071cb0ef41Sopenharmony_ci res2 += s.write(buf); 1081cb0ef41Sopenharmony_ci res2 += s.end(); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci // .toString() on the buffer 1111cb0ef41Sopenharmony_ci const res3 = buf.toString(encoding); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci // One byte at a time should match toString 1141cb0ef41Sopenharmony_ci assert.strictEqual(res1, res3); 1151cb0ef41Sopenharmony_ci // All bytes at once should match toString 1161cb0ef41Sopenharmony_ci assert.strictEqual(res2, res3); 1171cb0ef41Sopenharmony_ci} 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_cifunction testEnd(encoding, incomplete, next, expected) { 1201cb0ef41Sopenharmony_ci let res = ''; 1211cb0ef41Sopenharmony_ci const s = new SD(encoding); 1221cb0ef41Sopenharmony_ci res += s.write(incomplete); 1231cb0ef41Sopenharmony_ci res += s.end(); 1241cb0ef41Sopenharmony_ci res += s.write(next); 1251cb0ef41Sopenharmony_ci res += s.end(); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci assert.strictEqual(res, expected); 1281cb0ef41Sopenharmony_ci} 129