Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test-buffer-alloc.js 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var common = { skip: function () {} };
  4. var assert = require('assert');
  5. var Buffer = require('../../').Buffer;
  6. // counter to ensure unique value is always copied
  7. var cntr = 0;
  8. var b = Buffer.allocUnsafe(1024);
  9. // console.log('b.length == %d', b.length);
  10. assert.strictEqual(1024, b.length);
  11. b[0] = -1;
  12. assert.strictEqual(b[0], 255);
  13. for (var i = 0; i < 1024; i++) {
  14. b[i] = i % 256;
  15. }
  16. for (var i = 0; i < 1024; i++) {
  17. assert.strictEqual(i % 256, b[i]);
  18. }
  19. var c = Buffer.allocUnsafe(512);
  20. // console.log('c.length == %d', c.length);
  21. assert.strictEqual(512, c.length);
  22. var d = Buffer.from([]);
  23. assert.strictEqual(0, d.length);
  24. var ui32 = new Uint32Array(4).fill(42);
  25. var e = Buffer.from(ui32);
  26. for (var [index, value] of e.entries()) {
  27. assert.strictEqual(value, ui32[index]);
  28. }
  29. // First check Buffer#fill() works as expected.
  30. assert.throws(function() {
  31. Buffer.allocUnsafe(8).fill('a', -1);
  32. });
  33. assert.throws(function() {
  34. Buffer.allocUnsafe(8).fill('a', 0, 9);
  35. });
  36. // Make sure this doesn't hang indefinitely.
  37. Buffer.allocUnsafe(8).fill('');
  38. Buffer.alloc(8, '');
  39. {
  40. var buf = Buffer.alloc(64, 10);
  41. for (var i = 0; i < buf.length; i++)
  42. assert.equal(buf[i], 10);
  43. buf.fill(11, 0, buf.length >> 1);
  44. for (var i = 0; i < buf.length >> 1; i++)
  45. assert.equal(buf[i], 11);
  46. for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
  47. assert.equal(buf[i], 10);
  48. buf.fill('h');
  49. for (var i = 0; i < buf.length; i++)
  50. assert.equal('h'.charCodeAt(0), buf[i]);
  51. buf.fill(0);
  52. for (var i = 0; i < buf.length; i++)
  53. assert.equal(0, buf[i]);
  54. buf.fill(null);
  55. for (var i = 0; i < buf.length; i++)
  56. assert.equal(0, buf[i]);
  57. buf.fill(1, 16, 32);
  58. for (var i = 0; i < 16; i++)
  59. assert.equal(0, buf[i]);
  60. for (var i = 16; i < 32; i++)
  61. assert.equal(1, buf[i]);
  62. for (var i = 32; i < buf.length; i++)
  63. assert.equal(0, buf[i]);
  64. }
  65. {
  66. var buf = Buffer.alloc(10, 'abc');
  67. assert.equal(buf.toString(), 'abcabcabca');
  68. buf.fill('է');
  69. assert.equal(buf.toString(), 'էէէէէ');
  70. }
  71. {
  72. // copy 512 bytes, from 0 to 512.
  73. b.fill(++cntr);
  74. c.fill(++cntr);
  75. var copied = b.copy(c, 0, 0, 512);
  76. // console.log('copied %d bytes from b into c', copied);
  77. assert.strictEqual(512, copied);
  78. for (var i = 0; i < c.length; i++) {
  79. assert.strictEqual(b[i], c[i]);
  80. }
  81. }
  82. {
  83. // copy c into b, without specifying sourceEnd
  84. b.fill(++cntr);
  85. c.fill(++cntr);
  86. var copied = c.copy(b, 0, 0);
  87. // console.log('copied %d bytes from c into b w/o sourceEnd', copied);
  88. assert.strictEqual(c.length, copied);
  89. for (var i = 0; i < c.length; i++) {
  90. assert.strictEqual(c[i], b[i]);
  91. }
  92. }
  93. {
  94. // copy c into b, without specifying sourceStart
  95. b.fill(++cntr);
  96. c.fill(++cntr);
  97. var copied = c.copy(b, 0);
  98. // console.log('copied %d bytes from c into b w/o sourceStart', copied);
  99. assert.strictEqual(c.length, copied);
  100. for (var i = 0; i < c.length; i++) {
  101. assert.strictEqual(c[i], b[i]);
  102. }
  103. }
  104. {
  105. // copy longer buffer b to shorter c without targetStart
  106. b.fill(++cntr);
  107. c.fill(++cntr);
  108. var copied = b.copy(c);
  109. // console.log('copied %d bytes from b into c w/o targetStart', copied);
  110. assert.strictEqual(c.length, copied);
  111. for (var i = 0; i < c.length; i++) {
  112. assert.strictEqual(b[i], c[i]);
  113. }
  114. }
  115. {
  116. // copy starting near end of b to c
  117. b.fill(++cntr);
  118. c.fill(++cntr);
  119. var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
  120. // console.log('copied %d bytes from end of b into beginning of c', copied);
  121. assert.strictEqual(Math.floor(c.length / 2), copied);
  122. for (var i = 0; i < Math.floor(c.length / 2); i++) {
  123. assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
  124. }
  125. for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
  126. assert.strictEqual(c[c.length - 1], c[i]);
  127. }
  128. }
  129. {
  130. // try to copy 513 bytes, and check we don't overrun c
  131. b.fill(++cntr);
  132. c.fill(++cntr);
  133. var copied = b.copy(c, 0, 0, 513);
  134. // console.log('copied %d bytes from b trying to overrun c', copied);
  135. assert.strictEqual(c.length, copied);
  136. for (var i = 0; i < c.length; i++) {
  137. assert.strictEqual(b[i], c[i]);
  138. }
  139. }
  140. {
  141. // copy 768 bytes from b into b
  142. b.fill(++cntr);
  143. b.fill(++cntr, 256);
  144. var copied = b.copy(b, 0, 256, 1024);
  145. // console.log('copied %d bytes from b into b', copied);
  146. assert.strictEqual(768, copied);
  147. for (var i = 0; i < b.length; i++) {
  148. assert.strictEqual(cntr, b[i]);
  149. }
  150. }
  151. // copy string longer than buffer length (failure will segfault)
  152. var bb = Buffer.allocUnsafe(10);
  153. bb.fill('hello crazy world');
  154. // try to copy from before the beginning of b
  155. assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });
  156. // copy throws at negative sourceStart
  157. assert.throws(function() {
  158. Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1);
  159. }, RangeError);
  160. {
  161. // check sourceEnd resets to targetEnd if former is greater than the latter
  162. b.fill(++cntr);
  163. c.fill(++cntr);
  164. var copied = b.copy(c, 0, 0, 1025);
  165. // console.log('copied %d bytes from b into c', copied);
  166. for (var i = 0; i < c.length; i++) {
  167. assert.strictEqual(b[i], c[i]);
  168. }
  169. }
  170. // throw with negative sourceEnd
  171. // console.log('test copy at negative sourceEnd');
  172. assert.throws(function() {
  173. b.copy(c, 0, 0, -1);
  174. }, RangeError);
  175. // when sourceStart is greater than sourceEnd, zero copied
  176. assert.equal(b.copy(c, 0, 100, 10), 0);
  177. // when targetStart > targetLength, zero copied
  178. assert.equal(b.copy(c, 512, 0, 10), 0);
  179. var caught_error;
  180. // invalid encoding for Buffer.toString
  181. caught_error = null;
  182. try {
  183. b.toString('invalid');
  184. } catch (err) {
  185. caught_error = err;
  186. }
  187. assert.strictEqual('Unknown encoding: invalid', caught_error.message);
  188. // invalid encoding for Buffer.write
  189. caught_error = null;
  190. try {
  191. b.write('test string', 0, 5, 'invalid');
  192. } catch (err) {
  193. caught_error = err;
  194. }
  195. assert.strictEqual('Unknown encoding: invalid', caught_error.message);
  196. // try to create 0-length buffers
  197. Buffer.from('');
  198. Buffer.from('', 'ascii');
  199. Buffer.from('', 'latin1');
  200. Buffer.alloc(0);
  201. Buffer.allocUnsafe(0);
  202. // try to write a 0-length string beyond the end of b
  203. assert.throws(function() {
  204. b.write('', 2048);
  205. }, RangeError);
  206. // throw when writing to negative offset
  207. assert.throws(function() {
  208. b.write('a', -1);
  209. }, RangeError);
  210. // throw when writing past bounds from the pool
  211. assert.throws(function() {
  212. b.write('a', 2048);
  213. }, RangeError);
  214. // throw when writing to negative offset
  215. assert.throws(function() {
  216. b.write('a', -1);
  217. }, RangeError);
  218. // try to copy 0 bytes worth of data into an empty buffer
  219. b.copy(Buffer.alloc(0), 0, 0, 0);
  220. // try to copy 0 bytes past the end of the target buffer
  221. b.copy(Buffer.alloc(0), 1, 1, 1);
  222. b.copy(Buffer.alloc(1), 1, 1, 1);
  223. // try to copy 0 bytes from past the end of the source buffer
  224. b.copy(Buffer.alloc(1), 0, 2048, 2048);
  225. var rangeBuffer = Buffer.from('abc');
  226. // if start >= buffer's length, empty string will be returned
  227. assert.equal(rangeBuffer.toString('ascii', 3), '');
  228. assert.equal(rangeBuffer.toString('ascii', +Infinity), '');
  229. assert.equal(rangeBuffer.toString('ascii', 3.14, 3), '');
  230. assert.equal(rangeBuffer.toString('ascii', 'Infinity', 3), '');
  231. // if end <= 0, empty string will be returned
  232. assert.equal(rangeBuffer.toString('ascii', 1, 0), '');
  233. assert.equal(rangeBuffer.toString('ascii', 1, -1.2), '');
  234. assert.equal(rangeBuffer.toString('ascii', 1, -100), '');
  235. assert.equal(rangeBuffer.toString('ascii', 1, -Infinity), '');
  236. // if start < 0, start will be taken as zero
  237. assert.equal(rangeBuffer.toString('ascii', -1, 3), 'abc');
  238. assert.equal(rangeBuffer.toString('ascii', -1.99, 3), 'abc');
  239. assert.equal(rangeBuffer.toString('ascii', -Infinity, 3), 'abc');
  240. assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
  241. assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
  242. assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
  243. // if start is an invalid integer, start will be taken as zero
  244. assert.equal(rangeBuffer.toString('ascii', 'node.js', 3), 'abc');
  245. assert.equal(rangeBuffer.toString('ascii', {}, 3), 'abc');
  246. assert.equal(rangeBuffer.toString('ascii', [], 3), 'abc');
  247. assert.equal(rangeBuffer.toString('ascii', NaN, 3), 'abc');
  248. assert.equal(rangeBuffer.toString('ascii', null, 3), 'abc');
  249. assert.equal(rangeBuffer.toString('ascii', undefined, 3), 'abc');
  250. assert.equal(rangeBuffer.toString('ascii', false, 3), 'abc');
  251. assert.equal(rangeBuffer.toString('ascii', '', 3), 'abc');
  252. // but, if start is an integer when coerced, then it will be coerced and used.
  253. assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
  254. assert.equal(rangeBuffer.toString('ascii', '1', 3), 'bc');
  255. assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
  256. assert.equal(rangeBuffer.toString('ascii', '3', 3), '');
  257. assert.equal(rangeBuffer.toString('ascii', Number(3), 3), '');
  258. assert.equal(rangeBuffer.toString('ascii', '3.14', 3), '');
  259. assert.equal(rangeBuffer.toString('ascii', '1.99', 3), 'bc');
  260. assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
  261. assert.equal(rangeBuffer.toString('ascii', 1.99, 3), 'bc');
  262. assert.equal(rangeBuffer.toString('ascii', true, 3), 'bc');
  263. // if end > buffer's length, end will be taken as buffer's length
  264. assert.equal(rangeBuffer.toString('ascii', 0, 5), 'abc');
  265. assert.equal(rangeBuffer.toString('ascii', 0, 6.99), 'abc');
  266. assert.equal(rangeBuffer.toString('ascii', 0, Infinity), 'abc');
  267. assert.equal(rangeBuffer.toString('ascii', 0, '5'), 'abc');
  268. assert.equal(rangeBuffer.toString('ascii', 0, '6.99'), 'abc');
  269. assert.equal(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc');
  270. // if end is an invalid integer, end will be taken as buffer's length
  271. assert.equal(rangeBuffer.toString('ascii', 0, 'node.js'), '');
  272. assert.equal(rangeBuffer.toString('ascii', 0, {}), '');
  273. assert.equal(rangeBuffer.toString('ascii', 0, NaN), '');
  274. assert.equal(rangeBuffer.toString('ascii', 0, undefined), 'abc');
  275. assert.equal(rangeBuffer.toString('ascii', 0), 'abc');
  276. assert.equal(rangeBuffer.toString('ascii', 0, null), '');
  277. assert.equal(rangeBuffer.toString('ascii', 0, []), '');
  278. assert.equal(rangeBuffer.toString('ascii', 0, false), '');
  279. assert.equal(rangeBuffer.toString('ascii', 0, ''), '');
  280. // but, if end is an integer when coerced, then it will be coerced and used.
  281. assert.equal(rangeBuffer.toString('ascii', 0, '-1'), '');
  282. assert.equal(rangeBuffer.toString('ascii', 0, '1'), 'a');
  283. assert.equal(rangeBuffer.toString('ascii', 0, '-Infinity'), '');
  284. assert.equal(rangeBuffer.toString('ascii', 0, '3'), 'abc');
  285. assert.equal(rangeBuffer.toString('ascii', 0, Number(3)), 'abc');
  286. assert.equal(rangeBuffer.toString('ascii', 0, '3.14'), 'abc');
  287. assert.equal(rangeBuffer.toString('ascii', 0, '1.99'), 'a');
  288. assert.equal(rangeBuffer.toString('ascii', 0, '-1.99'), '');
  289. assert.equal(rangeBuffer.toString('ascii', 0, 1.99), 'a');
  290. assert.equal(rangeBuffer.toString('ascii', 0, true), 'a');
  291. // try toString() with a object as a encoding
  292. assert.equal(rangeBuffer.toString({toString: function() {
  293. return 'ascii';
  294. }}), 'abc');
  295. // testing for smart defaults and ability to pass string values as offset
  296. var writeTest = Buffer.from('abcdes');
  297. writeTest.write('n', 'ascii');
  298. writeTest.write('o', '1', 'ascii');
  299. writeTest.write('d', '2', 'ascii');
  300. writeTest.write('e', 3, 'ascii');
  301. writeTest.write('j', 4, 'ascii');
  302. assert.equal(writeTest.toString(), 'nodejs');
  303. // ASCII slice test
  304. {
  305. var asciiString = 'hello world';
  306. for (var i = 0; i < asciiString.length; i++) {
  307. b[i] = asciiString.charCodeAt(i);
  308. }
  309. var asciiSlice = b.toString('ascii', 0, asciiString.length);
  310. assert.equal(asciiString, asciiSlice);
  311. }
  312. {
  313. var asciiString = 'hello world';
  314. var offset = 100;
  315. var written = b.write(asciiString, offset, 'ascii');
  316. assert.equal(asciiString.length, written);
  317. var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
  318. assert.equal(asciiString, asciiSlice);
  319. }
  320. {
  321. var asciiString = 'hello world';
  322. var offset = 100;
  323. var sliceA = b.slice(offset, offset + asciiString.length);
  324. var sliceB = b.slice(offset, offset + asciiString.length);
  325. for (var i = 0; i < asciiString.length; i++) {
  326. assert.equal(sliceA[i], sliceB[i]);
  327. }
  328. }
  329. // UTF-8 slice test
  330. var utf8String = '¡hέlló wôrld!';
  331. var offset = 100;
  332. b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8');
  333. var utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String));
  334. assert.equal(utf8String, utf8Slice);
  335. var written = b.write(utf8String, offset, 'utf8');
  336. assert.equal(Buffer.byteLength(utf8String), written);
  337. utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String));
  338. assert.equal(utf8String, utf8Slice);
  339. var sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String));
  340. var sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String));
  341. for (var i = 0; i < Buffer.byteLength(utf8String); i++) {
  342. assert.equal(sliceA[i], sliceB[i]);
  343. }
  344. {
  345. var slice = b.slice(100, 150);
  346. assert.equal(50, slice.length);
  347. for (var i = 0; i < 50; i++) {
  348. assert.equal(b[100 + i], slice[i]);
  349. }
  350. }
  351. {
  352. // make sure only top level parent propagates from allocPool
  353. var b = Buffer.allocUnsafe(5);
  354. var c = b.slice(0, 4);
  355. var d = c.slice(0, 2);
  356. assert.equal(b.parent, c.parent);
  357. assert.equal(b.parent, d.parent);
  358. }
  359. {
  360. // also from a non-pooled instance
  361. var b = Buffer.allocUnsafeSlow(5);
  362. var c = b.slice(0, 4);
  363. var d = c.slice(0, 2);
  364. assert.equal(c.parent, d.parent);
  365. }
  366. {
  367. // Bug regression test
  368. var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
  369. var buffer = Buffer.allocUnsafe(32);
  370. var size = buffer.write(testValue, 0, 'utf8');
  371. // console.log('bytes written to buffer: ' + size);
  372. var slice = buffer.toString('utf8', 0, size);
  373. assert.equal(slice, testValue);
  374. }
  375. {
  376. // Test triple slice
  377. var a = Buffer.allocUnsafe(8);
  378. for (var i = 0; i < 8; i++) a[i] = i;
  379. var b = a.slice(4, 8);
  380. assert.equal(4, b[0]);
  381. assert.equal(5, b[1]);
  382. assert.equal(6, b[2]);
  383. assert.equal(7, b[3]);
  384. var c = b.slice(2, 4);
  385. assert.equal(6, c[0]);
  386. assert.equal(7, c[1]);
  387. }
  388. {
  389. var d = Buffer.from([23, 42, 255]);
  390. assert.equal(d.length, 3);
  391. assert.equal(d[0], 23);
  392. assert.equal(d[1], 42);
  393. assert.equal(d[2], 255);
  394. assert.deepStrictEqual(d, Buffer.from(d));
  395. }
  396. {
  397. var e = Buffer.from('über');
  398. // console.error('uber: \'%s\'', e.toString());
  399. assert.deepStrictEqual(e, Buffer.from([195, 188, 98, 101, 114]));
  400. }
  401. {
  402. var f = Buffer.from('über', 'ascii');
  403. // console.error('f.length: %d (should be 4)', f.length);
  404. assert.deepStrictEqual(f, Buffer.from([252, 98, 101, 114]));
  405. }
  406. ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  407. {
  408. var f = Buffer.from('über', encoding);
  409. // console.error('f.length: %d (should be 8)', f.length);
  410. assert.deepStrictEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0]));
  411. }
  412. {
  413. var f = Buffer.from('привет', encoding);
  414. // console.error('f.length: %d (should be 12)', f.length);
  415. assert.deepStrictEqual(f,
  416. Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
  417. assert.equal(f.toString(encoding), 'привет');
  418. }
  419. {
  420. var f = Buffer.from([0, 0, 0, 0, 0]);
  421. assert.equal(f.length, 5);
  422. var size = f.write('あいうえお', encoding);
  423. // console.error('bytes written to buffer: %d (should be 4)', size);
  424. assert.equal(size, 4);
  425. assert.deepStrictEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00]));
  426. }
  427. });
  428. {
  429. var f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
  430. assert.equal(f.length, 4);
  431. assert.deepStrictEqual(f, Buffer.from('3DD84DDC', 'hex'));
  432. }
  433. var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
  434. var g = Buffer.from(arrayIsh);
  435. assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3]));
  436. var strArrayIsh = {0: '0', 1: '1', 2: '2', 3: '3', length: 4};
  437. g = Buffer.from(strArrayIsh);
  438. assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3]));
  439. //
  440. // Test toString('base64')
  441. //
  442. assert.equal('TWFu', (Buffer.from('Man')).toString('base64'));
  443. {
  444. // test that regular and URL-safe base64 both work
  445. var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
  446. assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'),
  447. Buffer.from(expected));
  448. assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'),
  449. Buffer.from(expected));
  450. }
  451. {
  452. // big example
  453. var quote = 'Man is distinguished, not only by his reason, but by this ' +
  454. 'singular passion from other animals, which is a lust ' +
  455. 'of the mind, that by a perseverance of delight in the ' +
  456. 'continued and indefatigable generation of knowledge, ' +
  457. 'exceeds the short vehemence of any carnal pleasure.';
  458. var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' +
  459. '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' +
  460. 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' +
  461. 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' +
  462. 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' +
  463. 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' +
  464. '5hbCBwbGVhc3VyZS4=';
  465. assert.equal(expected, (Buffer.from(quote)).toString('base64'));
  466. var b = Buffer.allocUnsafe(1024);
  467. var bytesWritten = b.write(expected, 0, 'base64');
  468. assert.equal(quote.length, bytesWritten);
  469. assert.equal(quote, b.toString('ascii', 0, quote.length));
  470. // check that the base64 decoder ignores whitespace
  471. var expectedWhite = expected.slice(0, 60) + ' \n' +
  472. expected.slice(60, 120) + ' \n' +
  473. expected.slice(120, 180) + ' \n' +
  474. expected.slice(180, 240) + ' \n' +
  475. expected.slice(240, 300) + '\n' +
  476. expected.slice(300, 360) + '\n';
  477. b = Buffer.allocUnsafe(1024);
  478. bytesWritten = b.write(expectedWhite, 0, 'base64');
  479. assert.equal(quote.length, bytesWritten);
  480. assert.equal(quote, b.toString('ascii', 0, quote.length));
  481. // check that the base64 decoder on the constructor works
  482. // even in the presence of whitespace.
  483. b = Buffer.from(expectedWhite, 'base64');
  484. assert.equal(quote.length, b.length);
  485. assert.equal(quote, b.toString('ascii', 0, quote.length));
  486. // check that the base64 decoder ignores illegal chars
  487. var expectedIllegal = expected.slice(0, 60) + ' \x80' +
  488. expected.slice(60, 120) + ' \xff' +
  489. expected.slice(120, 180) + ' \x00' +
  490. expected.slice(180, 240) + ' \x98' +
  491. expected.slice(240, 300) + '\x03' +
  492. expected.slice(300, 360);
  493. b = Buffer.from(expectedIllegal, 'base64');
  494. assert.equal(quote.length, b.length);
  495. assert.equal(quote, b.toString('ascii', 0, quote.length));
  496. }
  497. assert.equal(Buffer.from('', 'base64').toString(), '');
  498. assert.equal(Buffer.from('K', 'base64').toString(), '');
  499. // multiple-of-4 with padding
  500. assert.equal(Buffer.from('Kg==', 'base64').toString(), '*');
  501. assert.equal(Buffer.from('Kio=', 'base64').toString(), '**');
  502. assert.equal(Buffer.from('Kioq', 'base64').toString(), '***');
  503. assert.equal(Buffer.from('KioqKg==', 'base64').toString(), '****');
  504. assert.equal(Buffer.from('KioqKio=', 'base64').toString(), '*****');
  505. assert.equal(Buffer.from('KioqKioq', 'base64').toString(), '******');
  506. assert.equal(Buffer.from('KioqKioqKg==', 'base64').toString(), '*******');
  507. assert.equal(Buffer.from('KioqKioqKio=', 'base64').toString(), '********');
  508. assert.equal(Buffer.from('KioqKioqKioq', 'base64').toString(), '*********');
  509. assert.equal(Buffer.from('KioqKioqKioqKg==', 'base64').toString(),
  510. '**********');
  511. assert.equal(Buffer.from('KioqKioqKioqKio=', 'base64').toString(),
  512. '***********');
  513. assert.equal(Buffer.from('KioqKioqKioqKioq', 'base64').toString(),
  514. '************');
  515. assert.equal(Buffer.from('KioqKioqKioqKioqKg==', 'base64').toString(),
  516. '*************');
  517. assert.equal(Buffer.from('KioqKioqKioqKioqKio=', 'base64').toString(),
  518. '**************');
  519. assert.equal(Buffer.from('KioqKioqKioqKioqKioq', 'base64').toString(),
  520. '***************');
  521. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKg==', 'base64').toString(),
  522. '****************');
  523. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKio=', 'base64').toString(),
  524. '*****************');
  525. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioq', 'base64').toString(),
  526. '******************');
  527. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 'base64').toString(),
  528. '*******************');
  529. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 'base64').toString(),
  530. '********************');
  531. // no padding, not a multiple of 4
  532. assert.equal(Buffer.from('Kg', 'base64').toString(), '*');
  533. assert.equal(Buffer.from('Kio', 'base64').toString(), '**');
  534. assert.equal(Buffer.from('KioqKg', 'base64').toString(), '****');
  535. assert.equal(Buffer.from('KioqKio', 'base64').toString(), '*****');
  536. assert.equal(Buffer.from('KioqKioqKg', 'base64').toString(), '*******');
  537. assert.equal(Buffer.from('KioqKioqKio', 'base64').toString(), '********');
  538. assert.equal(Buffer.from('KioqKioqKioqKg', 'base64').toString(), '**********');
  539. assert.equal(Buffer.from('KioqKioqKioqKio', 'base64').toString(),
  540. '***********');
  541. assert.equal(Buffer.from('KioqKioqKioqKioqKg', 'base64').toString(),
  542. '*************');
  543. assert.equal(Buffer.from('KioqKioqKioqKioqKio', 'base64').toString(),
  544. '**************');
  545. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKg', 'base64').toString(),
  546. '****************');
  547. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKio', 'base64').toString(),
  548. '*****************');
  549. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKg', 'base64').toString(),
  550. '*******************');
  551. assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKio', 'base64').toString(),
  552. '********************');
  553. // handle padding graciously, multiple-of-4 or not
  554. assert.equal(
  555. Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==', 'base64').length,
  556. 32
  557. );
  558. assert.equal(
  559. Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=', 'base64').length,
  560. 32
  561. );
  562. assert.equal(
  563. Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw', 'base64').length,
  564. 32
  565. );
  566. assert.equal(
  567. Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==', 'base64').length,
  568. 31
  569. );
  570. assert.equal(
  571. Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=', 'base64').length,
  572. 31
  573. );
  574. assert.equal(
  575. Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg', 'base64').length,
  576. 31
  577. );
  578. // This string encodes single '.' character in UTF-16
  579. var dot = Buffer.from('//4uAA==', 'base64');
  580. assert.equal(dot[0], 0xff);
  581. assert.equal(dot[1], 0xfe);
  582. assert.equal(dot[2], 0x2e);
  583. assert.equal(dot[3], 0x00);
  584. assert.equal(dot.toString('base64'), '//4uAA==');
  585. {
  586. // Writing base64 at a position > 0 should not mangle the result.
  587. //
  588. // https://github.com/joyent/node/issues/402
  589. var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
  590. var b = Buffer.allocUnsafe(64);
  591. var pos = 0;
  592. for (var i = 0; i < segments.length; ++i) {
  593. pos += b.write(segments[i], pos, 'base64');
  594. }
  595. assert.equal(b.toString('latin1', 0, pos), 'Madness?! This is node.js!');
  596. }
  597. // Regression test for https://github.com/nodejs/node/issues/3496.
  598. // assert.equal(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
  599. {
  600. // Creating buffers larger than pool size.
  601. var l = Buffer.poolSize + 5;
  602. var s = '';
  603. for (var i = 0; i < l; i++) {
  604. s += 'h';
  605. }
  606. var b = Buffer.from(s);
  607. for (var i = 0; i < l; i++) {
  608. assert.equal('h'.charCodeAt(0), b[i]);
  609. }
  610. var sb = b.toString();
  611. assert.equal(sb.length, s.length);
  612. assert.equal(sb, s);
  613. }
  614. {
  615. // Single argument slice
  616. var b = Buffer.from('abcde');
  617. assert.equal('bcde', b.slice(1).toString());
  618. }
  619. // slice(0,0).length === 0
  620. assert.equal(0, Buffer.from('hello').slice(0, 0).length);
  621. // test hex toString
  622. // console.log('Create hex string from buffer');
  623. var hexb = Buffer.allocUnsafe(256);
  624. for (var i = 0; i < 256; i++) {
  625. hexb[i] = i;
  626. }
  627. var hexStr = hexb.toString('hex');
  628. assert.equal(hexStr,
  629. '000102030405060708090a0b0c0d0e0f' +
  630. '101112131415161718191a1b1c1d1e1f' +
  631. '202122232425262728292a2b2c2d2e2f' +
  632. '303132333435363738393a3b3c3d3e3f' +
  633. '404142434445464748494a4b4c4d4e4f' +
  634. '505152535455565758595a5b5c5d5e5f' +
  635. '606162636465666768696a6b6c6d6e6f' +
  636. '707172737475767778797a7b7c7d7e7f' +
  637. '808182838485868788898a8b8c8d8e8f' +
  638. '909192939495969798999a9b9c9d9e9f' +
  639. 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' +
  640. 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' +
  641. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' +
  642. 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' +
  643. 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' +
  644. 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff');
  645. // console.log('Create buffer from hex string');
  646. var hexb2 = Buffer.from(hexStr, 'hex');
  647. for (var i = 0; i < 256; i++) {
  648. assert.equal(hexb2[i], hexb[i]);
  649. }
  650. {
  651. // test an invalid slice end.
  652. // console.log('Try to slice off the end of the buffer');
  653. var b = Buffer.from([1, 2, 3, 4, 5]);
  654. var b2 = b.toString('hex', 1, 10000);
  655. var b3 = b.toString('hex', 1, 5);
  656. var b4 = b.toString('hex', 1);
  657. assert.equal(b2, b3);
  658. assert.equal(b2, b4);
  659. }
  660. function buildBuffer(data) {
  661. if (Array.isArray(data)) {
  662. var buffer = Buffer.allocUnsafe(data.length);
  663. data.forEach(function(v, k) {
  664. buffer[k] = v;
  665. });
  666. return buffer;
  667. }
  668. return null;
  669. }
  670. var x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]);
  671. // console.log(x.inspect());
  672. assert.equal('<Buffer 81 a3 66 6f 6f a3 62 61 72>', x.inspect());
  673. {
  674. var z = x.slice(4);
  675. // console.log(z.inspect());
  676. // console.log(z.length);
  677. assert.equal(5, z.length);
  678. assert.equal(0x6f, z[0]);
  679. assert.equal(0xa3, z[1]);
  680. assert.equal(0x62, z[2]);
  681. assert.equal(0x61, z[3]);
  682. assert.equal(0x72, z[4]);
  683. }
  684. {
  685. var z = x.slice(0);
  686. // console.log(z.inspect());
  687. // console.log(z.length);
  688. assert.equal(z.length, x.length);
  689. }
  690. {
  691. var z = x.slice(0, 4);
  692. // console.log(z.inspect());
  693. // console.log(z.length);
  694. assert.equal(4, z.length);
  695. assert.equal(0x81, z[0]);
  696. assert.equal(0xa3, z[1]);
  697. }
  698. {
  699. var z = x.slice(0, 9);
  700. // console.log(z.inspect());
  701. // console.log(z.length);
  702. assert.equal(9, z.length);
  703. }
  704. {
  705. var z = x.slice(1, 4);
  706. // console.log(z.inspect());
  707. // console.log(z.length);
  708. assert.equal(3, z.length);
  709. assert.equal(0xa3, z[0]);
  710. }
  711. {
  712. var z = x.slice(2, 4);
  713. // console.log(z.inspect());
  714. // console.log(z.length);
  715. assert.equal(2, z.length);
  716. assert.equal(0x66, z[0]);
  717. assert.equal(0x6f, z[1]);
  718. }
  719. assert.equal(0, Buffer.from('hello').slice(0, 0).length);
  720. ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  721. var b = Buffer.allocUnsafe(10);
  722. b.write('あいうえお', encoding);
  723. assert.equal(b.toString(encoding), 'あいうえお');
  724. });
  725. {
  726. // Binary encoding should write only one byte per character.
  727. var b = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
  728. var s = String.fromCharCode(0xffff);
  729. b.write(s, 0, 'latin1');
  730. assert.equal(0xff, b[0]);
  731. assert.equal(0xad, b[1]);
  732. assert.equal(0xbe, b[2]);
  733. assert.equal(0xef, b[3]);
  734. s = String.fromCharCode(0xaaee);
  735. b.write(s, 0, 'latin1');
  736. assert.equal(0xee, b[0]);
  737. assert.equal(0xad, b[1]);
  738. assert.equal(0xbe, b[2]);
  739. assert.equal(0xef, b[3]);
  740. }
  741. {
  742. // #1210 Test UTF-8 string includes null character
  743. var buf = Buffer.from('\0');
  744. assert.equal(buf.length, 1);
  745. buf = Buffer.from('\0\0');
  746. assert.equal(buf.length, 2);
  747. }
  748. {
  749. var buf = Buffer.allocUnsafe(2);
  750. var written = buf.write(''); // 0byte
  751. assert.equal(written, 0);
  752. written = buf.write('\0'); // 1byte (v8 adds null terminator)
  753. assert.equal(written, 1);
  754. written = buf.write('a\0'); // 1byte * 2
  755. assert.equal(written, 2);
  756. written = buf.write('あ'); // 3bytes
  757. assert.equal(written, 0);
  758. written = buf.write('\0あ'); // 1byte + 3bytes
  759. assert.equal(written, 1);
  760. written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
  761. assert.equal(written, 2);
  762. }
  763. {
  764. var buf = Buffer.allocUnsafe(10);
  765. written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
  766. assert.equal(written, 9);
  767. written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
  768. assert.equal(written, 10);
  769. }
  770. {
  771. // #243 Test write() with maxLength
  772. var buf = Buffer.allocUnsafe(4);
  773. buf.fill(0xFF);
  774. var written = buf.write('abcd', 1, 2, 'utf8');
  775. // console.log(buf);
  776. assert.equal(written, 2);
  777. assert.equal(buf[0], 0xFF);
  778. assert.equal(buf[1], 0x61);
  779. assert.equal(buf[2], 0x62);
  780. assert.equal(buf[3], 0xFF);
  781. buf.fill(0xFF);
  782. written = buf.write('abcd', 1, 4);
  783. // console.log(buf);
  784. assert.equal(written, 3);
  785. assert.equal(buf[0], 0xFF);
  786. assert.equal(buf[1], 0x61);
  787. assert.equal(buf[2], 0x62);
  788. assert.equal(buf[3], 0x63);
  789. buf.fill(0xFF);
  790. written = buf.write('abcd', 1, 2, 'utf8');
  791. // console.log(buf);
  792. assert.equal(written, 2);
  793. assert.equal(buf[0], 0xFF);
  794. assert.equal(buf[1], 0x61);
  795. assert.equal(buf[2], 0x62);
  796. assert.equal(buf[3], 0xFF);
  797. buf.fill(0xFF);
  798. written = buf.write('abcdef', 1, 2, 'hex');
  799. // console.log(buf);
  800. assert.equal(written, 2);
  801. assert.equal(buf[0], 0xFF);
  802. assert.equal(buf[1], 0xAB);
  803. assert.equal(buf[2], 0xCD);
  804. assert.equal(buf[3], 0xFF);
  805. ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
  806. buf.fill(0xFF);
  807. written = buf.write('abcd', 0, 2, encoding);
  808. // console.log(buf);
  809. assert.equal(written, 2);
  810. assert.equal(buf[0], 0x61);
  811. assert.equal(buf[1], 0x00);
  812. assert.equal(buf[2], 0xFF);
  813. assert.equal(buf[3], 0xFF);
  814. });
  815. }
  816. {
  817. // test offset returns are correct
  818. var b = Buffer.allocUnsafe(16);
  819. assert.equal(4, b.writeUInt32LE(0, 0));
  820. assert.equal(6, b.writeUInt16LE(0, 4));
  821. assert.equal(7, b.writeUInt8(0, 6));
  822. assert.equal(8, b.writeInt8(0, 7));
  823. assert.equal(16, b.writeDoubleLE(0, 8));
  824. }
  825. {
  826. // test unmatched surrogates not producing invalid utf8 output
  827. // ef bf bd = utf-8 representation of unicode replacement character
  828. // see https://codereview.chromium.org/121173009/
  829. var buf = Buffer.from('ab\ud800cd', 'utf8');
  830. assert.equal(buf[0], 0x61);
  831. assert.equal(buf[1], 0x62);
  832. assert.equal(buf[2], 0xef);
  833. assert.equal(buf[3], 0xbf);
  834. assert.equal(buf[4], 0xbd);
  835. assert.equal(buf[5], 0x63);
  836. assert.equal(buf[6], 0x64);
  837. }
  838. {
  839. // test for buffer overrun
  840. var buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5
  841. var sub = buf.slice(0, 4); // length: 4
  842. written = sub.write('12345', 'latin1');
  843. assert.equal(written, 4);
  844. assert.equal(buf[4], 0);
  845. }
  846. // Check for fractional length args, junk length args, etc.
  847. // https://github.com/joyent/node/issues/1758
  848. // Call .fill() first, stops valgrind warning about uninitialized memory reads.
  849. Buffer.allocUnsafe(3.3).fill().toString();
  850. // throws bad argument error in commit 43cb4ec
  851. Buffer.alloc(3.3).fill().toString();
  852. assert.equal(Buffer.allocUnsafe(NaN).length, 0);
  853. assert.equal(Buffer.allocUnsafe(3.3).length, 3);
  854. assert.equal(Buffer.from({length: 3.3}).length, 3);
  855. assert.equal(Buffer.from({length: 'BAM'}).length, 0);
  856. // Make sure that strings are not coerced to numbers.
  857. assert.equal(Buffer.from('99').length, 2);
  858. assert.equal(Buffer.from('13.37').length, 5);
  859. // Ensure that the length argument is respected.
  860. 'ascii utf8 hex base64 latin1'.split(' ').forEach(function(enc) {
  861. assert.equal(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1);
  862. });
  863. {
  864. // Regression test, guard against buffer overrun in the base64 decoder.
  865. var a = Buffer.allocUnsafe(3);
  866. var b = Buffer.from('xxx');
  867. a.write('aaaaaaaa', 'base64');
  868. assert.equal(b.toString(), 'xxx');
  869. }
  870. // issue GH-3416
  871. Buffer.from(Buffer.allocUnsafe(0), 0, 0);
  872. [ 'hex',
  873. 'utf8',
  874. 'utf-8',
  875. 'ascii',
  876. 'latin1',
  877. 'binary',
  878. 'base64',
  879. 'ucs2',
  880. 'ucs-2',
  881. 'utf16le',
  882. 'utf-16le' ].forEach(function(enc) {
  883. assert.equal(Buffer.isEncoding(enc), true);
  884. });
  885. [ 'utf9',
  886. 'utf-7',
  887. 'Unicode-FTW',
  888. 'new gnu gun',
  889. false,
  890. NaN,
  891. {},
  892. Infinity,
  893. [],
  894. 1,
  895. 0,
  896. -1 ].forEach(function(enc) {
  897. assert.equal(Buffer.isEncoding(enc), false);
  898. });
  899. // GH-5110
  900. {
  901. var buffer = Buffer.from('test');
  902. var string = JSON.stringify(buffer);
  903. assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
  904. assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
  905. return value && value.type === 'Buffer'
  906. ? Buffer.from(value.data)
  907. : value;
  908. }));
  909. }
  910. // issue GH-7849
  911. {
  912. var buf = Buffer.from('test');
  913. var json = JSON.stringify(buf);
  914. var obj = JSON.parse(json);
  915. var copy = Buffer.from(obj);
  916. assert(buf.equals(copy));
  917. }
  918. // issue GH-4331
  919. assert.throws(function() {
  920. Buffer.allocUnsafe(0xFFFFFFFF);
  921. }, RangeError);
  922. assert.throws(function() {
  923. Buffer.allocUnsafe(0xFFFFFFFFF);
  924. }, RangeError);
  925. // attempt to overflow buffers, similar to previous bug in array buffers
  926. assert.throws(function() {
  927. var buf = Buffer.allocUnsafe(8);
  928. buf.readFloatLE(0xffffffff);
  929. }, RangeError);
  930. assert.throws(function() {
  931. var buf = Buffer.allocUnsafe(8);
  932. buf.writeFloatLE(0.0, 0xffffffff);
  933. }, RangeError);
  934. assert.throws(function() {
  935. var buf = Buffer.allocUnsafe(8);
  936. buf.readFloatLE(0xffffffff);
  937. }, RangeError);
  938. assert.throws(function() {
  939. var buf = Buffer.allocUnsafe(8);
  940. buf.writeFloatLE(0.0, 0xffffffff);
  941. }, RangeError);
  942. // ensure negative values can't get past offset
  943. assert.throws(function() {
  944. var buf = Buffer.allocUnsafe(8);
  945. buf.readFloatLE(-1);
  946. }, RangeError);
  947. assert.throws(function() {
  948. var buf = Buffer.allocUnsafe(8);
  949. buf.writeFloatLE(0.0, -1);
  950. }, RangeError);
  951. assert.throws(function() {
  952. var buf = Buffer.allocUnsafe(8);
  953. buf.readFloatLE(-1);
  954. }, RangeError);
  955. assert.throws(function() {
  956. var buf = Buffer.allocUnsafe(8);
  957. buf.writeFloatLE(0.0, -1);
  958. }, RangeError);
  959. // offset checks
  960. {
  961. var buf = Buffer.allocUnsafe(0);
  962. assert.throws(function() { buf.readUInt8(0); }, RangeError);
  963. assert.throws(function() { buf.readInt8(0); }, RangeError);
  964. }
  965. {
  966. var buf = Buffer.from([0xFF]);
  967. assert.equal(buf.readUInt8(0), 255);
  968. assert.equal(buf.readInt8(0), -1);
  969. }
  970. [16, 32].forEach(function(bits) {
  971. var buf = Buffer.allocUnsafe(bits / 8 - 1);
  972. assert.throws(function() { buf['readUInt' + bits + 'BE'](0); },
  973. RangeError,
  974. 'readUInt' + bits + 'BE');
  975. assert.throws(function() { buf['readUInt' + bits + 'LE'](0); },
  976. RangeError,
  977. 'readUInt' + bits + 'LE');
  978. assert.throws(function() { buf['readInt' + bits + 'BE'](0); },
  979. RangeError,
  980. 'readInt' + bits + 'BE()');
  981. assert.throws(function() { buf['readInt' + bits + 'LE'](0); },
  982. RangeError,
  983. 'readInt' + bits + 'LE()');
  984. });
  985. [16, 32].forEach(function(bits) {
  986. var buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
  987. assert.equal(buf['readUInt' + bits + 'BE'](0),
  988. (0xFFFFFFFF >>> (32 - bits)));
  989. assert.equal(buf['readUInt' + bits + 'LE'](0),
  990. (0xFFFFFFFF >>> (32 - bits)));
  991. assert.equal(buf['readInt' + bits + 'BE'](0),
  992. (0xFFFFFFFF >> (32 - bits)));
  993. assert.equal(buf['readInt' + bits + 'LE'](0),
  994. (0xFFFFFFFF >> (32 - bits)));
  995. });
  996. // test for common read(U)IntLE/BE
  997. {
  998. var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
  999. assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
  1000. assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
  1001. assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
  1002. assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
  1003. assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
  1004. assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
  1005. assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
  1006. assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
  1007. assert.strictEqual(buf.readIntLE(0, 1), 0x01);
  1008. assert.strictEqual(buf.readIntBE(0, 1), 0x01);
  1009. assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
  1010. assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
  1011. assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
  1012. assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
  1013. assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
  1014. assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
  1015. }
  1016. // test for common write(U)IntLE/BE
  1017. {
  1018. var buf = Buffer.allocUnsafe(3);
  1019. buf.writeUIntLE(0x123456, 0, 3);
  1020. assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
  1021. assert.equal(buf.readUIntLE(0, 3), 0x123456);
  1022. buf = Buffer.allocUnsafe(3);
  1023. buf.writeUIntBE(0x123456, 0, 3);
  1024. assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
  1025. assert.equal(buf.readUIntBE(0, 3), 0x123456);
  1026. buf = Buffer.allocUnsafe(3);
  1027. buf.writeIntLE(0x123456, 0, 3);
  1028. assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
  1029. assert.equal(buf.readIntLE(0, 3), 0x123456);
  1030. buf = Buffer.allocUnsafe(3);
  1031. buf.writeIntBE(0x123456, 0, 3);
  1032. assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
  1033. assert.equal(buf.readIntBE(0, 3), 0x123456);
  1034. buf = Buffer.allocUnsafe(3);
  1035. buf.writeIntLE(-0x123456, 0, 3);
  1036. assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
  1037. assert.equal(buf.readIntLE(0, 3), -0x123456);
  1038. buf = Buffer.allocUnsafe(3);
  1039. buf.writeIntBE(-0x123456, 0, 3);
  1040. assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
  1041. assert.equal(buf.readIntBE(0, 3), -0x123456);
  1042. buf = Buffer.allocUnsafe(3);
  1043. buf.writeIntLE(-0x123400, 0, 3);
  1044. assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
  1045. assert.equal(buf.readIntLE(0, 3), -0x123400);
  1046. buf = Buffer.allocUnsafe(3);
  1047. buf.writeIntBE(-0x123400, 0, 3);
  1048. assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
  1049. assert.equal(buf.readIntBE(0, 3), -0x123400);
  1050. buf = Buffer.allocUnsafe(3);
  1051. buf.writeIntLE(-0x120000, 0, 3);
  1052. assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
  1053. assert.equal(buf.readIntLE(0, 3), -0x120000);
  1054. buf = Buffer.allocUnsafe(3);
  1055. buf.writeIntBE(-0x120000, 0, 3);
  1056. assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
  1057. assert.equal(buf.readIntBE(0, 3), -0x120000);
  1058. buf = Buffer.allocUnsafe(5);
  1059. buf.writeUIntLE(0x1234567890, 0, 5);
  1060. assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
  1061. assert.equal(buf.readUIntLE(0, 5), 0x1234567890);
  1062. buf = Buffer.allocUnsafe(5);
  1063. buf.writeUIntBE(0x1234567890, 0, 5);
  1064. assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
  1065. assert.equal(buf.readUIntBE(0, 5), 0x1234567890);
  1066. buf = Buffer.allocUnsafe(5);
  1067. buf.writeIntLE(0x1234567890, 0, 5);
  1068. assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
  1069. assert.equal(buf.readIntLE(0, 5), 0x1234567890);
  1070. buf = Buffer.allocUnsafe(5);
  1071. buf.writeIntBE(0x1234567890, 0, 5);
  1072. assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
  1073. assert.equal(buf.readIntBE(0, 5), 0x1234567890);
  1074. buf = Buffer.allocUnsafe(5);
  1075. buf.writeIntLE(-0x1234567890, 0, 5);
  1076. assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
  1077. assert.equal(buf.readIntLE(0, 5), -0x1234567890);
  1078. buf = Buffer.allocUnsafe(5);
  1079. buf.writeIntBE(-0x1234567890, 0, 5);
  1080. assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
  1081. assert.equal(buf.readIntBE(0, 5), -0x1234567890);
  1082. buf = Buffer.allocUnsafe(5);
  1083. buf.writeIntLE(-0x0012000000, 0, 5);
  1084. assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
  1085. assert.equal(buf.readIntLE(0, 5), -0x0012000000);
  1086. buf = Buffer.allocUnsafe(5);
  1087. buf.writeIntBE(-0x0012000000, 0, 5);
  1088. assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
  1089. assert.equal(buf.readIntBE(0, 5), -0x0012000000);
  1090. }
  1091. // test Buffer slice
  1092. {
  1093. var buf = Buffer.from('0123456789');
  1094. assert.equal(buf.slice(-10, 10), '0123456789');
  1095. assert.equal(buf.slice(-20, 10), '0123456789');
  1096. assert.equal(buf.slice(-20, -10), '');
  1097. assert.equal(buf.slice(), '0123456789');
  1098. assert.equal(buf.slice(0), '0123456789');
  1099. assert.equal(buf.slice(0, 0), '');
  1100. assert.equal(buf.slice(undefined), '0123456789');
  1101. assert.equal(buf.slice('foobar'), '0123456789');
  1102. assert.equal(buf.slice(undefined, undefined), '0123456789');
  1103. assert.equal(buf.slice(2), '23456789');
  1104. assert.equal(buf.slice(5), '56789');
  1105. assert.equal(buf.slice(10), '');
  1106. assert.equal(buf.slice(5, 8), '567');
  1107. assert.equal(buf.slice(8, -1), '8');
  1108. assert.equal(buf.slice(-10), '0123456789');
  1109. assert.equal(buf.slice(0, -9), '0');
  1110. assert.equal(buf.slice(0, -10), '');
  1111. assert.equal(buf.slice(0, -1), '012345678');
  1112. assert.equal(buf.slice(2, -2), '234567');
  1113. assert.equal(buf.slice(0, 65536), '0123456789');
  1114. assert.equal(buf.slice(65536, 0), '');
  1115. assert.equal(buf.slice(-5, -8), '');
  1116. assert.equal(buf.slice(-5, -3), '56');
  1117. assert.equal(buf.slice(-10, 10), '0123456789');
  1118. for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
  1119. assert.equal(buf.slice(i), s.slice(i));
  1120. assert.equal(buf.slice(0, i), s.slice(0, i));
  1121. assert.equal(buf.slice(-i), s.slice(-i));
  1122. assert.equal(buf.slice(0, -i), s.slice(0, -i));
  1123. }
  1124. var utf16Buf = Buffer.from('0123456789', 'utf16le');
  1125. // assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
  1126. assert.equal(buf.slice('0', '1'), '0');
  1127. assert.equal(buf.slice('-5', '10'), '56789');
  1128. assert.equal(buf.slice('-10', '10'), '0123456789');
  1129. assert.equal(buf.slice('-10', '-5'), '01234');
  1130. assert.equal(buf.slice('-10', '-0'), '');
  1131. assert.equal(buf.slice('111'), '');
  1132. assert.equal(buf.slice('0', '-111'), '');
  1133. // try to slice a zero length Buffer
  1134. // see https://github.com/joyent/node/issues/5881
  1135. Buffer.alloc(0).slice(0, 1);
  1136. }
  1137. // Regression test for #5482: should throw but not assert in C++ land.
  1138. assert.throws(function() {
  1139. Buffer.from('', 'buffer');
  1140. }, TypeError);
  1141. // Regression test for #6111. Constructing a buffer from another buffer
  1142. // should a) work, and b) not corrupt the source buffer.
  1143. {
  1144. var a = [0];
  1145. for (var i = 0; i < 7; ++i) a = a.concat(a);
  1146. a = a.map(function(_, i) { return i; });
  1147. var b = Buffer.from(a);
  1148. var c = Buffer.from(b);
  1149. assert.strictEqual(b.length, a.length);
  1150. assert.strictEqual(c.length, a.length);
  1151. for (var i = 0, k = a.length; i < k; ++i) {
  1152. assert.strictEqual(a[i], i);
  1153. assert.strictEqual(b[i], i);
  1154. assert.strictEqual(c[i], i);
  1155. }
  1156. }
  1157. assert.throws(function() {
  1158. Buffer.allocUnsafe((-1 >>> 0) + 1);
  1159. }, RangeError);
  1160. assert.throws(function() {
  1161. Buffer.allocUnsafeSlow((-1 >>> 0) + 1);
  1162. }, RangeError);
  1163. if (common.hasCrypto) {
  1164. // Test truncation after decode
  1165. var crypto = require('crypto');
  1166. var b1 = Buffer.from('YW55=======', 'base64');
  1167. var b2 = Buffer.from('YW55', 'base64');
  1168. assert.equal(
  1169. crypto.createHash('sha1').update(b1).digest('hex'),
  1170. crypto.createHash('sha1').update(b2).digest('hex')
  1171. );
  1172. } else {
  1173. common.skip('missing crypto');
  1174. }
  1175. // Test Compare
  1176. {
  1177. var b = Buffer.alloc(1, 'a');
  1178. var c = Buffer.alloc(1, 'c');
  1179. var d = Buffer.alloc(2, 'aa');
  1180. assert.equal(b.compare(c), -1);
  1181. assert.equal(c.compare(d), 1);
  1182. assert.equal(d.compare(b), 1);
  1183. assert.equal(b.compare(d), -1);
  1184. assert.equal(b.compare(b), 0);
  1185. assert.equal(Buffer.compare(b, c), -1);
  1186. assert.equal(Buffer.compare(c, d), 1);
  1187. assert.equal(Buffer.compare(d, b), 1);
  1188. assert.equal(Buffer.compare(b, d), -1);
  1189. assert.equal(Buffer.compare(c, c), 0);
  1190. assert.equal(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
  1191. assert.equal(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
  1192. assert.equal(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);
  1193. }
  1194. assert.throws(function() {
  1195. var b = Buffer.allocUnsafe(1);
  1196. Buffer.compare(b, 'abc');
  1197. });
  1198. assert.throws(function() {
  1199. var b = Buffer.allocUnsafe(1);
  1200. Buffer.compare('abc', b);
  1201. });
  1202. assert.throws(function() {
  1203. var b = Buffer.allocUnsafe(1);
  1204. b.compare('abc');
  1205. });
  1206. // Test Equals
  1207. {
  1208. var b = Buffer.alloc(5, 'abcdf');
  1209. var c = Buffer.alloc(5, 'abcdf');
  1210. var d = Buffer.alloc(5, 'abcde');
  1211. var e = Buffer.alloc(6, 'abcdef');
  1212. assert.ok(b.equals(c));
  1213. assert.ok(!c.equals(d));
  1214. assert.ok(!d.equals(e));
  1215. assert.ok(d.equals(d));
  1216. }
  1217. assert.throws(function() {
  1218. var b = Buffer.allocUnsafe(1);
  1219. b.equals('abc');
  1220. });
  1221. // Regression test for https://github.com/nodejs/node/issues/649.
  1222. assert.throws(() => { Buffer.allocUnsafe(1422561062959).toString('utf8');});
  1223. var ps = Buffer.poolSize;
  1224. Buffer.poolSize = 0;
  1225. assert.equal(Buffer.allocUnsafe(1).parent, undefined);
  1226. Buffer.poolSize = ps;
  1227. // Test Buffer.copy() segfault
  1228. assert.throws(function() {
  1229. Buffer.allocUnsafe(10).copy();
  1230. });
  1231. var regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
  1232. 'ArrayBuffer, Array, or array-like object.');
  1233. assert.throws(function() {
  1234. Buffer.from();
  1235. }, regErrorMsg);
  1236. assert.throws(function() {
  1237. Buffer.from(null);
  1238. }, regErrorMsg);
  1239. // Test that ParseArrayIndex handles full uint32
  1240. assert.throws(function() {
  1241. Buffer.from(new ArrayBuffer(0), -1 >>> 0);
  1242. }, /RangeError: 'offset' is out of bounds/);
  1243. // ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
  1244. assert.throws(() => {
  1245. var a = Buffer(1).fill(0);
  1246. var b = Buffer(1).fill(0);
  1247. a.copy(b, 0, 0x100000000, 0x100000001);
  1248. }), /out of range index/;
  1249. // Unpooled buffer (replaces SlowBuffer)
  1250. var ubuf = Buffer.allocUnsafeSlow(10);
  1251. assert(ubuf);
  1252. assert(ubuf.buffer);
  1253. assert.equal(ubuf.buffer.byteLength, 10);
  1254. // Regression test
  1255. assert.doesNotThrow(() => {
  1256. Buffer.from(new ArrayBuffer());
  1257. });
  1258. assert.throws(() => Buffer.alloc(-Buffer.poolSize),
  1259. '"size" argument must not be negative');
  1260. assert.throws(() => Buffer.alloc(-100),
  1261. '"size" argument must not be negative');
  1262. assert.throws(() => Buffer.allocUnsafe(-Buffer.poolSize),
  1263. '"size" argument must not be negative');
  1264. assert.throws(() => Buffer.allocUnsafe(-100),
  1265. '"size" argument must not be negative');
  1266. assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize),
  1267. '"size" argument must not be negative');
  1268. assert.throws(() => Buffer.allocUnsafeSlow(-100),
  1269. '"size" argument must not be negative');
  1270. assert.throws(() => Buffer.alloc({ valueOf: () => 1 }),
  1271. /"size" argument must be a number/);
  1272. assert.throws(() => Buffer.alloc({ valueOf: () => -1 }),
  1273. /"size" argument must be a number/);