tor-browser

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

import-tests.js (7489B)


      1 // Runs a series of tests related to importing scripts on a worklet.
      2 //
      3 // Usage:
      4 // runImportTests("paint");
      5 function runImportTests(worklet_type) {
      6    const worklet = get_worklet(worklet_type);
      7 
      8    promise_test(() => {
      9        const kScriptURL = 'resources/empty-worklet-script.js';
     10        return worklet.addModule(kScriptURL).then(undefined_arg => {
     11            assert_equals(undefined_arg, undefined,
     12                          'Promise should resolve with no arguments.');
     13        });
     14    }, 'Importing a script resolves the given promise.');
     15 
     16    promise_test(() => {
     17        const kScriptURL = 'resources/empty-worklet-script.js';
     18        return Promise.all([
     19            worklet.addModule(kScriptURL + '?1'),
     20            worklet.addModule(kScriptURL + '?2'),
     21            worklet.addModule(kScriptURL + '?3')
     22        ]).then(undefined_args => {
     23            assert_array_equals(undefined_args,
     24                                [undefined, undefined, undefined],
     25                                'Promise should resolve with no arguments.');
     26        });
     27    }, 'Importing scripts resolves all the given promises.');
     28 
     29    promise_test(() => {
     30        const kScriptURL = 'resources/import-nested-worklet-script.js';
     31        return worklet.addModule(kScriptURL).then(undefined_arg => {
     32            assert_equals(undefined_arg, undefined,
     33                          'Promise should resolve with no arguments.');
     34        });
     35    }, 'Importing nested scripts resolves the given promise');
     36 
     37    promise_test(() => {
     38        const kScriptURL = 'resources/import-cyclic-worklet-script.js';
     39        return worklet.addModule(kScriptURL).then(undefined_arg => {
     40            assert_equals(undefined_arg, undefined,
     41                          'Promise should resolve with no arguments.');
     42        });
     43    }, 'Importing cyclic scripts resolves the given promise');
     44 
     45    promise_test(() => {
     46        const kScriptURL = 'resources/throwing-worklet-script.js';
     47        return worklet.addModule(kScriptURL).then(undefined_arg => {
     48            assert_equals(undefined_arg, undefined,
     49                          'Promise should resolve with no arguments.');
     50        });
     51    }, 'Importing a script which throws should still resolve the given ' +
     52       'promise.');
     53 
     54    promise_test(t => {
     55        const kScriptURL = 'non-existent-worklet-script.js';
     56        return promise_rejects_dom(t, 'AbortError',
     57                                   worklet.addModule(kScriptURL));
     58    }, 'Importing a non-existent script rejects the given promise with an ' +
     59       'AbortError.');
     60 
     61    promise_test(t => {
     62        const kInvalidScriptURL = 'http://invalid:123$'
     63        return promise_rejects_dom(t, 'SyntaxError',
     64                                   worklet.addModule(kInvalidScriptURL));
     65    }, 'Importing an invalid URL should reject the given promise with a ' +
     66       'SyntaxError.');
     67 
     68    promise_test(() => {
     69        const kBlob = new Blob([""], {type: 'text/javascript'});
     70        const kBlobURL = URL.createObjectURL(kBlob);
     71        return worklet.addModule(kBlobURL).then(undefined_arg => {
     72            assert_equals(undefined_arg, undefined);
     73        });
     74    }, 'Importing a blob URL should resolve the given promise.');
     75 
     76    promise_test(t => {
     77        const kFileURL = 'file:///empty-worklet-script.js';
     78        return promise_rejects_dom(t, 'AbortError',
     79                                   worklet.addModule(kFileURL));
     80    }, 'Importing a file:// URL should reject the given promise.');
     81 
     82    promise_test(() => {
     83        const kDataURL = 'data:text/javascript, // Do nothing.';
     84        return worklet.addModule(kDataURL).then(undefined_arg => {
     85            assert_equals(undefined_arg, undefined);
     86        });
     87    }, 'Importing a data URL should resolve the given promise.');
     88 
     89    promise_test(t => {
     90        const kScriptURL = 'about:blank';
     91        return promise_rejects_dom(t, 'AbortError',
     92                                   worklet.addModule(kScriptURL));
     93    }, 'Importing about:blank should reject the given promise.');
     94 
     95    promise_test(() => {
     96        // Specify the Access-Control-Allow-Origin header to enable cross origin
     97        // access.
     98        const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
     99                           '/worklets/resources/empty-worklet-script.js' +
    100                           '?pipe=header(Access-Control-Allow-Origin, *)';
    101        return worklet.addModule(kScriptURL).then(undefined_arg => {
    102            assert_equals(undefined_arg, undefined);
    103        });
    104    }, 'Importing a cross origin resource with the ' +
    105       'Access-Control-Allow-Origin header should resolve the given promise');
    106 
    107    promise_test(t => {
    108        // Don't specify the Access-Control-Allow-Origin header. addModule()
    109        // should be rejected because of disallowed cross origin access.
    110        const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
    111                           '/worklets/resources/empty-worklet-script.js';
    112        return promise_rejects_dom(t, 'AbortError',
    113                                   worklet.addModule(kScriptURL));
    114    }, 'Importing a cross origin resource without the ' +
    115       'Access-Control-Allow-Origin header should reject the given promise');
    116 
    117    promise_test(() => {
    118        const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
    119                           '/worklets/resources/empty-worklet-script.js' +
    120                           '?pipe=header(Access-Control-Allow-Origin, ' +
    121                           location.origin + ')';
    122        return worklet.addModule('/common/redirect.py?location=' +
    123                                 encodeURIComponent(kScriptURL))
    124          .then(undefined_arg => {
    125              assert_equals(undefined_arg, undefined);
    126          });
    127    }, 'Importing a cross-origin-redirected resource with the ' +
    128       'Access-Control-Allow-Origin header should resolve the given promise');
    129 
    130    promise_test(t => {
    131        const kScriptURL = get_host_info().HTTPS_REMOTE_ORIGIN +
    132                           '/worklets/resources/empty-worklet-script.js';
    133        return promise_rejects_dom(t, 'AbortError',
    134                                   worklet.addModule(
    135                                       '/common/redirect.py?location=' +
    136                                       encodeURIComponent(kScriptURL)));
    137    }, 'Importing a cross-origin-redirected resource without the ' +
    138       'Access-Control-Allow-Origin header should reject the given promise');
    139 
    140    promise_test(t => {
    141        const kScriptURL = 'resources/syntax-error-worklet-script.js';
    142        return promise_rejects_js(t, SyntaxError,
    143                                  worklet.addModule(kScriptURL));
    144    }, 'Importing a script that has a syntax error should reject the given ' +
    145       'promise.');
    146 
    147    promise_test(t => {
    148        const kScriptURL = 'resources/import-syntax-error-worklet-script.js';
    149        return promise_rejects_js(t, SyntaxError,
    150                                  worklet.addModule(kScriptURL));
    151    }, 'Importing a nested script that has a syntax error should reject the ' +
    152       'given promise.');
    153 
    154    promise_test(t => {
    155        const kBlob = new Blob(["import 'invalid-specifier.js';"],
    156                               {type: 'text/javascript'});
    157        const kBlobURL = URL.createObjectURL(kBlob);
    158        return promise_rejects_js(t, TypeError,
    159                                  worklet.addModule(kBlobURL));
    160    }, 'Importing a script that imports an invalid identifier should reject ' +
    161       'the given promise.');
    162 }