tor-browser

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

test_permmanager_migrate_6-7a.js (10750B)


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