Bug 1525076 - Part 0: Test more scenarios on macOS; handle URI component encoding. r=mixedpuppy

The existing code handles Windows and macOS quite differently.  On
macOS, the tests were not as comprehensive; this patch brings them
level and makes the form of the attribution data uniform on Windows
and macOS.  In particular, attribution data fields will now be URI
coded, just as they are on Windows.

This will allow us to re-use the parsing machinery we have on Windows
when we cache attribution codes on macOS.

Differential Revision: https://phabricator.services.mozilla.com/D92692
This commit is contained in:
Nick Alexander
2020-10-11 18:18:11 +00:00
parent 7c8e2b37bb
commit 39987ecf12
5 changed files with 80 additions and 43 deletions

View File

@@ -0,0 +1,76 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
add_task(async function testValidAttrCodes() {
let appPath = Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent.path;
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
Ci.nsIMacAttributionService
);
for (let entry of validAttrCodes) {
// Set a url referrer. In the macOS quarantine database, the
// referrer URL has components that areURI-encoded. Our test data
// URI-encodes the components and also the separators (?, &, =).
// So we decode it and re-encode it to leave just the components
// URI-encoded.
let url = `http://example.com?${encodeURI(decodeURIComponent(entry.code))}`;
attributionSvc.setReferrerUrl(appPath, url, true);
let referrer = attributionSvc.getReferrerUrl(appPath);
equal(referrer, url, "overwrite referrer url");
// Read attribution code from referrer to ensure cache is fresh.
AttributionCode._clearCache();
let result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
entry.parsed,
"Parsed code should match expected value, code was: " + entry.code
);
// Does not overwrite cached existing attribution code.
attributionSvc.setReferrerUrl(appPath, "http://test.com", false);
referrer = attributionSvc.getReferrerUrl(appPath);
equal(referrer, url, "update referrer url");
result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
entry.parsed,
"Parsed code should match expected value, code was: " + entry.code
);
}
});
add_task(async function testInvalidAttrCodes() {
let appPath = Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent.path;
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
Ci.nsIMacAttributionService
);
for (let code of invalidAttrCodes) {
// Set a url referrer. Not all of these invalid codes can be represented
// in the quarantine database; skip those ones.
let url = `http://example.com?${code}`;
let referrer;
try {
attributionSvc.setReferrerUrl(appPath, url, true);
referrer = attributionSvc.getReferrerUrl(appPath);
} catch (ex) {
continue;
}
if (!referrer) {
continue;
}
equal(referrer, url, "overwrite referrer url");
// Read attribution code from referrer to ensure cache is fresh.
AttributionCode._clearCache();
let result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(result, {}, "Code should have failed to parse: " + code);
}
});

View File

@@ -1,39 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { AttributionCode } = ChromeUtils.import(
"resource:///modules/AttributionCode.jsm"
);
add_task(async function test_attribution() {
let appPath = Services.dirsvc.get("GreD", Ci.nsIFile).parent.parent.path;
let attributionSvc = Cc["@mozilla.org/mac-attribution;1"].getService(
Ci.nsIMacAttributionService
);
attributionSvc.setReferrerUrl(appPath, "", true);
let referrer = attributionSvc.getReferrerUrl(appPath);
equal(referrer, "", "force an empty referrer url");
// Set a url referrer, testing both utm and non-utm codes
let url = "http://example.com?content=foo&utm_source=bar&utm_content=baz";
attributionSvc.setReferrerUrl(appPath, url, true);
referrer = attributionSvc.getReferrerUrl(appPath);
equal(referrer, url, "overwrite referrer url");
// Does not overwrite existing properties.
attributionSvc.setReferrerUrl(appPath, "http://test.com", false);
referrer = attributionSvc.getReferrerUrl(appPath);
equal(referrer, url, "referrer url is not changed");
let result = await AttributionCode.getAttrDataAsync();
Assert.deepEqual(
result,
{ content: "foo", source: "bar" },
"parsed attributes match"
);
});

View File

@@ -1,12 +1,11 @@
[DEFAULT]
firefox-appdir = browser
skip-if = toolkit == 'android'
head = head.js
[test_AttributionCode.js]
head = head_win.js
skip-if = os != 'win' # windows specific tests
[test_attribution.js]
[test_MacAttribution.js]
skip-if = toolkit != "cocoa" # osx specific tests
[test_attribution_parsing.js]
head = head_win.js
skip-if = os != 'win' # windows specific tests