You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var test = require('tape')
  2. var randomBytes = require('./')
  3. var MAX_BYTES = 65536
  4. var MAX_UINT32 = 4294967295
  5. test('sync', function (t) {
  6. t.plan(9)
  7. t.equals(randomBytes(0).length, 0, 'len: ' + 0)
  8. t.equals(randomBytes(3).length, 3, 'len: ' + 3)
  9. t.equals(randomBytes(30).length, 30, 'len: ' + 30)
  10. t.equals(randomBytes(300).length, 300, 'len: ' + 300)
  11. t.equals(randomBytes(17 + MAX_BYTES).length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES)
  12. t.equals(randomBytes(MAX_BYTES * 100).length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100)
  13. t.throws(function () {
  14. randomBytes(MAX_UINT32 + 1)
  15. })
  16. t.throws(function () {
  17. t.equals(randomBytes(-1))
  18. })
  19. t.throws(function () {
  20. t.equals(randomBytes('hello'))
  21. })
  22. })
  23. test('async', function (t) {
  24. t.plan(9)
  25. randomBytes(0, function (err, resp) {
  26. if (err) throw err
  27. t.equals(resp.length, 0, 'len: ' + 0)
  28. })
  29. randomBytes(3, function (err, resp) {
  30. if (err) throw err
  31. t.equals(resp.length, 3, 'len: ' + 3)
  32. })
  33. randomBytes(30, function (err, resp) {
  34. if (err) throw err
  35. t.equals(resp.length, 30, 'len: ' + 30)
  36. })
  37. randomBytes(300, function (err, resp) {
  38. if (err) throw err
  39. t.equals(resp.length, 300, 'len: ' + 300)
  40. })
  41. randomBytes(17 + MAX_BYTES, function (err, resp) {
  42. if (err) throw err
  43. t.equals(resp.length, 17 + MAX_BYTES, 'len: ' + 17 + MAX_BYTES)
  44. })
  45. randomBytes(MAX_BYTES * 100, function (err, resp) {
  46. if (err) throw err
  47. t.equals(resp.length, MAX_BYTES * 100, 'len: ' + MAX_BYTES * 100)
  48. })
  49. t.throws(function () {
  50. randomBytes(MAX_UINT32 + 1, function () {
  51. t.ok(false, 'should not get here')
  52. })
  53. })
  54. t.throws(function () {
  55. randomBytes(-1, function () {
  56. t.ok(false, 'should not get here')
  57. })
  58. })
  59. t.throws(function () {
  60. randomBytes('hello', function () {
  61. t.ok(false, 'should not get here')
  62. })
  63. })
  64. })