From da4c7e7ed675c3bf405668739c3012d140856109 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 15 May 2024 05:34:42 +0200 Subject: Adding upstream version 126.0. Signed-off-by: Daniel Baumann --- .../search/tests/xpcshell/head_search.js | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) (limited to 'toolkit/components/search/tests/xpcshell/head_search.js') 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 @@ -306,6 +306,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} + * 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. -- cgit v1.2.3