Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Bug 860110 - Add backwards compatibility for profile user_version cha…
Browse files Browse the repository at this point in the history
…nge, r=jhammel
  • Loading branch information
Jonathan Griffin committed Apr 16, 2013
1 parent 6c32b26 commit 335f0fb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
22 changes: 15 additions & 7 deletions mozprofile/mozprofile/permissions.py
Expand Up @@ -234,8 +234,6 @@ def write_db(self, locations):
permDB = sqlite3.connect(os.path.join(self._profileDir, "permissions.sqlite"))
cursor = permDB.cursor();

cursor.execute("PRAGMA user_version=3;")

# SQL copied from
# http://mxr.mozilla.org/mozilla-central/source/extensions/cookie/nsPermissionManager.cpp
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
Expand All @@ -244,9 +242,7 @@ def write_db(self, locations):
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
expireTime INTEGER)""")

for location in locations:
# set the permissions
Expand All @@ -257,9 +253,21 @@ def write_db(self, locations):
permission_type = 1
else:
permission_type = 2
cursor.execute("INSERT INTO moz_hosts values(?, ?, ?, ?, 0, 0, 0, 0)",

rows = cursor.execute("PRAGMA table_info(moz_hosts)")
count = len(rows.fetchall())

# if the db contains 8 columns, we're using user_version 3
if count == 8:
statement = "INSERT INTO moz_hosts values(?, ?, ?, ?, 0, 0, 0, 0)"
cursor.execute("PRAGMA user_version=3;")
else:
statement = "INSERT INTO moz_hosts values(?, ?, ?, ?, 0, 0)"
cursor.execute("PRAGMA user_version=2;")

cursor.execute(statement,
(self._num_permissions, location.host, perm,
permission_type))
permission_type))

# Commit and close
permDB.commit()
Expand Down
2 changes: 1 addition & 1 deletion mozprofile/tests/bug785146.py
Expand Up @@ -49,7 +49,7 @@ def test_schema_version(self):
entries = cur.fetchall()

schema_version = entries[0][0]
self.assertEqual(schema_version, 3)
self.assertEqual(schema_version, 2)

if __name__ == '__main__':
unittest.main()
70 changes: 66 additions & 4 deletions mozprofile/tests/permissions.py
Expand Up @@ -36,7 +36,37 @@ def tearDown(self):
if self.locations_file:
self.locations_file.close()

def test_permissions_db(self):
def write_perm_db(self, version=3):
permDB = sqlite3.connect(os.path.join(self.profile_dir, "permissions.sqlite"))
cursor = permDB.cursor()

cursor.execute("PRAGMA user_version=%d;" % version)

if version == 3:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER,
appId INTEGER,
isInBrowserElement INTEGER)""")
elif version == 2:
cursor.execute("""CREATE TABLE IF NOT EXISTS moz_hosts (
id INTEGER PRIMARY KEY,
host TEXT,
type TEXT,
permission INTEGER,
expireType INTEGER,
expireTime INTEGER)""")
else:
raise Exception("version must be 2 or 3")

permDB.commit()
cursor.close()

def test_create_permissions_db(self):
perms = Permissions(self.profile_dir, self.locations_file.name)
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')

Expand All @@ -48,7 +78,7 @@ def test_permissions_db(self):
entries = cur.fetchall()

self.assertEqual(len(entries), 3)

self.assertEqual(entries[0][0], 'mochi.test')
self.assertEqual(entries[0][1], 'allowXULXBL')
self.assertEqual(entries[0][2], 1)
Expand All @@ -71,12 +101,17 @@ def test_permissions_db(self):
self.assertEqual(entries[3][1], 'allowXULXBL')
self.assertEqual(entries[3][2], 2)

# when creating a DB we should default to user_version==2
cur.execute('PRAGMA user_version')
entries = cur.fetchall()
self.assertEqual(entries[0][0], 2)

perms.clean_db()
# table should be removed
cur.execute("select * from sqlite_master where type='table'")
entries = cur.fetchall()
self.assertEqual(len(entries), 0)

def test_nw_prefs(self):
perms = Permissions(self.profile_dir, self.locations_file.name)

Expand All @@ -97,7 +132,6 @@ def test_nw_prefs(self):
'http://127.0.0.1:8888'))
self.assertEqual(prefs[5], ('capability.principal.codebase.p2.subjectName', ''))


prefs, user_prefs = perms.network_prefs(True)
self.assertEqual(len(user_prefs), 2)
self.assertEqual(user_prefs[0], ('network.proxy.type', 2))
Expand All @@ -112,6 +146,34 @@ def test_nw_prefs(self):
"if (isWebSocketSSL) return 'PROXY mochi.test:4443';")
self.assertTrue(all(c in user_prefs[1][1] for c in proxy_check))

def verify_user_version(self, version):
"""Verifies that we call INSERT statements using the correct number
of columns for existing databases.
"""
self.write_perm_db(version=version)
Permissions(self.profile_dir, self.locations_file.name)
perms_db_filename = os.path.join(self.profile_dir, 'permissions.sqlite')

select_stmt = 'select * from moz_hosts'

con = sqlite3.connect(perms_db_filename)
cur = con.cursor()
cur.execute(select_stmt)
entries = cur.fetchall()

self.assertEqual(len(entries), 3)

columns = 8 if version == 3 else 6
self.assertEqual(len(entries[0]), columns)
for x in range(4, columns):
self.assertEqual(entries[0][x], 0)

def test_existing_permissions_db_v2(self):
self.verify_user_version(2)

def test_existing_permissions_db_v3(self):
self.verify_user_version(3)


if __name__ == '__main__':
unittest.main()

0 comments on commit 335f0fb

Please sign in to comment.