11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto');
71cb0ef41Sopenharmony_cicommon.requireNoPackageJSONAbove();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst Manifest = require('internal/policy/manifest').Manifest;
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// #region files
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  const baseURLs = [
151cb0ef41Sopenharmony_ci    // Localhost is special cased in spec
161cb0ef41Sopenharmony_ci    'file://localhost/root',
171cb0ef41Sopenharmony_ci    'file:///root',
181cb0ef41Sopenharmony_ci    'file:///',
191cb0ef41Sopenharmony_ci    'file:///root/dir1',
201cb0ef41Sopenharmony_ci    'file:///root/dir1/',
211cb0ef41Sopenharmony_ci    'file:///root/dir1/dir2',
221cb0ef41Sopenharmony_ci    'file:///root/dir1/dir2/',
231cb0ef41Sopenharmony_ci  ];
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  {
261cb0ef41Sopenharmony_ci    const manifest = new Manifest({
271cb0ef41Sopenharmony_ci      scopes: {
281cb0ef41Sopenharmony_ci        'file:///': {
291cb0ef41Sopenharmony_ci          dependencies: true
301cb0ef41Sopenharmony_ci        }
311cb0ef41Sopenharmony_ci      }
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
351cb0ef41Sopenharmony_ci      assert.strictEqual(
361cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
371cb0ef41Sopenharmony_ci        true
381cb0ef41Sopenharmony_ci      );
391cb0ef41Sopenharmony_ci    }
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci  {
421cb0ef41Sopenharmony_ci    const manifest = new Manifest({
431cb0ef41Sopenharmony_ci      scopes: {
441cb0ef41Sopenharmony_ci        '': {
451cb0ef41Sopenharmony_ci          dependencies: true
461cb0ef41Sopenharmony_ci        }
471cb0ef41Sopenharmony_ci      }
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
511cb0ef41Sopenharmony_ci      assert.strictEqual(
521cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
531cb0ef41Sopenharmony_ci        true
541cb0ef41Sopenharmony_ci      );
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci  {
581cb0ef41Sopenharmony_ci    const manifest = new Manifest({
591cb0ef41Sopenharmony_ci      scopes: {
601cb0ef41Sopenharmony_ci        '': {
611cb0ef41Sopenharmony_ci          dependencies: true
621cb0ef41Sopenharmony_ci        },
631cb0ef41Sopenharmony_ci        'file:': {
641cb0ef41Sopenharmony_ci          cascade: true
651cb0ef41Sopenharmony_ci        }
661cb0ef41Sopenharmony_ci      }
671cb0ef41Sopenharmony_ci    });
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
701cb0ef41Sopenharmony_ci      assert.strictEqual(
711cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
721cb0ef41Sopenharmony_ci        true
731cb0ef41Sopenharmony_ci      );
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci  {
771cb0ef41Sopenharmony_ci    const manifest = new Manifest({
781cb0ef41Sopenharmony_ci      scopes: {
791cb0ef41Sopenharmony_ci        'file:': {
801cb0ef41Sopenharmony_ci          dependencies: true
811cb0ef41Sopenharmony_ci        }
821cb0ef41Sopenharmony_ci      }
831cb0ef41Sopenharmony_ci    });
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
861cb0ef41Sopenharmony_ci      assert.strictEqual(
871cb0ef41Sopenharmony_ci        manifest
881cb0ef41Sopenharmony_ci          .getDependencyMapper(href)
891cb0ef41Sopenharmony_ci          .resolve('fs'),
901cb0ef41Sopenharmony_ci        true);
911cb0ef41Sopenharmony_ci    }
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci    assert.strictEqual(
941cb0ef41Sopenharmony_ci      manifest
951cb0ef41Sopenharmony_ci        .getDependencyMapper('file://host/')
961cb0ef41Sopenharmony_ci        .resolve('fs'),
971cb0ef41Sopenharmony_ci      true);
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci  {
1001cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1011cb0ef41Sopenharmony_ci      resources: {
1021cb0ef41Sopenharmony_ci        'file:///root/dir1': {
1031cb0ef41Sopenharmony_ci          dependencies: {
1041cb0ef41Sopenharmony_ci            fs: 'test:fs1'
1051cb0ef41Sopenharmony_ci          }
1061cb0ef41Sopenharmony_ci        },
1071cb0ef41Sopenharmony_ci        'file:///root/dir1/isolated': {},
1081cb0ef41Sopenharmony_ci        'file:///root/dir1/cascade': {
1091cb0ef41Sopenharmony_ci          cascade: true
1101cb0ef41Sopenharmony_ci        }
1111cb0ef41Sopenharmony_ci      },
1121cb0ef41Sopenharmony_ci      scopes: {
1131cb0ef41Sopenharmony_ci        'file:///root/dir1/': {
1141cb0ef41Sopenharmony_ci          dependencies: {
1151cb0ef41Sopenharmony_ci            fs: 'test:fs2'
1161cb0ef41Sopenharmony_ci          }
1171cb0ef41Sopenharmony_ci        },
1181cb0ef41Sopenharmony_ci        'file:///root/dir1/censor/': {
1191cb0ef41Sopenharmony_ci        },
1201cb0ef41Sopenharmony_ci      }
1211cb0ef41Sopenharmony_ci    });
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1241cb0ef41Sopenharmony_ci      const redirector = manifest.getDependencyMapper(href);
1251cb0ef41Sopenharmony_ci      if (href.startsWith('file:///root/dir1/')) {
1261cb0ef41Sopenharmony_ci        assert.strictEqual(
1271cb0ef41Sopenharmony_ci          redirector.resolve('fs').href,
1281cb0ef41Sopenharmony_ci          'test:fs2'
1291cb0ef41Sopenharmony_ci        );
1301cb0ef41Sopenharmony_ci      } else if (href === 'file:///root/dir1') {
1311cb0ef41Sopenharmony_ci        assert.strictEqual(
1321cb0ef41Sopenharmony_ci          redirector.resolve('fs').href,
1331cb0ef41Sopenharmony_ci          'test:fs1'
1341cb0ef41Sopenharmony_ci        );
1351cb0ef41Sopenharmony_ci      } else {
1361cb0ef41Sopenharmony_ci        assert.strictEqual(redirector.resolve('fs'), null);
1371cb0ef41Sopenharmony_ci      }
1381cb0ef41Sopenharmony_ci    }
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci    assert.strictEqual(
1411cb0ef41Sopenharmony_ci      manifest
1421cb0ef41Sopenharmony_ci        .getDependencyMapper('file:///root/dir1/isolated')
1431cb0ef41Sopenharmony_ci        .resolve('fs'),
1441cb0ef41Sopenharmony_ci      null
1451cb0ef41Sopenharmony_ci    );
1461cb0ef41Sopenharmony_ci    assert.strictEqual(
1471cb0ef41Sopenharmony_ci      manifest
1481cb0ef41Sopenharmony_ci        .getDependencyMapper('file:///root/dir1/cascade')
1491cb0ef41Sopenharmony_ci        .resolve('fs').href,
1501cb0ef41Sopenharmony_ci      'test:fs2'
1511cb0ef41Sopenharmony_ci    );
1521cb0ef41Sopenharmony_ci    assert.strictEqual(
1531cb0ef41Sopenharmony_ci      manifest
1541cb0ef41Sopenharmony_ci        .getDependencyMapper('file:///root/dir1/censor/foo')
1551cb0ef41Sopenharmony_ci        .resolve('fs'),
1561cb0ef41Sopenharmony_ci      null
1571cb0ef41Sopenharmony_ci    );
1581cb0ef41Sopenharmony_ci  }
1591cb0ef41Sopenharmony_ci}
1601cb0ef41Sopenharmony_ci// #endregion
1611cb0ef41Sopenharmony_ci// #region data
1621cb0ef41Sopenharmony_ci{
1631cb0ef41Sopenharmony_ci  const baseURLs = [
1641cb0ef41Sopenharmony_ci    'data:text/javascript,0',
1651cb0ef41Sopenharmony_ci    'data:text/javascript,0/1',
1661cb0ef41Sopenharmony_ci  ];
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci  {
1691cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1701cb0ef41Sopenharmony_ci      scopes: {
1711cb0ef41Sopenharmony_ci        'data:text/': {
1721cb0ef41Sopenharmony_ci          dependencies: {
1731cb0ef41Sopenharmony_ci            fs: true
1741cb0ef41Sopenharmony_ci          }
1751cb0ef41Sopenharmony_ci        }
1761cb0ef41Sopenharmony_ci      }
1771cb0ef41Sopenharmony_ci    });
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1801cb0ef41Sopenharmony_ci      assert.strictEqual(
1811cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
1821cb0ef41Sopenharmony_ci        null);
1831cb0ef41Sopenharmony_ci    }
1841cb0ef41Sopenharmony_ci  }
1851cb0ef41Sopenharmony_ci  {
1861cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1871cb0ef41Sopenharmony_ci      scopes: {
1881cb0ef41Sopenharmony_ci        'data:/': {
1891cb0ef41Sopenharmony_ci          dependencies: {
1901cb0ef41Sopenharmony_ci            fs: true
1911cb0ef41Sopenharmony_ci          }
1921cb0ef41Sopenharmony_ci        }
1931cb0ef41Sopenharmony_ci      }
1941cb0ef41Sopenharmony_ci    });
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1971cb0ef41Sopenharmony_ci      assert.strictEqual(
1981cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
1991cb0ef41Sopenharmony_ci        null);
2001cb0ef41Sopenharmony_ci    }
2011cb0ef41Sopenharmony_ci  }
2021cb0ef41Sopenharmony_ci  {
2031cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2041cb0ef41Sopenharmony_ci      scopes: {
2051cb0ef41Sopenharmony_ci        'data:': {
2061cb0ef41Sopenharmony_ci          dependencies: true
2071cb0ef41Sopenharmony_ci        }
2081cb0ef41Sopenharmony_ci      }
2091cb0ef41Sopenharmony_ci    });
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
2121cb0ef41Sopenharmony_ci      assert.strictEqual(
2131cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
2141cb0ef41Sopenharmony_ci        true
2151cb0ef41Sopenharmony_ci      );
2161cb0ef41Sopenharmony_ci    }
2171cb0ef41Sopenharmony_ci  }
2181cb0ef41Sopenharmony_ci  {
2191cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2201cb0ef41Sopenharmony_ci      scopes: {
2211cb0ef41Sopenharmony_ci        'data:text/javascript,0/': {
2221cb0ef41Sopenharmony_ci          dependencies: {
2231cb0ef41Sopenharmony_ci            fs: 'test:fs1'
2241cb0ef41Sopenharmony_ci          }
2251cb0ef41Sopenharmony_ci        },
2261cb0ef41Sopenharmony_ci      }
2271cb0ef41Sopenharmony_ci    });
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
2301cb0ef41Sopenharmony_ci      assert.strictEqual(
2311cb0ef41Sopenharmony_ci        manifest.getDependencyMapper(href).resolve('fs'),
2321cb0ef41Sopenharmony_ci        null);
2331cb0ef41Sopenharmony_ci    }
2341cb0ef41Sopenharmony_ci  }
2351cb0ef41Sopenharmony_ci}
2361cb0ef41Sopenharmony_ci// #endregion
2371cb0ef41Sopenharmony_ci// #region blob
2381cb0ef41Sopenharmony_ci{
2391cb0ef41Sopenharmony_ci  {
2401cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2411cb0ef41Sopenharmony_ci      scopes: {
2421cb0ef41Sopenharmony_ci        'https://example.com/': {
2431cb0ef41Sopenharmony_ci          dependencies: true
2441cb0ef41Sopenharmony_ci        }
2451cb0ef41Sopenharmony_ci      }
2461cb0ef41Sopenharmony_ci    });
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci    assert.strictEqual(
2491cb0ef41Sopenharmony_ci      manifest
2501cb0ef41Sopenharmony_ci          .getDependencyMapper('blob:https://example.com/has-origin')
2511cb0ef41Sopenharmony_ci          .resolve('fs'),
2521cb0ef41Sopenharmony_ci      true
2531cb0ef41Sopenharmony_ci    );
2541cb0ef41Sopenharmony_ci  }
2551cb0ef41Sopenharmony_ci  {
2561cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2571cb0ef41Sopenharmony_ci      scopes: {
2581cb0ef41Sopenharmony_ci        'https://example.com': {
2591cb0ef41Sopenharmony_ci          dependencies: true
2601cb0ef41Sopenharmony_ci        }
2611cb0ef41Sopenharmony_ci      }
2621cb0ef41Sopenharmony_ci    });
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci    assert.strictEqual(
2651cb0ef41Sopenharmony_ci      manifest
2661cb0ef41Sopenharmony_ci          .getDependencyMapper('blob:https://example.com/has-origin')
2671cb0ef41Sopenharmony_ci          .resolve('fs'),
2681cb0ef41Sopenharmony_ci      true
2691cb0ef41Sopenharmony_ci    );
2701cb0ef41Sopenharmony_ci  }
2711cb0ef41Sopenharmony_ci  {
2721cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2731cb0ef41Sopenharmony_ci      scopes: {
2741cb0ef41Sopenharmony_ci      }
2751cb0ef41Sopenharmony_ci    });
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci    assert.strictEqual(
2781cb0ef41Sopenharmony_ci      manifest
2791cb0ef41Sopenharmony_ci        .getDependencyMapper('blob:https://example.com/has-origin')
2801cb0ef41Sopenharmony_ci        .resolve('fs'),
2811cb0ef41Sopenharmony_ci      null);
2821cb0ef41Sopenharmony_ci  }
2831cb0ef41Sopenharmony_ci  {
2841cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2851cb0ef41Sopenharmony_ci      scopes: {
2861cb0ef41Sopenharmony_ci        'blob:https://example.com/has-origin': {
2871cb0ef41Sopenharmony_ci          cascade: true
2881cb0ef41Sopenharmony_ci        }
2891cb0ef41Sopenharmony_ci      }
2901cb0ef41Sopenharmony_ci    });
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci    assert.strictEqual(
2931cb0ef41Sopenharmony_ci      manifest
2941cb0ef41Sopenharmony_ci        .getDependencyMapper('blob:https://example.com/has-origin')
2951cb0ef41Sopenharmony_ci        .resolve('fs'),
2961cb0ef41Sopenharmony_ci      null);
2971cb0ef41Sopenharmony_ci  }
2981cb0ef41Sopenharmony_ci  {
2991cb0ef41Sopenharmony_ci    const manifest = new Manifest({
3001cb0ef41Sopenharmony_ci      scopes: {
3011cb0ef41Sopenharmony_ci        // FIXME
3021cb0ef41Sopenharmony_ci        'https://example.com/': {
3031cb0ef41Sopenharmony_ci          dependencies: true
3041cb0ef41Sopenharmony_ci        },
3051cb0ef41Sopenharmony_ci        'blob:https://example.com/has-origin': {
3061cb0ef41Sopenharmony_ci          cascade: true
3071cb0ef41Sopenharmony_ci        }
3081cb0ef41Sopenharmony_ci      }
3091cb0ef41Sopenharmony_ci    });
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci    assert.strictEqual(
3121cb0ef41Sopenharmony_ci      manifest
3131cb0ef41Sopenharmony_ci        .getDependencyMapper('blob:https://example.com/has-origin')
3141cb0ef41Sopenharmony_ci        .resolve('fs'),
3151cb0ef41Sopenharmony_ci      true
3161cb0ef41Sopenharmony_ci    );
3171cb0ef41Sopenharmony_ci  }
3181cb0ef41Sopenharmony_ci  {
3191cb0ef41Sopenharmony_ci    const manifest = new Manifest({
3201cb0ef41Sopenharmony_ci      scopes: {
3211cb0ef41Sopenharmony_ci        'blob:': {
3221cb0ef41Sopenharmony_ci          dependencies: true
3231cb0ef41Sopenharmony_ci        },
3241cb0ef41Sopenharmony_ci        'blob:https://example.com/has-origin': {
3251cb0ef41Sopenharmony_ci          cascade: true
3261cb0ef41Sopenharmony_ci        }
3271cb0ef41Sopenharmony_ci      }
3281cb0ef41Sopenharmony_ci    });
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ci    assert.strictEqual(
3311cb0ef41Sopenharmony_ci      manifest
3321cb0ef41Sopenharmony_ci        .getDependencyMapper('blob:https://example.com/has-origin')
3331cb0ef41Sopenharmony_ci        .resolve('fs'),
3341cb0ef41Sopenharmony_ci      null);
3351cb0ef41Sopenharmony_ci    assert.strictEqual(
3361cb0ef41Sopenharmony_ci      manifest
3371cb0ef41Sopenharmony_ci        .getDependencyMapper('blob:foo').resolve('fs'),
3381cb0ef41Sopenharmony_ci      true
3391cb0ef41Sopenharmony_ci    );
3401cb0ef41Sopenharmony_ci  }
3411cb0ef41Sopenharmony_ci}
3421cb0ef41Sopenharmony_ci// #endregion
343