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.

45 lines
1.3 KiB

4 years ago
  1. var md5 = require('./md5.js');
  2. var assert = require('assert');
  3. describe('md5', function () {
  4. it('should throw an error for `undefined`', function() {
  5. assert.throws(function() {
  6. md5(undefined);
  7. });
  8. });
  9. it('should throw an error for `null`', function() {
  10. assert.throws(function() {
  11. md5(null);
  12. });
  13. });
  14. it('should return the expected MD5 hash for "message"', function() {
  15. assert.equal('78e731027d8fd50ed642340b7c9a63b3', md5('message'));
  16. });
  17. it('should not return the same hash for random numbers twice', function() {
  18. var msg1 = Math.floor((Math.random() * 100000) + 1) + (new Date).getTime();
  19. var msg2 = Math.floor((Math.random() * 100000) + 1) + (new Date).getTime();
  20. if (msg1 !== msg2) {
  21. assert.notEqual(md5(msg1), md5(msg2));
  22. } else {
  23. assert.equal(md5(msg1), md5(msg1));
  24. }
  25. });
  26. it('should support Node.js Buffers', function() {
  27. var buffer = new Buffer('message áßäöü', 'utf8');
  28. assert.equal(md5(buffer), md5('message áßäöü'));
  29. })
  30. it('should be able to use a binary encoded string', function() {
  31. var hash1 = md5('abc', { asString: true });
  32. var hash2 = md5(hash1 + 'a', { asString: true, encoding : 'binary' });
  33. var hash3 = md5(hash1 + 'a', { encoding : 'binary' });
  34. assert.equal(hash3, '131f0ac52813044f5110e4aec638c169');
  35. });
  36. });