tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_MacAttribution.js (3768B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 
      5 "use strict";
      6 
      7 const { MacAttribution } = ChromeUtils.importESModule(
      8  "moz-src:///browser/components/attribution/MacAttribution.sys.mjs"
      9 );
     10 
     11 let extendedAttributeTestCases = [
     12  {
     13    raw: "__MOZCUSTOM__dlsource%3D=mozci",
     14    expected: "dlsource%3D=mozci",
     15    error: false,
     16  },
     17  {
     18    raw: "__MOZCUSTOM__dlsource%3D=mozci\0\0\0\0\0",
     19    expected: "dlsource%3D=mozci",
     20    error: false,
     21  },
     22  {
     23    raw: "__MOZCUSTOM__dlsource%3D=mozci\t\t\t\t\t",
     24    expected: "dlsource%3D=mozci",
     25    error: false,
     26  },
     27  {
     28    raw: "__MOZCUSTOM__dlsource%3D=mozci\0\0\0\t\t",
     29    expected: "dlsource%3D=mozci",
     30    error: false,
     31  },
     32  {
     33    raw: "__MOZCUSTOM__dlsource%3D=mozci\t\t\0\0",
     34    expected: "dlsource%3D=mozci",
     35    error: false,
     36  },
     37  {
     38    raw: "__MOZCUSTOM__dlsource%3D=mozci\0\t\0\t\0\t",
     39    expected: "dlsource%3D=mozci",
     40    error: false,
     41  },
     42  {
     43    raw: "",
     44    expected: "",
     45    error: true,
     46  },
     47  {
     48    raw: "dlsource%3D=mozci\0\t\0\t\0\t",
     49    expected: "",
     50    error: true,
     51  },
     52 ];
     53 
     54 add_task(async () => {
     55  await setupStubs();
     56 });
     57 
     58 // Tests to ensure that MacAttribution.getAttributionString
     59 // strips away the parts of the extended attribute that it should,
     60 // and that invalid extended attribute values result in no attribution
     61 // data.
     62 add_task(async function testExtendedAttributeProcessing() {
     63  for (let entry of extendedAttributeTestCases) {
     64    // We use setMacXAttr directly here rather than setAttributionString because
     65    // we need the ability to set invalid attribution strings.
     66    await IOUtils.setMacXAttr(
     67      MacAttribution.applicationPath,
     68      "com.apple.application-instance",
     69      new TextEncoder().encode(entry.raw)
     70    );
     71    try {
     72      let got = await MacAttribution.getAttributionString();
     73      if (entry.error === true) {
     74        Assert.ok(false, "Expected error, raw code was: " + entry.raw);
     75      }
     76      Assert.equal(
     77        got,
     78        entry.expected,
     79        "Returned code should match expected value, raw code was: " + entry.raw
     80      );
     81    } catch (err) {
     82      if (entry.error === false) {
     83        Assert.ok(
     84          false,
     85          "Unexpected error, raw code was: " + entry.raw + " error is: " + err
     86        );
     87      }
     88    }
     89  }
     90 });
     91 
     92 add_task(async function testValidAttrCodes() {
     93  for (let entry of validAttrCodes) {
     94    if (entry.platforms && !entry.platforms.includes("mac")) {
     95      continue;
     96    }
     97 
     98    await MacAttribution.setAttributionString(entry.code);
     99 
    100    // Read attribution code from xattr.
    101    AttributionCode._clearCache();
    102    let result = await AttributionCode.getAttrDataAsync();
    103    Assert.deepEqual(
    104      result,
    105      entry.parsed,
    106      "Parsed code should match expected value, code was: " + entry.code
    107    );
    108 
    109    // Read attribution code from cache.
    110    result = await AttributionCode.getAttrDataAsync();
    111    Assert.deepEqual(
    112      result,
    113      entry.parsed,
    114      "Parsed code should match expected value, code was: " + entry.code
    115    );
    116 
    117    // Does not overwrite cached existing attribution code.
    118    await MacAttribution.setAttributionString("__MOZCUSTOM__testcode");
    119    result = await AttributionCode.getAttrDataAsync();
    120    Assert.deepEqual(
    121      result,
    122      entry.parsed,
    123      "Parsed code should match expected value, code was: " + entry.code
    124    );
    125  }
    126 });
    127 
    128 add_task(async function testInvalidAttrCodes() {
    129  for (let code of invalidAttrCodes) {
    130    await MacAttribution.setAttributionString("__MOZCUSTOM__" + code);
    131 
    132    // Read attribution code from xattr
    133    AttributionCode._clearCache();
    134    let result = await AttributionCode.getAttrDataAsync();
    135    Assert.deepEqual(result, {}, "Code should have failed to parse: " + code);
    136  }
    137 });