test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* eslint-disable node/no-deprecated-api */
  2. var test = require('tape')
  3. var SafeBuffer = require('./').Buffer
  4. test('new SafeBuffer(value) works just like Buffer', function (t) {
  5. t.deepEqual(new SafeBuffer('hey'), new Buffer('hey'))
  6. t.deepEqual(new SafeBuffer('hey', 'utf8'), new Buffer('hey', 'utf8'))
  7. t.deepEqual(new SafeBuffer('686579', 'hex'), new Buffer('686579', 'hex'))
  8. t.deepEqual(new SafeBuffer([1, 2, 3]), new Buffer([1, 2, 3]))
  9. t.deepEqual(new SafeBuffer(new Uint8Array([1, 2, 3])), new Buffer(new Uint8Array([1, 2, 3])))
  10. t.equal(typeof SafeBuffer.isBuffer, 'function')
  11. t.equal(SafeBuffer.isBuffer(new SafeBuffer('hey')), true)
  12. t.equal(Buffer.isBuffer(new SafeBuffer('hey')), true)
  13. t.notOk(SafeBuffer.isBuffer({}))
  14. t.end()
  15. })
  16. test('SafeBuffer.from(value) converts to a Buffer', function (t) {
  17. t.deepEqual(SafeBuffer.from('hey'), new Buffer('hey'))
  18. t.deepEqual(SafeBuffer.from('hey', 'utf8'), new Buffer('hey', 'utf8'))
  19. t.deepEqual(SafeBuffer.from('686579', 'hex'), new Buffer('686579', 'hex'))
  20. t.deepEqual(SafeBuffer.from([1, 2, 3]), new Buffer([1, 2, 3]))
  21. t.deepEqual(SafeBuffer.from(new Uint8Array([1, 2, 3])), new Buffer(new Uint8Array([1, 2, 3])))
  22. t.end()
  23. })
  24. test('SafeBuffer.alloc(number) returns zeroed-out memory', function (t) {
  25. for (var i = 0; i < 10; i++) {
  26. var expected1 = new Buffer(1000)
  27. expected1.fill(0)
  28. t.deepEqual(SafeBuffer.alloc(1000), expected1)
  29. var expected2 = new Buffer(1000 * 1000)
  30. expected2.fill(0)
  31. t.deepEqual(SafeBuffer.alloc(1000 * 1000), expected2)
  32. }
  33. t.end()
  34. })
  35. test('SafeBuffer.allocUnsafe(number)', function (t) {
  36. var buf = SafeBuffer.allocUnsafe(100) // unitialized memory
  37. t.equal(buf.length, 100)
  38. t.equal(SafeBuffer.isBuffer(buf), true)
  39. t.equal(Buffer.isBuffer(buf), true)
  40. t.end()
  41. })
  42. test('SafeBuffer.from() throws with number types', function (t) {
  43. t.plan(5)
  44. t.throws(function () {
  45. SafeBuffer.from(0)
  46. })
  47. t.throws(function () {
  48. SafeBuffer.from(-1)
  49. })
  50. t.throws(function () {
  51. SafeBuffer.from(NaN)
  52. })
  53. t.throws(function () {
  54. SafeBuffer.from(Infinity)
  55. })
  56. t.throws(function () {
  57. SafeBuffer.from(99)
  58. })
  59. })
  60. test('SafeBuffer.allocUnsafe() throws with non-number types', function (t) {
  61. t.plan(4)
  62. t.throws(function () {
  63. SafeBuffer.allocUnsafe('hey')
  64. })
  65. t.throws(function () {
  66. SafeBuffer.allocUnsafe('hey', 'utf8')
  67. })
  68. t.throws(function () {
  69. SafeBuffer.allocUnsafe([1, 2, 3])
  70. })
  71. t.throws(function () {
  72. SafeBuffer.allocUnsafe({})
  73. })
  74. })
  75. test('SafeBuffer.alloc() throws with non-number types', function (t) {
  76. t.plan(4)
  77. t.throws(function () {
  78. SafeBuffer.alloc('hey')
  79. })
  80. t.throws(function () {
  81. SafeBuffer.alloc('hey', 'utf8')
  82. })
  83. t.throws(function () {
  84. SafeBuffer.alloc([1, 2, 3])
  85. })
  86. t.throws(function () {
  87. SafeBuffer.alloc({})
  88. })
  89. })