tor-browser

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

test_permmanager_migrate_6-7b.js (5536B)


      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 function GetPermissionsFile(profile) {
      7  let file = profile.clone();
      8  file.append(PERMISSIONS_FILE_NAME);
      9  return file;
     10 }
     11 
     12 add_task(function test() {
     13  // Create and set up the permissions database.
     14  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
     15  let profile = do_get_profile();
     16 
     17  // We need to execute a pm method to be sure that the DB is fully
     18  // initialized.
     19  var pm = Services.perms;
     20  Assert.equal(pm.all.length, 0, "No cookies");
     21 
     22  let db = Services.storage.openDatabase(GetPermissionsFile(profile));
     23  db.schemaVersion = 6;
     24  db.executeSimpleSQL("DROP TABLE moz_perms");
     25  db.executeSimpleSQL("DROP TABLE moz_hosts");
     26 
     27  /*
     28   * V5 table
     29   */
     30  db.executeSimpleSQL(
     31    "CREATE TABLE moz_perms (" +
     32      " id INTEGER PRIMARY KEY" +
     33      ",origin TEXT" +
     34      ",type TEXT" +
     35      ",permission INTEGER" +
     36      ",expireType INTEGER" +
     37      ",expireTime INTEGER" +
     38      ",modificationTime INTEGER" +
     39      ")"
     40  );
     41 
     42  let stmt6Insert = db.createStatement(
     43    "INSERT INTO moz_perms (" +
     44      "id, origin, type, permission, expireType, expireTime, modificationTime" +
     45      ") VALUES (" +
     46      ":id, :origin, :type, :permission, :expireType, :expireTime, :modificationTime" +
     47      ")"
     48  );
     49 
     50  let id = 0;
     51  function insertOrigin(
     52    origin,
     53    type,
     54    permission,
     55    expireType,
     56    expireTime,
     57    modificationTime
     58  ) {
     59    let thisId = id++;
     60 
     61    stmt6Insert.bindByName("id", thisId);
     62    stmt6Insert.bindByName("origin", origin);
     63    stmt6Insert.bindByName("type", type);
     64    stmt6Insert.bindByName("permission", permission);
     65    stmt6Insert.bindByName("expireType", expireType);
     66    stmt6Insert.bindByName("expireTime", expireTime);
     67    stmt6Insert.bindByName("modificationTime", modificationTime);
     68 
     69    try {
     70      stmt6Insert.execute();
     71    } finally {
     72      stmt6Insert.reset();
     73    }
     74 
     75    return {
     76      id: thisId,
     77      host: origin,
     78      type,
     79      permission,
     80      expireType,
     81      expireTime,
     82      modificationTime,
     83    };
     84  }
     85 
     86  // eslint-disable-next-line no-unused-vars
     87  let created6 = [
     88    insertOrigin("https://foo.com", "A", 2, 0, 0, 0),
     89    insertOrigin("http://foo.com", "A", 2, 0, 0, 0),
     90    insertOrigin("http://foo.com^appId=1000&inBrowser=1", "A", 2, 0, 0, 0),
     91  ];
     92 
     93  let created4 = []; // Didn't create any v4 entries, so the DB should be empty
     94 
     95  // CLose the db connection
     96  stmt6Insert.finalize();
     97  db.close();
     98  stmt6Insert = null;
     99  db = null;
    100 
    101  let expected = [
    102    ["https://foo.com", "A", 2, 0, 0, 0],
    103    ["http://foo.com", "A", 2, 0, 0, 0],
    104  ];
    105 
    106  let found = expected.map(() => 0);
    107 
    108  // This will force the permission-manager to reload the data.
    109  Services.obs.notifyObservers(null, "testonly-reload-permissions-from-disk");
    110 
    111  // Force initialization of the PermissionManager
    112  for (let permission of Services.perms.all) {
    113    let isExpected = false;
    114 
    115    expected.forEach((it, i) => {
    116      if (
    117        permission.principal.origin == it[0] &&
    118        permission.type == it[1] &&
    119        permission.capability == it[2] &&
    120        permission.expireType == it[3] &&
    121        permission.expireTime == it[4]
    122      ) {
    123        isExpected = true;
    124        found[i]++;
    125      }
    126    });
    127 
    128    Assert.ok(
    129      isExpected,
    130      "Permission " +
    131        (isExpected ? "should" : "shouldn't") +
    132        " be in permission database: " +
    133        permission.principal.origin +
    134        ", " +
    135        permission.type +
    136        ", " +
    137        permission.capability +
    138        ", " +
    139        permission.expireType +
    140        ", " +
    141        permission.expireTime
    142    );
    143  }
    144 
    145  found.forEach((count, i) => {
    146    Assert.equal(
    147      count,
    148      1,
    149      "Expected count = 1, got count = " +
    150        count +
    151        " for permission " +
    152        expected[i]
    153    );
    154  });
    155 
    156  // Check to make sure that all of the tables which we care about are present
    157  {
    158    db = Services.storage.openDatabase(GetPermissionsFile(profile));
    159    Assert.ok(db.tableExists("moz_perms"));
    160    Assert.ok(db.tableExists("moz_hosts"));
    161    Assert.ok(!db.tableExists("moz_hosts_is_backup"));
    162    Assert.ok(!db.tableExists("moz_perms_v6"));
    163 
    164    let mozHostsStmt = db.createStatement(
    165      "SELECT " +
    166        "host, type, permission, expireType, expireTime, " +
    167        "modificationTime, isInBrowserElement " +
    168        "FROM moz_hosts WHERE id = :id"
    169    );
    170    try {
    171      // Check that the moz_hosts table still contains the correct values.
    172      created4.forEach(it => {
    173        mozHostsStmt.reset();
    174        mozHostsStmt.bindByName("id", it.id);
    175        mozHostsStmt.executeStep();
    176        Assert.equal(mozHostsStmt.getUTF8String(0), it.host);
    177        Assert.equal(mozHostsStmt.getUTF8String(1), it.type);
    178        Assert.equal(mozHostsStmt.getInt64(2), it.permission);
    179        Assert.equal(mozHostsStmt.getInt64(3), it.expireType);
    180        Assert.equal(mozHostsStmt.getInt64(4), it.expireTime);
    181        Assert.equal(mozHostsStmt.getInt64(5), it.modificationTime);
    182        Assert.equal(mozHostsStmt.getInt64(6), it.isInBrowserElement);
    183      });
    184    } finally {
    185      mozHostsStmt.finalize();
    186    }
    187 
    188    // Check that there are the right number of values
    189    let mozHostsCount = db.createStatement("SELECT count(*) FROM moz_hosts");
    190    try {
    191      mozHostsCount.executeStep();
    192      Assert.equal(mozHostsCount.getInt64(0), created4.length);
    193    } finally {
    194      mozHostsCount.finalize();
    195    }
    196 
    197    db.close();
    198  }
    199 });