buffer: use stricter from() input validation · nodejs/node@75eaf25 (original) (raw)

1

1

`'use strict';

`

2

2

``

3

``

`-

const common = require('../common');

`

``

3

`+

require('../common');

`

4

4

`const { deepStrictEqual, throws } = require('assert');

`

5

5

`const { runInNewContext } = require('vm');

`

6

6

``

`@@ -40,27 +40,25 @@ deepStrictEqual(

`

40

40

`[{ valueOf() { return null; } }, 'object'],

`

41

41

`[{ valueOf() { return undefined; } }, 'object'],

`

42

42

`[{ valueOf: null }, 'object'],

`

43

``

`-

[Object.create(null), 'object']

`

``

43

`+

[Object.create(null), 'object'],

`

``

44

`+

[new Number(true), 'number'],

`

``

45

`+

[new MyBadPrimitive(), 'number'],

`

``

46

`+

[Symbol(), 'symbol'],

`

``

47

`+

[5n, 'bigint'],

`

``

48

`+

[(one, two, three) => {}, 'function'],

`

``

49

`+

[undefined, 'undefined'],

`

``

50

`+

[null, 'object']

`

44

51

`].forEach(([input, actualType]) => {

`

45

``

`-

const err = common.expectsError({

`

``

52

`+

const errObj = {

`

46

53

`code: 'ERR_INVALID_ARG_TYPE',

`

47

``

`-

type: TypeError,

`

``

54

`+

name: 'TypeError',

`

48

55

`message: 'The first argument must be one of type string, Buffer, ' +

`

49

56

`'ArrayBuffer, Array, or Array-like Object. Received ' +

`

50

57

`` type ${actualType}

``

51

``

`-

});

`

52

``

`-

throws(() => Buffer.from(input), err);

`

``

58

`+

};

`

``

59

`+

throws(() => Buffer.from(input), errObj);

`

``

60

`+

throws(() => Buffer.from(input, 'hex'), errObj);

`

53

61

`});

`

54

62

``

55

``

`-

[

`

56

``

`-

new Number(true),

`

57

``

`-

new MyBadPrimitive()

`

58

``

`-

].forEach((input) => {

`

59

``

`-

const errMsg = common.expectsError({

`

60

``

`-

code: 'ERR_INVALID_ARG_TYPE',

`

61

``

`-

type: TypeError,

`

62

``

`-

message: 'The "value" argument must not be of type number. ' +

`

63

``

`-

'Received type number'

`

64

``

`-

});

`

65

``

`-

throws(() => Buffer.from(input), errMsg);

`

66

``

`-

});

`

``

63

`+

Buffer.allocUnsafe(10); // Should not throw.

`

``

64

`+

Buffer.from('deadbeaf', 'hex'); // Should not throw.

`