Skip to content

Commit

Permalink
Update Sec-CH-UA tests: fix existing tests, and test brand size.
Browse files Browse the repository at this point in the history
As of WICG/ua-client-hints#179,
https://wicg.github.io/ua-client-hints/#user-agent-brand now requires
that a brand in a brand list not be longer than 32 bytes.

This CL adds a new test for the userAgentData.brands interface, and
modifies the existing sec-ch-ua HTTP tests.

Also, the existing sec-ch-ua HTTP tests were updated to more closely
reflect the current state of UA-CH ("UA" is not a valid token, and
"Accept-CH-Lifetime" is not shipping).

Bug: 1263180
Change-Id: Ifa7cbf18d67271e2dd1dc1d485f432d4ca9327fa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3243544
Commit-Queue: Aaron Tagliaboschi <[email protected]>
Auto-Submit: Mike Taylor <[email protected]>
Reviewed-by: Aaron Tagliaboschi <[email protected]>
Cr-Commit-Position: refs/heads/main@{#935138}
  • Loading branch information
miketaylr authored and Chromium LUCI CQ committed Oct 26, 2021
1 parent c6e2e4a commit ca7ab45
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
def main(request, response):
ua = request.headers.get(b'sec-ch-ua', b'')
ua = request.headers.get(b'Sec-CH-UA', b'')
response.headers.set(b"Content-Type", b"text/html")
response.headers.set(b"Accept-CH", b"UA")
response.headers.set(b"Accept-CH-Lifetime", b"10")
response.content = b'''
<script>
window.opener.postMessage({ header: '%s' }, "*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,48 @@
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
var minor = "";
promise_test(t => {
var sec_ch_ua_header = "";

function grabSECCHUAHeader(t) {
return new Promise((resolve, reject) => {
var w;
window.onmessage = e => {
try {
assert_not_equals(e.data.header, "", "The `Sec-CH-UA` header is delivered.");
minor = e.data.header;
resolve(e.data.header)
} catch (ex) {
reject(ex);
}
w.close();
resolve();
};
w = window.open("./resources/sec-ch-ua.py");
t.add_cleanup(w.close);
});
}
promise_test(t => {
return grabSECCHUAHeader(t).then(header => {
sec_ch_ua_header = header;
assert_not_equals(sec_ch_ua_header, "", "`Sec-CH-UA` is sent.");
});
}, "Open HTTPS window prior to opt-in: `Sec-CH-UA` header with minor version.")
}, "Open HTTPS window: `Sec-CH-UA` header returned by default.");

promise_test(t => {
return new Promise((resolve, reject) => {
var w;
window.onmessage = e => {
try {
assert_not_equals(e.data.header, "", "The `Sec-CH-UA` header is delivered.");
assert_equals(e.data.header, minor, "The `Sec-CH-UA` header did not change after the opt-in.");
} catch (ex) {
reject(ex);
}
w.close();
resolve();
};
w = window.open("./resources/sec-ch-ua.py");
return grabSECCHUAHeader(t).then(header => {
assert_not_equals(header, "", "The `Sec-CH-UA` header is delivered.");
assert_equals(header, sec_ch_ua_header,
"The `Sec-CH-UA` header did not change between requests.");
});
}, "Open HTTPS window: `Sec-CH-UA` header is consistent across versions.");

promise_test(t => {
return grabSECCHUAHeader(t).then(header => {
assert_true(header.split(", ").every((brand) => {
let brandEnd = brand.indexOf(";v=");
assert_true(brandEnd !== -1,
"A well-formed Sec-CH-UA header must have version (v=) params");
/* 32 + 2, becuase of the extra quotes padding the brand,
e.g. '"lol";v=22"' */
return brandEnd < 34;
}));
});
}, "Open HTTPS window post-opt-in: `Sec-CH-UA` header with minor version.")
}, "Open HTTPS window: No brand in `Sec-CH-UA` header is > than 32 chars.");
</script>
</head>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// META: title=tests for navigator.userAgentData

test(t => {
const brands = navigator.userAgentData.brands;
assert_true(brands.every(brand => brand.brand.length < 32),
"No brand should be longer than 32 characters.");
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
This is a testharness.js-based test.
FAIL Open HTTPS window prior to opt-in: `Sec-CH-UA` header with minor version. assert_not_equals: The `Sec-CH-UA` header is delivered. got disallowed value ""
FAIL Open HTTPS window post-opt-in: `Sec-CH-UA` header with minor version. assert_not_equals: The `Sec-CH-UA` header is delivered. got disallowed value ""
FAIL Open HTTPS window: `Sec-CH-UA` header returned by default. assert_not_equals: `Sec-CH-UA` is sent. got disallowed value ""
FAIL Open HTTPS window: `Sec-CH-UA` header is consistent across versions. assert_not_equals: The `Sec-CH-UA` header is delivered. got disallowed value ""
FAIL Open HTTPS window: No brand in `Sec-CH-UA` header is > than 32 chars. assert_true: A well-formed Sec-CH-UA header must have version (v=) params expected true got false
Harness: the test ran to completion.

0 comments on commit ca7ab45

Please sign in to comment.