tor-browser

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

test_permmanager_migrate_11-12.js (5731B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 ChromeUtils.defineESModuleGetters(this, {
      5  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
      6 });
      7 
      8 var PERMISSIONS_FILE_NAME = "permissions.sqlite";
      9 
     10 function GetPermissionsFile(profile) {
     11  let file = profile.clone();
     12  file.append(PERMISSIONS_FILE_NAME);
     13  return file;
     14 }
     15 
     16 add_task(async function test() {
     17  // Create and set up the permissions database.
     18  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
     19  let profile = do_get_profile();
     20 
     21  // We need to execute a pm method to be sure that the DB is fully
     22  // initialized.
     23  var pm = Services.perms;
     24  Assert.equal(pm.all.length, 0, "No cookies");
     25 
     26  let db = Services.storage.openDatabase(GetPermissionsFile(profile));
     27  db.schemaVersion = 11;
     28 
     29  let stmt6Insert = db.createStatement(
     30    "INSERT INTO moz_perms (" +
     31      "id, origin, type, permission, expireType, expireTime, modificationTime" +
     32      ") VALUES (" +
     33      ":id, :origin, :type, :permission, :expireType, :expireTime, :modificationTime" +
     34      ")"
     35  );
     36 
     37  let id = 0;
     38 
     39  function insertOrigin(
     40    origin,
     41    type,
     42    permission,
     43    expireType,
     44    expireTime,
     45    modificationTime
     46  ) {
     47    let thisId = id++;
     48 
     49    stmt6Insert.bindByName("id", thisId);
     50    stmt6Insert.bindByName("origin", origin);
     51    stmt6Insert.bindByName("type", type);
     52    stmt6Insert.bindByName("permission", permission);
     53    stmt6Insert.bindByName("expireType", expireType);
     54    stmt6Insert.bindByName("expireTime", expireTime);
     55    stmt6Insert.bindByName("modificationTime", modificationTime);
     56 
     57    try {
     58      stmt6Insert.execute();
     59    } finally {
     60      stmt6Insert.reset();
     61    }
     62 
     63    return {
     64      id: thisId,
     65      origin,
     66      type,
     67      permission,
     68      expireType,
     69      expireTime,
     70      modificationTime,
     71    };
     72  }
     73 
     74  insertOrigin("https://a.com", "3rdPartyStorage^https://b.com", 2, 0, 0, 0);
     75  insertOrigin(
     76    "https://www.a.com",
     77    "3rdPartyStorage^https://www.c.com",
     78    2,
     79    0,
     80    0,
     81    0
     82  );
     83  insertOrigin(
     84    "https://localhost",
     85    "3rdPartyStorage^http://www.c.com",
     86    2,
     87    0,
     88    0,
     89    0
     90  );
     91 
     92  insertOrigin(
     93    "https://www.b.co.uk",
     94    "3rdPartyStorage^https://www.a.co.uk",
     95    2,
     96    0,
     97    0,
     98    0
     99  );
    100 
    101  insertOrigin(
    102    "https://sub.www.b.co.uk",
    103    "3rdPartyStorage^https://sub.www.a.co.uk",
    104    2,
    105    0,
    106    0,
    107    0
    108  );
    109 
    110  insertOrigin(
    111    "https://example.b.co.uk",
    112    "3rdPartyStorage^https://www.a.co.uk",
    113    2,
    114    0,
    115    0,
    116    0
    117  );
    118 
    119  insertOrigin(
    120    "https://[::1]",
    121    "3rdPartyStorage^https://www.a.co.uk",
    122    2,
    123    0,
    124    0,
    125    0
    126  );
    127  // Close the db connection
    128  stmt6Insert.finalize();
    129  db.close();
    130  db = null;
    131  info(Services.perms.all);
    132 
    133  let expected = [
    134    ["https://a.com", "3rdPartyStorage^https://b.com", 2, 0, 0, 0],
    135    ["https://a.com", "3rdPartyStorage^https://www.c.com", 2, 0, 0, 0],
    136    ["https://localhost", "3rdPartyStorage^http://www.c.com", 2, 0, 0, 0],
    137    ["https://b.co.uk", "3rdPartyStorage^https://www.a.co.uk", 2, 0, 0, 0],
    138    ["https://b.co.uk", "3rdPartyStorage^https://sub.www.a.co.uk", 2, 0, 0, 0],
    139    ["https://[::1]", "3rdPartyStorage^https://www.a.co.uk", 2, 0, 0, 0],
    140  ];
    141 
    142  let found = expected.map(() => 0);
    143 
    144  // Add some places to the places database
    145  await PlacesTestUtils.addVisits(
    146    Services.io.newURI("https://foo.com/some/other/subdirectory")
    147  );
    148  await PlacesTestUtils.addVisits(
    149    Services.io.newURI("ftp://some.subdomain.of.foo.com:8000/some/subdirectory")
    150  );
    151  await PlacesTestUtils.addVisits(Services.io.newURI("ftp://127.0.0.1:8080"));
    152  await PlacesTestUtils.addVisits(Services.io.newURI("https://localhost:8080"));
    153 
    154  // This will force the permission-manager to reload the data.
    155  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    156 
    157  info(Services.perms.all);
    158 
    159  // Force initialization of the PermissionManager
    160  for (let permission of Services.perms.all) {
    161    let isExpected = false;
    162 
    163    expected.forEach((it, i) => {
    164      if (
    165        permission.principal.origin == it[0] &&
    166        permission.type == it[1] &&
    167        permission.capability == it[2] &&
    168        permission.expireType == it[3] &&
    169        permission.expireTime == it[4]
    170      ) {
    171        isExpected = true;
    172        found[i]++;
    173      }
    174    });
    175 
    176    Assert.ok(
    177      isExpected,
    178      "Permission " +
    179        (isExpected ? "should" : "shouldn't") +
    180        " be in permission database: " +
    181        permission.principal.origin +
    182        ", " +
    183        permission.type +
    184        ", " +
    185        permission.capability +
    186        ", " +
    187        permission.expireType +
    188        ", " +
    189        permission.expireTime
    190    );
    191  }
    192  info(expected);
    193  info(found);
    194 
    195  found.forEach((count, i) => {
    196    Assert.equal(
    197      count,
    198      1,
    199      "Expected count = 1, got count = " +
    200        count +
    201        " for permission " +
    202        expected[i]
    203    );
    204  });
    205 
    206  // Check to make sure that all of the tables which we care about are present
    207  {
    208    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    209    Assert.ok(db.tableExists("moz_perms"));
    210    Assert.ok(db.tableExists("moz_hosts"));
    211    Assert.ok(!db.tableExists("moz_perms_v6"));
    212 
    213    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    214    try {
    215      mozHostsCount.executeStep();
    216      Assert.equal(mozHostsCount.getInt64(0), 0);
    217    } finally {
    218      mozHostsCount.finalize();
    219    }
    220 
    221    let mozPermsCount = db.createStatement("SELECT count(*) FROM moz_perms");
    222    try {
    223      mozPermsCount.executeStep();
    224      Assert.equal(mozPermsCount.getInt64(0), expected.length);
    225    } finally {
    226      mozPermsCount.finalize();
    227    }
    228 
    229    db.close();
    230  }
    231 });