• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4
5// Test hex strings and bad hex strings
6{
7  const buf = Buffer.alloc(4);
8  assert.strictEqual(buf.length, 4);
9  assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
10  assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2);
11  assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0x00, 0x00]));
12  assert.strictEqual(buf.toString('hex'), 'abcd0000');
13  assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4);
14  assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0xef, 0x01]));
15  assert.strictEqual(buf.toString('hex'), 'abcdef01');
16
17  const copy = Buffer.from(buf.toString('hex'), 'hex');
18  assert.strictEqual(buf.toString('hex'), copy.toString('hex'));
19}
20
21{
22  const buf = Buffer.alloc(5);
23  assert.strictEqual(buf.write('abcdxx', 1, 'hex'), 2);
24  assert.strictEqual(buf.toString('hex'), '00abcd0000');
25}
26
27{
28  const buf = Buffer.alloc(4);
29  assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
30  assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0);
31  assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
32  assert.strictEqual(buf.write('xxab', 1, 'hex'), 0);
33  assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
34  assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1);
35  assert.deepStrictEqual(buf, Buffer.from([0xcd, 0, 0, 0]));
36}
37
38{
39  const buf = Buffer.alloc(256);
40  for (let i = 0; i < 256; i++)
41    buf[i] = i;
42
43  const hex = buf.toString('hex');
44  assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf);
45
46  const badHex = `${hex.slice(0, 256)}xx${hex.slice(256, 510)}`;
47  assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf.slice(0, 128));
48}
49