test_box.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Created by bmf on 11/2/13.
  3. */
  4. var assert = require('assert');
  5. var sodium = require('../build/Release/sodium');
  6. var Box = require('../lib/box');
  7. if (process.env.COVERAGE) {
  8. Box = require('../lib-cov/box');
  9. }
  10. describe("Box", function () {
  11. it("encrypt/decrypt and validate message", function (done) {
  12. var box = new Box();
  13. box.setEncoding('utf8');
  14. var cipherBox = box.encrypt("This is a test");
  15. assert.equal(box.decrypt(cipherBox), "This is a test");
  16. done();
  17. });
  18. it("encrypt should return a valid cipherbox", function (done) {
  19. var box = new Box();
  20. box.setEncoding('utf8');
  21. var cipherBox = box.encrypt("This is a test");
  22. assert.equal(typeof cipherBox, 'object');
  23. assert.notEqual(typeof cipherBox.cipherText, 'undefined');
  24. assert.notEqual(typeof cipherBox.nonce, 'undefined');
  25. assert.ok(cipherBox.cipherText instanceof Buffer);
  26. assert.ok(cipherBox.nonce instanceof Buffer);
  27. done();
  28. });
  29. it("encrypt should throw if no first argument", function () {
  30. var box = new Box();
  31. assert.throws(function() {
  32. box.decrypt();
  33. });
  34. });
  35. it("encrypt should throw if the first argument is not an object with `cipherText` and `nonce` properties", function () {
  36. var box = new Box();
  37. assert.throws(function() {
  38. box.decrypt({});
  39. })
  40. assert.throws(function() {
  41. box.decrypt({cipherText: Buffer.from('foo')});
  42. });
  43. assert.throws(function() {
  44. box.decrypt({nonce: 'bar'});
  45. });
  46. });
  47. it("encrypt show throw if cipherBox.cipherText is not a buffer", function () {
  48. var box = new Box();
  49. assert.throws(function() {
  50. box.decrypt({cipherText: "not a buffer", nonce: "foo"});
  51. });
  52. });
  53. it("key size should match that of sodium", function (done) {
  54. var box = new Box();
  55. assert.equal(box.key().getPublicKey().size(), sodium.crypto_box_PUBLICKEYBYTES);
  56. assert.equal(box.key().getSecretKey().size(), sodium.crypto_box_SECRETKEYBYTES);
  57. done();
  58. });
  59. it("generate throw on a bad cipherBox buffer", function (done) {
  60. var box = new Box();
  61. var cipherBox = box.encrypt("This is a test", 'utf8');
  62. cipherBox.cipherText[0] = 99;
  63. cipherBox.cipherText[1] = 99;
  64. cipherBox.cipherText[2] = 99;
  65. assert.throws(function() {
  66. box.decrypt(cipherBox);
  67. });
  68. done();
  69. });
  70. it("generate throw on a bad cipherBox buffer", function (done) {
  71. var box = new Box();
  72. var cipherBox = box.encrypt("This is a test", 'utf8');
  73. cipherBox.cipherText[18] = 99;
  74. cipherBox.cipherText[19] = 99;
  75. cipherBox.cipherText[20] = 99;
  76. assert.throws(function() {
  77. box.decrypt(cipherBox);
  78. });
  79. done();
  80. });
  81. it("set bad secretKey should fail", function (done) {
  82. var box = new Box();
  83. assert.throws(function() {
  84. box.set(Buffer.allocUnsafe(2));
  85. });
  86. done();
  87. });
  88. it("set/get secretKey", function (done) {
  89. var box = new Box();
  90. box.key().generate();
  91. var k = box.key().get();
  92. var auth2 = new Box();
  93. auth2.key().set(k);
  94. k2 = auth2.key().get();
  95. assert.deepEqual(k2, k);
  96. done();
  97. });
  98. it('should set an encoding if a supported encoding is passed to setEncoding', function() {
  99. var box = new Box();
  100. box.setEncoding('base64');
  101. assert.equal(box.defaultEncoding, 'base64');
  102. });
  103. it('should fail to set an encoding if an unsupported encoding is passed to setEncoding', function() {
  104. var box = new Box();
  105. assert.throws(function () {
  106. box.setEncoding('unsupported-encoding');
  107. });
  108. });
  109. });