tor-browser

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

test_permmanager_migrate_10-11.js (5094B)


      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 = 10;
     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(
     75    "https://foo.com",
     76    "storageAccessAPI^https://foo.com",
     77    2,
     78    0,
     79    0,
     80    0
     81  );
     82  insertOrigin(
     83    "http://foo.com",
     84    "storageAccessAPI^https://bar.com^https://foo.com",
     85    2,
     86    0,
     87    0,
     88    0
     89  );
     90  insertOrigin(
     91    "http://foo.com",
     92    "storageAccessAPI^https://bar.com^https://baz.com",
     93    2,
     94    0,
     95    0,
     96    0
     97  );
     98  insertOrigin("http://foo.com^inBrowser=1", "A", 2, 0, 0, 0);
     99 
    100  // CLose the db connection
    101  stmt6Insert.finalize();
    102  db.close();
    103  db = null;
    104 
    105  let expected = [
    106    ["https://foo.com", "storageAccessAPI^https://foo.com", 2, 0, 0, 0],
    107    ["http://foo.com", "storageAccessAPI^https://bar.com", 2, 0, 0, 0],
    108    ["http://foo.com", "storageAccessAPI^https://bar.com", 2, 0, 0, 0],
    109    ["http://foo.com", "A", 2, 0, 0, 0],
    110  ];
    111 
    112  let found = expected.map(() => 0);
    113 
    114  // Add some places to the places database
    115  await PlacesTestUtils.addVisits(
    116    Services.io.newURI("https://foo.com/some/other/subdirectory")
    117  );
    118  await PlacesTestUtils.addVisits(
    119    Services.io.newURI("ftp://some.subdomain.of.foo.com:8000/some/subdirectory")
    120  );
    121  await PlacesTestUtils.addVisits(Services.io.newURI("ftp://127.0.0.1:8080"));
    122  await PlacesTestUtils.addVisits(Services.io.newURI("https://localhost:8080"));
    123 
    124  // This will force the permission-manager to reload the data.
    125  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    126 
    127  // Force initialization of the PermissionManager
    128  for (let permission of Services.perms.all) {
    129    let isExpected = false;
    130 
    131    expected.forEach((it, i) => {
    132      if (
    133        permission.principal.origin == it[0] &&
    134        permission.type == it[1] &&
    135        permission.capability == it[2] &&
    136        permission.expireType == it[3] &&
    137        permission.expireTime == it[4]
    138      ) {
    139        isExpected = true;
    140        found[i]++;
    141      }
    142    });
    143 
    144    Assert.ok(
    145      isExpected,
    146      "Permission " +
    147        (isExpected ? "should" : "shouldn't") +
    148        " be in permission database: " +
    149        permission.principal.origin +
    150        ", " +
    151        permission.type +
    152        ", " +
    153        permission.capability +
    154        ", " +
    155        permission.expireType +
    156        ", " +
    157        permission.expireTime
    158    );
    159  }
    160 
    161  found.forEach((count, i) => {
    162    Assert.equal(
    163      count,
    164      1,
    165      "Expected count = 1, got count = " +
    166        count +
    167        " for permission " +
    168        expected[i]
    169    );
    170  });
    171 
    172  // Check to make sure that all of the tables which we care about are present
    173  {
    174    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    175    Assert.ok(db.tableExists("moz_perms"));
    176    Assert.ok(db.tableExists("moz_hosts"));
    177    Assert.ok(!db.tableExists("moz_perms_v6"));
    178 
    179    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    180    try {
    181      mozHostsCount.executeStep();
    182      Assert.equal(mozHostsCount.getInt64(0), 0);
    183    } finally {
    184      mozHostsCount.finalize();
    185    }
    186 
    187    let mozPermsCount = db.createStatement("SELECT count(*) FROM moz_perms");
    188    try {
    189      mozPermsCount.executeStep();
    190      Assert.equal(mozPermsCount.getInt64(0), expected.length);
    191    } finally {
    192      mozPermsCount.finalize();
    193    }
    194 
    195    db.close();
    196  }
    197 });