tor-browser

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

test_permmanager_migrate_5-7b.js (5648B)


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