# Request headers (S023; the web process extension since S049) and third-party cookies (WebKit's cookie policy since
# S050); frame document.cookie and privacy mode paused per site (S024). Offline: names resolve through the
# secure DNS stub in tests/smoke/net/server.py (*.nacre.test, *.other.test -> 127.0.0.1). site.nacre.test and
# img.nacre.test are one site (nacre.test); cdn.other.test is another. The cookies are SameSite=None; Secure, so the
# cookie pages are https (WebKit, like Chromium, treats a cookie without SameSite as Lax: never cross-site, whatever
# the switch says); the server's self-signed certificate is allowed per host through Nacre's warning first.
# Never Tor or proxies. Start the server first:
#   python3 tests/smoke/net/server.py &   then   tools/smoke.sh tests/smoke/privacy-net.txt
wait 3
set /vpn/enabled false
set /privacy/doh_custom "http://127.0.0.1:8766/dns-query"
set /privacy/doh "custom"
set /privacy/https_only false
set /privacy/dnt true
set /privacy/save_data true
set /privacy/mode false
set /privacy/block_third_party true
# The ad blocker would take the test pictures (/pixel); this run is about cookies.
set /adblock/enabled false
wait 1
open http://127.0.0.1:8767/reset
waitfor 20 document.title === "Reset"
# The test certificate, allowed for each https host ("Open anyway").
open https://site.nacre.test:8766/
waitfor 20 document.title === "Connection not secure"
locate #allow
wait 0.5
click @
waitfor 20 document.title === "Net test https"
open https://img.nacre.test:8766/
waitfor 20 document.title === "Connection not secure"
locate #allow
wait 0.5
click @
waitfor 20 document.title === "Net test https"
open https://cdn.other.test:8766/
waitfor 20 document.title === "Connection not secure"
locate #allow
wait 0.5
click @
waitfor 20 document.title === "Net test https"
# First-party cookies: one for cdn.other.test, one for the whole nacre.test site.
open https://cdn.other.test:8766/setcookie?name=fp
waitfor 20 document.title === "Set fp"
open https://site.nacre.test:8766/setcookie?name=sub&domain=nacre.test
waitfor 20 document.title === "Set sub"
open https://cdn.other.test:8766/cookies
waitfor 20 document.title === "Cookies fp=1"
# Blocking on: the cross-site frame and picture get no cookie and can't store one; the same-site picture keeps its.
open https://site.nacre.test:8766/tp?tag=on
waitfor 20 document.title === "TP on done"
shot blocked
# Blocking off: the same page sends and stores third-party cookies again.
set /privacy/block_third_party false
wait 1
open https://site.nacre.test:8766/tp?tag=off
waitfor 20 document.title === "TP off done"
# Privacy mode turns blocking on even with the switch off.
set /privacy/mode true
wait 1
open https://site.nacre.test:8766/tp?tag=mode
waitfor 20 document.title === "TP mode done"
set /privacy/mode false
# cdn.other.test's own jar: fp, plus tpoff from the unblocked run; never tpon or tpmode.
open https://cdn.other.test:8766/cookies
waitfor 20 document.title.startsWith("Cookies ")
assert document.title === "Cookies fp=1; tpoff=1"
# document.cookie in a cross-site frame (S024, script layer): empty and not stored with blocking on, works off.
set /privacy/block_third_party true
wait 1
open https://site.nacre.test:8766/jstp?tag=jon
waitfor 20 document.title === "JSTP jon done"
set /privacy/block_third_party false
wait 1
open https://site.nacre.test:8766/jstp?tag=joff
waitfor 20 document.title === "JSTP joff done"
open https://cdn.other.test:8766/cookies
waitfor 20 document.title.startsWith("Cookies ")
assert !document.title.includes("jsjon") && document.title.includes("jsjoff=1")
# Privacy mode paused on one site (S024): common values on other sites, the engine's own on the paused one. The
# real core count goes to the paused page in its hash, read on a page loaded before privacy mode was on.
open http://127.0.0.1:8767/echo?real
waitfor 20 document.title === "Echo real"
set /privacy/mode true
set /privacy/mode_off ["127.0.0.1"]
wait 1
js location.href = "/echo?paused#" + navigator.hardwareConcurrency
waitfor 20 document.title === "Echo paused"
assert navigator.hardwareConcurrency === Number(location.hash.slice(1))
open http://site.nacre.test:8766/echo?pm
waitfor 20 document.title === "Echo pm"
# WebKit's user agent is Safari-style, so appVersion is the user agent after "Mozilla/" (S050).
assert navigator.hardwareConcurrency === 8 && navigator.platform === "Linux x86_64" && navigator.appVersion === navigator.userAgent.slice(8)
set /privacy/mode_off []
set /privacy/mode false
# Do Not Track off: the headers stop.
set /privacy/dnt false
set /privacy/save_data false
wait 1
open http://127.0.0.1:8767/echo?nodnt
waitfor 20 document.title === "Echo nodnt"
js fetch("/log").then(r => r.json()).then(l => { window.reqs = l.reqs; })
waitfor 5 window.reqs !== undefined
js window.pick = (tag, host) => window.reqs.filter(r => r.path.includes("tag=" + tag) && r.host.startsWith(host)); JSON.stringify(window.reqs.filter(r => r.path.includes("tag=")))
# Headers: every request with the switches on carried DNT, Sec-GPC and Save-Data; the last one none.
assert window.reqs.filter(r => r.path.startsWith("/tp")).every(r => r.dnt === "1" && r.gpc === "1" && r.savedata === "on")
assert window.reqs.some(r => r.path === "/echo?nodnt") && window.reqs.filter(r => r.path === "/echo?nodnt").every(r => r.dnt === null && r.gpc === null && r.savedata === null)
# Blocked: cross-site frame and picture without cookies, the same-site picture with sub=1.
assert window.pick("on", "cdn.other.test").length === 2 && window.pick("on", "cdn.other.test").every(r => r.cookie === null)
assert window.pick("on", "img.nacre.test").length === 1 && window.pick("on", "img.nacre.test")[0].cookie === "sub=1"
# Not blocked: the cross-site requests carry cdn.other.test's cookie.
assert window.pick("off", "cdn.other.test").length === 2 && window.pick("off", "cdn.other.test").every(r => (r.cookie || "").includes("fp=1"))
# The cross-site frame's document.cookie: empty when blocked, cdn.other.test's cookies when not.
assert window.reqs.some(r => r.path === "/note?tag=jon&seen=") && window.reqs.some(r => r.path.startsWith("/note?tag=joff&seen=") && r.path.includes("fp%3D1"))
# Accept-Language: Firefox's list in privacy mode (S024), WebKit's weights for the same list otherwise (S050).
assert window.reqs.filter(r => r.path === "/echo?pm").every(r => r.lang === navigator.language + "," + navigator.language.split("-")[0] + ";q=0.5") && window.reqs.filter(r => r.path === "/echo?nodnt").every(r => r.lang === navigator.language + "," + navigator.language.split("-")[0] + ";q=0.90")
# Privacy mode: blocked again.
assert window.pick("mode", "cdn.other.test").length === 2 && window.pick("mode", "cdn.other.test").every(r => r.cookie === null)
status done
quit
