diff options
Diffstat (limited to 'testing/web-platform/tests/encrypted-media/util')
6 files changed, 1118 insertions, 0 deletions
diff --git a/testing/web-platform/tests/encrypted-media/util/clearkey-messagehandler.js b/testing/web-platform/tests/encrypted-media/util/clearkey-messagehandler.js new file mode 100644 index 000000000..c91a57f6d --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/clearkey-messagehandler.js @@ -0,0 +1,64 @@ +// Expect utf8decoder and utf8decoder to be TextEncoder('utf-8') and TextDecoder('utf-8') respectively + +function MessageHandler( keysystem, content ) { + this._keysystem = keysystem; + this._content = content; + this.messagehandler = MessageHandler.prototype.messagehandler.bind( this ); + this.servercertificate = undefined; +} + +MessageHandler.prototype.messagehandler = function messagehandler( messageType, message ) +{ + if ( messageType === 'license-request' ) + { + var request = fromUtf8( message ); + + var keys = request.kids.map( function( kid ) { + + var key; + for( var i=0; i < this._content.keys.length; ++i ) + { + if ( base64urlEncode( this._content.keys[ i ].kid ) === kid ) + { + key = base64urlEncode( this._content.keys[ i ].key ); + break; + } + } + + return { kty: 'oct', kid: kid, k: key }; + + }.bind( this ) ); + + return Promise.resolve( toUtf8( { keys: keys } ) ); + } + else if ( messageType === 'license-release' ) + { + var release = fromUtf8( message ); + + // TODO: Check the license release message here + + return Promise.resolve( toUtf8( { kids: release.kids } ) ); + } + + throw new TypeError( 'Unsupported message type for ClearKey' ); +}; + +MessageHandler.prototype.createJWKSet = function createJWKSet(keyId, key) { + var jwkSet = '{"keys":['; + for (var i = 0; i < arguments.length; i++) { + if (i != 0) + jwkSet += ','; + jwkSet += arguments[i]; + } + jwkSet += ']}'; + return jwkSet; +}; + +MessageHandler.prototype.createJWK = function createJWK(keyId, key) { + var jwk = '{"kty":"oct","alg":"A128KW","kid":"'; + jwk += base64urlEncode(keyId); + jwk += '","k":"'; + jwk += base64urlEncode(key); + jwk += '"}'; + return jwk; +};
\ No newline at end of file diff --git a/testing/web-platform/tests/encrypted-media/util/drm-messagehandler.js b/testing/web-platform/tests/encrypted-media/util/drm-messagehandler.js new file mode 100644 index 000000000..256c069e5 --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/drm-messagehandler.js @@ -0,0 +1,262 @@ +(function(){ +// Expect utf8decoder and utf8decoder to be TextEncoder('utf-8') and TextDecoder('utf-8') respectively +// +// drmconfig format: +// { <keysystem> : { "serverURL" : <the url for the server>, +// "httpRequestHeaders" : <map of HTTP request headers>, +// "servertype" : "microsoft" | "drmtoday", // affects how request parameters are formed +// "certificate" : <base64 encoded server certificate> } } +// + +drmtodaysecret = Uint8Array.from( [144, 34, 109, 76, 134, 7, 97, 107, 98, 251, 140, 28, 98, 79, 153, 222, 231, 245, 154, 226, 193, 1, 213, 207, 152, 204, 144, 15, 13, 2, 37, 236] ); + +drmconfig = { + "com.widevine.alpha": [ { + "serverURL": "https://lic.staging.drmtoday.com/license-proxy-widevine/cenc/", + "servertype" : "drmtoday", + "merchant" : "w3c-eme-test", + "secret" : drmtodaysecret + } ], + "com.microsoft.playready": [ { + "serverURL": "http://playready-testserver.azurewebsites.net/rightsmanager.asmx", + "servertype": "microsoft", + "sessionTypes" : [ "persistent-usage-record" ], + "certificate" : "Q0hBSQAAAAEAAAUEAAAAAAAAAAJDRVJUAAAAAQAAAfQAAAFkAAEAAQAAAFjt9G6KdSncCkrjbTQPN+/2AAAAAAAAAAAAAAAJIPbrW9dj0qydQFIomYFHOwbhGZVGP2ZsPwcvjh+NFkP/////AAAAAAAAAAAAAAAAAAAAAAABAAoAAABYxw6TjIuUUmvdCcl00t4RBAAAADpodHRwOi8vcGxheXJlYWR5LmRpcmVjdHRhcHMubmV0L3ByL3N2Yy9yaWdodHNtYW5hZ2VyLmFzbXgAAAAAAQAFAAAADAAAAAAAAQAGAAAAXAAAAAEAAQIAAAAAADBRmRRpqV4cfRLcWz9WoXIGZ5qzD9xxJe0CSI2mXJQdPHEFZltrTkZtdmurwVaEI2etJY0OesCeOCzCqmEtTkcAAAABAAAAAgAAAAcAAAA8AAAAAAAAAAVEVEFQAAAAAAAAABVNZXRlcmluZyBDZXJ0aWZpY2F0ZQAAAAAAAAABAAAAAAABAAgAAACQAAEAQGHic/IPbmLCKXxc/MH20X/RtjhXH4jfowBWsQE1QWgUUBPFId7HH65YuQJ5fxbQJCT6Hw0iHqKzaTkefrhIpOoAAAIAW+uRUsdaChtq/AMUI4qPlK2Bi4bwOyjJcSQWz16LAFfwibn5yHVDEgNA4cQ9lt3kS4drx7pCC+FR/YLlHBAV7ENFUlQAAAABAAAC/AAAAmwAAQABAAAAWMk5Z0ovo2X0b2C9K5PbFX8AAAAAAAAAAAAAAARTYd1EkpFovPAZUjOj2doDLnHiRSfYc89Fs7gosBfar/////8AAAAAAAAAAAAAAAAAAAAAAAEABQAAAAwAAAAAAAEABgAAAGAAAAABAAECAAAAAABb65FSx1oKG2r8AxQjio+UrYGLhvA7KMlxJBbPXosAV/CJufnIdUMSA0DhxD2W3eRLh2vHukIL4VH9guUcEBXsAAAAAgAAAAEAAAAMAAAABwAAAZgAAAAAAAAAgE1pY3Jvc29mdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgFBsYXlSZWFkeSBTTDAgTWV0ZXJpbmcgUm9vdCBDQQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgDEuMC4wLjEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEACAAAAJAAAQBArAKJsEIDWNG5ulOgLvSUb8I2zZ0c5lZGYvpIO56Z0UNk/uC4Mq3jwXQUUN6m/48V5J/vuLDhWu740aRQc1dDDAAAAgCGTWHP8iVuQixWizwoABz7PhUnZYWEugUht5sYKNk23h2Cao/D5uf6epDVyilG8fZKLvufXc/+fkNOtEKT+sWr" + }, + { + "serverURL": "http://playready.directtaps.net/pr/svc/rightsmanager.asmx", + "servertype": "microsoft", + "sessionTypes" : [ "persistent-usage-record" ], + "certificate" : "Q0hBSQAAAAEAAAUEAAAAAAAAAAJDRVJUAAAAAQAAAfQAAAFkAAEAAQAAAFjt9G6KdSncCkrjbTQPN+/2AAAAAAAAAAAAAAAJIPbrW9dj0qydQFIomYFHOwbhGZVGP2ZsPwcvjh+NFkP/////AAAAAAAAAAAAAAAAAAAAAAABAAoAAABYxw6TjIuUUmvdCcl00t4RBAAAADpodHRwOi8vcGxheXJlYWR5LmRpcmVjdHRhcHMubmV0L3ByL3N2Yy9yaWdodHNtYW5hZ2VyLmFzbXgAAAAAAQAFAAAADAAAAAAAAQAGAAAAXAAAAAEAAQIAAAAAADBRmRRpqV4cfRLcWz9WoXIGZ5qzD9xxJe0CSI2mXJQdPHEFZltrTkZtdmurwVaEI2etJY0OesCeOCzCqmEtTkcAAAABAAAAAgAAAAcAAAA8AAAAAAAAAAVEVEFQAAAAAAAAABVNZXRlcmluZyBDZXJ0aWZpY2F0ZQAAAAAAAAABAAAAAAABAAgAAACQAAEAQGHic/IPbmLCKXxc/MH20X/RtjhXH4jfowBWsQE1QWgUUBPFId7HH65YuQJ5fxbQJCT6Hw0iHqKzaTkefrhIpOoAAAIAW+uRUsdaChtq/AMUI4qPlK2Bi4bwOyjJcSQWz16LAFfwibn5yHVDEgNA4cQ9lt3kS4drx7pCC+FR/YLlHBAV7ENFUlQAAAABAAAC/AAAAmwAAQABAAAAWMk5Z0ovo2X0b2C9K5PbFX8AAAAAAAAAAAAAAARTYd1EkpFovPAZUjOj2doDLnHiRSfYc89Fs7gosBfar/////8AAAAAAAAAAAAAAAAAAAAAAAEABQAAAAwAAAAAAAEABgAAAGAAAAABAAECAAAAAABb65FSx1oKG2r8AxQjio+UrYGLhvA7KMlxJBbPXosAV/CJufnIdUMSA0DhxD2W3eRLh2vHukIL4VH9guUcEBXsAAAAAgAAAAEAAAAMAAAABwAAAZgAAAAAAAAAgE1pY3Jvc29mdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgFBsYXlSZWFkeSBTTDAgTWV0ZXJpbmcgUm9vdCBDQQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgDEuMC4wLjEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEACAAAAJAAAQBArAKJsEIDWNG5ulOgLvSUb8I2zZ0c5lZGYvpIO56Z0UNk/uC4Mq3jwXQUUN6m/48V5J/vuLDhWu740aRQc1dDDAAAAgCGTWHP8iVuQixWizwoABz7PhUnZYWEugUht5sYKNk23h2Cao/D5uf6epDVyilG8fZKLvufXc/+fkNOtEKT+sWr" + }, + { + "serverURL": "https://lic.staging.drmtoday.com/license-proxy-headerauth/drmtoday/RightsManager.asmx", + "servertype" : "drmtoday", + "sessionTypes" : [ "temporary", "persistent-usage-record", "persistent-license" ], + "merchant" : "w3c-eme-test", + "secret" : drmtodaysecret + } ] +}; + + +var keySystemWrappers = { + // Key System wrappers map messages and pass to a handler, then map the response and return to caller + // + // function wrapper(handler, messageType, message, params) + // + // where: + // Promise<response> handler(messageType, message, responseType, headers, params); + // + + 'com.widevine.alpha': function(handler, messageType, message, params) { + return handler.call(this, messageType, new Uint8Array(message), 'json', null, params).then(function(response){ + return base64DecodeToUnit8Array(response.license); + }); + }, + + 'com.microsoft.playready': function(handler, messageType, message, params) { + var msg, xmlDoc; + var licenseRequest = null; + var headers = {}; + var parser = new DOMParser(); + var dataview = new Uint16Array(message); + + msg = String.fromCharCode.apply(null, dataview); + xmlDoc = parser.parseFromString(msg, 'application/xml'); + + if (xmlDoc.getElementsByTagName('Challenge')[0]) { + var challenge = xmlDoc.getElementsByTagName('Challenge')[0].childNodes[0].nodeValue; + if (challenge) { + licenseRequest = atob(challenge); + } + } + + var headerNameList = xmlDoc.getElementsByTagName('name'); + var headerValueList = xmlDoc.getElementsByTagName('value'); + for (var i = 0; i < headerNameList.length; i++) { + headers[headerNameList[i].childNodes[0].nodeValue] = headerValueList[i].childNodes[0].nodeValue; + } + // some versions of the PlayReady CDM return 'Content' instead of 'Content-Type', + // but the license server expects 'Content-Type', so we fix it up here. + if (headers.hasOwnProperty('Content')) { + headers['Content-Type'] = headers.Content; + delete headers.Content; + } + + return handler.call(this, messageType, licenseRequest, 'arraybuffer', headers, params).catch(function(response){ + return response.text().then( function( error ) { throw error; } ); + }); + } +}; + +const requestConstructors = { + // Server request construction functions + // + // Promise<request> constructRequest(config, sessionType, content, messageType, message, params) + // + // request = { url: ..., headers: ..., body: ... } + // + // content = { assetId: ..., variantId: ..., key: ... } + // params = { expiration: ... } + + 'drmtoday': function(config, sessionType, content, messageType, message, headers, params) { + var optData = JSON.stringify({merchant: config.merchant, userId:"12345", sessionId:""}); + var crt = {}; + if (messageType === 'license-request') { + crt = {assetId: content.assetId, + outputProtection: {digital : false, analogue: false, enforce: false}, + storeLicense: (sessionType === 'persistent-license')}; + + if (!params || params.expiration === undefined) { + crt.profile = {purchase: {}}; + } else { + crt.profile = {rental: {absoluteExpiration: (new Date(params.expiration)).toISOString(), + playDuration: 3600000 } }; + } + + if (content.variantId !== undefined) { + crt.variantId = content.variantId; + } + } + + return JWT.encode("HS256", {optData: optData, crt: JSON.stringify([crt])}, config.secret).then(function(jwt){ + headers = headers || {}; + headers['x-dt-auth-token'] = jwt; + return {url: config.serverURL, headers: headers, body: message}; + }); + }, + + 'microsoft': function(config, sessionType, content, messageType, message, headers, params) { + var url = config.serverURL; + if (messageType === 'license-request') { + url += "?"; + if (sessionType === 'temporary' || sessionType === 'persistent-usage-record') { + url += "UseSimpleNonPersistentLicense=1&"; + } + if (sessionType === 'persistent-usage-record') { + url += "SecureStop=1&"; + } + url += "PlayEnablers=B621D91F-EDCC-4035-8D4B-DC71760D43E9&"; // disable output protection + url += "ContentKey=" + btoa(String.fromCharCode.apply(null, content.key)); + return url; + } + + // TODO: Include expiration time in URL + return Promise.resolve({url: url, headers: headers, body: message}); + } +}; + +MessageHandler = function(keysystem, content, sessionType) { + sessionType = sessionType || "temporary"; + + this._keysystem = keysystem; + this._content = content; + this._sessionType = sessionType; + try { + this._drmconfig = drmconfig[this._keysystem].filter(function(drmconfig) { + return drmconfig.sessionTypes === undefined || (drmconfig.sessionTypes.indexOf(sessionType) !== -1); + })[0]; + this._requestConstructor = requestConstructors[this._drmconfig.servertype]; + + this.messagehandler = keySystemWrappers[keysystem].bind(this, MessageHandler.prototype.messagehandler); + + if (this._drmconfig && this._drmconfig.certificate) { + this.servercertificate = stringToUint8Array(atob(this._drmconfig.certificate)); + } + } catch(e) { + return null; + } +} + +MessageHandler.prototype.messagehandler = function messagehandler(messageType, message, responseType, headers, params) { + + var variantId = params ? params.variantId : undefined; + var key; + if( variantId ) { + var keys = this._content.keys.filter(function(k){return k.variantId === variantId;}); + if (keys[0]) key = keys[0].key; + } + if (!key) { + key = this._content.keys[0].key; + } + + var content = {assetId: this._content.assetId, + variantId: variantId, + key: key}; + + return this._requestConstructor(this._drmconfig, this._sessionType, content, messageType, message, headers, params).then(function(request){ + return fetch(request.url, { + method: 'POST', + headers: request.headers, + body: request.body }); + }).then(function(fetchresponse){ + if(fetchresponse.status !== 200) { + throw fetchresponse; + } + + if(responseType === 'json') { + return fetchresponse.json(); + } else if(responseType === 'arraybuffer') { + return fetchresponse.arrayBuffer(); + } + }); +} + +})(); + +(function() { + + var subtlecrypto = window.crypto.subtle; + + // Encoding / decoding utilities + function b64pad(b64) { return b64+"==".substr(0,(b64.length%4)?(4-b64.length%4):0); } + function str2b64url(str) { return btoa(str).replace(/=+$/g, '').replace(/\+/g, "-").replace(/\//g, "_"); } + function b64url2str(b64) { return atob(b64pad(b64.replace(/\-/g, "+").replace(/\_/g, "/"))); } + function str2ab(str) { return Uint8Array.from( str.split(''), function(s){return s.charCodeAt(0)} ); } + function ab2str(ab) { return String.fromCharCode.apply(null, new Uint8Array(ab)); } + + function jwt2webcrypto(alg) { + if (alg === "HS256") return {name: "HMAC", hash: "SHA-256", length: 256}; + else if (alg === "HS384") return { name: "HMAC", hash: "SHA-384", length: 384}; + else if (alg === "HS512") return { name: "HMAC", hash: "SHA-512", length: 512}; + else throw new Error("Unrecognized JWT algorithm: " + alg); + } + + JWT = { + encode: function encode(alg, claims, secret) { + var algorithm = jwt2webcrypto(alg); + if (secret.byteLength !== algorithm.length / 8) throw new Error("Unexpected secret length: " + secret.byteLength); + + if (!claims.iat) claims.iat = (Date.now() / 1000) | 0; + if (!claims.jti) { + var nonce = new Uint8Array(16); + window.crypto.getRandomValues(nonce); + claims.jti = str2b64url( ab2str(nonce) ); + } + + var header = {typ: "JWT", alg: alg}; + var plaintext = str2b64url(JSON.stringify(header)) + '.' + str2b64url(JSON.stringify(claims)); + return subtlecrypto.importKey("raw", secret, algorithm, false, [ "sign" ]).then( function(key) { + return subtlecrypto.sign(algorithm, key, str2ab(plaintext)); + }).then(function(hmac){ + return plaintext + '.' + str2b64url(ab2str(hmac)); + }); + }, + + decode: function decode(jwt, secret) { + var jwtparts = jwt.split('.'); + var header = JSON.parse( b64url2str(jwtparts[0])); + var claims = JSON.parse( b64url2str(jwtparts[1])); + var hmac = str2ab(b64url2str(jwtparts[2])); + var algorithm = jwt2webcrypto(header.alg); + if (secret.byteLength !== algorithm.length / 8) throw new Error("Unexpected secret length: " + secret.byteLength); + + return subtlecrypto.importKey("raw", secret, algorithm, false, ["sign", "verify"]).then(function(key) { + return subtlecrypto.verify(algorithm, key, hmac, str2ab(jwtparts[0] + '.' + jwtparts[1])); + }).then(function(success){ + if (!success) throw new Error("Invalid signature"); + return claims; + }); + } + }; +})(); diff --git a/testing/web-platform/tests/encrypted-media/util/fetch.js b/testing/web-platform/tests/encrypted-media/util/fetch.js new file mode 100644 index 000000000..d14d00bdb --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/fetch.js @@ -0,0 +1,456 @@ +// https://github.com/github/fetch +// +// Copyright (c) 2014-2016 GitHub, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +(function(self) { + 'use strict'; + + if (self.fetch) { + return + } + + var support = { + searchParams: 'URLSearchParams' in self, + iterable: 'Symbol' in self && 'iterator' in Symbol, + blob: 'FileReader' in self && 'Blob' in self && (function() { + try { + new Blob() + return true + } catch(e) { + return false + } + })(), + formData: 'FormData' in self, + arrayBuffer: 'ArrayBuffer' in self + } + + function normalizeName(name) { + if (typeof name !== 'string') { + name = String(name) + } + if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) { + throw new TypeError('Invalid character in header field name') + } + return name.toLowerCase() + } + + function normalizeValue(value) { + if (typeof value !== 'string') { + value = String(value) + } + return value + } + + // Build a destructive iterator for the value list + function iteratorFor(items) { + var iterator = { + next: function() { + var value = items.shift() + return {done: value === undefined, value: value} + } + } + + if (support.iterable) { + iterator[Symbol.iterator] = function() { + return iterator + } + } + + return iterator + } + + function Headers(headers) { + this.map = {} + + if (headers instanceof Headers) { + headers.forEach(function(value, name) { + this.append(name, value) + }, this) + + } else if (headers) { + Object.getOwnPropertyNames(headers).forEach(function(name) { + this.append(name, headers[name]) + }, this) + } + } + + Headers.prototype.append = function(name, value) { + name = normalizeName(name) + value = normalizeValue(value) + var list = this.map[name] + if (!list) { + list = [] + this.map[name] = list + } + list.push(value) + } + + Headers.prototype['delete'] = function(name) { + delete this.map[normalizeName(name)] + } + + Headers.prototype.get = function(name) { + var values = this.map[normalizeName(name)] + return values ? values[0] : null + } + + Headers.prototype.getAll = function(name) { + return this.map[normalizeName(name)] || [] + } + + Headers.prototype.has = function(name) { + return this.map.hasOwnProperty(normalizeName(name)) + } + + Headers.prototype.set = function(name, value) { + this.map[normalizeName(name)] = [normalizeValue(value)] + } + + Headers.prototype.forEach = function(callback, thisArg) { + Object.getOwnPropertyNames(this.map).forEach(function(name) { + this.map[name].forEach(function(value) { + callback.call(thisArg, value, name, this) + }, this) + }, this) + } + + Headers.prototype.keys = function() { + var items = [] + this.forEach(function(value, name) { items.push(name) }) + return iteratorFor(items) + } + + Headers.prototype.values = function() { + var items = [] + this.forEach(function(value) { items.push(value) }) + return iteratorFor(items) + } + + Headers.prototype.entries = function() { + var items = [] + this.forEach(function(value, name) { items.push([name, value]) }) + return iteratorFor(items) + } + + if (support.iterable) { + Headers.prototype[Symbol.iterator] = Headers.prototype.entries + } + + function consumed(body) { + if (body.bodyUsed) { + return Promise.reject(new TypeError('Already read')) + } + body.bodyUsed = true + } + + function fileReaderReady(reader) { + return new Promise(function(resolve, reject) { + reader.onload = function() { + resolve(reader.result) + } + reader.onerror = function() { + reject(reader.error) + } + }) + } + + function readBlobAsArrayBuffer(blob) { + var reader = new FileReader() + reader.readAsArrayBuffer(blob) + return fileReaderReady(reader) + } + + function readBlobAsText(blob) { + var reader = new FileReader() + reader.readAsText(blob) + return fileReaderReady(reader) + } + + function Body() { + this.bodyUsed = false + + this._initBody = function(body) { + this._bodyInit = body + if (typeof body === 'string') { + this._bodyText = body + } else if (support.blob && Blob.prototype.isPrototypeOf(body)) { + this._bodyBlob = body + } else if (support.formData && FormData.prototype.isPrototypeOf(body)) { + this._bodyFormData = body + } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { + this._bodyText = body.toString() + } else if (!body) { + this._bodyText = '' + } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) { + // Only support ArrayBuffers for POST method. + // Receiving ArrayBuffers happens via Blobs, instead. + } else { + throw new Error('unsupported BodyInit type') + } + + if (!this.headers.get('content-type')) { + if (typeof body === 'string') { + this.headers.set('content-type', 'text/plain;charset=UTF-8') + } else if (this._bodyBlob && this._bodyBlob.type) { + this.headers.set('content-type', this._bodyBlob.type) + } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) { + this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8') + } + } + } + + if (support.blob) { + this.blob = function() { + var rejected = consumed(this) + if (rejected) { + return rejected + } + + if (this._bodyBlob) { + return Promise.resolve(this._bodyBlob) + } else if (this._bodyFormData) { + throw new Error('could not read FormData body as blob') + } else { + return Promise.resolve(new Blob([this._bodyText])) + } + } + + this.arrayBuffer = function() { + return this.blob().then(readBlobAsArrayBuffer) + } + + this.text = function() { + var rejected = consumed(this) + if (rejected) { + return rejected + } + + if (this._bodyBlob) { + return readBlobAsText(this._bodyBlob) + } else if (this._bodyFormData) { + throw new Error('could not read FormData body as text') + } else { + return Promise.resolve(this._bodyText) + } + } + } else { + this.text = function() { + var rejected = consumed(this) + return rejected ? rejected : Promise.resolve(this._bodyText) + } + } + + if (support.formData) { + this.formData = function() { + return this.text().then(decode) + } + } + + this.json = function() { + return this.text().then(JSON.parse) + } + + return this + } + + // HTTP methods whose capitalization should be normalized + var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'] + + function normalizeMethod(method) { + var upcased = method.toUpperCase() + return (methods.indexOf(upcased) > -1) ? upcased : method + } + + function Request(input, options) { + options = options || {} + var body = options.body + if (Request.prototype.isPrototypeOf(input)) { + if (input.bodyUsed) { + throw new TypeError('Already read') + } + this.url = input.url + this.credentials = input.credentials + if (!options.headers) { + this.headers = new Headers(input.headers) + } + this.method = input.method + this.mode = input.mode + if (!body) { + body = input._bodyInit + input.bodyUsed = true + } + } else { + this.url = input + } + + this.credentials = options.credentials || this.credentials || 'omit' + if (options.headers || !this.headers) { + this.headers = new Headers(options.headers) + } + this.method = normalizeMethod(options.method || this.method || 'GET') + this.mode = options.mode || this.mode || null + this.referrer = null + + if ((this.method === 'GET' || this.method === 'HEAD') && body) { + throw new TypeError('Body not allowed for GET or HEAD requests') + } + this._initBody(body) + } + + Request.prototype.clone = function() { + return new Request(this) + } + + function decode(body) { + var form = new FormData() + body.trim().split('&').forEach(function(bytes) { + if (bytes) { + var split = bytes.split('=') + var name = split.shift().replace(/\+/g, ' ') + var value = split.join('=').replace(/\+/g, ' ') + form.append(decodeURIComponent(name), decodeURIComponent(value)) + } + }) + return form + } + + function headers(xhr) { + var head = new Headers() + var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n') + pairs.forEach(function(header) { + var split = header.trim().split(':') + var key = split.shift().trim() + var value = split.join(':').trim() + head.append(key, value) + }) + return head + } + + Body.call(Request.prototype) + + function Response(bodyInit, options) { + if (!options) { + options = {} + } + + this.type = 'default' + this.status = options.status + this.ok = this.status >= 200 && this.status < 300 + this.statusText = options.statusText + this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers) + this.url = options.url || '' + this._initBody(bodyInit) + } + + Body.call(Response.prototype) + + Response.prototype.clone = function() { + return new Response(this._bodyInit, { + status: this.status, + statusText: this.statusText, + headers: new Headers(this.headers), + url: this.url + }) + } + + Response.error = function() { + var response = new Response(null, {status: 0, statusText: ''}) + response.type = 'error' + return response + } + + var redirectStatuses = [301, 302, 303, 307, 308] + + Response.redirect = function(url, status) { + if (redirectStatuses.indexOf(status) === -1) { + throw new RangeError('Invalid status code') + } + + return new Response(null, {status: status, headers: {location: url}}) + } + + self.Headers = Headers + self.Request = Request + self.Response = Response + + self.fetch = function(input, init) { + return new Promise(function(resolve, reject) { + var request + if (Request.prototype.isPrototypeOf(input) && !init) { + request = input + } else { + request = new Request(input, init) + } + + var xhr = new XMLHttpRequest() + + function responseURL() { + if ('responseURL' in xhr) { + return xhr.responseURL + } + + // Avoid security warnings on getResponseHeader when not allowed by CORS + if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) { + return xhr.getResponseHeader('X-Request-URL') + } + + return + } + + xhr.onload = function() { + var options = { + status: xhr.status, + statusText: xhr.statusText, + headers: headers(xhr), + url: responseURL() + } + var body = 'response' in xhr ? xhr.response : xhr.responseText + resolve(new Response(body, options)) + } + + xhr.onerror = function() { + reject(new TypeError('Network request failed')) + } + + xhr.ontimeout = function() { + reject(new TypeError('Network request failed')) + } + + xhr.open(request.method, request.url, true) + + if (request.credentials === 'include') { + xhr.withCredentials = true + } + + if ('responseType' in xhr && support.blob) { + xhr.responseType = 'blob' + } + + request.headers.forEach(function(value, name) { + xhr.setRequestHeader(name, value) + }) + + xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit) + }) + } + self.fetch.polyfill = true +})(typeof self !== 'undefined' ? self : this);
\ No newline at end of file diff --git a/testing/web-platform/tests/encrypted-media/util/testmediasource.js b/testing/web-platform/tests/encrypted-media/util/testmediasource.js new file mode 100644 index 000000000..62c2d577d --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/testmediasource.js @@ -0,0 +1,43 @@ +function testmediasource(config) { + + return new Promise(function(resolve, reject) { + // Fetch the media resources + var fetches = [config.audioPath, config.videoPath].map(function(path) { + return fetch(path).then(function(response) { + if (!response.ok) throw new Error('Resource fetch failed'); + return response.arrayBuffer(); + }); + }); + + Promise.all(fetches).then(function(resources) { + config.audioMedia = resources[0]; + config.videoMedia = resources[1]; + + // Create media source + var source = new MediaSource(); + + // Create and fill source buffers when the media source is opened + source.addEventListener('sourceopen', onSourceOpen); + + function onSourceOpen(event) { + var audioSourceBuffer = source.addSourceBuffer(config.audioType), + videoSourceBuffer = source.addSourceBuffer(config.videoType); + + audioSourceBuffer.appendBuffer(config.audioMedia); + videoSourceBuffer.appendBuffer(config.videoMedia); + + function endOfStream() { + if (audioSourceBuffer.updating || videoSourceBuffer.updating) { + setTimeout(endOfStream, 250); + } else { + source.endOfStream(); + } + } + + endOfStream(); + } + + resolve(source); + }); + }); +}
\ No newline at end of file diff --git a/testing/web-platform/tests/encrypted-media/util/utf8.js b/testing/web-platform/tests/encrypted-media/util/utf8.js new file mode 100644 index 000000000..5b1176013 --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/utf8.js @@ -0,0 +1,22 @@ +if ( typeof TextEncoder !== "undefined" && typeof TextDecoder !== "undefined" ) +{ + utf8encoder = new TextEncoder('utf-8'); + utf8decoder = new TextDecoder('utf-8'); +} +else +{ + utf8encoder = { encode: function( text ) + { + var result = new Uint8Array(text.length); + for(var i = 0; i < text.length; i++) { result[i] = text.charCodeAt(i); } + return result; + } }; + + utf8decoder = { decode: function( buffer ) + { + return String.fromCharCode.apply(null, new Uint8Array(buffer)); + } }; +} + +toUtf8 = function( o ) { return utf8encoder.encode( JSON.stringify( o ) ); } +fromUtf8 = function( t ) { return JSON.parse( utf8decoder.decode( t ) ); }
\ No newline at end of file diff --git a/testing/web-platform/tests/encrypted-media/util/utils.js b/testing/web-platform/tests/encrypted-media/util/utils.js new file mode 100644 index 000000000..98ac8c44a --- /dev/null +++ b/testing/web-platform/tests/encrypted-media/util/utils.js @@ -0,0 +1,271 @@ +function testnamePrefix( qualifier, keysystem ) { + return ( qualifier || '' ) + ( keysystem === 'org.w3.clearkey' ? keysystem : 'drm' ); +} + +function getInitData(initDataType) { + + // FIXME: This is messed up, because here we are hard coding the key ids for the different content + // that we use for clearkey testing: webm and mp4. For keyids we return the mp4 one + // + // The content used with the DRM today servers has a different key id altogether + + if (initDataType == 'webm') { + return new Uint8Array([ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + ]); + } + + if (initDataType == 'cenc') { + return new Uint8Array([ + 0x00, 0x00, 0x00, 0x34, // size + 0x70, 0x73, 0x73, 0x68, // 'pssh' + 0x01, // version = 1 + 0x00, 0x00, 0x00, // flags + 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // Common SystemID + 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B, + 0x00, 0x00, 0x00, 0x01, // key count + 0x00, 0x00, 0x00, 0x00, 0x03, 0xd2, 0xfc, 0x41, // key id + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 // datasize + ]); + } + if (initDataType == 'keyids') { + var keyId = new Uint8Array([ + 0x00, 0x00, 0x00, 0x00, 0x03, 0xd2, 0xfc, 0x41, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + ]); + return stringToUint8Array(createKeyIDs(keyId)); + } + throw 'initDataType ' + initDataType + ' not supported.'; +} + +function stringToUint8Array(str) +{ + var result = new Uint8Array(str.length); + for(var i = 0; i < str.length; i++) { + result[i] = str.charCodeAt(i); + } + return result; +} +// Encodes |data| into base64url string. There is no '=' padding, and the +// characters '-' and '_' must be used instead of '+' and '/', respectively. +function base64urlEncode(data) { + var result = btoa(String.fromCharCode.apply(null, data)); + return result.replace(/=+$/g, '').replace(/\+/g, "-").replace(/\//g, "_"); +} +// Decode |encoded| using base64url decoding. +function base64urlDecode(encoded) { + return atob(encoded.replace(/\-/g, "+").replace(/\_/g, "/")); +} +// Decode |encoded| using base64 to a Uint8Array +function base64DecodeToUnit8Array(encoded) { + return new Uint8Array( atob( encoded ).split('').map( function(c){return c.charCodeAt(0);} ) ); +} +// Clear Key can also support Key IDs Initialization Data. +// ref: http://w3c.github.io/encrypted-media/keyids-format.html +// Each parameter is expected to be a key id in an Uint8Array. +function createKeyIDs() { + var keyIds = '{"kids":["'; + for (var i = 0; i < arguments.length; i++) { + if (i != 0) keyIds += '","'; + keyIds += base64urlEncode(arguments[i]); + } + keyIds += '"]}'; + return keyIds; +} + +function getSupportedKeySystem() { + var userAgent = navigator.userAgent.toLowerCase(); + var keysystem = undefined; + if (userAgent.indexOf('edge') > -1 ) { + keysystem = 'com.microsoft.playready'; + } else if ( userAgent.indexOf('chrome') > -1 || userAgent.indexOf('firefox') > -1 ) { + keysystem = 'com.widevine.alpha'; + } + return keysystem; +} + +function waitForEventAndRunStep(eventName, element, func, stepTest) +{ + var eventCallback = function(event) { + if (func) + func(event); + } + + element.addEventListener(eventName, stepTest.step_func(eventCallback), true); +} + +function waitForEvent(eventName, element) { + return new Promise(function(resolve) { + element.addEventListener(eventName, resolve, true); + }) +} + +var consoleDiv = null; + +function consoleWrite(text) +{ + if (!consoleDiv && document.body) { + consoleDiv = document.createElement('div'); + document.body.appendChild(consoleDiv); + } + var span = document.createElement('span'); + span.appendChild(document.createTextNode(text)); + span.appendChild(document.createElement('br')); + consoleDiv.appendChild(span); +} + +function forceTestFailureFromPromise(test, error, message) +{ + // Promises convert exceptions into rejected Promises. Since there is + // currently no way to report a failed test in the test harness, errors + // are reported using force_timeout(). + if (message) + consoleWrite(message + ': ' + error.message); + else if (error) + consoleWrite(error); + + test.force_timeout(); + test.done(); +} + +// Returns an array of audioCapabilities that includes entries for a set of +// codecs that should cover all user agents. +function getPossibleAudioCapabilities() +{ + return [ + { contentType: 'audio/mp4; codecs="mp4a.40.2"' }, + { contentType: 'audio/webm; codecs="opus"' }, + ]; +} + +// Returns a trivial MediaKeySystemConfiguration that should be accepted, +// possibly as a subset of the specified capabilities, by all user agents. +function getSimpleConfiguration() +{ + return [ { + initDataTypes : [ 'webm', 'cenc', 'keyids' ], + audioCapabilities: getPossibleAudioCapabilities() + } ]; +} + +// Returns a MediaKeySystemConfiguration for |initDataType| that should be +// accepted, possibly as a subset of the specified capabilities, by all +// user agents. +function getSimpleConfigurationForInitDataType(initDataType) +{ + return [ { + initDataTypes: [ initDataType ], + audioCapabilities: getPossibleAudioCapabilities() + } ]; +} + +// Returns a promise that is fulfilled with true if |initDataType| is supported, +// by keysystem or false if not. +function isInitDataTypeSupported(keysystem,initDataType) +{ + return navigator.requestMediaKeySystemAccess( + keysystem, getSimpleConfigurationForInitDataType(initDataType)) + .then(function() { return true; }, function() { return false; }); +} + +function getSupportedInitDataTypes( keysystem ) +{ + return [ 'cenc', 'keyids', 'webm' ].filter( isInitDataTypeSupported.bind( null, keysystem ) ); +} + +function arrayBufferAsString(buffer) +{ + var array = []; + Array.prototype.push.apply( array, new Uint8Array( buffer ) ); + return '0x' + array.map( function( x ) { return x < 16 ? '0'+x.toString(16) : x.toString(16); } ).join(''); +} + +function dumpKeyStatuses(keyStatuses) +{ + var userAgent = navigator.userAgent.toLowerCase(); + if (userAgent.indexOf('edge') === -1) { + consoleWrite("for (var entry of keyStatuses)"); + for (var entry of keyStatuses) { + consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]); + } + consoleWrite("for (var keyId of keyStatuses.keys())"); + for (var keyId of keyStatuses.keys()) { + consoleWrite(arrayBufferAsString(keyId)); + } + consoleWrite("for (var status of keyStatuses.values())"); + for (var status of keyStatuses.values()) { + consoleWrite(status); + } + consoleWrite("for (var entry of keyStatuses.entries())"); + for (var entry of keyStatuses.entries()) { + consoleWrite(arrayBufferAsString(entry[0]) + ": " + entry[1]); + } + consoleWrite("keyStatuses.forEach()"); + keyStatuses.forEach(function(status, keyId) { + consoleWrite(arrayBufferAsString(keyId) + ": " + status); + }); + } else { + consoleWrite("keyStatuses.forEach()"); + keyStatuses.forEach(function(keyId, status) { + consoleWrite(arrayBufferAsString(keyId) + ": " + status); + }); + } +} + +// Verify that |keyStatuses| contains just the keys in |keys.expected| +// and none of the keys in |keys.unexpected|. All keys should have status +// 'usable'. Example call: verifyKeyStatuses(mediaKeySession.keyStatuses, +// { expected: [key1], unexpected: [key2] }); +function verifyKeyStatuses(keyStatuses, keys) +{ + var expected = keys.expected || []; + var unexpected = keys.unexpected || []; + + // |keyStatuses| should have same size as number of |keys.expected|. + assert_equals(keyStatuses.size, expected.length, "keystatuses should have expected size"); + + // All |keys.expected| should be found. + expected.map(function(key) { + assert_true(keyStatuses.has(key), "keystatuses should have the expected keys"); + assert_equals(keyStatuses.get(key), 'usable', "keystatus value should be 'usable'"); + }); + + // All |keys.unexpected| should not be found. + unexpected.map(function(key) { + assert_false(keyStatuses.has(key), "keystatuses should not have unexpected keys"); + assert_equals(keyStatuses.get(key), undefined, "keystatus for unexpected key should be undefined"); + }); +} + +// This function checks that calling |testCase.func| returns a +// rejected Promise with the error.name equal to +// |testCase.exception|. +function test_exception(testCase /*...*/) { + var func = testCase.func; + var exception = testCase.exception; + var args = Array.prototype.slice.call(arguments, 1); + + // Currently blink throws for TypeErrors rather than returning + // a rejected promise (http://crbug.com/359386). + // FIXME: Remove try/catch once they become failed promises. + try { + return func.apply(null, args).then( + function (result) { + assert_unreached(format_value(func)); + }, + function (error) { + assert_equals(error.name, exception, format_value(func)); + assert_not_equals(error.message, "", format_value(func)); + } + ); + } catch (e) { + // Only allow 'TypeError' exceptions to be thrown. + // Everything else should be a failed promise. + assert_equals('TypeError', exception, format_value(func)); + assert_equals(e.name, exception, format_value(func)); + } +} + + |