11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// Test hex strings and bad hex strings 61cb0ef41Sopenharmony_ci{ 71cb0ef41Sopenharmony_ci const buf = Buffer.alloc(4); 81cb0ef41Sopenharmony_ci assert.strictEqual(buf.length, 4); 91cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); 101cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2); 111cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0x00, 0x00])); 121cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString('hex'), 'abcd0000'); 131cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4); 141cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0xef, 0x01])); 151cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString('hex'), 'abcdef01'); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci const copy = Buffer.from(buf.toString('hex'), 'hex'); 181cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString('hex'), copy.toString('hex')); 191cb0ef41Sopenharmony_ci} 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci{ 221cb0ef41Sopenharmony_ci const buf = Buffer.alloc(5); 231cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('abcdxx', 1, 'hex'), 2); 241cb0ef41Sopenharmony_ci assert.strictEqual(buf.toString('hex'), '00abcd0000'); 251cb0ef41Sopenharmony_ci} 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci{ 281cb0ef41Sopenharmony_ci const buf = Buffer.alloc(4); 291cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); 301cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0); 311cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); 321cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('xxab', 1, 'hex'), 0); 331cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0])); 341cb0ef41Sopenharmony_ci assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1); 351cb0ef41Sopenharmony_ci assert.deepStrictEqual(buf, Buffer.from([0xcd, 0, 0, 0])); 361cb0ef41Sopenharmony_ci} 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci{ 391cb0ef41Sopenharmony_ci const buf = Buffer.alloc(256); 401cb0ef41Sopenharmony_ci for (let i = 0; i < 256; i++) 411cb0ef41Sopenharmony_ci buf[i] = i; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci const hex = buf.toString('hex'); 441cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci const badHex = `${hex.slice(0, 256)}xx${hex.slice(256, 510)}`; 471cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf.slice(0, 128)); 481cb0ef41Sopenharmony_ci} 49