tor-browser

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

test_permmanager_migrate_5-7a.js (10755B)


      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 = 5;
     28  db.executeSimpleSQL("DROP TABLE moz_perms");
     29  db.executeSimpleSQL("DROP TABLE moz_hosts");
     30 
     31  /*
     32   * V5 table
     33   */
     34  db.executeSimpleSQL(
     35    "CREATE TABLE moz_hosts (" +
     36      " id INTEGER PRIMARY KEY" +
     37      ",origin TEXT" +
     38      ",type TEXT" +
     39      ",permission INTEGER" +
     40      ",expireType INTEGER" +
     41      ",expireTime INTEGER" +
     42      ",modificationTime INTEGER" +
     43      ")"
     44  );
     45 
     46  let stmt5Insert = db.createStatement(
     47    "INSERT INTO moz_hosts (" +
     48      "id, origin, type, permission, expireType, expireTime, modificationTime" +
     49      ") VALUES (" +
     50      ":id, :origin, :type, :permission, :expireType, :expireTime, :modificationTime" +
     51      ")"
     52  );
     53 
     54  /*
     55   * V4 table
     56   */
     57  db.executeSimpleSQL(
     58    "CREATE TABLE moz_hosts_v4 (" +
     59      " id INTEGER PRIMARY KEY" +
     60      ",host TEXT" +
     61      ",type TEXT" +
     62      ",permission INTEGER" +
     63      ",expireType INTEGER" +
     64      ",expireTime INTEGER" +
     65      ",modificationTime INTEGER" +
     66      ",appId INTEGER" +
     67      ",isInBrowserElement INTEGER" +
     68      ")"
     69  );
     70 
     71  let stmtInsert = db.createStatement(
     72    "INSERT INTO moz_hosts_v4 (" +
     73      "id, host, type, permission, expireType, expireTime, modificationTime, appId, isInBrowserElement" +
     74      ") VALUES (" +
     75      ":id, :host, :type, :permission, :expireType, :expireTime, :modificationTime, :appId, :isInBrowserElement" +
     76      ")"
     77  );
     78 
     79  let id = 0;
     80 
     81  function insertOrigin(
     82    origin,
     83    type,
     84    permission,
     85    expireType,
     86    expireTime,
     87    modificationTime
     88  ) {
     89    let thisId = id++;
     90 
     91    stmt5Insert.bindByName("id", thisId);
     92    stmt5Insert.bindByName("origin", origin);
     93    stmt5Insert.bindByName("type", type);
     94    stmt5Insert.bindByName("permission", permission);
     95    stmt5Insert.bindByName("expireType", expireType);
     96    stmt5Insert.bindByName("expireTime", expireTime);
     97    stmt5Insert.bindByName("modificationTime", modificationTime);
     98 
     99    try {
    100      stmt5Insert.execute();
    101    } finally {
    102      stmt5Insert.reset();
    103    }
    104 
    105    return {
    106      id: thisId,
    107      origin,
    108      type,
    109      permission,
    110      expireType,
    111      expireTime,
    112      modificationTime,
    113    };
    114  }
    115  function insertHost(
    116    host,
    117    type,
    118    permission,
    119    expireType,
    120    expireTime,
    121    modificationTime,
    122    appId,
    123    isInBrowserElement
    124  ) {
    125    let thisId = id++;
    126 
    127    stmtInsert.bindByName("id", thisId);
    128    stmtInsert.bindByName("host", host);
    129    stmtInsert.bindByName("type", type);
    130    stmtInsert.bindByName("permission", permission);
    131    stmtInsert.bindByName("expireType", expireType);
    132    stmtInsert.bindByName("expireTime", expireTime);
    133    stmtInsert.bindByName("modificationTime", modificationTime);
    134    stmtInsert.bindByName("appId", appId);
    135    stmtInsert.bindByName("isInBrowserElement", isInBrowserElement);
    136 
    137    try {
    138      stmtInsert.execute();
    139    } finally {
    140      stmtInsert.reset();
    141    }
    142 
    143    return {
    144      id: thisId,
    145      host,
    146      type,
    147      permission,
    148      expireType,
    149      expireTime,
    150      modificationTime,
    151      appId,
    152      isInBrowserElement,
    153    };
    154  }
    155 
    156  let created5 = [
    157    insertOrigin("https://foo.com", "A", 2, 0, 0, 0),
    158    insertOrigin("http://foo.com", "A", 2, 0, 0, 0),
    159    insertOrigin("http://foo.com^inBrowser=1", "A", 2, 0, 0, 0),
    160  ];
    161 
    162  // Add some rows to the database
    163  // eslint-disable-next-line no-unused-vars
    164  let created = [
    165    insertHost("foo.com", "A", 1, 0, 0, 0, 0, false),
    166    insertHost("foo.com", "C", 1, 0, 0, 0, 0, false),
    167    insertHost("foo.com", "A", 1, 0, 0, 0, 1000, false),
    168    insertHost("foo.com", "A", 1, 0, 0, 0, 2000, true),
    169    insertHost("sub.foo.com", "B", 1, 0, 0, 0, 0, false),
    170    insertHost("subber.sub.foo.com", "B", 1, 0, 0, 0, 0, false),
    171    insertHost("bar.ca", "B", 1, 0, 0, 0, 0, false),
    172    insertHost("bar.ca", "B", 1, 0, 0, 0, 1000, false),
    173    insertHost("bar.ca", "A", 1, 0, 0, 0, 1000, true),
    174    insertHost("localhost", "A", 1, 0, 0, 0, 0, false),
    175    insertHost("127.0.0.1", "A", 1, 0, 0, 0, 0, false),
    176    insertHost("192.0.2.235", "A", 1, 0, 0, 0, 0, false),
    177    insertHost("file:///some/path/to/file.html", "A", 1, 0, 0, 0, 0, false),
    178    insertHost("file:///another/file.html", "A", 1, 0, 0, 0, 0, false),
    179    insertHost(
    180      "moz-nullprincipal:{8695105a-adbe-4e4e-8083-851faa5ca2d7}",
    181      "A",
    182      1,
    183      0,
    184      0,
    185      0,
    186      0,
    187      false
    188    ),
    189    insertHost(
    190      "moz-nullprincipal:{12ahjksd-akjs-asd3-8393-asdu2189asdu}",
    191      "B",
    192      1,
    193      0,
    194      0,
    195      0,
    196      0,
    197      false
    198    ),
    199    insertHost("<file>", "A", 1, 0, 0, 0, 0, false),
    200    insertHost("<file>", "B", 1, 0, 0, 0, 0, false),
    201  ];
    202 
    203  // CLose the db connection
    204  stmt5Insert.finalize();
    205  stmtInsert.finalize();
    206  db.close();
    207  stmtInsert = null;
    208  db = null;
    209 
    210  let expected = [
    211    // The http:// entries under foo.com won't be inserted, as there are history entries for foo.com,
    212    // and http://foo.com or a subdomain are never visited.
    213    // ["http://foo.com", "A", 1, 0, 0],
    214    //
    215    // Because we search for port/scheme combinations under eTLD+1, we should not have http:// entries
    216    // for subdomains of foo.com either
    217    // ["http://sub.foo.com", "B", 1, 0, 0],
    218    // ["http://subber.sub.foo.com", "B", 1, 0, 0],
    219 
    220    ["https://foo.com", "A", 1, 0, 0],
    221    ["https://foo.com", "C", 1, 0, 0],
    222    ["https://sub.foo.com", "B", 1, 0, 0],
    223    ["https://subber.sub.foo.com", "B", 1, 0, 0],
    224 
    225    // bar.ca will have both http:// and https:// for all entries, because there are no associated history entries
    226    ["http://bar.ca", "B", 1, 0, 0],
    227    ["https://bar.ca", "B", 1, 0, 0],
    228    ["http://bar.ca", "A", 1, 0, 0],
    229    ["https://bar.ca", "A", 1, 0, 0],
    230    ["file:///some/path/to/file.html", "A", 1, 0, 0],
    231    ["file:///another/file.html", "A", 1, 0, 0],
    232 
    233    // Because we put ftp://some.subdomain.of.foo.com:8000/some/subdirectory in the history, we should
    234    // also have these entries
    235    ["ftp://foo.com:8000", "A", 1, 0, 0],
    236    ["ftp://foo.com:8000", "C", 1, 0, 0],
    237 
    238    // In addition, because we search for port/scheme combinations under eTLD+1, we should have the
    239    // following entries
    240    ["ftp://sub.foo.com:8000", "B", 1, 0, 0],
    241    ["ftp://subber.sub.foo.com:8000", "B", 1, 0, 0],
    242 
    243    // Make sure that we also support localhost, and IP addresses
    244    ["http://localhost", "A", 1, 0, 0],
    245    ["https://localhost", "A", 1, 0, 0],
    246    ["http://127.0.0.1", "A", 1, 0, 0],
    247    ["https://127.0.0.1", "A", 1, 0, 0],
    248    ["http://192.0.2.235", "A", 1, 0, 0],
    249    ["https://192.0.2.235", "A", 1, 0, 0],
    250  ];
    251 
    252  let found = expected.map(() => 0);
    253 
    254  // Add some places to the places database
    255  await PlacesTestUtils.addVisits(
    256    Services.io.newURI("https://foo.com/some/other/subdirectory")
    257  );
    258  await PlacesTestUtils.addVisits(
    259    Services.io.newURI("ftp://some.subdomain.of.foo.com:8000/some/subdirectory")
    260  );
    261 
    262  // This will force the permission-manager to reload the data.
    263  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    264 
    265  // Force initialization of the PermissionManager
    266  for (let permission of Services.perms.all) {
    267    let isExpected = false;
    268 
    269    expected.forEach((it, i) => {
    270      if (
    271        permission.principal.origin == it[0] &&
    272        permission.type == it[1] &&
    273        permission.capability == it[2] &&
    274        permission.expireType == it[3] &&
    275        permission.expireTime == it[4]
    276      ) {
    277        isExpected = true;
    278        found[i]++;
    279      }
    280    });
    281 
    282    Assert.ok(
    283      isExpected,
    284      "Permission " +
    285        (isExpected ? "should" : "shouldn't") +
    286        " be in permission database: " +
    287        permission.principal.origin +
    288        ", " +
    289        permission.type +
    290        ", " +
    291        permission.capability +
    292        ", " +
    293        permission.expireType +
    294        ", " +
    295        permission.expireTime
    296    );
    297  }
    298 
    299  found.forEach((count, i) => {
    300    Assert.equal(
    301      count,
    302      1,
    303      "Expected count = 1, got count = " +
    304        count +
    305        " for permission " +
    306        expected[i]
    307    );
    308  });
    309 
    310  // Check to make sure that all of the tables which we care about are present
    311  {
    312    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    313    Assert.ok(db.tableExists("moz_perms"));
    314    Assert.ok(db.tableExists("moz_hosts"));
    315    Assert.ok(!db.tableExists("moz_hosts_is_backup"));
    316    Assert.ok(db.tableExists("moz_perms_v6"));
    317 
    318    // The moz_hosts table should still exist but be empty
    319    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    320    try {
    321      mozHostsCount.executeStep();
    322      Assert.equal(mozHostsCount.getInt64(0), 0);
    323    } finally {
    324      mozHostsCount.finalize();
    325    }
    326 
    327    // Check that the moz_perms_v6 table contains the backup of the entry we created
    328    let mozPermsV6Stmt = db.createStatement(
    329      "SELECT " +
    330        "origin, type, permission, expireType, expireTime, modificationTime " +
    331        "FROM moz_perms_v6 WHERE id = :id"
    332    );
    333    try {
    334      // Check that the moz_hosts table still contains the correct values.
    335      created5.forEach(it => {
    336        mozPermsV6Stmt.reset();
    337        mozPermsV6Stmt.bindByName("id", it.id);
    338        mozPermsV6Stmt.executeStep();
    339        Assert.equal(mozPermsV6Stmt.getUTF8String(0), it.origin);
    340        Assert.equal(mozPermsV6Stmt.getUTF8String(1), it.type);
    341        Assert.equal(mozPermsV6Stmt.getInt64(2), it.permission);
    342        Assert.equal(mozPermsV6Stmt.getInt64(3), it.expireType);
    343        Assert.equal(mozPermsV6Stmt.getInt64(4), it.expireTime);
    344        Assert.equal(mozPermsV6Stmt.getInt64(5), it.modificationTime);
    345      });
    346    } finally {
    347      mozPermsV6Stmt.finalize();
    348    }
    349 
    350    // Check that there are the right number of values
    351    let mozPermsV6Count = db.createStatement(
    352      "SELECT count(*) FROM moz_perms_v6"
    353    );
    354    try {
    355      mozPermsV6Count.executeStep();
    356      Assert.equal(mozPermsV6Count.getInt64(0), created5.length);
    357    } finally {
    358      mozPermsV6Count.finalize();
    359    }
    360 
    361    db.close();
    362  }
    363 });