tor-browser

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

test_permmanager_migrate_4-7.js (7889B)


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