11cb0ef41Sopenharmony_ciThis directory contains the common infrastructure for the following tests (also referred below as projects).
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci- referrer-policy/
41cb0ef41Sopenharmony_ci- mixed-content/
51cb0ef41Sopenharmony_ci- upgrade-insecure-requests/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciSubdirectories:
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci- `resources`:
101cb0ef41Sopenharmony_ci    Serves JavaScript test helpers.
111cb0ef41Sopenharmony_ci- `subresource`:
121cb0ef41Sopenharmony_ci    Serves subresources, with support for redirects, stash, etc.
131cb0ef41Sopenharmony_ci    The subresource paths are managed by `subresourceMap` and
141cb0ef41Sopenharmony_ci    fetched in `requestVia*()` functions in `resources/common.js`.
151cb0ef41Sopenharmony_ci- `scope`:
161cb0ef41Sopenharmony_ci    Serves nested contexts, such as iframe documents or workers.
171cb0ef41Sopenharmony_ci    Used from `invokeFrom*()` functions in `resources/common.js`.
181cb0ef41Sopenharmony_ci- `tools`:
191cb0ef41Sopenharmony_ci    Scripts that generate test HTML files. Not used while running tests.
201cb0ef41Sopenharmony_ci- `/referrer-policy/generic/subresource-test`:
211cb0ef41Sopenharmony_ci    Sanity checking tests for subresource invocation
221cb0ef41Sopenharmony_ci    (This is still placed outside common/)
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci# Test generator
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciThe test generator ([common/security-features/tools/generate.py](tools/generate.py)) generates test HTML files from templates and a seed (`spec.src.json`) that defines all the test scenarios.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciThe project (i.e. a WPT subdirectory, for example `referrer-policy/`) that uses the generator should define per-project data and invoke the common generator logic in `common/security-features/tools`.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciThis is the overview of the project structure:
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci```
331cb0ef41Sopenharmony_cicommon/security-features/
341cb0ef41Sopenharmony_ci└── tools/ - the common test generator logic
351cb0ef41Sopenharmony_ci    ├── spec.src.json
361cb0ef41Sopenharmony_ci    └── template/ - the test files templates
371cb0ef41Sopenharmony_ciproject-directory/ (e.g. referrer-policy/)
381cb0ef41Sopenharmony_ci├── spec.src.json
391cb0ef41Sopenharmony_ci├── generic/
401cb0ef41Sopenharmony_ci│   ├── test-case.sub.js - Per-project test helper
411cb0ef41Sopenharmony_ci│   ├── sanity-checker.js (Used by debug target only)
421cb0ef41Sopenharmony_ci│   └── spec_json.js (Used by debug target only)
431cb0ef41Sopenharmony_ci└── gen/ - generated tests
441cb0ef41Sopenharmony_ci```
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci## Generating the tests
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciNote: When the repository already contains generated tests, [remove all generated tests](#removing-all-generated-tests) first.
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci```bash
511cb0ef41Sopenharmony_ci# Install json5 module if needed.
521cb0ef41Sopenharmony_cipip install --user json5
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci# Generate the test files under gen/ (HTMLs and .headers files).
551cb0ef41Sopenharmony_cipath/to/common/security-features/tools/generate.py --spec path/to/project-directory/
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci# Add all generated tests to the repo.
581cb0ef41Sopenharmony_cigit add path/to/project-directory/gen/ && git commit -m "Add generated tests"
591cb0ef41Sopenharmony_ci```
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciThis will parse the spec JSON5 files and determine which tests to generate (or skip) while using templates.
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci- The default spec JSON5: `common/security-features/tools/spec.src.json`.
641cb0ef41Sopenharmony_ci    - Describes common configurations, such as subresource types, source context types, etc.
651cb0ef41Sopenharmony_ci- The per-project spec JSON5: `project-directory/spec.src.json`.
661cb0ef41Sopenharmony_ci    - Describes project-specific configurations, particularly those related to test generation patterns (`specification`), policy deliveries (e.g. `delivery_type`, `delivery_value`) and `expectation`.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciFor how these two spec JSON5 files are merged, see [Sub projects](#sub-projects) section.
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciNote: `spec.src.json` is transitioning to JSON5 [#21710](https://github.com/web-platform-tests/wpt/issues/21710).
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciDuring the generation, the spec is validated by ```common/security-features/tools/spec_validator.py```. This is specially important when you're making changes to  `spec.src.json`. Make sure it's a valid JSON (no comments or trailing commas). The validator reports specific errors (missing keys etc.), if any.
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci### Removing all generated tests
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ciSimply remove all files under `project-directory/gen/`.
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci```bash
791cb0ef41Sopenharmony_cirm -r path/to/project-directory/gen/
801cb0ef41Sopenharmony_ci```
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci### Options for generating tests
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciNote: this section is currently obsolete. Only the release template is working.
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciThe generator script has two targets: ```release``` and ```debug```.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci* Using **release** for the target will produce tests using a template for optimizing size and performance. The release template is intended for the official web-platform-tests and possibly other test suites. No sanity checking is done in release mode. Use this option whenever you're checking into web-platform-tests.
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci* When generating for ```debug```, the produced tests will contain more verbosity and sanity checks. Use this target to identify problems with the test suites when making changes locally. Make sure you don't check in tests generated with the debug target.
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciNote that **release** is the default target when invoking ```generate.py```.
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci## Sub projects
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciProjects can be nested, for example to reuse a single `spec.src.json` across similar but slightly different sets of generated tests.
981cb0ef41Sopenharmony_ciThe directory structure would look like:
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci```
1011cb0ef41Sopenharmony_ciproject-directory/ (e.g. referrer-policy/)
1021cb0ef41Sopenharmony_ci├── spec.src.json - Parent project's spec JSON
1031cb0ef41Sopenharmony_ci├── generic/
1041cb0ef41Sopenharmony_ci│   └── test-case.sub.js - Parent project's test helper
1051cb0ef41Sopenharmony_ci├── gen/ - parent project's generated tests
1061cb0ef41Sopenharmony_ci└── sub-project-directory/ (e.g. 4K)
1071cb0ef41Sopenharmony_ci    ├── spec.src.json - Child project's spec JSON
1081cb0ef41Sopenharmony_ci    ├── generic/
1091cb0ef41Sopenharmony_ci    │   └── test-case.sub.js - Child project's test helper
1101cb0ef41Sopenharmony_ci    └── gen/ - child project's generated tests
1111cb0ef41Sopenharmony_ci```
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci`generate.py --spec project-directory/sub-project-directory` generates test files under `project-directory/sub-project-directory/gen`, based on `project-directory/spec.src.json` and `project-directory/sub-project-directory/spec.src.json`.
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci- The child project's `spec.src.json` is merged into parent project's `spec.src.json`.
1161cb0ef41Sopenharmony_ci    - Two spec JSON objects are merged recursively.
1171cb0ef41Sopenharmony_ci    - If a same key exists in both objects, the child's value overwrites the parent's value.
1181cb0ef41Sopenharmony_ci        - If both (child's and parent's) values are arrays, then the child's value is concatenated to the parent's value.
1191cb0ef41Sopenharmony_ci    - For debugging, `generate.py` dumps the merged spec JSON object as `generic/debug-output.spec.src.json`.
1201cb0ef41Sopenharmony_ci- The child project's generated tests include both of the parent and child project's `test-case.sub.js`:
1211cb0ef41Sopenharmony_ci  ```html
1221cb0ef41Sopenharmony_ci  <script src="project-directory/test-case.sub.js"></script>
1231cb0ef41Sopenharmony_ci  <script src="project-directory/sub-project-directory/test-case.sub.js"></script>
1241cb0ef41Sopenharmony_ci  <script>
1251cb0ef41Sopenharmony_ci    TestCase(...);
1261cb0ef41Sopenharmony_ci  </script>
1271cb0ef41Sopenharmony_ci  ```
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci## Updating the tests
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciThe main test logic lives in ```project-directory/generic/test-case.sub.js``` with helper functions defined in ```/common/security-features/resources/common.js``` so you should probably start there.
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciFor updating the test suites you will most likely do **a subset** of the following:
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci* Add a new subresource type:
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  * Add a new sub-resource python script to `/common/security-features/subresource/`.
1391cb0ef41Sopenharmony_ci  * Add a sanity check test for a sub-resource to `referrer-policy/generic/subresource-test/`.
1401cb0ef41Sopenharmony_ci  * Add a new entry to `subresourceMap` in `/common/security-features/resources/common.js`.
1411cb0ef41Sopenharmony_ci  * Add a new entry to `valid_subresource_names` in `/common/security-features/tools/spec_validator.py`.
1421cb0ef41Sopenharmony_ci  * Add a new entry to `subresource_schema` in `spec.src.json`.
1431cb0ef41Sopenharmony_ci  * Update `source_context_schema` to specify in which source context the subresource can be used.
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci* Add a new subresource redirection type
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  * TODO: to be documented. Example: [https://github.com/web-platform-tests/wpt/pull/18939](https://github.com/web-platform-tests/wpt/pull/18939)
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci* Add a new subresource origin type
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  * TODO: to be documented. Example: [https://github.com/web-platform-tests/wpt/pull/18940](https://github.com/web-platform-tests/wpt/pull/18940)
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci* Add a new source context (e.g. "module sharedworker global scope")
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci  * TODO: to be documented. Example: [https://github.com/web-platform-tests/wpt/pull/18904](https://github.com/web-platform-tests/wpt/pull/18904)
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci* Add a new source context list (e.g. "subresource request from a dedicated worker in a `<iframe srcdoc>`")
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  * TODO: to be documented.
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci* Implement new or update existing assertions in ```project-directory/generic/test-case.sub.js```.
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci* Exclude or add some tests by updating ```spec.src.json``` test expansions.
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci* Implement a new delivery method.
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  * TODO: to be documented. Currently the support for delivery methods are implemented in many places across `common/security-features/`.
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci* Regenerate the tests and MANIFEST.json
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci## How the generator works
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ciThis section describes how `spec.src.json` is turned into scenario data in test HTML files which are then processed by JavaScript test helpers and server-side scripts, and describes the objects/types used in the process.
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci### The spec JSON
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci`spec.src.json` is the input for the generator that defines what to generate. For examples of spec JSON files, see [referrer-policy/spec.src.json](../../referrer-policy/spec.src.json) or  [mixed-content/spec.src.json](../../mixed-content/spec.src.json).
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci#### Main sections
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci* **`specification`**
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci  Top level requirements with description fields and a ```test_expansion``` rule.
1841cb0ef41Sopenharmony_ci  This is closely mimicking the [Referrer Policy specification](http://w3c.github.io/webappsec/specs/referrer-policy/) structure.
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci* **`excluded_tests`**
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  List of ```test_expansion``` patterns expanding into selections which get skipped when generating the tests (aka. blocklisting/suppressing)
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci* **`test_expansion_schema`**
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  Provides valid values for each field.
1931cb0ef41Sopenharmony_ci  Each test expansion can only contain fields and values defined by this schema (or `"*"` values that indicate all the valid values defined this schema).
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci* **`subresource_schema`**
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  Provides metadata of subresources, e.g. supported delivery types for each subresource.
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci* **`source_context_schema`**
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  Provides metadata of each single source context, e.g. supported delivery types and subresources that can be sent from the context.
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci* **`source_context_list_schema`**
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci  Provides possible nested combinations of source contexts. See [SourceContexts Resolution](#sourcecontexts-resolution) section below for details.
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci### Test Expansion Pattern Object
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ciTest expansion patterns (`test_expansion`s in `specification` section) define the combinations of test configurations (*selections*) to be generated.
2101cb0ef41Sopenharmony_ciEach field in a test expansion can be in one of the following formats:
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci* Single match: ```"value"```
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci* Match any of: ```["value1", "value2", ...]```
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci* Match all: ```"*"```
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ciThe following fields have special meaning:
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci- **`name`**: just ignored. (Previously this was used as a part of filenames but now this is merely a label for human and is never used by generator. This field might be removed in the future (https://github.com/web-platform-tests/wpt/issues/21708))
2211cb0ef41Sopenharmony_ci- **`expansion`**: if there is more than one pattern expanding into a same selection, the pattern appearing later in the spec JSON will overwrite a previously generated selection. To make clear this is intentional, set the value of the ```expansion``` field to ```default``` for an expansion appearing earlier and ```override``` for the one appearing later.
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ciFor example a test expansion pattern (taken from [referrer-policy/spec.src.json](../../referrer-policy/spec.src.json), sorted/formatted for explanation):
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci```json
2261cb0ef41Sopenharmony_ci{
2271cb0ef41Sopenharmony_ci  "name": "insecure-protocol",
2281cb0ef41Sopenharmony_ci  "expansion": "default",
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  "delivery_type": "*",
2311cb0ef41Sopenharmony_ci  "delivery_value": "no-referrer-when-downgrade",
2321cb0ef41Sopenharmony_ci  "source_context_list": "*",
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  "expectation": "stripped-referrer",
2351cb0ef41Sopenharmony_ci  "origin": ["same-http", "cross-http"],
2361cb0ef41Sopenharmony_ci  "redirection": "*",
2371cb0ef41Sopenharmony_ci  "source_scheme": "http",
2381cb0ef41Sopenharmony_ci  "subresource": "*"
2391cb0ef41Sopenharmony_ci}
2401cb0ef41Sopenharmony_ci```
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_cimeans: "All combinations with all possible `delivery_type`,  `delivery_value`=`no-referrer-when-downgrade`, all possible `source_context_list`, `expectation`=`stripped-referrer`, `origin`=`same-http` or `cross-http`,  all possible `redirection`, `source_scheme`=`http`, and all possible `subresource`.
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci### Selection Object
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ciA selection is an object that defines a single test, with keys/values from `test_expansion_schema`.
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ciA single test expansion pattern gets expanded into a list of selections as follows:
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci* Expand each field's pattern (single, any of, or all) to list of allowed values (defined by the ```test_expansion_schema```)
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci* Permute - Recursively enumerate all selections across all fields
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ciThe following field has special meaning:
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci- **`delivery_key`**: This doesn't exist in test expansion patterns, and instead is taken from `delivery_key` field of the spec JSON and added into selections. (TODO(https://github.com/web-platform-tests/wpt/issues/21708): probably this should be added to test expansion patterns to remove this special handling)
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ciFor example, the test expansion in the example above generates selections like the following selection (which eventually generates [this test file](../../referrer-policy/gen/worker-classic.http-rp/no-referrer-when-downgrade/fetch/same-http.no-redirect.http.html )):
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci```json
2611cb0ef41Sopenharmony_ci{
2621cb0ef41Sopenharmony_ci  "delivery_type": "http-rp",
2631cb0ef41Sopenharmony_ci  "delivery_key": "referrerPolicy",
2641cb0ef41Sopenharmony_ci  "delivery_value": "no-referrer-when-downgrade",
2651cb0ef41Sopenharmony_ci  "source_context_list": "worker-classic",
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci  "expectation": "stripped-referrer",
2681cb0ef41Sopenharmony_ci  "origin": "same-http",
2691cb0ef41Sopenharmony_ci  "redirection": "no-redirect",
2701cb0ef41Sopenharmony_ci  "source_scheme": "http",
2711cb0ef41Sopenharmony_ci  "subresource": "fetch"
2721cb0ef41Sopenharmony_ci}
2731cb0ef41Sopenharmony_ci```
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci### Excluding Test Expansion Patterns
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ciThe ```excluded_tests``` section have objects with the same format as [Test Expansion Patterns](#test-expansion-patterns) that define selections to be excluded.
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ciTaking the spec JSON, the generator follows this algorithm:
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci* Expand all ```excluded_tests``` to create a denylist of selections
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci* For each `specification` entries: Expand the ```test_expansion``` pattern into selections and check each against the denylist, if not marked as suppresed, generate the test resources for the selection
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci###  SourceContext Resolution
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ciThe `source_context_list_schema` section of `spec.src.json` defines templates of policy deliveries and source contexts.
2881cb0ef41Sopenharmony_ciThe `source_context_list` value in a selection specifies the key of the template to be used in `source_context_list_schema`, and the following fields in the selection are filled into the template (these three values define the **target policy delivery** to be tested):
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci- `delivery_type`
2911cb0ef41Sopenharmony_ci- `delivery_key`
2921cb0ef41Sopenharmony_ci- `delivery_value`
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci#### Source Context List Schema
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ciEach entry of **`source_context_list_schema`**, defines a single template of how/what policies to be delivered in what source contexts (See also [PolicyDelivery](types.md#policydelivery) and [SourceContext](types.md#sourcecontext)).
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci- The key: the name of the template which matches with the `source_context_list` value in a selection.
2991cb0ef41Sopenharmony_ci- `sourceContextList`: an array of `SourceContext` objects that represents a (possibly nested) context.
3001cb0ef41Sopenharmony_ci    - `sourceContextType` of the first entry of `sourceContextList` should be always `"top"`, which represents the top-level generated test HTML. This entry is omitted in the scenario JSON object passed to JavaScript runtime, but the policy deliveries specified here are handled by the generator, e.g. written as `<meta>` elements in the generated test HTML.
3011cb0ef41Sopenharmony_ci- `subresourcePolicyDeliveries`: an array of `PolicyDelivery` objects that represents policies specified at subresource requests (e.g. `referrerPolicy` attribute of `<img>` elements).
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci#### PolicyDelivery placeholders
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ciInstead to ordinal `PolicyDelivery` objects, the following placeholder strings can be used in `sourceContextList` or `subresourcePolicyDeliveries`.
3061cb0ef41Sopenharmony_ci
3071cb0ef41Sopenharmony_ci- `"policy"`:
3081cb0ef41Sopenharmony_ci    - Replaced with the target policy delivery.
3091cb0ef41Sopenharmony_ci- `"policyIfNonNull"`:
3101cb0ef41Sopenharmony_ci    - Replaced with the target policy delivery, only if `delivery_value` is not `null`.
3111cb0ef41Sopenharmony_ci      If `delivery_value` is `null`, then the test is not generated.
3121cb0ef41Sopenharmony_ci- `"anotherPolicy"`:
3131cb0ef41Sopenharmony_ci    - Replaced with a `PolicyDelivery` object that has a different value from
3141cb0ef41Sopenharmony_ci      the target policy delivery.
3151cb0ef41Sopenharmony_ci    - Can be used to specify e.g. a policy that should be overridden by
3161cb0ef41Sopenharmony_ci      the target policy delivery.
3171cb0ef41Sopenharmony_ci
3181cb0ef41Sopenharmony_ci#### `source_context_schema` and `subresource_schema`
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ciThese represent supported delivery types and subresources
3211cb0ef41Sopenharmony_cifor each source context or subresource type. These are used
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci- To filter out test files for unsupported combinations of delivery types,
3241cb0ef41Sopenharmony_ci  source contexts and subresources during SourceContext resolution.
3251cb0ef41Sopenharmony_ci- To determine what delivery types can be used for `anotherPolicy`
3261cb0ef41Sopenharmony_ci  placeholder.
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci#### Example
3291cb0ef41Sopenharmony_ci
3301cb0ef41Sopenharmony_ciFor example, the following entry in `source_context_list_schema`:
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci```json
3331cb0ef41Sopenharmony_ci"worker-classic": {
3341cb0ef41Sopenharmony_ci  "sourceContextList": [
3351cb0ef41Sopenharmony_ci    {
3361cb0ef41Sopenharmony_ci      "sourceContextType": "top",
3371cb0ef41Sopenharmony_ci      "policyDeliveries": [
3381cb0ef41Sopenharmony_ci        "anotherPolicy"
3391cb0ef41Sopenharmony_ci      ]
3401cb0ef41Sopenharmony_ci    },
3411cb0ef41Sopenharmony_ci    {
3421cb0ef41Sopenharmony_ci      "sourceContextType": "worker-classic",
3431cb0ef41Sopenharmony_ci      "policyDeliveries": [
3441cb0ef41Sopenharmony_ci        "policy"
3451cb0ef41Sopenharmony_ci      ]
3461cb0ef41Sopenharmony_ci    }
3471cb0ef41Sopenharmony_ci  ],
3481cb0ef41Sopenharmony_ci  "subresourcePolicyDeliveries": []
3491cb0ef41Sopenharmony_ci}
3501cb0ef41Sopenharmony_ci```
3511cb0ef41Sopenharmony_ci
3521cb0ef41Sopenharmony_ciDefines a template to be instantiated with `delivery_key`, `delivery_type` and `delivery_value` values defined outside `source_context_list_schema`, which reads:
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci- A classic `WorkerGlobalScope` is created under the top-level Document, and has a policy defined by `delivery_key`, `delivery_type` and `delivery_value`.
3551cb0ef41Sopenharmony_ci- The top-level Document has a policy different from the policy given to the classic worker (to confirm that the policy of the classic worker, not of the top-level Document, is used).
3561cb0ef41Sopenharmony_ci- The subresource request is sent from the classic `WorkerGlobalScope`, with no additional policies specified at the subresource request.
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ciAnd when filled with the following values from a selection:
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci- `delivery_type`: `"http-rp"`
3611cb0ef41Sopenharmony_ci- `delivery_key`: `"referrerPolicy"`
3621cb0ef41Sopenharmony_ci- `delivery_value`: `"no-referrer-when-downgrade"`
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_ciThis becomes:
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci```json
3671cb0ef41Sopenharmony_ci"worker-classic": {
3681cb0ef41Sopenharmony_ci  "sourceContextList": [
3691cb0ef41Sopenharmony_ci    {
3701cb0ef41Sopenharmony_ci      "sourceContextType": "top",
3711cb0ef41Sopenharmony_ci      "policyDeliveries": [
3721cb0ef41Sopenharmony_ci        {
3731cb0ef41Sopenharmony_ci          "deliveryType": "meta",
3741cb0ef41Sopenharmony_ci          "key": "referrerPolicy",
3751cb0ef41Sopenharmony_ci          "value": "no-referrer"
3761cb0ef41Sopenharmony_ci        }
3771cb0ef41Sopenharmony_ci      ]
3781cb0ef41Sopenharmony_ci    },
3791cb0ef41Sopenharmony_ci    {
3801cb0ef41Sopenharmony_ci      "sourceContextType": "worker-classic",
3811cb0ef41Sopenharmony_ci      "policyDeliveries": [
3821cb0ef41Sopenharmony_ci        {
3831cb0ef41Sopenharmony_ci          "deliveryType": "http-rp",
3841cb0ef41Sopenharmony_ci          "key": "referrerPolicy",
3851cb0ef41Sopenharmony_ci          "value": "no-referrer-when-downgrade"
3861cb0ef41Sopenharmony_ci        }
3871cb0ef41Sopenharmony_ci      ]
3881cb0ef41Sopenharmony_ci    }
3891cb0ef41Sopenharmony_ci  ],
3901cb0ef41Sopenharmony_ci  "subresourcePolicyDeliveries": []
3911cb0ef41Sopenharmony_ci}
3921cb0ef41Sopenharmony_ci```
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ciwhich means
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ci- The top-level Document has `<meta name="referrer" content="no-referrer">`.
3971cb0ef41Sopenharmony_ci- The classic worker is created with
3981cb0ef41Sopenharmony_ci  `Referrer-Policy: no-referrer-when-downgrade` HTTP response headers.
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci### Scenario Object
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ciThe **scenario** object is the JSON object written to the generated HTML files, and passed to JavaScript test runtime (as an argument of `TestCase`).
4031cb0ef41Sopenharmony_ciA scenario object is an selection object, minus the keys used in [SourceContext Resolution](#sourceContext-resolution):
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci- `source_context_list`
4061cb0ef41Sopenharmony_ci- `delivery_type`
4071cb0ef41Sopenharmony_ci- `delivery_key`
4081cb0ef41Sopenharmony_ci- `delivery_value`
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_ciplus the keys instantiated by [SourceContext Resolution](#sourceContext-resolution):
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci- `source_context_list`, except for the first `"top"` entry.
4131cb0ef41Sopenharmony_ci- `subresource_policy_deliveries`
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ciFor example:
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci```json
4181cb0ef41Sopenharmony_ci{
4191cb0ef41Sopenharmony_ci  "source_context_list": [
4201cb0ef41Sopenharmony_ci    {
4211cb0ef41Sopenharmony_ci      "sourceContextType": "worker-classic",
4221cb0ef41Sopenharmony_ci      "policyDeliveries": [
4231cb0ef41Sopenharmony_ci        {
4241cb0ef41Sopenharmony_ci          "deliveryType": "http-rp",
4251cb0ef41Sopenharmony_ci          "key": "referrerPolicy",
4261cb0ef41Sopenharmony_ci          "value": "no-referrer-when-downgrade"
4271cb0ef41Sopenharmony_ci        }
4281cb0ef41Sopenharmony_ci      ]
4291cb0ef41Sopenharmony_ci    }
4301cb0ef41Sopenharmony_ci  ],
4311cb0ef41Sopenharmony_ci  "subresource_policy_deliveries": [],
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci  "expectation": "stripped-referrer",
4341cb0ef41Sopenharmony_ci  "origin": "same-http",
4351cb0ef41Sopenharmony_ci  "redirection": "no-redirect",
4361cb0ef41Sopenharmony_ci  "source_scheme": "http",
4371cb0ef41Sopenharmony_ci  "subresource": "fetch"
4381cb0ef41Sopenharmony_ci}
4391cb0ef41Sopenharmony_ci```
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci### TopLevelPolicyDelivery Object
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ciThe ***TopLevelPolicyDelivery** object is the first `"top"` entry of `SourceContextList` instantiated by [SourceContext Resolution](#sourceContext-resolution), which represents the policy delivery of the top-level HTML Document.
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ciThe generator generates `<meta>` elements and `.headers` files of the top-level HTML files from the TopLevelPolicyDelivery object.
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ciThis is handled separately by the generator from other parts of selection objects and scenario objects, because the `<meta>` and `.headers` are hard-coded directly to the files in the WPT repository, while policies of subcontexts are generated via server-side `common/security-features/scope` scripts.
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ciTODO(https://github.com/web-platform-tests/wpt/issues/21710): Currently the name `TopLevelPolicyDelivery` doesn't appear in the code.
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci## How the test runtime works
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ciAll the information needed at runtime is contained in an scenario object. See the code/comments of the following files.
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci- `project-directory/generic/test-case.js` defines `TestCase`, the entry point that receives a scenario object. `resources/common.sub.js` does the most of common JavaScript work.
4561cb0ef41Sopenharmony_ci    - Subresource URLs (which point to `subresource/` scripts) are calculated from `origin` and `redirection` values.
4571cb0ef41Sopenharmony_ci    - Initiating fetch requests based on `subresource` and `subresource_policy_deliveries`.
4581cb0ef41Sopenharmony_ci- `scope/` server-side scripts serve non-toplevel contexts, while the top-level Document is generated by the generator.
4591cb0ef41Sopenharmony_ci   TODO(https://github.com/web-platform-tests/wpt/issues/21709): Merge the logics of `scope/` and the generator.
4601cb0ef41Sopenharmony_ci- `subresource/` server-side scripts serve subresource responses.
461