test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var md5 = require('./md5.js');
  2. var assert = require('assert');
  3. describe('md5', function () {
  4. it('should throw an error for an undefined value', function() {
  5. assert.throws(function() {
  6. md5(undefined);
  7. });
  8. });
  9. it('should allow the hashing of the string `undefined`', function() {
  10. assert.equal('5e543256c480ac577d30f76f9120eb74', md5('undefined'));
  11. });
  12. it('should throw an error for `null`', function() {
  13. assert.throws(function() {
  14. md5(null);
  15. });
  16. });
  17. it('should return the expected MD5 hash for "message"', function() {
  18. assert.equal('78e731027d8fd50ed642340b7c9a63b3', md5('message'));
  19. });
  20. it('should not return the same hash for random numbers twice', function() {
  21. var msg1 = Math.floor((Math.random() * 100000) + 1) + (new Date).getTime();
  22. var msg2 = Math.floor((Math.random() * 100000) + 1) + (new Date).getTime();
  23. if (msg1 !== msg2) {
  24. assert.notEqual(md5(msg1), md5(msg2));
  25. } else {
  26. assert.equal(md5(msg1), md5(msg1));
  27. }
  28. });
  29. it('should support Node.js Buffers', function() {
  30. var buffer = new Buffer('message áßäöü', 'utf8');
  31. assert.equal(md5(buffer), md5('message áßäöü'));
  32. })
  33. it('should be able to use a binary encoded string', function() {
  34. var hash1 = md5('abc', { asString: true });
  35. var hash2 = md5(hash1 + 'a', { asString: true, encoding : 'binary' });
  36. var hash3 = md5(hash1 + 'a', { encoding : 'binary' });
  37. assert.equal(hash3, '131f0ac52813044f5110e4aec638c169');
  38. });
  39. it('should support Uint8Array', function() {
  40. // Polyfills
  41. if (!Array.from) {
  42. Array.from = function(src, fn) {
  43. var result = new Array(src.length);
  44. for (var i = 0; i < src.length; ++i)
  45. result[i] = fn(src[i]);
  46. return result;
  47. };
  48. }
  49. if (!Uint8Array.from) {
  50. Uint8Array.from = function(src) {
  51. var result = new Uint8Array(src.length);
  52. for (var i = 0; i < src.length; ++i)
  53. result[i] = src[i];
  54. return result;
  55. };
  56. }
  57. var message = 'foobarbaz';
  58. var u8arr = Uint8Array.from(
  59. Array.from(message, function(c) { return c.charCodeAt(0); }));
  60. var u8aHash = md5(u8arr);
  61. assert.equal(u8aHash, md5(message));
  62. });
  63. });