Skip to content

Truly independent web browser

LibWeb: Exercise CSS resource cache sharing across request modifiers

Andreas Kling6946c9c5641a
diff --git a/Tests/LibWeb/Ref/expected/css-image-request-modifiers-not-shared-ref.html b/Tests/LibWeb/Ref/expected/css-image-request-modifiers-not-shared-ref.html
new file mode 100644
index 00000000000..7bd9cc0a134
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css-image-request-modifiers-not-shared-ref.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<style>
+ section {
+ display: flex;
+ }
+ div {
+ width: 50px;
+ height: 50px;
+ }
+</style>
+<body>
+ <section><div style="background-color: green"></div><div style="background-color: green"></div></section>
+ <section><div style="background-color: green"></div><div style="background-color: green"></div></section>
+ <section><div style="background-color: green"></div><div style="background-color: green"></div></section>
+ <section><div></div><div></div></section>
+ <section><div style="background-color: green"></div><div style="background-color: green"></div></section>
+ <section><div></div><div></div></section>
+</body>
diff --git a/Tests/LibWeb/Ref/input/css-image-request-modifiers-not-shared.html b/Tests/LibWeb/Ref/input/css-image-request-modifiers-not-shared.html
new file mode 100644
index 00000000000..63565ebeadf
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css-image-request-modifiers-not-shared.html
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/css-image-request-modifiers-not-shared-ref.html">
+<script src="../../Text/input/include.js"></script>
+<style>
+ section {
+ display: flex;
+ }
+ div {
+ width: 50px;
+ height: 50px;
+ background-size: 50px 50px;
+ }
+</style>
+<body>
+<script>
+ // Create the echoes synchronously so the image loads start while parsing and delay the load event.
+ const server = httpTestServer();
+ const greenPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgaGAAAAEEAIFw9selAAAAAElFTkSuQmCC";
+ const createImageEcho = (name, headers) => {
+ const url = server.echoURL(`/${name}.png`);
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", `${server.baseURL}/echo`, false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path: new URL(url).pathname,
+ status: 200,
+ headers: { "Content-Type": "image/png", "Cache-Control": "no-store", ...headers },
+ body: greenPNG,
+ body_encoding: "base64",
+ }));
+ return url;
+ };
+
+ // A CORS load passes the Cross-Origin-Resource-Policy check, but a no-cors load must not.
+ const corpURL = createImageEcho("same-origin-corp", {
+ "Access-Control-Allow-Origin": "*",
+ "Cross-Origin-Resource-Policy": "same-origin",
+ });
+ // A no-cors load succeeds, but a CORS load must fail the CORS check.
+ const noCorsURL = createImageEcho("no-acao", {});
+ const anonymousFirstURL = createImageEcho("anonymous-first", { "Access-Control-Allow-Origin": "*" });
+ const credentialsFirstURL = createImageEcho("credentials-first", { "Access-Control-Allow-Origin": "*" });
+ const validIntegrityFirstURL = createImageEcho("valid-integrity-first", { "Access-Control-Allow-Origin": "*" });
+ const invalidIntegrityFirstURL = createImageEcho("invalid-integrity-first", { "Access-Control-Allow-Origin": "*" });
+ const validIntegrity = "sha256-ybJ6Zd0X2BV7UdNtNrVDBewTzqiRUroOJLVGXrPLsMc=";
+ const invalidIntegrity = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
+
+ document.write(`
+ <section>
+ <div style="background-image: url('${corpURL}' cross-origin(anonymous))"></div>
+ <div style="background-image: url('${corpURL}')"></div>
+ </section>
+ <section>
+ <div style="background-image: url('${noCorsURL}')"></div>
+ <div style="background-image: url('${noCorsURL}' cross-origin(anonymous))"></div>
+ </section>
+ <section>
+ <div style="background-image: url('${anonymousFirstURL}' cross-origin(anonymous))"></div>
+ <div style="background-image: url('${anonymousFirstURL}' cross-origin(use-credentials))"></div>
+ </section>
+ <section>
+ <div style="background-image: url('${credentialsFirstURL}' cross-origin(use-credentials))"></div>
+ <div style="background-image: url('${credentialsFirstURL}' cross-origin(anonymous))"></div>
+ </section>
+ <section>
+ <div style="background-image: url('${validIntegrityFirstURL}' cross-origin(anonymous) integrity('${validIntegrity}'))"></div>
+ <div style="background-image: url('${validIntegrityFirstURL}' cross-origin(anonymous) integrity('${invalidIntegrity}'))"></div>
+ </section>
+ <section>
+ <div style="background-image: url('${invalidIntegrityFirstURL}' cross-origin(anonymous) integrity('${invalidIntegrity}'))"></div>
+ <div style="background-image: url('${invalidIntegrityFirstURL}' cross-origin(anonymous) integrity('${validIntegrity}'))"></div>
+ </section>
+ `);
+ document.body.offsetWidth;
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/expected/css-image-request-modifier-sharing.txt b/Tests/LibWeb/Text/expected/css-image-request-modifier-sharing.txt
new file mode 100644
index 00000000000..95797beafdd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css-image-request-modifier-sharing.txt
@@ -0,0 +1,4 @@
+Identical URLs: 1 request(s)
+Reordered modifiers: 1 request(s)
+Different referrer policies: 1 request(s)
+Default and explicit referrer policy: 1 request(s)
diff --git a/Tests/LibWeb/Text/expected/font-face-same-relative-url-different-base-urls.txt b/Tests/LibWeb/Text/expected/font-face-same-relative-url-different-base-urls.txt
new file mode 100644
index 00000000000..ff304adcc44
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/font-face-same-relative-url-different-base-urls.txt
@@ -0,0 +1,2 @@
+WithFont: loaded
+WithoutFont: loaded
diff --git a/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html b/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html
new file mode 100644
index 00000000000..5c1ee57f5d7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<body>
+<script>
+ const server = httpTestServer();
+ const greenPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgaGAAAAEEAIFw9selAAAAAElFTkSuQmCC";
+ const createImageEcho = name => {
+ const url = server.echoURL(`/${name}.png`);
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", `${server.baseURL}/echo`, false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path: new URL(url).pathname,
+ status: 200,
+ headers: { "Content-Type": "image/png", "Cache-Control": "no-store" },
+ body: greenPNG,
+ body_encoding: "base64",
+ }));
+ return url;
+ };
+
+ const identicalURL = createImageEcho("identical");
+ const reorderedModifiersURL = createImageEcho("reordered-modifiers");
+ const differentPoliciesURL = createImageEcho("different-policies");
+ const defaultPolicyURL = createImageEcho("default-policy");
+ document.write(`
+ <div style="background-image: url('${identicalURL}')"></div>
+ <div style="background-image: url('${identicalURL}')"></div>
+ <div style="background-image: url('${reorderedModifiersURL}' cross-origin(anonymous) referrer-policy(no-referrer))"></div>
+ <div style="background-image: url('${reorderedModifiersURL}' referrer-policy(no-referrer) cross-origin(anonymous))"></div>
+ <div style="background-image: url('${differentPoliciesURL}' referrer-policy(no-referrer))"></div>
+ <div style="background-image: url('${differentPoliciesURL}' referrer-policy(unsafe-url))"></div>
+ <div style="background-image: url('${defaultPolicyURL}')"></div>
+ <div style="background-image: url('${defaultPolicyURL}' referrer-policy(no-referrer))"></div>
+ `);
+ document.body.offsetWidth;
+
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ const entries = performance.getEntriesByType("resource");
+ for (const [label, url] of [
+ ["Identical URLs", identicalURL],
+ ["Reordered modifiers", reorderedModifiersURL],
+ ["Different referrer policies", differentPoliciesURL],
+ ["Default and explicit referrer policy", defaultPolicyURL],
+ ]) {
+ println(`${label}: ${entries.filter(entry => entry.name === url).length} request(s)`);
+ }
+ done();
+ });
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html.headers b/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css-image-request-modifier-sharing.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html
diff --git a/Tests/LibWeb/Text/input/font-face-same-relative-url-different-base-urls.html b/Tests/LibWeb/Text/input/font-face-same-relative-url-different-base-urls.html
new file mode 100644
index 00000000000..6534f4c9106
--- /dev/null
+++ b/Tests/LibWeb/Text/input/font-face-same-relative-url-different-base-urls.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ promiseTest(async () => {
+ const createSheet = (family, baseURL) => {
+ const sheet = new CSSStyleSheet({ baseURL });
+ sheet.replaceSync(`@font-face { font-family: ${family}; src: url(HashSans.woff); }`);
+ return sheet;
+ };
+ document.adoptedStyleSheets = [
+ createSheet("WithFont", new URL("../../Assets/", location.href).href),
+ createSheet("WithoutFont", new URL("../../Assets/missing/", location.href).href),
+ ];
+
+ for (const face of document.fonts) {
+ try {
+ await face.load();
+ } catch {
+ }
+ println(`${face.family}: ${face.status}`);
+ }
+ });
+</script>