Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / public / vendor / noVNC / tests / base64.html @ 5fcb9de8

History | View | Annotate | Download (3.08 KB)

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
 
12
        <br> 
13
        Messages:<br> 
14
        <textarea id="debug" style="font-size: 9px;" cols=80 rows=25></textarea> 
15
    
16
        <br>
17
  </body> 
18
 
19
<script> 
20
    function debug(str) {
21
        console.log(str);
22
        cell = $D('debug');
23
        cell.innerHTML += str + "\n";
24
        cell.scrollTop = cell.scrollHeight;
25
    }
26

27
    function assertRun(code, result) {
28
        try {
29
            var actual = eval(code);
30
        } catch (exc) {
31
            debug("FAIL: '" + code + "' threw an exception");
32
            fail += 1;
33
            return false;
34
        }
35
        if (actual !== result) {
36
            debug("FAIL: '" + code + "' returned '" + actual + "', expected '" + result + "'");
37
            fail += 1;
38
            return false;
39
        }
40
        debug("PASS: '" + code + "' returned expected '" + result +"'");
41
        pass += 1;
42
        return true;
43
    }
44

45
    function Base64_decode(data) {
46
        var arr = Base64.decode (data);
47
        return arr.map(function (num) {
48
            return String.fromCharCode(num); } ).join('');
49

50
    }
51

52
    window.onload = function() {
53
        var str;
54
        debug('onload');
55
        fail = 0;
56
        pass = 0;
57
        assertRun('window.btoa("hello world")', 'aGVsbG8gd29ybGQ=');
58
        assertRun('window.btoa("a")', 'YQ==');
59
        assertRun('window.btoa("ab")', 'YWI=');
60
        assertRun('window.btoa("abc")', 'YWJj');
61
        assertRun('window.btoa("abcd")', 'YWJjZA==');
62
        assertRun('window.btoa("abcde")', 'YWJjZGU=');
63
        assertRun('window.btoa("abcdef")', 'YWJjZGVm');
64
        assertRun('window.btoa("abcdefg")', 'YWJjZGVmZw==');
65
        assertRun('window.btoa("abcdefgh")', 'YWJjZGVmZ2g=');
66

67
        assertRun('window.atob("aGVsbG8gd29ybGQ=")', 'hello world');
68
        assertRun('Base64_decode("aGVsbG8gd29ybGQ=")', 'hello world');
69
        assertRun('window.atob("YQ==")', 'a');
70
        assertRun('Base64_decode("YQ==")', 'a');
71
        assertRun('window.atob("YWI=")', 'ab');
72
        assertRun('Base64_decode("YWI=")', 'ab');
73
        assertRun('window.atob("YWJj")', 'abc');
74
        assertRun('Base64_decode("YWJj")', 'abc');
75
        assertRun('window.atob("YWJjZA==")', 'abcd');
76
        assertRun('Base64_decode("YWJjZA==")', 'abcd');
77
        assertRun('window.atob("YWJjZGU=")', 'abcde');
78
        assertRun('Base64_decode("YWJjZGU=")', 'abcde');
79
        assertRun('window.atob("YWJjZGVm")', 'abcdef');
80
        assertRun('Base64_decode("YWJjZGVm")', 'abcdef');
81

82
        assertRun('typeof window.btoa', 'function');
83
        assertRun('window.btoa("")', '');
84
        assertRun('window.btoa(null)', '');
85
        assertRun('window.atob(window.btoa(window))', window.toString()); // "[object DOMWindow]"
86
        assertRun('window.btoa("\\u0080\\u0081")', 'gIE=');
87

88
        debug("Tests failed: " + fail);
89
        debug("Tests passed: " + pass);
90
    }
91
</script>