From b436e5cb98ae981b4cbe9ef6a9d7917ebc975997 Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Fri, 9 Oct 2020 17:28:46 +0000 Subject: [PATCH] Bug 1525076 - Part 2: Add `AttributionCode.writeAttributionFile`. r=mixedpuppy The next step is to have the cached attribution file location vary depending on the platform, and to have the tests run on additional platforms. Differential Revision: https://phabricator.services.mozilla.com/D84264 --- .../attribution/AttributionCode.jsm | 41 ++++++++++++------- .../browser_AttributionCode_telemetry.js | 14 +------ .../test/xpcshell/test_AttributionCode.js | 18 ++------ browser/components/newtab/lib/ASRouter.jsm | 14 +------ .../tests/unit/test_TelemetryEnvironment.js | 19 ++------- 5 files changed, 35 insertions(+), 71 deletions(-) diff --git a/browser/components/attribution/AttributionCode.jsm b/browser/components/attribution/AttributionCode.jsm index b9b0ca6fe300..4297a37a9493 100644 --- a/browser/components/attribution/AttributionCode.jsm +++ b/browser/components/attribution/AttributionCode.jsm @@ -37,19 +37,32 @@ const ATTR_CODE_KEYS = [ let gCachedAttrData = null; -/** - * Returns an nsIFile for the file containing the attribution data. - */ -function getAttributionFile() { - let file = Services.dirsvc.get("LocalAppData", Ci.nsIFile); - // appinfo does not exist in xpcshell, so we need defaults. - file.append(Services.appinfo.vendor || "mozilla"); - file.append(AppConstants.MOZ_APP_NAME); - file.append("postSigningData"); - return file; -} - var AttributionCode = { + /** + * Returns an nsIFile for the file containing the attribution data. + */ + get attributionFile() { + let file = Services.dirsvc.get("LocalAppData", Ci.nsIFile); + // appinfo does not exist in xpcshell, so we need defaults. + file.append(Services.appinfo.vendor || "mozilla"); + file.append(AppConstants.MOZ_APP_NAME); + file.append("postSigningData"); + return file; + }, + + /** + * Write the given attribution code to the attribution file. + * @param {String} code to write. + */ + async writeAttributionFile(code) { + const file = AttributionCode.attributionFile; + await OS.File.makeDir(file.parent.path, { + ignoreExisting: true, + from: file.parent.parent.path, + }); + await OS.File.writeAtomic(file.path, code); + }, + /** * Returns an array of allowed attribution code keys. */ @@ -111,7 +124,7 @@ var AttributionCode = { if (AppConstants.platform == "win") { let bytes; try { - bytes = await OS.File.read(getAttributionFile().path); + bytes = await OS.File.read(this.attributionFile.path); } catch (ex) { if (ex instanceof OS.File.Error && ex.becauseNoSuchFile) { return gCachedAttrData; @@ -176,7 +189,7 @@ var AttributionCode = { */ async deleteFileAsync() { try { - await OS.File.remove(getAttributionFile().path); + await OS.File.remove(this.attributionFile.path); } catch (ex) { // The attribution file may already have been deleted, // or it may have never been installed at all; diff --git a/browser/components/attribution/test/browser/browser_AttributionCode_telemetry.js b/browser/components/attribution/test/browser/browser_AttributionCode_telemetry.js index e16e052118a6..e0fef7c845e4 100644 --- a/browser/components/attribution/test/browser/browser_AttributionCode_telemetry.js +++ b/browser/components/attribution/test/browser/browser_AttributionCode_telemetry.js @@ -9,18 +9,6 @@ const { AttributionCode } = ChromeUtils.import( ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm"); const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm"); -async function writeAttributionFile(data) { - let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile); - let file = appDir.clone(); - file.append(Services.appinfo.vendor || "mozilla"); - file.append(AppConstants.MOZ_APP_NAME); - - await OS.File.makeDir(file.path, { from: appDir.path, ignoreExisting: true }); - - file.append("postSigningData"); - await OS.File.writeAtomic(file.path, data); -} - add_task(function setup() { // Clear cache call is only possible in a testing environment let env = Cc["@mozilla.org/process/environment;1"].getService( @@ -56,7 +44,7 @@ add_task(async function test_parse_error() { // Write an invalid file to trigger a decode error await AttributionCode.deleteFileAsync(); AttributionCode._clearCache(); - await writeAttributionFile(""); // empty string is invalid + await AttributionCode.writeAttributionFile(""); // empty string is invalid result = await AttributionCode.getAttrDataAsync(); Assert.deepEqual(result, {}, "Should have failed to parse"); diff --git a/browser/components/attribution/test/xpcshell/test_AttributionCode.js b/browser/components/attribution/test/xpcshell/test_AttributionCode.js index 3edb8c20aec9..f9c171f1ec18 100644 --- a/browser/components/attribution/test/xpcshell/test_AttributionCode.js +++ b/browser/components/attribution/test/xpcshell/test_AttributionCode.js @@ -9,18 +9,6 @@ const { AppConstants } = ChromeUtils.import( const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm"); const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); -async function writeAttributionFile(data) { - let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile); - let file = appDir.clone(); - file.append(Services.appinfo.vendor || "mozilla"); - file.append(AppConstants.MOZ_APP_NAME); - - await OS.File.makeDir(file.path, { from: appDir.path, ignoreExisting: true }); - - file.append("postSigningData"); - await OS.File.writeAtomic(file.path, data); -} - /** * Test validation of attribution codes, * to make sure we reject bad ones and accept good ones. @@ -28,7 +16,7 @@ async function writeAttributionFile(data) { add_task(async function testValidAttrCodes() { for (let entry of validAttrCodes) { AttributionCode._clearCache(); - await writeAttributionFile(entry.code); + await AttributionCode.writeAttributionFile(entry.code); let result = await AttributionCode.getAttrDataAsync(); Assert.deepEqual( result, @@ -45,7 +33,7 @@ add_task(async function testValidAttrCodes() { add_task(async function testInvalidAttrCodes() { for (let code of invalidAttrCodes) { AttributionCode._clearCache(); - await writeAttributionFile(code); + await AttributionCode.writeAttributionFile(code); let result = await AttributionCode.getAttrDataAsync(); Assert.deepEqual(result, {}, "Code should have failed to parse: " + code); } @@ -58,7 +46,7 @@ add_task(async function testInvalidAttrCodes() { */ add_task(async function testDeletedFile() { // Set up the test by clearing the cache and writing a valid file. - await writeAttributionFile(validAttrCodes[0].code); + await AttributionCode.writeAttributionFile(validAttrCodes[0].code); let result = await AttributionCode.getAttrDataAsync(); Assert.deepEqual( result, diff --git a/browser/components/newtab/lib/ASRouter.jsm b/browser/components/newtab/lib/ASRouter.jsm index 6da2670b044d..788ae944096e 100644 --- a/browser/components/newtab/lib/ASRouter.jsm +++ b/browser/components/newtab/lib/ASRouter.jsm @@ -10,7 +10,6 @@ const { XPCOMUtils } = ChromeUtils.import( XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]); XPCOMUtils.defineLazyModuleGetters(this, { AppConstants: "resource://gre/modules/AppConstants.jsm", - OS: "resource://gre/modules/osfile.jsm", BookmarkPanelHub: "resource://activity-stream/lib/BookmarkPanelHub.jsm", SnippetsTestMessageProvider: "resource://activity-stream/lib/SnippetsTestMessageProvider.jsm", @@ -1722,18 +1721,7 @@ class _ASRouter { // RTAMO messages. This should only be called from within about:newtab#asrouter /* istanbul ignore next */ async _writeAttributionFile(data) { - let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile); - let file = appDir.clone(); - file.append(Services.appinfo.vendor || "mozilla"); - file.append(AppConstants.MOZ_APP_NAME); - - await OS.File.makeDir(file.path, { - from: appDir.path, - ignoreExisting: true, - }); - - file.append("postSigningData"); - await OS.File.writeAtomic(file.path, data); + await AttributionCode.writeAttributionFile(data); } /** diff --git a/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js b/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js index 5f9c33e9e869..f1b392c0309a 100644 --- a/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js +++ b/toolkit/components/telemetry/tests/unit/test_TelemetryEnvironment.js @@ -340,29 +340,16 @@ function spoofPartnerInfo() { } } -function getAttributionFile() { - return FileUtils.getFile("LocalAppData", [ - "mozilla", - AppConstants.MOZ_APP_NAME, - "postSigningData", - ]); -} - -function spoofAttributionData() { +async function spoofAttributionData() { if (gIsWindows) { AttributionCode._clearCache(); - let stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( - Ci.nsIFileOutputStream - ); - stream.init(getAttributionFile(), -1, -1, 0); - stream.write(ATTRIBUTION_CODE, ATTRIBUTION_CODE.length); - stream.close(); + await AttributionCode.writeAttributionFile(ATTRIBUTION_CODE); } } function cleanupAttributionData() { if (gIsWindows) { - getAttributionFile().remove(false); + AttributionCode.attributionFile.remove(false); AttributionCode._clearCache(); } }