summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/xpcshell/head.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/cache/test/xpcshell/head.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/cache/test/xpcshell/head.js')
-rw-r--r--dom/cache/test/xpcshell/head.js198
1 files changed, 198 insertions, 0 deletions
diff --git a/dom/cache/test/xpcshell/head.js b/dom/cache/test/xpcshell/head.js
new file mode 100644
index 0000000000..bf691e2065
--- /dev/null
+++ b/dom/cache/test/xpcshell/head.js
@@ -0,0 +1,198 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ *
+ * All images in schema_15_profile.zip are from https://github.com/mdn/sw-test/
+ * and are CC licensed by https://www.flickr.com/photos/legofenris/.
+ */
+
+// testSteps is expected to be defined by the file including this file.
+/* global testSteps */
+
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+
+const NS_APP_USER_PROFILE_50_DIR = "ProfD";
+const osWindowsName = "WINNT";
+const pathDelimiter = "/";
+
+const storageDirName = "storage";
+const defaultPersistenceDirName = "default";
+const cacheClientDirName = "cache";
+
+// services required be initialized in order to run CacheStorage
+var ss = Cc["@mozilla.org/storage/service;1"].createInstance(
+ Ci.mozIStorageService
+);
+var sts = Cc["@mozilla.org/network/stream-transport-service;1"].getService(
+ Ci.nsIStreamTransportService
+);
+var hash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
+
+class RequestError extends Error {
+ constructor(resultCode, resultName) {
+ super(`Request failed (code: ${resultCode}, name: ${resultName})`);
+ this.name = "RequestError";
+ this.resultCode = resultCode;
+ this.resultName = resultName;
+ }
+}
+
+function run_test() {
+ runTest();
+}
+
+function runTest() {
+ do_get_profile();
+
+ enableTesting();
+
+ // Expose Cache and Fetch symbols on the global
+ Cu.importGlobalProperties(["caches", "fetch"]);
+
+ Assert.ok(
+ typeof testSteps === "function",
+ "There should be a testSteps function"
+ );
+ Assert.ok(
+ testSteps.constructor.name === "AsyncFunction",
+ "testSteps should be an async function"
+ );
+
+ registerCleanupFunction(resetTesting);
+
+ add_task(testSteps);
+
+ // Since we defined run_test, we must invoke run_next_test() to start the
+ // async test.
+ run_next_test();
+}
+
+function enableTesting() {
+ Services.prefs.setBoolPref("dom.quotaManager.testing", true);
+}
+
+function resetTesting() {
+ Services.prefs.clearUserPref("dom.quotaManager.testing");
+}
+
+function initStorage() {
+ return Services.qms.init();
+}
+
+function initTemporaryStorage() {
+ return Services.qms.initTemporaryStorage();
+}
+
+function initTemporaryOrigin(principal) {
+ return Services.qms.initializeTemporaryOrigin("default", principal);
+}
+
+function clearOrigin(principal) {
+ let request = Services.qms.clearStoragesForPrincipal(principal, "default");
+
+ return request;
+}
+
+function reset() {
+ return Services.qms.reset();
+}
+
+async function requestFinished(request) {
+ await new Promise(function(resolve) {
+ request.callback = function() {
+ resolve();
+ };
+ });
+
+ if (request.resultCode !== Cr.NS_OK) {
+ throw new RequestError(request.resultCode, request.resultName);
+ }
+
+ return request.result;
+}
+
+// Extract a zip file into the profile
+function create_test_profile(zipFileName) {
+ var directoryService = Services.dirsvc;
+
+ var profileDir = directoryService.get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile);
+ var currentDir = directoryService.get("CurWorkD", Ci.nsIFile);
+
+ var packageFile = currentDir.clone();
+ packageFile.append(zipFileName);
+
+ var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance(
+ Ci.nsIZipReader
+ );
+ zipReader.open(packageFile);
+
+ var entryNames = Array.from(zipReader.findEntries(null));
+ entryNames.sort();
+
+ for (var entryName of entryNames) {
+ var zipentry = zipReader.getEntry(entryName);
+
+ var file = profileDir.clone();
+ entryName.split(pathDelimiter).forEach(function(part) {
+ file.append(part);
+ });
+
+ if (zipentry.isDirectory) {
+ file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
+ } else {
+ var istream = zipReader.getInputStream(entryName);
+
+ var ostream = Cc[
+ "@mozilla.org/network/file-output-stream;1"
+ ].createInstance(Ci.nsIFileOutputStream);
+ ostream.init(file, -1, parseInt("0644", 8), 0);
+
+ var bostream = Cc[
+ "@mozilla.org/network/buffered-output-stream;1"
+ ].createInstance(Ci.nsIBufferedOutputStream);
+ bostream.init(ostream, 32 * 1024);
+
+ bostream.writeFrom(istream, istream.available());
+
+ istream.close();
+ bostream.close();
+ }
+ }
+
+ zipReader.close();
+}
+
+function getCacheDir() {
+ return getRelativeFile(
+ `${storageDirName}/${defaultPersistenceDirName}/chrome/${cacheClientDirName}`
+ );
+}
+
+function getPrincipal(url, attrs) {
+ let uri = Services.io.newURI(url);
+ if (!attrs) {
+ attrs = {};
+ }
+ return Services.scriptSecurityManager.createContentPrincipal(uri, attrs);
+}
+
+function getRelativeFile(relativePath) {
+ let file = Services.dirsvc
+ .get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile)
+ .clone();
+
+ if (Services.appinfo.OS === osWindowsName) {
+ let winFile = file.QueryInterface(Ci.nsILocalFileWin);
+ winFile.useDOSDevicePathSyntax = true;
+ }
+
+ relativePath.split(pathDelimiter).forEach(function(component) {
+ if (component == "..") {
+ file = file.parent;
+ } else {
+ file.append(component);
+ }
+ });
+
+ return file;
+}