base64.html 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Native Base64 Tests</title>
  5. <script src="../include/util.js"></script>
  6. <script src="../include/webutil.js"></script>
  7. <script src="../include/base64.js"></script>
  8. </head>
  9. <body>
  10. <h1>Native Base64 Tests</h1>
  11. <br>
  12. Messages:<br>
  13. <textarea id="debug" style="font-size: 9px;" cols=80 rows=25></textarea>
  14. <br>
  15. </body>
  16. <script>
  17. function debug(str) {
  18. console.log(str);
  19. cell = $D('debug');
  20. cell.textContent += str + "\n";
  21. cell.scrollTop = cell.scrollHeight;
  22. }
  23. function assertRun(code, result) {
  24. try {
  25. var actual = eval(code);
  26. } catch (exc) {
  27. debug("FAIL: '" + code + "' threw an exception");
  28. fail += 1;
  29. return false;
  30. }
  31. if (actual !== result) {
  32. debug("FAIL: '" + code + "' returned '" + actual + "', expected '" + result + "'");
  33. fail += 1;
  34. return false;
  35. }
  36. debug("PASS: '" + code + "' returned expected '" + result +"'");
  37. pass += 1;
  38. return true;
  39. }
  40. function Base64_decode(data) {
  41. var arr = Base64.decode (data);
  42. return arr.map(function (num) {
  43. return String.fromCharCode(num); } ).join('');
  44. }
  45. window.onload = function() {
  46. var str;
  47. debug('onload');
  48. fail = 0;
  49. pass = 0;
  50. assertRun('window.btoa("hello world")', 'aGVsbG8gd29ybGQ=');
  51. assertRun('window.btoa("a")', 'YQ==');
  52. assertRun('window.btoa("ab")', 'YWI=');
  53. assertRun('window.btoa("abc")', 'YWJj');
  54. assertRun('window.btoa("abcd")', 'YWJjZA==');
  55. assertRun('window.btoa("abcde")', 'YWJjZGU=');
  56. assertRun('window.btoa("abcdef")', 'YWJjZGVm');
  57. assertRun('window.btoa("abcdefg")', 'YWJjZGVmZw==');
  58. assertRun('window.btoa("abcdefgh")', 'YWJjZGVmZ2g=');
  59. assertRun('window.atob("aGVsbG8gd29ybGQ=")', 'hello world');
  60. assertRun('Base64_decode("aGVsbG8gd29ybGQ=")', 'hello world');
  61. assertRun('window.atob("YQ==")', 'a');
  62. assertRun('Base64_decode("YQ==")', 'a');
  63. assertRun('window.atob("YWI=")', 'ab');
  64. assertRun('Base64_decode("YWI=")', 'ab');
  65. assertRun('window.atob("YWJj")', 'abc');
  66. assertRun('Base64_decode("YWJj")', 'abc');
  67. assertRun('window.atob("YWJjZA==")', 'abcd');
  68. assertRun('Base64_decode("YWJjZA==")', 'abcd');
  69. assertRun('window.atob("YWJjZGU=")', 'abcde');
  70. assertRun('Base64_decode("YWJjZGU=")', 'abcde');
  71. assertRun('window.atob("YWJjZGVm")', 'abcdef');
  72. assertRun('Base64_decode("YWJjZGVm")', 'abcdef');
  73. assertRun('typeof window.btoa', 'function');
  74. assertRun('window.btoa("")', '');
  75. assertRun('window.btoa(null)', '');
  76. assertRun('window.atob(window.btoa(window))', window.toString()); // "[object DOMWindow]"
  77. assertRun('window.btoa("\\u0080\\u0081")', 'gIE=');
  78. debug("Tests failed: " + fail);
  79. debug("Tests passed: " + pass);
  80. }
  81. </script>