test.js 2.7 KB

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