summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/scripts/syntax-mediakeysession.js
blob: 3663a7b930db8c8d66cc9345a394ee59aa85ae50 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
function runTest(config) {
    var keysystem = config.keysystem;
    var testname  = testnamePrefix(null, config.keysystem);
    var initDataType = config.initDataType;
    var initData = config.initData;
    var configuration = {
        initDataTypes: [config.initDataType],
        audioCapabilities: [{contentType: config.audioType}],
        videoCapabilities: [{contentType: config.videoType}],
        sessionTypes: ['temporary']
    };

    var kTypeSpecificGenerateRequestExceptionsTestCases = [
        // Tests in this set use a shortened parameter name due to
        // format_value() only returning the first 60 characters as the
        // result. With a longer name the first 60 characters is not
        // enough to determine which test failed. Even with the
        // shortened name, the error message for the last couple of
        // tests is the same.

        // Too few parameters.
        {
            exception: 'TypeError',
            func: function (mk1, type) {
                return mk1.createSession().generateRequest(type);
            }
        },
        // Invalid parameters.
        {
            exception: 'TypeError',
            func: function (mk2, type) {
                return mk2.createSession().generateRequest(type, '');
            }
        },
        {
            exception: 'TypeError',
            func: function (mk3, type) {
                return mk3.createSession().generateRequest(type, null);
            }
        },
        {
            exception: 'TypeError',
            func: function (mk4, type) {
                return mk4.createSession().generateRequest(type, undefined);
            }
        },
        {
            exception: 'TypeError',
            func: function (mk5, type) {
                return mk5.createSession().generateRequest(type, 1);
            }
        },
        // (new Uint8Array(0)) returns empty array. So 'TypeError' should
        // be returned.
        {
            exception: 'TypeError',
            func: function (mk6, type) {
                return mk6.createSession().generateRequest(type, new Uint8Array(0));
            }
        }
    ];
    function generateRequestTestExceptions(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    var mp4SessionPromises = kTypeSpecificGenerateRequestExceptionsTestCases.map(function (testCase) {
                        return test_exception(testCase, mediaKeys, initDataType, initData);
                    });
                    assert_not_equals(mp4SessionPromises.length, 0);
                    return Promise.all(mp4SessionPromises);
                }).then(function (result) {
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        })
    }
    promise_test(function() {
        return generateRequestTestExceptions();
    }, testname + ' test MediaKeySession generateRequest() exceptions.');

    var kLoadExceptionsTestCases = [
        // Too few parameters.
        {
            exception: 'TypeError',
            func: function (mk1) {
                return mk1.createSession('temporary').load();
            }
        },
        {
            exception: 'TypeError',
            func: function (mk3) {
                return mk3.createSession('temporary').load('');
            }
        },
        {
            exception: 'TypeError',
            func: function (mk4) {
                return mk4.createSession('temporary').load(1);
            }
        },
        {
            exception: 'TypeError',
            func: function (mk5) {
                return mk5.createSession('temporary').load('!@#$%^&*()');
            }
        },
        {
            exception: 'TypeError',
            func: function (mk6) {
                return mk6.createSession('temporary').load('1234');
            }
        }
    ];
    function loadTestExceptions(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    var sessionPromises = kLoadExceptionsTestCases.map(function (testCase) {
                        return test_exception(testCase, mediaKeys);
                    });
                    assert_not_equals(sessionPromises.length, 0);
                    return Promise.all(sessionPromises);
                }).then(function () {
                    resolve();
                }).catch(function (error) {
                   reject(error);
                });
        })
    }
    promise_test(function() {
        return loadTestExceptions();
    }, testname + ' test MediaKeySession load() exceptions.');

    // All calls to |func| in this group are supposed to succeed.
    // However, the spec notes that some things are optional for
    // Clear Key. In particular, support for persistent sessions
    // is optional. Since some implementations won't support some
    // features, a NotSupportedError is treated as a success
    // if |isNotSupportedAllowed| is true.
    var kCreateSessionTestCases = [
        // Use the default sessionType.
        {
            func: function(mk) { return mk.createSession(); },
            isNotSupportedAllowed: false
        },
        // Try variations of sessionType.
        {
            func: function(mk) { return mk.createSession('temporary'); },
            isNotSupportedAllowed: false
        },
        {
            func: function(mk) { return mk.createSession(undefined); },
            isNotSupportedAllowed: false
        },
        {
            // Since this is optional, some Clear Key implementations
            // will succeed, others will return a "NotSupportedError".
            // Both are allowed results.
            func: function(mk) { return mk.createSession('persistent-license'); },
            isNotSupportedAllowed: true
        },
        // Try additional parameter, which should be ignored.
        {
            func: function(mk) { return mk.createSession('temporary', 'extra'); },
            isNotSupportedAllowed: false
        }
    ];
    // This function checks that calling generateRequest() works for
    // various sessions. |testCase.func| creates a MediaKeySession
    // object, and then generateRequest() is called on that object. It
    // allows for an NotSupportedError to be generated and treated as a
    // success, if allowed. See comment above kCreateSessionTestCases.
    function test_generateRequest(testCase, mediaKeys, type, initData) {
        var mediaKeySession;
        try {
            mediaKeySession = testCase.func.call(null, mediaKeys);
        } catch (e) {
            assert_true(testCase.isNotSupportedAllowed);
            assert_equals(e.name, 'NotSupportedError');
            return Promise.resolve('not supported');
        }
        return mediaKeySession.generateRequest(type, initData);
    }
    function generateRequestForVariousSessions(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType should be supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    var mp4SessionPromises = kCreateSessionTestCases.map(function (testCase) {
                        return test_generateRequest(testCase, mediaKeys, initDataType, initData);
                    });
                    assert_not_equals(mp4SessionPromises.length, 0);
                    return Promise.all(mp4SessionPromises);
                }).then(function () {
                   resolve();
                }).catch(function (error) {
                   reject(error);
                });
        })
    }
    promise_test(function() {
        return generateRequestForVariousSessions();
    }, testname + ' test if MediaKeySession generateRequest() resolves for various sessions');

    var kUpdateSessionExceptionsTestCases = [
        // Tests in this set use a shortened parameter name due to
        // format_value() only returning the first 60 characters as the
        // result. With a longer name (mediaKeySession) the first 60
        // characters is not enough to determine which test failed.

        // Too few parameters.
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update();
            }
        },
        // Invalid parameters.
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update('');
            }
        },
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update(null);
            }
        },
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update(undefined);
            }
        },
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update(1);
            }
        },
        // (new Uint8Array(0)) returns empty array. So 'TypeError' should
        // be returned.
        {
            exception: 'TypeError',
            func: function (s) {
                return s.update(new Uint8Array(0));
            }
        }
    ];

    function updateTestExceptions(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    var mp4SessionPromises = kUpdateSessionExceptionsTestCases.map(function (testCase) {
                        var mediaKeySession = mediaKeys.createSession();
                        return mediaKeySession.generateRequest(initDataType, initData).then(function (result) {
                            return test_exception(testCase, mediaKeySession);
                        });
                    });
                    assert_not_equals(mp4SessionPromises.length, 0);
                    return Promise.all(mp4SessionPromises);
                }).then(function () {
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        })
    }
    promise_test(function() {
        return updateTestExceptions();
    }, testname + ' test MediaKeySession update() exceptions.');

    function create_close_exception_test(mediaKeys) {
        var mediaKeySession = mediaKeys.createSession();
        return mediaKeySession.close().then(function (result) {
                assert_unreached('close() should not succeed if session uninitialized');
            }).catch(function (error) {
                assert_equals(error.name, 'InvalidStateError');
                // Return something so the promise resolves.
                return Promise.resolve();
            });
    }
    function closeTestExceptions(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    return create_close_exception_test(mediaKeys);
                }).then(function () {
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        });
    }
    promise_test(function() {
        return closeTestExceptions();
    }, testname + ' test MediaKeySession close() exceptions.');

    function create_remove_exception_test(mediaKeys, type, initData) {
        // remove() on an uninitialized session should fail.
        var mediaKeySession = mediaKeys.createSession('temporary');
        return mediaKeySession.remove().then(function (result) {
                assert_unreached('remove() should not succeed if session uninitialized');
            }, function (error) {
                assert_equals(error.name, 'InvalidStateError');
            });
    }
    function removeTestException(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    return create_remove_exception_test(mediaKeys, initDataType, initData);
                }).then(function () {
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        });
    }
    promise_test(function() {
        return removeTestException();
    }, testname + ' test MediaKeySession remove() exceptions.');

    // All calls to |func| in this group are supposed to succeed.
    // However, the spec notes that some things are optional for
    // Clear Key. In particular, support for persistent sessions
    // is optional. Since some implementations won't support some
    // features, a NotSupportedError is treated as a success
    // if |isNotSupportedAllowed| is true.
    var kCreateSessionTestCases = [
        // Use the default sessionType.
        {
            func: function (mk) {
                return mk.createSession();
            },
            isNotSupportedAllowed: false
        },
        // Try variations of sessionType.
        {
            func: function (mk) {
                return mk.createSession('temporary');
            },
            isNotSupportedAllowed: false
        },
        {
            func: function (mk) {
                return mk.createSession(undefined);
            },
            isNotSupportedAllowed: false
        },
        {
            // Since this is optional, some Clear Key implementations
            // will succeed, others will return a "NotSupportedError".
            // Both are allowed results.
            func: function (mk) {
                return mk.createSession('persistent-license');
            },
            isNotSupportedAllowed: true
        },
        // Try additional parameter, which should be ignored.
        {
            func: function (mk) {
                return mk.createSession('temporary', 'extra');
            },
            isNotSupportedAllowed: false
        }
    ];

    // This function checks that calling |testCase.func| creates a
    // MediaKeySession object with some default values. It also
    // allows for an NotSupportedError to be generated and treated as a
    // success, if allowed. See comment above kCreateSessionTestCases.
    function test_createSession(testCase, mediaKeys) {
        var mediaKeySession;
        try {
            mediaKeySession = testCase.func.call(null, mediaKeys);
        } catch (e) {
            assert_true(testCase.isNotSupportedAllowed);
            return;
        }
        assert_equals(typeof mediaKeySession, 'object');
        assert_equals(typeof mediaKeySession.addEventListener, 'function');
        assert_equals(typeof mediaKeySession.sessionId, 'string');
        assert_equals(typeof mediaKeySession.expiration, 'number');
        assert_equals(typeof mediaKeySession.closed, 'object');
        assert_equals(typeof mediaKeySession.keyStatuses, 'object');
        assert_equals(typeof mediaKeySession.onkeystatuseschange, 'object');
        assert_equals(typeof mediaKeySession.onmessage, 'object');
        assert_equals(typeof mediaKeySession.generateRequest, 'function');
        assert_equals(typeof mediaKeySession.load, 'function');
        assert_equals(typeof mediaKeySession.update, 'function');
        assert_equals(typeof mediaKeySession.close, 'function');
        assert_equals(typeof mediaKeySession.remove, 'function');
        assert_equals(mediaKeySession.sessionId, '');
    }
    function createSessionTest(){
        return new Promise(function(resolve, reject){
            isInitDataTypeSupported(keysystem, initDataType).then(function (isTypeSupported) {
                    assert_true(isTypeSupported, "initDataType not supported");
                    return navigator.requestMediaKeySystemAccess(keysystem, [configuration]);
                }).then(function (access) {
                    return access.createMediaKeys();
                }).then(function (mediaKeys) {
                    kCreateSessionTestCases.map(function (testCase) {
                        test_createSession(testCase, mediaKeys);
                    });
                    resolve();
                }).catch(function (error) {
                    reject(error);
                });
        })
    }
    promise_test(function() {
        return createSessionTest();
    }, testname + ' test MediaKeySession attribute syntax.');


}