test-buffer.js 46KB

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