chromium/third_party/blink/web_tests/http/tests/security/agent-equality.html

<!DOCTYPE html>
<html>
<head>
<title>Agent Equality Test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
// This test checks whether the JavaScript agent is created in a proper
// granularity. See:
// https://html.spec.whatwg.org/multipage/webappapis.html#integration-with-the-javascript-agent-formalism

// For details on each test, see the comments of the iframe file.
async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-same-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin test') {
        return;
      }
      assert_equals(evt.data.length, 4);
      assert_equals(evt.data[1], evt.data[2]);
      assert_equals(evt.data[1], evt.data[3]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-cross-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'cross-origin test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Cross-origin documents should receive a different agent.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-skip-level-same-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'skip-level same-origin test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent even if opened by cross-origin document.');

async_test(t => {
  // This test got more complicated by default-enabling origin-agent clusters:
  // If Origin-Agent clusters are default disabled - i.e. legacy behaviour -
  // then frames on the same domain but different port should be assigned to the
  // same agent cluster, because document.domain setting might make them
  // quasi same-origin with synchronous access between them. But with OAC
  // default enabled, this is no longer possible and they should receive
  // different clusters. So the desired outcome of this test depends on whether
  // origin-agent clusters are default enabled.
  //
  // We solve this by querying for default enabling first, and then
  // assert_equals or assert_not_equals based on it. We use the origin agent
  // cluster state of the main document - which does not request OAC - as a
  // proxy for whether OAC is default enabled or not.
  const is_the_main_document_origin_agent_clustered = window.originAgentCluster;

  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-same-origin-different-ports.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin different-ports test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      if (!is_the_main_document_origin_agent_clustered) {
        assert_equals(evt.data[1], evt.data[2]);
      } else {
        assert_not_equals(evt.data[1], evt.data[2]);
      }
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Documents on same-origin-but-different-ports should receive the same agent (if default OAC disabled).');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-different-schemes.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'different-schemes test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Documents with different schemes (HTTP and HTTPS) should receive different agents.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-data-url.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'data scheme test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Frame loaded as data: URL should receive a unique agent that is different from the parent\'s.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-srcdoc.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'srcdoc iframe test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'srcdoc iframe should receive the same agent as the parent\'s.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-after-detach.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin detach test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent even after detaching.');

// TODO(yutak): Add tests that check agents after navigations.
</script>
</body>
</html>