decompression-bad-chunks.any.js (2520B)
1 // META: global=window,worker,shadowrealm 2 // META: script=resources/formats.js 3 4 'use strict'; 5 6 const badChunks = [ 7 { 8 name: 'undefined', 9 value: undefined 10 }, 11 { 12 name: 'null', 13 value: null 14 }, 15 { 16 name: 'numeric', 17 value: 3.14 18 }, 19 { 20 name: 'object, not BufferSource', 21 value: {} 22 }, 23 { 24 name: 'array', 25 value: [65] 26 }, 27 { 28 name: 'SharedArrayBuffer', 29 // Use a getter to postpone construction so that all tests don't fail where 30 // SharedArrayBuffer is not yet implemented. 31 get value() { 32 // See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()` 33 return new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer; 34 } 35 }, 36 { 37 name: 'shared Uint8Array', 38 get value() { 39 // See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()` 40 return new Uint8Array(new WebAssembly.Memory({ shared:true, initial:1, maximum:1 }).buffer) 41 } 42 }, 43 { 44 name: 'invalid deflate bytes', 45 value: new Uint8Array([0, 156, 75, 173, 40, 72, 77, 46, 73, 77, 81, 200, 47, 45, 41, 40, 45, 1, 0, 48, 173, 6, 36]) 46 }, 47 { 48 name: 'invalid gzip bytes', 49 value: new Uint8Array([0, 139, 8, 0, 0, 0, 0, 0, 0, 3, 75, 173, 40, 72, 77, 46, 73, 77, 81, 200, 47, 45, 41, 40, 45, 1, 0, 176, 1, 57, 179, 15, 0, 0, 0]) 50 }, 51 ]; 52 53 // Test Case Design 54 // We need to wait until after we close the writable stream to check if the decoded stream is valid. 55 // We can end up in a state where all reads/writes are valid, but upon closing the writable stream an error is detected. 56 // (Example: A zlib encoded chunk w/o the checksum). 57 58 async function decompress(chunk, format, t) 59 { 60 const ds = new DecompressionStream(format); 61 const reader = ds.readable.getReader(); 62 const writer = ds.writable.getWriter(); 63 64 writer.write(chunk.value).then(() => {}, () => {}); 65 reader.read().then(() => {}, () => {}); 66 67 await promise_rejects_js(t, TypeError, writer.close(), 'writer.close() should reject'); 68 await promise_rejects_js(t, TypeError, writer.closed, 'write.closed should reject'); 69 70 await promise_rejects_js(t, TypeError, reader.read(), 'reader.read() should reject'); 71 await promise_rejects_js(t, TypeError, reader.closed, 'read.closed should reject'); 72 } 73 74 for (const format of formats) { 75 for (const chunk of badChunks) { 76 promise_test(async t => { 77 await decompress(chunk, format, t); 78 }, `chunk of type ${chunk.name} should error the stream for ${format}`); 79 } 80 }