No Description
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.

index.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var tape = require("tape");
  2. var base64 = require("..");
  3. var strings = {
  4. "": "",
  5. "a": "YQ==",
  6. "ab": "YWI=",
  7. "abcdefg": "YWJjZGVmZw==",
  8. "abcdefgh": "YWJjZGVmZ2g=",
  9. "abcdefghi": "YWJjZGVmZ2hp"
  10. };
  11. tape.test("base64", function(test) {
  12. Object.keys(strings).forEach(function(str) {
  13. var enc = strings[str];
  14. test.equal(base64.test(enc), true, "should detect '" + enc + "' to be base64 encoded");
  15. var len = base64.length(enc);
  16. test.equal(len, str.length, "should calculate '" + enc + "' as " + str.length + " bytes");
  17. var buf = new Array(len);
  18. var len2 = base64.decode(enc, buf, 0);
  19. test.equal(len2, len, "should decode '" + enc + "' to " + len + " bytes");
  20. test.equal(String.fromCharCode.apply(String, buf), str, "should decode '" + enc + "' to '" + str + "'");
  21. var enc2 = base64.encode(buf, 0, buf.length);
  22. test.equal(enc2, enc, "should encode '" + str + "' to '" + enc + "'");
  23. });
  24. test.throws(function() {
  25. var buf = new Array(10);
  26. base64.decode("YQ!", buf, 0);
  27. }, Error, "should throw if encoding is invalid");
  28. test.throws(function() {
  29. var buf = new Array(10);
  30. base64.decode("Y", buf, 0);
  31. }, Error, "should throw if string is truncated");
  32. test.end();
  33. });