tor-browser

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

test_permmanager_migrate_4-7_no_history.js (7969B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 var PERMISSIONS_FILE_NAME = "permissions.sqlite";
      5 
      6 /*
      7 * Prevent the nsINavHistoryService from being avaliable for the migration
      8 */
      9 
     10 var CONTRACT_ID = "@mozilla.org/browser/nav-history-service;1";
     11 var factory = {
     12  createInstance() {
     13    throw new Error("There is no history service");
     14  },
     15  QueryInterface: ChromeUtils.generateQI(["nsIFactory"]),
     16 };
     17 
     18 var newClassID = Services.uuid.generateUUID();
     19 
     20 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
     21 var oldClassID = registrar.contractIDToCID(CONTRACT_ID);
     22 // TODO: There was a var oldFactory = here causing linter errors as it
     23 // was unused. We should check if this function call is needed at all.
     24 Components.manager.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
     25 registrar.registerFactory(newClassID, "", CONTRACT_ID, factory);
     26 
     27 function cleanupFactory() {
     28  registrar.unregisterFactory(newClassID, factory);
     29  registrar.registerFactory(oldClassID, "", CONTRACT_ID, null);
     30 }
     31 
     32 function GetPermissionsFile(profile) {
     33  let file = profile.clone();
     34  file.append(PERMISSIONS_FILE_NAME);
     35  return file;
     36 }
     37 
     38 /*
     39 * Done nsINavHistoryService code
     40 */
     41 
     42 add_task(function test() {
     43  // Create and set up the permissions database.
     44  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
     45  let profile = do_get_profile();
     46 
     47  // Make sure that we can't resolve the nsINavHistoryService
     48  try {
     49    Cc["@mozilla.org/browser/nav-history-service;1"].getService(
     50      Ci.nsINavHistoryService
     51    );
     52    Assert.ok(false, "There shouldn't have been a nsINavHistoryService");
     53  } catch (e) {
     54    Assert.ok(true, "There wasn't a nsINavHistoryService");
     55  }
     56 
     57  // We need to execute a pm method to be sure that the DB is fully
     58  // initialized.
     59  var pm = Services.perms;
     60  Assert.greaterOrEqual(
     61    pm.all.length,
     62    0,
     63    "Permission manager not initialized?"
     64  );
     65 
     66  let db = Services.storage.openDatabase(GetPermissionsFile(profile));
     67  db.schemaVersion = 4;
     68  db.executeSimpleSQL("DROP TABLE moz_perms");
     69  db.executeSimpleSQL("DROP TABLE moz_hosts");
     70 
     71  db.executeSimpleSQL(
     72    "CREATE TABLE moz_hosts (" +
     73      " id INTEGER PRIMARY KEY" +
     74      ",host TEXT" +
     75      ",type TEXT" +
     76      ",permission INTEGER" +
     77      ",expireType INTEGER" +
     78      ",expireTime INTEGER" +
     79      ",modificationTime INTEGER" +
     80      ",appId INTEGER" +
     81      ",isInBrowserElement INTEGER" +
     82      ")"
     83  );
     84 
     85  let stmtInsert = db.createStatement(
     86    "INSERT INTO moz_hosts (" +
     87      "id, host, type, permission, expireType, expireTime, modificationTime, appId, isInBrowserElement" +
     88      ") VALUES (" +
     89      ":id, :host, :type, :permission, :expireType, :expireTime, :modificationTime, :appId, :isInBrowserElement" +
     90      ")"
     91  );
     92 
     93  let id = 0;
     94 
     95  function insertHost(
     96    host,
     97    type,
     98    permission,
     99    expireType,
    100    expireTime,
    101    modificationTime,
    102    appId,
    103    isInBrowserElement
    104  ) {
    105    let thisId = id++;
    106 
    107    stmtInsert.bindByName("id", thisId);
    108    stmtInsert.bindByName("host", host);
    109    stmtInsert.bindByName("type", type);
    110    stmtInsert.bindByName("permission", permission);
    111    stmtInsert.bindByName("expireType", expireType);
    112    stmtInsert.bindByName("expireTime", expireTime);
    113    stmtInsert.bindByName("modificationTime", modificationTime);
    114    stmtInsert.bindByName("appId", appId);
    115    stmtInsert.bindByName("isInBrowserElement", isInBrowserElement);
    116 
    117    try {
    118      stmtInsert.execute();
    119    } finally {
    120      stmtInsert.reset();
    121    }
    122 
    123    return {
    124      id: thisId,
    125      host,
    126      type,
    127      permission,
    128      expireType,
    129      expireTime,
    130      modificationTime,
    131      appId,
    132      isInBrowserElement,
    133    };
    134  }
    135 
    136  // Add some rows to the database
    137  // eslint-disable-next-line no-unused-vars
    138  let created = [
    139    insertHost("foo.com", "A", 1, 0, 0, 0, 0, false),
    140    insertHost("foo.com", "C", 1, 0, 0, 0, 0, false),
    141    insertHost("foo.com", "A", 1, 0, 0, 0, 1000, false),
    142    insertHost("foo.com", "A", 1, 0, 0, 0, 2000, true),
    143    insertHost("sub.foo.com", "B", 1, 0, 0, 0, 0, false),
    144    insertHost("subber.sub.foo.com", "B", 1, 0, 0, 0, 0, false),
    145    insertHost("bar.ca", "B", 1, 0, 0, 0, 0, false),
    146    insertHost("bar.ca", "B", 1, 0, 0, 0, 1000, false),
    147    insertHost("bar.ca", "A", 1, 0, 0, 0, 1000, true),
    148    insertHost("localhost", "A", 1, 0, 0, 0, 0, false),
    149    insertHost("127.0.0.1", "A", 1, 0, 0, 0, 0, false),
    150    insertHost("file:///some/path/to/file.html", "A", 1, 0, 0, 0, 0, false),
    151    insertHost("file:///another/file.html", "A", 1, 0, 0, 0, 0, false),
    152    insertHost(
    153      "moz-nullprincipal:{8695105a-adbe-4e4e-8083-851faa5ca2d7}",
    154      "A",
    155      1,
    156      0,
    157      0,
    158      0,
    159      0,
    160      false
    161    ),
    162    insertHost(
    163      "moz-nullprincipal:{12ahjksd-akjs-asd3-8393-asdu2189asdu}",
    164      "B",
    165      1,
    166      0,
    167      0,
    168      0,
    169      0,
    170      false
    171    ),
    172    insertHost("<file>", "A", 1, 0, 0, 0, 0, false),
    173    insertHost("<file>", "B", 1, 0, 0, 0, 0, false),
    174  ];
    175 
    176  // CLose the db connection
    177  stmtInsert.finalize();
    178  db.close();
    179  stmtInsert = null;
    180  db = null;
    181 
    182  let expected = [
    183    ["http://foo.com", "A", 1, 0, 0],
    184    ["http://foo.com", "C", 1, 0, 0],
    185    ["http://sub.foo.com", "B", 1, 0, 0],
    186    ["http://subber.sub.foo.com", "B", 1, 0, 0],
    187 
    188    ["https://foo.com", "A", 1, 0, 0],
    189    ["https://foo.com", "C", 1, 0, 0],
    190    ["https://sub.foo.com", "B", 1, 0, 0],
    191    ["https://subber.sub.foo.com", "B", 1, 0, 0],
    192 
    193    // bar.ca will have both http:// and https:// for all entries, because there are no associated history entries
    194    ["http://bar.ca", "B", 1, 0, 0],
    195    ["https://bar.ca", "B", 1, 0, 0],
    196    ["http://bar.ca", "A", 1, 0, 0],
    197    ["https://bar.ca", "A", 1, 0, 0],
    198    ["file:///some/path/to/file.html", "A", 1, 0, 0],
    199    ["file:///another/file.html", "A", 1, 0, 0],
    200 
    201    // Make sure that we also support localhost, and IP addresses
    202    ["http://localhost", "A", 1, 0, 0],
    203    ["https://localhost", "A", 1, 0, 0],
    204    ["http://127.0.0.1", "A", 1, 0, 0],
    205    ["https://127.0.0.1", "A", 1, 0, 0],
    206  ];
    207 
    208  let found = expected.map(() => 0);
    209 
    210  // This will force the permission-manager to reload the data.
    211  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    212 
    213  // Force initialization of the PermissionManager
    214  for (let permission of Services.perms.all) {
    215    let isExpected = false;
    216 
    217    expected.forEach((it, i) => {
    218      if (
    219        permission.principal.origin == it[0] &&
    220        permission.type == it[1] &&
    221        permission.capability == it[2] &&
    222        permission.expireType == it[3] &&
    223        permission.expireTime == it[4]
    224      ) {
    225        isExpected = true;
    226        found[i]++;
    227      }
    228    });
    229 
    230    Assert.ok(
    231      isExpected,
    232      "Permission " +
    233        (isExpected ? "should" : "shouldn't") +
    234        " be in permission database: " +
    235        permission.principal.origin +
    236        ", " +
    237        permission.type +
    238        ", " +
    239        permission.capability +
    240        ", " +
    241        permission.expireType +
    242        ", " +
    243        permission.expireTime
    244    );
    245  }
    246 
    247  found.forEach((count, i) => {
    248    Assert.equal(
    249      count,
    250      1,
    251      "Expected count = 1, got count = " +
    252        count +
    253        " for permission " +
    254        expected[i]
    255    );
    256  });
    257 
    258  // Check to make sure that all of the tables which we care about are present
    259  {
    260    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    261    Assert.ok(db.tableExists("moz_perms"));
    262    Assert.ok(db.tableExists("moz_hosts"));
    263    Assert.ok(!db.tableExists("moz_hosts_is_backup"));
    264    Assert.ok(!db.tableExists("moz_perms_v6"));
    265 
    266    // The moz_hosts table should still exist but be empty
    267    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    268    try {
    269      mozHostsCount.executeStep();
    270      Assert.equal(mozHostsCount.getInt64(0), 0);
    271    } finally {
    272      mozHostsCount.finalize();
    273    }
    274 
    275    db.close();
    276  }
    277 
    278  cleanupFactory();
    279 });