def setUp(self): helpers.init_glsettings_for_unit_tests() GLSettings.db_path = os.path.join(GLSettings.ramdisk_path, 'db_test') shutil.rmtree(GLSettings.db_path, True) os.mkdir(GLSettings.db_path) db_name = 'glbackend-%d.db' % DATABASE_VERSION db_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'db', 'populated', db_name) shutil.copyfile(db_path, os.path.join(GLSettings.db_path, db_name)) self.db_file = os.path.join(GLSettings.db_path, db_name) GLSettings.db_uri = GLSettings.make_db_uri(self.db_file) # place a dummy version in the current db store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.dummy_ver = '2.XX.XX' prv.set_val(u'version', self.dummy_ver) self.assertEqual(prv.get_val(u'version'), self.dummy_ver) store.commit() store.close() # backup various mocks that we will use self._bck_f = config.is_cfg_valid GLConfig['private']['xx_smtp_password'] = GLConfig['private'].pop( 'smtp_password') self.dp = u'yes_you_really_should_change_me'
def test_mig_37_valid_tor_hs_key(self): self._initStartDB(36) from globaleaks.db.migrations import update_37 t = update_37.TOR_DIR update_37.TOR_DIR = GLSettings.db_path pk_path = os.path.join(update_37.TOR_DIR, 'private_key') hn_path = os.path.join(update_37.TOR_DIR, 'hostname') shutil.copy(os.path.join(helpers.DATA_DIR, 'tor/private_key'), pk_path) shutil.copy(os.path.join(helpers.DATA_DIR, 'tor/hostname'), hn_path) ret = update_db() self.assertEqual(ret, None) new_uri = GLSettings.make_db_uri( os.path.join(GLSettings.db_path, GLSettings.db_file_name)) store = Store(create_database(new_uri)) hs = config.NodeFactory(store).get_val('onionservice') pk = config.PrivateFactory(store).get_val('tor_onion_key') self.assertEqual('lftx7dbyvlc5txtl.onion', hs) with open(os.path.join(helpers.DATA_DIR, 'tor/ephemeral_service_key')) as f: saved_key = f.read().strip() self.assertEqual(saved_key, pk) store.close() shutil.rmtree(GLSettings.db_path) update_37.TOR_DIR = t
def test_check_unmodifiable_strings(self): # This test case asserts that data migration updates unmodifiable l10n strings self._initStartDB(34) notification_l10n = NotificationL10NFactory(self.store) t0 = notification_l10n.get_val('export_template', 'it') notification_l10n.set_val('export_template', 'it', '') t1 = notification_l10n.get_val('export_template', 'it') self.assertEqual(t1, '') self.store.commit() # place a dummy version in the current db store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.dummy_ver = '2.XX.XX' prv.set_val('version', self.dummy_ver) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.commit() store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) notification_l10n = NotificationL10NFactory(store) t2 = notification_l10n.get_val('export_template', 'it') self.assertEqual(t2, t0) store.commit() store.close() shutil.rmtree(GLSettings.db_path)
def test_version_change_success(self): migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), __version__) store.close()
def _test_missing_config(self, store): self.assertEqual(True, config.is_cfg_valid(store)) p = config.Config('private', 'smtp_password', 'XXXX') p.var_group = u'outside' store.add(p) self.assertEqual(False, config.is_cfg_valid(store)) node = config.NodeFactory(store) c = node.get_cfg(u'hostname') store.remove(c) store.commit() self.assertEqual(False, node.db_corresponds()) # Delete all of the vars in Private Factory prv = config.PrivateFactory(store) store.execute('DELETE FROM config WHERE var_group = "private"') self.assertEqual(False, prv.db_corresponds()) ntfn = config.NotificationFactory(store) c = config.Config('notification', 'server', 'guarda.giochi.con.occhi') c.var_name = u'anextravar' store.add(c) self.assertEqual(False, ntfn.db_corresponds()) config.update_defaults(store) self.assertEqual(True, config.is_cfg_valid(store))
def test_ver_change_exception(self): # Explicity throw an exception in managed_ver_update via is_cfg_valid config.is_cfg_valid = apply_gen(throw_excep) self.assertRaises(IOError, migration.perform_data_update, self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val(u'version'), self.dummy_ver) store.close()
def test_version_change_not_ok(self): # Set is_config_valid to false during managed ver update config.is_cfg_valid = apply_gen(mod_bool) self.assertRaises(Exception, migration.perform_data_update, self.db_file) # Ensure the rollback has succeeded store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val(u'version'), self.dummy_ver) store.close()
def postconditions_36(self): new_uri = GLSettings.make_db_uri( os.path.join(GLSettings.db_path, GLSettings.db_file_name)) store = Store(create_database(new_uri)) hs = config.NodeFactory(store).get_val(u'onionservice') pk = config.PrivateFactory(store).get_val(u'tor_onion_key') self.assertEqual('lftx7dbyvlc5txtl.onion', hs) with open(os.path.join(helpers.DATA_DIR, 'tor/ephemeral_service_key')) as f: saved_key = f.read().strip() self.assertEqual(saved_key, pk) store.close()
def test_ver_change_exception(self): # Explicity throw an exception in managed_ver_update via is_cfg_valid config.is_cfg_valid = apply_gen(throw_excep) try: yield migration.perform_data_update(self.db_file) self.fail() except IOError as e: self.assertIsInstance(e, IOError) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.close()
def test_detect_and_fix_cfg_change(self): store = Store(create_database(GLSettings.db_uri)) ret = config.is_cfg_valid(store) self.assertFalse(ret) store.close() migration.perform_data_update(self.db_file) store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val(u'version'), __version__) self.assertEqual(prv.get_val(u'xx_smtp_password'), self.dp) ret = config.is_cfg_valid(store) self.assertTrue(ret) store.close()
def test_version_change_not_ok(self): # Set is_config_valid to false during managed ver update config.is_cfg_valid = apply_gen(mod_bool) try: yield migration.perform_data_update(self.db_file) self.fail() except DatabaseIntegrityError as e: self.assertIsInstance(e, DatabaseIntegrityError) # Ensure the rollback has succeeded store = Store(create_database(GLSettings.db_uri)) prv = config.PrivateFactory(store) self.assertEqual(prv.get_val('version'), self.dummy_ver) store.close()