tor-browser

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

test_deserialization_format_before_100.js (8960B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const ReferrerInfo = Components.Constructor(
      8  "@mozilla.org/referrer-info;1",
      9  "nsIReferrerInfo",
     10  "init"
     11 );
     12 
     13 async function runTest(setupFunc, expected) {
     14  let objectOutStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
     15    Ci.nsIObjectOutputStream
     16  );
     17  let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
     18  pipe.init(
     19    false /* non-blocking input */,
     20    false /* non-blocking output */,
     21    0 /* segment size */,
     22    0 /* max segments */
     23  );
     24  objectOutStream.setOutputStream(pipe.outputStream);
     25 
     26  setupFunc(objectOutStream);
     27 
     28  let objectInStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
     29    Ci.nsIObjectInputStream
     30  );
     31  objectInStream.setInputStream(pipe.inputStream);
     32 
     33  let referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY);
     34  try {
     35    referrerInfo.read(objectInStream);
     36  } catch (e) {
     37    Assert.ok(false, "Shouldn't fail when deserializing.");
     38    return;
     39  }
     40 
     41  Assert.ok(true, "Successfully deserialize the referrerInfo.");
     42 
     43  let { referrerPolicy, sendReferrer, computedReferrerSpec } = expected;
     44  Assert.equal(
     45    referrerInfo.referrerPolicy,
     46    referrerPolicy,
     47    "The referrerInfo has the expected referrer policy."
     48  );
     49 
     50  Assert.equal(
     51    referrerInfo.sendReferrer,
     52    sendReferrer,
     53    "The referrerInfo has the expected sendReferrer value."
     54  );
     55 
     56  if (computedReferrerSpec) {
     57    Assert.equal(
     58      referrerInfo.computedReferrerSpec,
     59      computedReferrerSpec,
     60      "The referrerInfo has the expected computedReferrerSpec value."
     61    );
     62  }
     63 }
     64 
     65 // Test deserializing referrer info with the old format.
     66 add_task(async function test_deserializeOldReferrerInfo() {
     67  // Test with a regular old format.
     68  await runTest(
     69    stream => {
     70      // Write to the output stream with the old format.
     71      stream.writeBoolean(true); // nonNull
     72      stream.writeStringZ("https://example.com/"); // spec
     73      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
     74      stream.writeBoolean(false); // sendReferrer
     75      stream.writeBoolean(false); // isComputed
     76      stream.writeBoolean(true); // initialized
     77      stream.writeBoolean(false); // overridePolicyByDefault
     78    },
     79    {
     80      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
     81      sendReferrer: false,
     82    }
     83  );
     84 
     85  // Test with an old format with `sendReferrer` is true.
     86  await runTest(
     87    stream => {
     88      // Write to the output stream with the old format.
     89      stream.writeBoolean(true); // nonNull
     90      stream.writeStringZ("https://example.com/"); // spec
     91      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
     92      stream.writeBoolean(true); // sendReferrer
     93      stream.writeBoolean(false); // isComputed
     94      stream.writeBoolean(true); // initialized
     95      stream.writeBoolean(false); // overridePolicyByDefault
     96    },
     97    {
     98      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
     99      sendReferrer: true,
    100    }
    101  );
    102 
    103  // Test with an old format with a computed Referrer.
    104  await runTest(
    105    stream => {
    106      // Write to the output stream with the old format with a string.
    107      stream.writeBoolean(true); // nonNull
    108      stream.writeStringZ("https://example.com/"); // spec
    109      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    110      stream.writeBoolean(false); // sendReferrer
    111      stream.writeBoolean(true); // isComputed
    112      stream.writeStringZ("https://example.com/"); // computedReferrer
    113      stream.writeBoolean(true); // initialized
    114      stream.writeBoolean(false); // overridePolicyByDefault
    115    },
    116    {
    117      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    118      sendReferrer: false,
    119      computedReferrerSpec: "https://example.com/",
    120    }
    121  );
    122 
    123  // Test with an old format with a computed Referrer and sendReferrer as true.
    124  await runTest(
    125    stream => {
    126      // Write to the output stream with the old format with a string.
    127      stream.writeBoolean(true); // nonNull
    128      stream.writeStringZ("https://example.com/"); // spec
    129      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    130      stream.writeBoolean(true); // sendReferrer
    131      stream.writeBoolean(true); // isComputed
    132      stream.writeStringZ("https://example.com/"); // computedReferrer
    133      stream.writeBoolean(true); // initialized
    134      stream.writeBoolean(false); // overridePolicyByDefault
    135    },
    136    {
    137      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    138      sendReferrer: true,
    139      computedReferrerSpec: "https://example.com/",
    140    }
    141  );
    142 });
    143 
    144 // Test deserializing referrer info with the current format.
    145 add_task(async function test_deserializeReferrerInfo() {
    146  // Test with a current format.
    147  await runTest(
    148    stream => {
    149      // Write to the output stream with the new format.
    150      stream.writeBoolean(true); // nonNull
    151      stream.writeStringZ("https://example.com/"); // spec
    152      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    153      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // original policy
    154      stream.writeBoolean(false); // sendReferrer
    155      stream.writeBoolean(false); // isComputed
    156      stream.writeBoolean(true); // initialized
    157      stream.writeBoolean(false); // overridePolicyByDefault
    158    },
    159    {
    160      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    161      sendReferrer: false,
    162    }
    163  );
    164 
    165  // Test with a current format with sendReferrer as true.
    166  await runTest(
    167    stream => {
    168      // Write to the output stream with the new format.
    169      stream.writeBoolean(true); // nonNull
    170      stream.writeStringZ("https://example.com/"); // spec
    171      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    172      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // original policy
    173      stream.writeBoolean(true); // sendReferrer
    174      stream.writeBoolean(false); // isComputed
    175      stream.writeBoolean(true); // initialized
    176      stream.writeBoolean(false); // overridePolicyByDefault
    177    },
    178    {
    179      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    180      sendReferrer: true,
    181    }
    182  );
    183 
    184  // Test with a current format with a computedReferrer.
    185  await runTest(
    186    stream => {
    187      // Write to the output stream with the new format with a string.
    188      stream.writeBoolean(true); // nonNull
    189      stream.writeStringZ("https://example.com/"); // spec
    190      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    191      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // original policy
    192      stream.writeBoolean(false); // sendReferrer
    193      stream.writeBoolean(true); // isComputed
    194      stream.writeStringZ("https://example.com/"); // computedReferrer
    195      stream.writeBoolean(true); // initialized
    196      stream.writeBoolean(false); // overridePolicyByDefault
    197    },
    198    {
    199      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    200      sendReferrer: false,
    201      computedReferrerSpec: "https://example.com/",
    202    }
    203  );
    204 
    205  // Test with a current format with a computedReferrer and sendReferrer as true.
    206  await runTest(
    207    stream => {
    208      // Write to the output stream with the new format with a string.
    209      stream.writeBoolean(true); // nonNull
    210      stream.writeStringZ("https://example.com/"); // spec
    211      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // policy
    212      stream.write32(Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN); // original policy
    213      stream.writeBoolean(true); // sendReferrer
    214      stream.writeBoolean(true); // isComputed
    215      stream.writeStringZ("https://example.com/"); // computedReferrer
    216      stream.writeBoolean(true); // initialized
    217      stream.writeBoolean(false); // overridePolicyByDefault
    218    },
    219    {
    220      referrerPolicy: Ci.nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
    221      sendReferrer: true,
    222      computedReferrerSpec: "https://example.com/",
    223    }
    224  );
    225 
    226  // Test with a current format that the tailing bytes are all zero.
    227  await runTest(
    228    stream => {
    229      // Write to the output stream with the new format with a string.
    230      stream.writeBoolean(true); // nonNull
    231      stream.writeStringZ("https://example.com/"); // spec
    232      stream.write32(Ci.nsIReferrerInfo.EMPTY); // policy
    233      stream.write32(Ci.nsIReferrerInfo.EMPTY); // original policy
    234      stream.writeBoolean(false); // sendReferrer
    235      stream.writeBoolean(false); // isComputed
    236      stream.writeBoolean(false); // initialized
    237      stream.writeBoolean(false); // overridePolicyByDefault
    238    },
    239    {
    240      referrerPolicy: Ci.nsIReferrerInfo.EMPTY,
    241      sendReferrer: false,
    242    }
    243  );
    244 });