test_nonces_all.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Created by bmf on 11/2/13.
  3. */
  4. var assert = require('assert');
  5. var sodium = require('../build/Release/sodium');
  6. // Test all nonce classes
  7. testNonce('box-nonce',sodium.crypto_box_NONCEBYTES);
  8. testNonce('secretbox-nonce',sodium.crypto_secretbox_NONCEBYTES);
  9. testNonce('stream-nonce',sodium.crypto_stream_NONCEBYTES);
  10. function testNonce(modName, sizeBuffer) {
  11. var Nonce = require('../lib/nonces/' + modName);
  12. if (process.env.COVERAGE) {
  13. Nonce = require('../lib-cov/nonces/' + modName);
  14. }
  15. describe("stream-nonce", function () {
  16. it("generate a valid nonce", function (done) {
  17. var nonce = new Nonce();
  18. nonce.generate();
  19. var k = nonce.get();
  20. assert.ok(nonce.isValid(k));
  21. done();
  22. });
  23. it("nonce test string encoding utf8 should throw", function (done) {
  24. var nonce = new Nonce();
  25. assert.throws(function() {
  26. var n = nonce.toString('utf8');
  27. nonce.set(n, 'utf8');
  28. });
  29. done();
  30. });
  31. it("nonce test string encoding hex", function (done) {
  32. var nonce = new Nonce();
  33. var n = nonce.toString('hex');
  34. nonce.set(n, 'hex');
  35. done();
  36. });
  37. it("nonce test string encoding base64 should throw", function (done) {
  38. var nonce = new Nonce();
  39. var n = nonce.toString('base64');
  40. nonce.set(n, 'base64');
  41. done();
  42. });
  43. it("nonce test string encoding binary", function (done) {
  44. var nonce = new Nonce();
  45. assert.throws(function() {
  46. var n = nonce.toString('binary');
  47. nonce.set(n, 'binary');
  48. });
  49. done();
  50. });
  51. it("nonce test string encoding ascii should throw", function (done) {
  52. var nonce = new Nonce();
  53. assert.throws(function() {
  54. var n = nonce.toString('ascii');
  55. nonce.set(n, 'ascii');
  56. });
  57. done();
  58. });
  59. it("nonce test string encoding utf16le should throw", function (done) {
  60. var nonce = new Nonce();
  61. assert.throws(function() {
  62. var n = nonce.toString('utf16le');
  63. nonce.set(n, 'utf16le');
  64. });
  65. done();
  66. });
  67. it("nonce test string encoding ucs2 should throw", function (done) {
  68. var nonce = new Nonce();
  69. assert.throws(function() {
  70. var n = nonce.toString('ucs2');
  71. nonce.set(n, 'ucs2');
  72. });
  73. done();
  74. });
  75. it("nonce size should match that of sodium", function (done) {
  76. var nonce = new Nonce();
  77. assert.equal(nonce.size(), sizeBuffer);
  78. done();
  79. });
  80. it("nonce size should match that of sodium", function (done) {
  81. var nonce = new Nonce();
  82. assert.equal(nonce.bytes(), sizeBuffer);
  83. done();
  84. });
  85. it("isValid should return false on bad nonce", function (done) {
  86. var nonce = new Nonce();
  87. var k = Buffer.allocUnsafe(2);
  88. assert.equal(nonce.isValid(k), false);
  89. done();
  90. });
  91. it("isValid should return false on bad hex string", function (done) {
  92. var nonce = new Nonce();
  93. assert.equal(nonce.isValid("123"), false);
  94. done();
  95. });
  96. it("isValid should return false on bad nonce type", function (done) {
  97. var nonce = new Nonce();
  98. assert.equal(nonce.isValid(123), false);
  99. done();
  100. });
  101. it("isValid should return true on hex string", function (done) {
  102. var nonce = new Nonce();
  103. nonce.generate();
  104. var k = nonce.get();
  105. assert.ok(nonce.isValid(k.toString('hex'),'hex'));
  106. done();
  107. });
  108. it("isValid should return throw on bad encoding string", function (done) {
  109. var nonce = new Nonce();
  110. nonce.generate();
  111. var k = nonce.get();
  112. assert.throws(function() {
  113. nonce.isValid(k.toString('hex'),'sex').should.be.ok;
  114. });
  115. done();
  116. });
  117. it("reset should zero out the nonce", function (done) {
  118. var nonce = new Nonce();
  119. nonce.generate();
  120. nonce.wipe();
  121. var k = nonce.get();
  122. for(var i = 0; i < k.length; i++ ) {
  123. assert.equal(k[i], 0);
  124. }
  125. done();
  126. });
  127. it("set should throw on bad nonce length", function (done) {
  128. var nonce = new Nonce();
  129. assert.throws(function() {
  130. nonce.set(Buffer.allocUnsafe(2));
  131. });
  132. done();
  133. });
  134. it("set/get secretNonce", function (done) {
  135. var nonce = new Nonce();
  136. nonce.generate();
  137. var k = nonce.get();
  138. var nonce2 = new Nonce();
  139. nonce2.set(k);
  140. k2 = nonce2.get();
  141. assert.deepEqual(k2, k);
  142. done();
  143. });
  144. it("toString should return a string!", function (done) {
  145. var nonce = new Nonce();
  146. nonce.generate();
  147. assert.equal(typeof nonce.toString(), 'string');
  148. done();
  149. });
  150. it("toString should return a string!", function (done) {
  151. var nonce = new Nonce();
  152. nonce.generate();
  153. var k = nonce.get();
  154. assert.equal(nonce.toString('hex'), k.toString('hex'));
  155. done();
  156. });
  157. it("toString should throw with bad encoding!", function (done) {
  158. var nonce = new Nonce();
  159. assert.throws(function() {
  160. nonce.toString('sex');
  161. });
  162. done();
  163. });
  164. });
  165. }