summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/scripts/syntax-mediakeys.js
blob: 4ec6551e8fd73e6dd6007dc8483a0fed9939b76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function runTest(config) {
    var keysystem = config.keysystem;
    var testname  = testnamePrefix(null, config.keysystem);
    var initDataType = config.initDataType;
    var configuration = {
        initDataTypes: [config.initDataType],
        audioCapabilities: [{contentType: config.audioType}],
        videoCapabilities: [{contentType: config.videoType}],
        sessionTypes: ['temporary']
    };

    function createMediaKeysAttributeTest() {
        return new Promise(function (resolve, reject) {
            var access;
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                assert_equals(typeof navigator.requestMediaKeySystemAccess, 'function');
                assert_true(isTypeSupported, "initDataType should be supported");
                return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
            }).then(function (result) {
                access = result;
                assert_equals(access.keySystem, keysystem);
                return access.createMediaKeys();
            }).then(function (mediaKeys) {
                assert_not_equals(mediaKeys, null);
                assert_equals(typeof mediaKeys, 'object');
                assert_equals(typeof mediaKeys.createSession, 'function');
                assert_equals(typeof mediaKeys.setServerCertificate, 'function');

                // Test creation of a second MediaKeys.
                // The extra parameter is ignored.
                return access.createMediaKeys('extra');
            }).then(function (mediaKeys) {
                assert_not_equals(mediaKeys, null);
                assert_equals(typeof mediaKeys, 'object');
                assert_equals(typeof mediaKeys.createSession, 'function');
                assert_equals(typeof mediaKeys.setServerCertificate, 'function');
                resolve();
            }).catch(function (error) {
                reject(error);
            });
        })
    }

    promise_test(function() {
        return createMediaKeysAttributeTest();
    }, testname + ' test MediaKeys attribute syntax');

    var kSetServerCertificateExceptionsTestCases = [
        // Too few parameters.
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate();
            }
        },
        // Invalid parameters.
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate('');
            }
        },
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate(null);
            }
        },
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate(undefined);
            }
        },
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate(1);
            }
        },
        // Empty array.
        {
            exception: 'TypeError',
            func: function (mk) {
                return mk.setServerCertificate(new Uint8Array(0));
            }
        }
    ];


    function setServerCertificateTestExceptions() {
        return new Promise(function(resolve, reject) {
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                        assert_equals(typeof navigator.requestMediaKeySystemAccess, 'function');
                        assert_true(isTypeSupported, "initDataType not supported");
                        return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                    }).then(function (access) {
                        return access.createMediaKeys();
                    }).then(function (mediaKeys) {
                        var promises = kSetServerCertificateExceptionsTestCases.map(function (testCase) {
                            return test_exception(testCase, mediaKeys);
                        });
                        assert_not_equals(promises.length, 0);
                        return Promise.all(promises);
                    }).then(function () {
                        resolve();
                    }).catch(function (error) {
                        reject(error);
                    });
        })
    }
    promise_test(function() {
        return setServerCertificateTestExceptions();
    }, testname + ' test MediaKeys setServerCertificate() exceptions.');

    // All calls to |func| in this group resolve. setServerCertificate with these cert may either resolve with true
    // for clearkey or throw a DOMException.
    var kSetServerCertificateTestCases = [
        {
            // Pass in ArrayBufferView
            func: function (mk) {
                var cert = new Uint8Array(200);
                assert_true(ArrayBuffer.isView(cert));

                return new Promise(function (resolve, reject) {
                    mk.setServerCertificate(cert).then(function (value) {
                        resolve(value);
                    }).catch(function (error) {
                        if (Object.prototype.toString.call(error) === "[object DOMException]") {
                            resolve(false);
                        }
                    });
                })
            },
            expected: false
        },
        {
            // Pass in ArrayBuffer.
            func: function (mk) {
                var cert = new ArrayBuffer(200);
                assert_false(ArrayBuffer.isView(cert));
                return new Promise(function (resolve) {
                    mk.setServerCertificate(cert).then(function (resolveValue) {
                        resolve(resolveValue);
                    }).catch(function (error) {
                        if (Object.prototype.toString.call(error) === "[object DOMException]") {
                            resolve(false);
                        }
                    });
                })
            },
            expected: false
        }
    ];
    function setServerCertificateTest(){
        return new Promise(function(resolve, reject){
            var expected_result;
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_equals(typeof navigator.requestMediaKeySystemAccess, 'function');
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    var promises = kSetServerCertificateTestCases.map(function (testCase) {
                        return testCase.func.call(null, mediaKeys);
                    });
                    expected_result = kSetServerCertificateTestCases.map(function (testCase) {
                        return testCase.expected;
                    });
                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function (result) {
                    assert_array_equals(result, expected_result);
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        })
    }
    promise_test(function() {
       return  setServerCertificateTest();
    }, testname + ' test MediaKeys setServerCertificate() syntax with non-empty certificate.');
}