diff options
Diffstat (limited to '')
-rw-r--r-- | netwerk/dns/tests/moz.build | 7 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/data/fake_public_suffix_list.dat | 57 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/data/moz.build | 14 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/moz.build | 7 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/test_PublicSuffixList.js | 174 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js | 32 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/test_nsEffectiveTLDService_getKnownPublicSuffix.js | 44 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/test_nsEffectiveTLDService_getSite.js | 77 | ||||
-rw-r--r-- | netwerk/dns/tests/unit/xpcshell.ini | 13 |
9 files changed, 425 insertions, 0 deletions
diff --git a/netwerk/dns/tests/moz.build b/netwerk/dns/tests/moz.build new file mode 100644 index 0000000000..fb176c1bd3 --- /dev/null +++ b/netwerk/dns/tests/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +DIRS += ["unit"] diff --git a/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat b/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat new file mode 100644 index 0000000000..ca1da32eeb --- /dev/null +++ b/netwerk/dns/tests/unit/data/fake_public_suffix_list.dat @@ -0,0 +1,57 @@ +// 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 https://mozilla.org/MPL/2.0/. + +// This is a fake suffix list created only for the purposes of testing, +// The real PSL is rather large thus this file is kept small by cutting out most of the suffixes + +// The Original list can be found at https://publicsuffix.org/list/public_suffix_list.dat, +// Learn more about the PSL at https://publicsuffix.org. + +// .xpcshelltest is the fake domain created specifically for the the tests while +// the others are ripped from the real list, serving as examples. + +// This fake public suffix list was used to create the binary file fake_remote_dafsa.bin +// The binary is built at compile time and can be found at +// obj-dir/_tests/xpcshell/netwerk/dns/tests/unit/data/ + +// The list created with help of netwerk/dns/prepare_tlds.py and xpcom/ds/tools/make_dafsa.py +// The build directive for the binary is at moz.build at netwerk/dns/tests/unit/data/ + + + +// ===BEGIN ICANN DOMAINS=== + +// xpcshelltest : Used in tests +xpcshelltest +website.xpcshelltest +com.xpcshelltest +edu.xpcshelltest +gov.xpcshelltest +net.xpcshelltest +mil.xpcshelltest +org.xpcshelltest + +// ac : https://en.wikipedia.org/wiki/.ac +ac +coc.ac +com.ac +edu.ac +gov.ac +net.ac +mil.ac +org.ac + +// bj : https://en.wikipedia.org/wiki/.bj +bj +asso.bj +barreau.bj +gouv.bj + +// bm : http://www.bermudanic.bm/dnr-text.txt +bm +com.bm +edu.bm +gov.bm +net.bm +org.bm diff --git a/netwerk/dns/tests/unit/data/moz.build b/netwerk/dns/tests/unit/data/moz.build new file mode 100644 index 0000000000..0f5a164f77 --- /dev/null +++ b/netwerk/dns/tests/unit/data/moz.build @@ -0,0 +1,14 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +GeneratedFile( + "fake_remote_dafsa.bin", + script="../../../prepare_tlds.py", + inputs=["fake_public_suffix_list.dat"], + flags=["bin"], +) + +TEST_HARNESS_FILES.xpcshell.netwerk.dns.tests.unit.data += ["!fake_remote_dafsa.bin"] diff --git a/netwerk/dns/tests/unit/moz.build b/netwerk/dns/tests/unit/moz.build new file mode 100644 index 0000000000..e5ce568e70 --- /dev/null +++ b/netwerk/dns/tests/unit/moz.build @@ -0,0 +1,7 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +DIRS += ["data"] diff --git a/netwerk/dns/tests/unit/test_PublicSuffixList.js b/netwerk/dns/tests/unit/test_PublicSuffixList.js new file mode 100644 index 0000000000..ad61abea8e --- /dev/null +++ b/netwerk/dns/tests/unit/test_PublicSuffixList.js @@ -0,0 +1,174 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PublicSuffixList } = ChromeUtils.importESModule( + "resource://gre/modules/netwerk-dns/PublicSuffixList.sys.mjs" +); +const { TestUtils } = ChromeUtils.importESModule( + "resource://testing-common/TestUtils.sys.mjs" +); + +const CLIENT = PublicSuffixList.CLIENT; +const SIGNAL = "public-suffix-list-updated"; + +const PAYLOAD_UPDATED_RECORDS = { + current: [{ id: "tld-dafsa", "commit-hash": "current-commit-hash" }], + created: [], + updated: [ + { + old: { id: "tld-dafsa", "commit-hash": "current-commit-hash" }, + new: { id: "tld-dafsa", "commit-hash": "new-commit-hash" }, + }, + ], + deleted: [], +}; +const PAYLOAD_CREATED_RECORDS = { + current: [], + created: [ + { + id: "tld-dafsa", + "commit-hash": "new-commit-hash", + attachment: {}, + }, + ], + updated: [], + deleted: [], +}; +const PAYLOAD_UPDATED_AND_CREATED_RECORDS = { + current: [{ id: "tld-dafsa", "commit-hash": "current-commit-hash" }], + created: [{ id: "tld-dafsa", "commit-hash": "another-commit-hash" }], + updated: [ + { + old: { id: "tld-dafsa", "commit-hash": "current-commit-hash" }, + new: { id: "tld-dafsa", "commit-hash": "new-commit-hash" }, + }, + ], + deleted: [], +}; + +const fakeDafsaBinFile = do_get_file("data/fake_remote_dafsa.bin"); +const mockedFilePath = fakeDafsaBinFile.path; + +function setup() { + Services.prefs.setBoolPref("network.psl.onUpdate_notify", true); +} +setup(); +registerCleanupFunction(() => { + Services.prefs.clearUserPref("network.psl.onUpdate_notify"); +}); + +/** + * downloadToDiskCalled is used by mockDownloadToDisk() and resetMockDownloadToDisk() + * to keep track weather CLIENT.attachments.download is called or not + * downloadToDiskBackup will help restore CLIENT.attachments.download to original definition + * notifyUpdateBackup will help restore PublicSuffixList.notifyUpdate to original definition + */ +let downloadToDiskCalled = false; +const downloadToDiskBackup = CLIENT.attachments.downloadToDisk; + +// returns a fake fileURI and sends a signal with filePath and no nsifile +function mockDownloadToDisk() { + downloadToDiskCalled = false; + CLIENT.attachments.downloadToDisk = async rec => { + downloadToDiskCalled = true; + return `file://${mockedFilePath}`; // Create a fake file URI + }; +} + +// resetMockDownloadToDisk() must be run at the end of the test that uses mockDownloadToDisk() +const resetMockDownloadToDisk = () => { + CLIENT.attachments.downloadToDisk = downloadToDiskBackup; +}; + +add_task(async () => { + info("File path sent when record is in DB."); + + await CLIENT.db.clear(); // Make sure there's no record initially + await CLIENT.db.create({ + id: "tld-dafsa", + "commit-hash": "fake-commit-hash", + attachment: {}, + }); + + mockDownloadToDisk(); + + const promiseSignal = TestUtils.topicObserved(SIGNAL); + await PublicSuffixList.init(); + const observed = await promiseSignal; + + Assert.equal( + observed[1], + mockedFilePath, + "File path sent when record is in DB." + ); + await CLIENT.db.clear(); // Clean up the mockDownloaded record + resetMockDownloadToDisk(); +}); + +add_task(async () => { + info("File path sent when record updated."); + + mockDownloadToDisk(); + + const promiseSignal = TestUtils.topicObserved(SIGNAL); + await PublicSuffixList.init(); + await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_RECORDS }); + const observed = await promiseSignal; + + Assert.equal( + observed[1], + mockedFilePath, + "File path sent when record updated." + ); + resetMockDownloadToDisk(); +}); + +add_task(async () => { + info("Attachment downloaded when record created."); + + mockDownloadToDisk(); + + await PublicSuffixList.init(); + await CLIENT.emit("sync", { data: PAYLOAD_CREATED_RECORDS }); + + Assert.equal( + downloadToDiskCalled, + true, + "Attachment downloaded when record created." + ); + resetMockDownloadToDisk(); +}); + +add_task(async () => { + info("Attachment downloaded when record updated."); + + mockDownloadToDisk(); + + await PublicSuffixList.init(); + await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_RECORDS }); + + Assert.equal( + downloadToDiskCalled, + true, + "Attachment downloaded when record updated." + ); + resetMockDownloadToDisk(); +}); + +add_task(async () => { + info("No download when more than one record is changed."); + + mockDownloadToDisk(); + + await PublicSuffixList.init(); + await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_AND_CREATED_RECORDS }); + + Assert.equal( + downloadToDiskCalled, + false, + "No download when more than one record is changed." + ); + resetMockDownloadToDisk(); +}); diff --git a/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js new file mode 100644 index 0000000000..6b89b070d5 --- /dev/null +++ b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_Reload_DAFSA.js @@ -0,0 +1,32 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const SIGNAL = "public-suffix-list-updated"; + +add_task(async () => { + info("Before fake dafsa reload."); + + let suffix = Services.eTLD.getPublicSuffixFromHost("website.xpcshelltest"); + Assert.equal( + suffix, + "xpcshelltest", + "Fake Suffix does not exist in current PSL." + ); +}); + +add_task(async () => { + info("After fake dafsa reload."); + + // reload the PSL with fake data containing .xpcshelltest + const fakeDafsaFile = do_get_file("data/fake_remote_dafsa.bin"); + Services.obs.notifyObservers(fakeDafsaFile, SIGNAL, fakeDafsaFile.path); + + let suffix = Services.eTLD.getPublicSuffixFromHost("website.xpcshelltest"); + Assert.equal( + suffix, + "website.xpcshelltest", + "Fake Suffix now exists in PSL after DAFSA reload." + ); +}); diff --git a/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getKnownPublicSuffix.js b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getKnownPublicSuffix.js new file mode 100644 index 0000000000..333692af97 --- /dev/null +++ b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getKnownPublicSuffix.js @@ -0,0 +1,44 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests getPublicSuffix with the validate argument. + */ + +"use strict"; + +add_task(() => { + for (let [suffix, isKnown] of [ + ["", false], + [null, false], + ["mozbacon", false], + ["com", true], + ["circle", true], + ["bd", true], + ["gov.bd", true], + ["ck", true], + ["www.ck", true], + ["bs", true], + ["com.bs", true], + ["網絡.cn", true], + ["valléedaoste.it", true], + ["aurskog-høland.no", true], + ["公司.香港", true], + ["भारतम्", true], + ["فلسطين", true], + ]) { + let origin = "test." + suffix; + Assert.equal( + !!Services.eTLD.getKnownPublicSuffixFromHost(origin), + isKnown, + `"${suffix}" should ${isKnown ? " " : "not "}be a known public suffix` + ); + Assert.equal( + !!Services.eTLD.getKnownPublicSuffix( + Services.io.newURI("http://" + origin) + ), + isKnown, + `"${suffix}" should ${isKnown ? " " : "not "}be a known public suffix` + ); + } +}); diff --git a/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getSite.js b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getSite.js new file mode 100644 index 0000000000..5f6f3b1eb3 --- /dev/null +++ b/netwerk/dns/tests/unit/test_nsEffectiveTLDService_getSite.js @@ -0,0 +1,77 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Tests getSite and getSchemelessSite with example arguments + */ + +"use strict"; + +add_task(() => { + for (let [originString, result] of [ + ["http://.", null], + ["http://com", "http://com"], + ["http://test", "http://test"], + ["http://test.", "http://test."], + ["http://[::1]", "http://[::1]"], + ["http://[::1]:8888", "http://[::1]"], + ["http://localhost", "http://localhost"], + ["http://127.0.0.1", "http://127.0.0.1"], + ["http://user:pass@[::1]", "http://[::1]"], + ["http://example.com", "http://example.com"], + ["https://example.com", "https://example.com"], + ["https://test.example.com", "https://example.com"], + ["https://test1.test2.example.com", "https://example.com"], + ["https://test1.test2.example.co.uk", "https://example.co.uk"], + ["https://test.example.com:8888/index.html", "https://example.com"], + [ + "https://test1.test2.example.公司.香港", + "https://example.xn--55qx5d.xn--j6w193g", + ], + ]) { + let origin = Services.io.newURI(originString); + if (result === null) { + Assert.throws( + () => { + Services.eTLD.getSite(origin); + }, + /NS_ERROR_ILLEGAL_VALUE/, + "Invalid origin for getSite throws" + ); + } else { + let answer = Services.eTLD.getSite(origin); + Assert.equal( + answer, + result, + `"${originString}" should have site ${result}, got ${answer}.` + ); + } + } +}); + +add_task(() => { + for (let [originString, result] of [ + ["http://com", "com"], + ["http://test", "test"], + ["http://test.", "test."], + ["http://[::1]", "[::1]"], + ["http://[::1]:8888", "[::1]"], + ["http://localhost", "localhost"], + ["http://127.0.0.1", "127.0.0.1"], + ["http://user:pass@[::1]", "[::1]"], + ["http://example.com", "example.com"], + ["https://example.com", "example.com"], + ["https://test.example.com", "example.com"], + ["https://test1.test2.example.com", "example.com"], + ["https://test1.test2.example.co.uk", "example.co.uk"], + ["https://test1.test2.example.公司.香港", "example.xn--55qx5d.xn--j6w193g"], + ]) { + let origin = Services.io.newURI(originString); + let answer = Services.eTLD.getSchemelessSite(origin); + Assert.equal( + answer, + result, + `"${originString}" should have schemeless site ${result}, got ${answer}.` + ); + } +}); diff --git a/netwerk/dns/tests/unit/xpcshell.ini b/netwerk/dns/tests/unit/xpcshell.ini new file mode 100644 index 0000000000..8c6f341133 --- /dev/null +++ b/netwerk/dns/tests/unit/xpcshell.ini @@ -0,0 +1,13 @@ +[DEFAULT] +head = ../../../../services/common/tests/unit/head_global.js ../../../../services/common/tests/unit/head_helpers.js +firefox-appdir = browser +support-files = + data/** + !/services/common/tests/unit/head_global.js + !/services/common/tests/unit/head_helpers.js + +[test_PublicSuffixList.js] +tags = remote-settings +[test_nsEffectiveTLDService_getKnownPublicSuffix.js] +[test_nsEffectiveTLDService_getSite.js] +[test_nsEffectiveTLDService_Reload_DAFSA.js] |