tor-browser

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

test_permmanager_migrate_9-10.js (7044B)


      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 = 9;
     28  db.executeSimpleSQL("DROP TABLE moz_perms");
     29  db.executeSimpleSQL("DROP TABLE IF EXISTS moz_hosts");
     30 
     31  db.executeSimpleSQL(
     32    "CREATE TABLE moz_perms (" +
     33      " id INTEGER PRIMARY KEY" +
     34      ",origin TEXT" +
     35      ",type TEXT" +
     36      ",permission INTEGER" +
     37      ",expireType INTEGER" +
     38      ",expireTime INTEGER" +
     39      ",modificationTime INTEGER" +
     40      ")"
     41  );
     42 
     43  let stmt6Insert = db.createStatement(
     44    "INSERT INTO moz_perms (" +
     45      "id, origin, type, permission, expireType, expireTime, modificationTime" +
     46      ") VALUES (" +
     47      ":id, :origin, :type, :permission, :expireType, :expireTime, :modificationTime" +
     48      ")"
     49  );
     50 
     51  db.executeSimpleSQL(
     52    "CREATE TABLE moz_hosts (" +
     53      " id INTEGER PRIMARY KEY" +
     54      ",host TEXT" +
     55      ",type TEXT" +
     56      ",permission INTEGER" +
     57      ",expireType INTEGER" +
     58      ",expireTime INTEGER" +
     59      ",modificationTime INTEGER" +
     60      ",appId INTEGER" +
     61      ",isInBrowserElement INTEGER" +
     62      ")"
     63  );
     64 
     65  let stmtInsert = db.createStatement(
     66    "INSERT INTO moz_hosts (" +
     67      "id, host, type, permission, expireType, expireTime, modificationTime, appId, isInBrowserElement" +
     68      ") VALUES (" +
     69      ":id, :host, :type, :permission, :expireType, :expireTime, :modificationTime, :appId, :isInBrowserElement" +
     70      ")"
     71  );
     72 
     73  let id = 0;
     74 
     75  function insertOrigin(
     76    origin,
     77    type,
     78    permission,
     79    expireType,
     80    expireTime,
     81    modificationTime
     82  ) {
     83    let thisId = id++;
     84 
     85    stmt6Insert.bindByName("id", thisId);
     86    stmt6Insert.bindByName("origin", origin);
     87    stmt6Insert.bindByName("type", type);
     88    stmt6Insert.bindByName("permission", permission);
     89    stmt6Insert.bindByName("expireType", expireType);
     90    stmt6Insert.bindByName("expireTime", expireTime);
     91    stmt6Insert.bindByName("modificationTime", modificationTime);
     92 
     93    try {
     94      stmt6Insert.execute();
     95    } finally {
     96      stmt6Insert.reset();
     97    }
     98 
     99    return {
    100      id: thisId,
    101      origin,
    102      type,
    103      permission,
    104      expireType,
    105      expireTime,
    106      modificationTime,
    107    };
    108  }
    109 
    110  function insertHost(
    111    host,
    112    type,
    113    permission,
    114    expireType,
    115    expireTime,
    116    modificationTime,
    117    appId,
    118    isInBrowserElement
    119  ) {
    120    let thisId = id++;
    121 
    122    stmtInsert.bindByName("id", thisId);
    123    stmtInsert.bindByName("host", host);
    124    stmtInsert.bindByName("type", type);
    125    stmtInsert.bindByName("permission", permission);
    126    stmtInsert.bindByName("expireType", expireType);
    127    stmtInsert.bindByName("expireTime", expireTime);
    128    stmtInsert.bindByName("modificationTime", modificationTime);
    129    stmtInsert.bindByName("appId", appId);
    130    stmtInsert.bindByName("isInBrowserElement", isInBrowserElement);
    131 
    132    try {
    133      stmtInsert.execute();
    134    } finally {
    135      stmtInsert.reset();
    136    }
    137 
    138    return {
    139      id: thisId,
    140      host,
    141      type,
    142      permission,
    143      expireType,
    144      expireTime,
    145      modificationTime,
    146      appId,
    147      isInBrowserElement,
    148    };
    149  }
    150  // eslint-disable-next-line no-unused-vars
    151  let created7 = [
    152    insertOrigin("https://foo.com", "A", 2, 0, 0, 0),
    153    insertOrigin("http://foo.com", "A", 2, 0, 0, 0),
    154    insertOrigin("http://foo.com^inBrowser=1", "A", 2, 0, 0, 0),
    155  ];
    156 
    157  // Add some rows to the database
    158  // eslint-disable-next-line no-unused-vars
    159  let created = [
    160    insertHost("foo.com", "A", 1, 0, 0, 0, 0, false),
    161    insertHost("foo.com", "B", 1, 0, 0, 0, 1000, false),
    162    insertHost("foo.com", "C", 1, 0, 0, 0, 2000, true),
    163  ];
    164 
    165  // CLose the db connection
    166  stmt6Insert.finalize();
    167  stmtInsert.finalize();
    168  db.close();
    169  stmtInsert = null;
    170  db = null;
    171 
    172  let expected = [
    173    ["https://foo.com", "A", 2, 0, 0, 0],
    174    ["http://foo.com", "A", 2, 0, 0, 0],
    175    ["http://foo.com", "A", 2, 0, 0, 0],
    176  ];
    177 
    178  let found = expected.map(() => 0);
    179 
    180  // Add some places to the places database
    181  await PlacesTestUtils.addVisits(
    182    Services.io.newURI("https://foo.com/some/other/subdirectory")
    183  );
    184  await PlacesTestUtils.addVisits(
    185    Services.io.newURI("ftp://some.subdomain.of.foo.com:8000/some/subdirectory")
    186  );
    187  await PlacesTestUtils.addVisits(Services.io.newURI("ftp://127.0.0.1:8080"));
    188  await PlacesTestUtils.addVisits(Services.io.newURI("https://localhost:8080"));
    189 
    190  // This will force the permission-manager to reload the data.
    191  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    192 
    193  // Force initialization of the PermissionManager
    194  for (let permission of Services.perms.all) {
    195    let isExpected = false;
    196 
    197    expected.forEach((it, i) => {
    198      if (
    199        permission.principal.origin == it[0] &&
    200        permission.type == it[1] &&
    201        permission.capability == it[2] &&
    202        permission.expireType == it[3] &&
    203        permission.expireTime == it[4]
    204      ) {
    205        isExpected = true;
    206        found[i]++;
    207      }
    208    });
    209 
    210    Assert.ok(
    211      isExpected,
    212      "Permission " +
    213        (isExpected ? "should" : "shouldn't") +
    214        " be in permission database: " +
    215        permission.principal.origin +
    216        ", " +
    217        permission.type +
    218        ", " +
    219        permission.capability +
    220        ", " +
    221        permission.expireType +
    222        ", " +
    223        permission.expireTime
    224    );
    225  }
    226 
    227  found.forEach((count, i) => {
    228    Assert.equal(
    229      count,
    230      1,
    231      "Expected count = 1, got count = " +
    232        count +
    233        " for permission " +
    234        expected[i]
    235    );
    236  });
    237 
    238  // Check to make sure that all of the tables which we care about are present
    239  {
    240    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    241    Assert.ok(db.tableExists("moz_perms"));
    242    Assert.ok(db.tableExists("moz_hosts"));
    243    Assert.ok(!db.tableExists("moz_perms_v6"));
    244 
    245    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    246    try {
    247      mozHostsCount.executeStep();
    248      Assert.equal(mozHostsCount.getInt64(0), 3);
    249    } finally {
    250      mozHostsCount.finalize();
    251    }
    252 
    253    let mozPermsCount = db.createStatement("SELECT count(*) FROM moz_perms");
    254    try {
    255      mozPermsCount.executeStep();
    256      Assert.equal(mozPermsCount.getInt64(0), expected.length);
    257    } finally {
    258      mozPermsCount.finalize();
    259    }
    260 
    261    db.close();
    262  }
    263 });