tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

validate.any.js (2598B)


      1 // META: global=window,dedicatedworker,jsshell,shadowrealm
      2 // META: script=/wasm/jsapi/wasm-module-builder.js
      3 
      4 let emptyModuleBinary;
      5 setup(() => {
      6  emptyModuleBinary = new WasmModuleBuilder().toBuffer();
      7 });
      8 
      9 test(() => {
     10  assert_throws_js(TypeError, () => WebAssembly.validate());
     11 }, "Missing argument");
     12 
     13 test(() => {
     14  const invalidArguments = [
     15    undefined,
     16    null,
     17    true,
     18    "",
     19    Symbol(),
     20    1,
     21    {},
     22    ArrayBuffer,
     23    ArrayBuffer.prototype,
     24    Array.from(emptyModuleBinary),
     25  ];
     26  for (const argument of invalidArguments) {
     27    assert_throws_js(TypeError, () => WebAssembly.validate(argument),
     28                     `validate(${format_value(argument)})`);
     29  }
     30 }, "Invalid arguments");
     31 
     32 test(() => {
     33  const fn = WebAssembly.validate;
     34  const thisValues = [
     35    undefined,
     36    null,
     37    true,
     38    "",
     39    Symbol(),
     40    1,
     41    {},
     42    WebAssembly,
     43  ];
     44  for (const thisValue of thisValues) {
     45    assert_true(fn.call(thisValue, emptyModuleBinary), `this=${format_value(thisValue)}`);
     46  }
     47 }, "Branding");
     48 
     49 const modules = [
     50  // Incomplete header.
     51  [[], false],
     52  [[0x00], false],
     53  [[0x00, 0x61], false],
     54  [[0x00, 0x61, 0x73], false],
     55  [[0x00, 0x61, 0x73, 0x6d], false],
     56  [[0x00, 0x61, 0x73, 0x6d, 0x01], false],
     57  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00], false],
     58  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00], false],
     59 
     60  // Complete header.
     61  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00], true],
     62 
     63  // Invalid version.
     64  [[0x00, 0x61, 0x73, 0x6d, 0x00, 0x00, 0x00, 0x00], false],
     65  [[0x00, 0x61, 0x73, 0x6d, 0x02, 0x00, 0x00, 0x00], false],
     66 
     67  // Nameless custom section.
     68  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00], false],
     69 
     70  // Custom section with empty name.
     71  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00], true],
     72 
     73  // Custom section with name "a".
     74  [[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x61], true],
     75 ];
     76 const bufferTypes = [
     77  Uint8Array,
     78  Int8Array,
     79  Uint16Array,
     80  Int16Array,
     81  Uint32Array,
     82  Int32Array,
     83 ];
     84 for (const [module, expected] of modules) {
     85  const name = module.map(n => n.toString(16)).join(" ");
     86  for (const bufferType of bufferTypes) {
     87    if (module.length % bufferType.BYTES_PER_ELEMENT === 0) {
     88      test(() => {
     89        const bytes = new Uint8Array(module);
     90        const moduleBuffer = new bufferType(bytes.buffer);
     91        assert_equals(WebAssembly.validate(moduleBuffer), expected);
     92      }, `Validating module [${name}] in ${bufferType.name}`);
     93    }
     94  }
     95 }
     96 
     97 test(() => {
     98  assert_true(WebAssembly.validate(emptyModuleBinary, {}));
     99 }, "Stray argument");