diff options
Diffstat (limited to 'netwerk/test/browser')
-rw-r--r-- | netwerk/test/browser/browser.toml | 3 | ||||
-rw-r--r-- | netwerk/test/browser/browser_dns_prefetch_link_header.js | 282 | ||||
-rw-r--r-- | netwerk/test/browser/file_link_dns_prefetch.sjs | 25 |
3 files changed, 310 insertions, 0 deletions
diff --git a/netwerk/test/browser/browser.toml b/netwerk/test/browser/browser.toml index 002bd2769d..a12ffed212 100644 --- a/netwerk/test/browser/browser.toml +++ b/netwerk/test/browser/browser.toml @@ -72,6 +72,7 @@ support-files = [ "x_frame_options.html^headers^", "test_1629307.html", "file_link_header.sjs", + "file_link_dns_prefetch.sjs", ] prefs = [ @@ -156,6 +157,8 @@ skip-if = ["os == 'win'"] # Bug 1775761 ["browser_cookie_sync_across_tabs.js"] +["browser_dns_prefetch_link_header.js"] + ["browser_fetch_lnk.js"] run-if = ["os == 'win'"] support-files = ["file_lnk.lnk",] diff --git a/netwerk/test/browser/browser_dns_prefetch_link_header.js b/netwerk/test/browser/browser_dns_prefetch_link_header.js new file mode 100644 index 0000000000..a7954cadb5 --- /dev/null +++ b/netwerk/test/browser/browser_dns_prefetch_link_header.js @@ -0,0 +1,282 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// Test steps: +// 1. Load file_link_dns_prefetch.sjs +// 2.`<link rel="dns-prefetch" href="https://example.org">` is in +// the server-side sjs, so we will make the dns-request. +// 3. We verify that the dns request was made + +const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService( + Ci.nsIDashboard +); + +///////////////////////////////////////////////////////////////////////////// +// To observe DNS requests when running via the mochitest proxy we must first take a few steps: +// +// 1. Update the mochitest proxy pac file to include dns resolution. +// We do this by injecting "dnsResolve(host);" into the `FindProxyForURL()` pac function. +let existingPACScript = Services.prefs.getCharPref( + "network.proxy.autoconfig_url" +); + +let findProxyForURLFunction = "function FindProxyForURL(url, host){"; +let directDnsPacScript = existingPACScript.replace( + findProxyForURLFunction, + `${findProxyForURLFunction} + dnsResolve(host); + ` +); +Services.prefs.setStringPref( + "network.proxy.autoconfig_url", + directDnsPacScript +); + +// 2. Ensure we don't disable dns prefetch despite using a proxy (this would otherwise happen after every request that the proxy completed) +Services.prefs.setBoolPref("network.dns.prefetch_via_proxy", true); + +// 3. And finally enable dns prefetching via the private dns service api (generally disabled in mochitest proxy) +Services.dns.QueryInterface(Ci.nsPIDNSService).prefetchEnabled = true; +///////////////////////////////////////////////////////////////////////////// + +registerCleanupFunction(function () { + // Restore proxy pac and dns prefetch behaviour via proxy + Services.prefs.setCharPref("network.proxy.autoconfig_url", existingPACScript); + Services.prefs.clearUserPref("network.dns.prefetch_via_proxy"); + Services.dns.QueryInterface(Ci.nsPIDNSService).prefetchEnabled = false; +}); + +async function isRecordFound(hostname) { + return new Promise(resolve => { + gDashboard.requestDNSInfo(function (data) { + let found = false; + for (let i = 0; i < data.entries.length; i++) { + if (data.entries[i].hostname == hostname) { + found = true; + break; + } + } + resolve(found); + }); + }); +} + +let https_requestUrl = `https://example.com/browser/netwerk/test/browser/file_link_dns_prefetch.sjs`; +let http_requestUrl = `http://example.com/browser/netwerk/test/browser/file_link_dns_prefetch.sjs`; // eslint-disable-line @microsoft/sdl/no-insecure-url + +// Test dns-prefetch on https +add_task(async function test_https_dns_prefetch() { + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: https_requestUrl, + waitForLoad: true, + }, + async function () {} + ); + + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("example.org"); + }), + "Record from link rel=dns-prefetch element should be found" + ); + Assert.ok(await isRecordFound("example.com"), "Host record should be found"); +}); + +// Test dns-prefetch on http +add_task(async function test_http_dns_prefetch() { + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: http_requestUrl, + waitForLoad: true, + }, + async function () {} + ); + + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("example.org"); + }), + "Record from link rel=dns-prefetch element should be found" + ); + Assert.ok(await isRecordFound("example.com"), "Host record should be found"); +}); + +// Test dns-prefetch on https with the feature disabled +add_task(async function test_https_dns_prefetch_disabled() { + Services.dns.clearCache(true); + + // Disable the feature to verify that it will not prefetch + Services.prefs.setBoolPref("network.dns.disablePrefetchFromHTTPS", true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: https_requestUrl, + waitForLoad: true, + }, + async function () {} + ); + + Assert.ok(await isRecordFound("example.com"), "Host record should be found"); + Assert.ok( + !(await isRecordFound("example.org")), + "Record from link rel=dns-prefetch element should not be found with disablePrefetchFromHTTPS set" + ); + + Services.prefs.clearUserPref("network.dns.disablePrefetchFromHTTPS"); +}); + +// Test dns-prefetch on http with the feature disabled +add_task(async function test_http_dns_prefetch_disabled() { + Services.dns.clearCache(true); + + // Disable the feature to verify, but this test is http, and so prefetch will execute + Services.prefs.setBoolPref("network.dns.disablePrefetchFromHTTPS", true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: http_requestUrl, + waitForLoad: true, + }, + async function () {} + ); + + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("example.org"); + }), + "Record from link rel=dns-prefetch element should be found on http page with disablePrefetchFromHTTPS set" + ); + Assert.ok(await isRecordFound("example.com"), "Host record should be found"); + + Services.prefs.clearUserPref("network.dns.disablePrefetchFromHTTPS"); +}); + +// Test if we speculatively prefetch dns for anchor elements on https documents +add_task(async function test_https_anchor_speculative_dns_prefetch() { + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: https_requestUrl, + waitForLoad: true, + }, + async function () { + Assert.ok( + await isRecordFound("example.com"), + "Host record should be found" + ); + Assert.ok( + !(await isRecordFound("www.mozilla.org")), + "By default we do not speculatively prefetch dns for anchor elements on https documents" + ); + } + ); + + // And enable the pref to verify that it works + Services.prefs.setBoolPref( + "dom.prefetch_dns_for_anchor_https_document", + true + ); + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: https_requestUrl, + waitForLoad: true, + }, + async function () { + // The anchor element prefetchs are sent after pageload event; wait for them + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("www.mozilla.org"); + }), + "Speculatively prefetch dns for anchor elements on https documents" + ); + Assert.ok( + await isRecordFound("example.com"), + "Host record should be found" + ); + } + ); + + Services.prefs.clearUserPref("dom.prefetch_dns_for_anchor_https_document"); +}); + +// Test that we speculatively prefetch dns for anchor elements on http documents +add_task(async function test_http_anchor_speculative_dns_prefetch() { + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: http_requestUrl, + waitForLoad: true, + }, + async function () { + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("example.org"); + }), + "Record from link rel=dns-prefetch element should be found" + ); + + // The anchor element prefetchs are sent after pageload event; wait for them + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("www.mozilla.org"); + }), + "By default we speculatively prefetch dns for anchor elements on http documents" + ); + + Assert.ok( + await isRecordFound("example.com"), + "Host record should be found" + ); + } + ); + + // And disable the pref to verify that we no longer make the requests + Services.prefs.setBoolPref( + "dom.prefetch_dns_for_anchor_http_document", + false + ); + Services.dns.clearCache(true); + + await BrowserTestUtils.withNewTab( + { + gBrowser, + url: http_requestUrl, + waitForLoad: true, + }, + async function () { + Assert.ok( + await TestUtils.waitForCondition(() => { + return isRecordFound("example.org"); + }), + "Record from link rel=dns-prefetch element should be found" + ); + Assert.ok( + !(await isRecordFound("www.mozilla.org")), + "We disabled speculative prefetch dns for anchor elements on http documents" + ); + Assert.ok( + await isRecordFound("example.com"), + "Host record should be found" + ); + } + ); + + Services.prefs.clearUserPref("dom.prefetch_dns_for_anchor_http_document"); +}); diff --git a/netwerk/test/browser/file_link_dns_prefetch.sjs b/netwerk/test/browser/file_link_dns_prefetch.sjs new file mode 100644 index 0000000000..6ca959bda1 --- /dev/null +++ b/netwerk/test/browser/file_link_dns_prefetch.sjs @@ -0,0 +1,25 @@ +"use strict"; + +function handleRequest(request, response) { + // write to raw socket + response.seizePower(); + let body = `<!DOCTYPE html> + <html> + <head> + <link rel="dns-prefetch" href="https://example.org"> + </head> + <body> + <h1>Test rel=dns-prefetch<h1> + <a href="https://www.mozilla.org"> Test link </a> + </body> + </html>`; + + response.write("HTTP/1.1 200 OK\r\n"); + response.write("Content-Type: text/html;charset=utf-8\r\n"); + response.write("Cache-Control: no-cache\r\n"); + response.write(`Content-Length: ${body.length}\r\n`); + response.write("\r\n"); + response.write(body); + + response.finish(); +} |