1
|
|
|
/* eslint-disable node/no-deprecated-api */ |
2
|
|
|
|
3
|
|
|
var test = require('tape') |
4
|
|
|
var SafeBuffer = require('./').Buffer |
5
|
|
|
|
6
|
|
|
test('new SafeBuffer(value) works just like Buffer', function (t) { |
7
|
|
|
t.deepEqual(new SafeBuffer('hey'), new Buffer('hey')) |
|
|
|
|
8
|
|
|
t.deepEqual(new SafeBuffer('hey', 'utf8'), new Buffer('hey', 'utf8')) |
9
|
|
|
t.deepEqual(new SafeBuffer('686579', 'hex'), new Buffer('686579', 'hex')) |
10
|
|
|
t.deepEqual(new SafeBuffer([1, 2, 3]), new Buffer([1, 2, 3])) |
11
|
|
|
t.deepEqual(new SafeBuffer(new Uint8Array([1, 2, 3])), new Buffer(new Uint8Array([1, 2, 3]))) |
12
|
|
|
|
13
|
|
|
t.equal(typeof SafeBuffer.isBuffer, 'function') |
14
|
|
|
t.equal(SafeBuffer.isBuffer(new SafeBuffer('hey')), true) |
15
|
|
|
t.equal(Buffer.isBuffer(new SafeBuffer('hey')), true) |
16
|
|
|
t.notOk(SafeBuffer.isBuffer({})) |
17
|
|
|
|
18
|
|
|
t.end() |
19
|
|
|
}) |
20
|
|
|
|
21
|
|
|
test('SafeBuffer.from(value) converts to a Buffer', function (t) { |
22
|
|
|
t.deepEqual(SafeBuffer.from('hey'), new Buffer('hey')) |
|
|
|
|
23
|
|
|
t.deepEqual(SafeBuffer.from('hey', 'utf8'), new Buffer('hey', 'utf8')) |
24
|
|
|
t.deepEqual(SafeBuffer.from('686579', 'hex'), new Buffer('686579', 'hex')) |
25
|
|
|
t.deepEqual(SafeBuffer.from([1, 2, 3]), new Buffer([1, 2, 3])) |
26
|
|
|
t.deepEqual(SafeBuffer.from(new Uint8Array([1, 2, 3])), new Buffer(new Uint8Array([1, 2, 3]))) |
27
|
|
|
|
28
|
|
|
t.end() |
29
|
|
|
}) |
30
|
|
|
|
31
|
|
|
test('SafeBuffer.alloc(number) returns zeroed-out memory', function (t) { |
32
|
|
|
for (var i = 0; i < 10; i++) { |
33
|
|
|
var expected1 = new Buffer(1000) |
|
|
|
|
34
|
|
|
expected1.fill(0) |
35
|
|
|
t.deepEqual(SafeBuffer.alloc(1000), expected1) |
36
|
|
|
|
37
|
|
|
var expected2 = new Buffer(1000 * 1000) |
38
|
|
|
expected2.fill(0) |
39
|
|
|
t.deepEqual(SafeBuffer.alloc(1000 * 1000), expected2) |
40
|
|
|
} |
41
|
|
|
t.end() |
42
|
|
|
}) |
43
|
|
|
|
44
|
|
|
test('SafeBuffer.allocUnsafe(number)', function (t) { |
45
|
|
|
var buf = SafeBuffer.allocUnsafe(100) // unitialized memory |
46
|
|
|
t.equal(buf.length, 100) |
47
|
|
|
t.equal(SafeBuffer.isBuffer(buf), true) |
48
|
|
|
t.equal(Buffer.isBuffer(buf), true) |
|
|
|
|
49
|
|
|
t.end() |
50
|
|
|
}) |
51
|
|
|
|
52
|
|
|
test('SafeBuffer.from() throws with number types', function (t) { |
53
|
|
|
t.plan(5) |
54
|
|
|
t.throws(function () { |
55
|
|
|
SafeBuffer.from(0) |
56
|
|
|
}) |
57
|
|
|
t.throws(function () { |
58
|
|
|
SafeBuffer.from(-1) |
59
|
|
|
}) |
60
|
|
|
t.throws(function () { |
61
|
|
|
SafeBuffer.from(NaN) |
62
|
|
|
}) |
63
|
|
|
t.throws(function () { |
64
|
|
|
SafeBuffer.from(Infinity) |
65
|
|
|
}) |
66
|
|
|
t.throws(function () { |
67
|
|
|
SafeBuffer.from(99) |
68
|
|
|
}) |
69
|
|
|
}) |
70
|
|
|
|
71
|
|
|
test('SafeBuffer.allocUnsafe() throws with non-number types', function (t) { |
72
|
|
|
t.plan(4) |
73
|
|
|
t.throws(function () { |
74
|
|
|
SafeBuffer.allocUnsafe('hey') |
75
|
|
|
}) |
76
|
|
|
t.throws(function () { |
77
|
|
|
SafeBuffer.allocUnsafe('hey', 'utf8') |
78
|
|
|
}) |
79
|
|
|
t.throws(function () { |
80
|
|
|
SafeBuffer.allocUnsafe([1, 2, 3]) |
81
|
|
|
}) |
82
|
|
|
t.throws(function () { |
83
|
|
|
SafeBuffer.allocUnsafe({}) |
84
|
|
|
}) |
85
|
|
|
}) |
86
|
|
|
|
87
|
|
|
test('SafeBuffer.alloc() throws with non-number types', function (t) { |
88
|
|
|
t.plan(4) |
89
|
|
|
t.throws(function () { |
90
|
|
|
SafeBuffer.alloc('hey') |
91
|
|
|
}) |
92
|
|
|
t.throws(function () { |
93
|
|
|
SafeBuffer.alloc('hey', 'utf8') |
94
|
|
|
}) |
95
|
|
|
t.throws(function () { |
96
|
|
|
SafeBuffer.alloc([1, 2, 3]) |
97
|
|
|
}) |
98
|
|
|
t.throws(function () { |
99
|
|
|
SafeBuffer.alloc({}) |
100
|
|
|
}) |
101
|
|
|
}) |
102
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.