def test_insertBridgeAndGetRing_new_bridge(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings)
def test_getBridgeDistributor_recognised(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings) self.assertEqual(ringname, 'moat') db.commit() with Storage.getDB() as db: ringname = db.getBridgeDistributor(bridge, self.validRings) self.assertEqual(ringname, 'moat')
def test_fetchBridgeMeasurements(self): query = "INSERT INTO BridgeMeasurements (hex_key, bridge_type, " \ "address, port, blocking_country, blocking_asn, " \ "measured_by, last_measured, verdict) VALUES ('key', " \ "'obfs4', '1.2.3.4', '1234', 'RU', '1234', 'OONI', '%s', 1)" oldMsmt = query % "2017-01-01" newMsmt = query % datetime.datetime.utcnow().strftime("%Y-%m-%d") Storage.initializeDBLock() with Storage.getDB() as db: db._cur.execute(oldMsmt) # We're calling _Database__fetchBridgeMeasurements instead of # __fetchBridgeMeasurements to account for Python's name meddling. rows = db._Database__fetchBridgeMeasurements() # Outdated measurements should not be returned. self.assertEquals(len(rows), 0) db._cur.execute(newMsmt) rows = db._Database__fetchBridgeMeasurements() # Measurements that are "young enough" should be returned. self.assertEquals(len(rows), 1)
def test_getDB_AcquireLock(self): Storage.initializeDBLock() with Storage.getDB() as db: self.assertIsInstance(db, Storage.Database) self.assertTrue(Storage.dbIsLocked()) self.assertEqual(db, Storage._OPENED_DB)
def test_main_loadBlockedBridges(self): Storage.initializeDBLock() # Mock configuration object that we use to initialize our bridge rings. class Cfg(object): def __init__(self): self.FORCE_PORTS = [(443, 1)] self.FORCE_FLAGS = [("Stable", 1)] self.MOAT_DIST = False self.HTTPS_DIST = True self.HTTPS_SHARE = 10 self.N_IP_CLUSTERS = 1 self.EMAIL_DIST = False self.RESERVED_SHARE = 0 bridge = self.fakeBridges[0] addr, port, _ = bridge.orAddresses[0] cc = "de" # Mock object that we use to simulate a database connection. class DummyDB(object): def __init__(self): pass def __enter__(self): return self def __exit__(self, type, value, traceback): pass def getBlockedBridges(self): return {bridge.fingerprint: [(cc, addr, port)]} def getBridgeDistributor(self, bridge, validRings): return "https" def insertBridgeAndGetRing(self, bridge, setRing, seenAt, validRings, defaultPool="unallocated"): return "https" def commit(self): pass oldObj = Storage.getDB Storage.getDB = DummyDB hashring, _, _, _ = main.createBridgeRings(Cfg(), None, b'key') hashring.insert(bridge) self.assertEqual(len(hashring), 1) self.assertFalse(bridge.isBlockedIn(cc)) self.assertFalse(bridge.isBlockedIn("ab")) self.assertFalse(bridge.addressIsBlockedIn(cc, addr, port)) main.loadBlockedBridges(hashring) self.assertTrue(bridge.isBlockedIn(cc)) self.assertFalse(bridge.isBlockedIn("ab")) self.assertTrue(bridge.addressIsBlockedIn(cc, addr, port)) Storage.getDB = oldObj