diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
commit | da4c7e7ed675c3bf405668739c3012d140856109 (patch) | |
tree | cdd868dba063fecba609a1d819de271f0d51b23e /toolkit/components/search/tests/xpcshell/head_search.js | |
parent | Adding upstream version 125.0.3. (diff) | |
download | firefox-da4c7e7ed675c3bf405668739c3012d140856109.tar.xz firefox-da4c7e7ed675c3bf405668739c3012d140856109.zip |
Adding upstream version 126.0.upstream/126.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/search/tests/xpcshell/head_search.js')
-rw-r--r-- | toolkit/components/search/tests/xpcshell/head_search.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/toolkit/components/search/tests/xpcshell/head_search.js b/toolkit/components/search/tests/xpcshell/head_search.js index 1c0504e277..72bc90185c 100644 --- a/toolkit/components/search/tests/xpcshell/head_search.js +++ b/toolkit/components/search/tests/xpcshell/head_search.js @@ -307,6 +307,101 @@ async function setupRemoteSettings() { } /** + * Reads the specified file from the data directory and returns its contents as + * an Uint8Array. + * + * @param {string} filename + * The name of the file to read. + * @returns {Promise<Uint8Array>} + * The contents of the file in an Uint8Array. + */ +async function getFileDataBuffer(filename) { + return IOUtils.read(PathUtils.join(do_get_cwd().path, "data", filename)); +} + +/** + * Creates a mock attachment record for use in remote settings related tests. + * + * @param {object} item + * An object containing the details of the attachment. + * @param {string} item.filename + * The name of the attachmnet file in the data directory. + * @param {string[]} item.engineIdentifiers + * The engine identifiers for the attachment. + * @param {number} item.imageSize + * The size of the image. + * @param {string} [item.id] + * The ID to use for the record. If not provided, a new UUID will be generated. + * @param {number} [item.lastModified] + * The last modified time for the record. Defaults to the current time. + */ +async function mockRecordWithAttachment({ + filename, + engineIdentifiers, + imageSize, + id = Services.uuid.generateUUID().toString(), + lastModified = Date.now(), +}) { + let buffer = await getFileDataBuffer(filename); + + // Generate a hash. + let hasher = Cc["@mozilla.org/security/hash;1"].createInstance( + Ci.nsICryptoHash + ); + hasher.init(Ci.nsICryptoHash.SHA256); + hasher.update(buffer, buffer.length); + + let hash = hasher.finish(false); + hash = Array.from(hash, (_, i) => + ("0" + hash.charCodeAt(i).toString(16)).slice(-2) + ).join(""); + + let record = { + id, + engineIdentifiers, + imageSize, + attachment: { + hash, + location: `${filename}`, + filename, + size: buffer.byteLength, + mimetype: "application/json", + }, + last_modified: lastModified, + }; + + let attachment = { + record, + blob: new Blob([buffer]), + }; + + return { record, attachment }; +} + +/** + * Inserts an attachment record into the remote settings collection. + * + * @param {RemoteSettingsClient} client + * The remote settings client to use. + * @param {object} item + * An object containing the details of the attachment - see mockRecordWithAttachment. + * @param {boolean} [addAttachmentToCache] + * Whether to add the attachment file to the cache. Defaults to true. + */ +async function insertRecordIntoCollection( + client, + item, + addAttachmentToCache = true +) { + let { record, attachment } = await mockRecordWithAttachment(item); + await client.db.create(record); + if (addAttachmentToCache) { + await client.attachments.cacheImpl.set(record.id, attachment); + } + await client.db.importChanges({}, record.last_modified); +} + +/** * Helper function that sets up a server and respnds to region * fetch requests. * |