def test_Main_load_collect_timestamps(self): """When COLLECT_TIMESTAMPS=True, Main.load() should call Main.updateBridgeHistory(). """ # Mock the addOrUpdateBridgeHistory() function so that we don't try to # access the database: Main.addOrUpdateBridgeHistory = mockAddOrUpdateBridgeHistory state = self.state state.COLLECT_TIMESTAMPS = True # The reactor is deferring this to a thread, so the test execution # here isn't actually covering the Main.updateBridgeHistory() # function: Main.load(state, self.splitter)
def setUp(self): """Find the bridgedb.conf file in the top-level directory of this repo, copy it and the descriptor files it references to the current working directory, produce a state object from the loaded bridgedb.conf file, and make an HMAC key. """ # Get the bridgedb.conf file in the top-level directory of this repo: self.configFile = os.path.join(TOPDIR, 'bridgedb.conf') self.config = Main.loadConfig(self.configFile) # Copy the referenced descriptor files from bridgedb/run/ to CWD: self.config.STATUS_FILE = self._copyDescFilesHere([self.config.STATUS_FILE])[0] self.config.BRIDGE_FILES = self._copyDescFilesHere(self.config.BRIDGE_FILES) self.config.EXTRA_INFO_FILES = self._copyDescFilesHere(self.config.EXTRA_INFO_FILES) # Initialise the state self.state = Main.persistent.State(**self.config.__dict__) self.key = base64.b64decode('TvPS1y36BFguBmSOvhChgtXB2Lt+BOw0mGfz9SZe12Y=') # Create a BridgeSplitter self.splitter = MockBridgeHolder() # Functions which some tests mock, which we'll need to re-replace # later in tearDown(): self._orig_addOrUpdateBridgeHistory = Main.addOrUpdateBridgeHistory self._orig_sys_argv = sys.argv
def setUp(self): """Find the bridgedb.conf file in the top-level directory of this repo, copy it and the descriptor files it references to the current working directory, produce a state object from the loaded bridgedb.conf file, and make an HMAC key. """ # Get the bridgedb.conf file in the top-level directory of this repo: self.configFile = os.path.join(TOPDIR, 'bridgedb.conf') self.config = Main.loadConfig(self.configFile) # Copy the referenced descriptor files from bridgedb/run/ to CWD: self.config.STATUS_FILE = self._copyDescFilesHere( [self.config.STATUS_FILE])[0] self.config.BRIDGE_FILES = self._copyDescFilesHere( self.config.BRIDGE_FILES) self.config.EXTRA_INFO_FILES = self._copyDescFilesHere( self.config.EXTRA_INFO_FILES) # Initialise the state self.state = Main.persistent.State(**self.config.__dict__) self.key = base64.b64decode( 'TvPS1y36BFguBmSOvhChgtXB2Lt+BOw0mGfz9SZe12Y=') # Create a BridgeSplitter self.splitter = MockBridgeHolder() # Functions which some tests mock, which we'll need to re-replace # later in tearDown(): self._orig_addOrUpdateBridgeHistory = Main.addOrUpdateBridgeHistory self._orig_sys_argv = sys.argv
def test_Main_createBridgeRings(self): """Main.createBridgeRings() should add three hashrings to the splitter. """ proxyList = None (splitter, emailDist, httpsDist) = Main.createBridgeRings(self.config, proxyList, self.key) # Should have an IPBasedDistributor ring, an EmailDistributor ring, # and an UnallocatedHolder ring: self.assertEqual(len(splitter.ringsByName.keys()), 3)
def test_Main_createBridgeRings_no_reserved_share(self): """When RESERVED_SHARE=0, Main.createBridgeRings() should add only two hashrings to the splitter. """ proxyList = Main.proxy.ProxySet() config = self.config config.RESERVED_SHARE = 0 (splitter, emailDist, httpsDist) = Main.createBridgeRings(config, proxyList, self.key) # Should have an IPBasedDistributor ring, and an EmailDistributor ring: self.assertEqual(len(splitter.ringsByName.keys()), 2) self.assertNotIn('unallocated', splitter.rings)
def test_Main_createBridgeRings_no_email_dist(self): """When EMAIL_DIST=False, Main.createBridgeRings() should add only two hashrings to the splitter. """ proxyList = Main.proxy.ProxySet() config = self.config config.EMAIL_DIST = False (splitter, emailDist, httpsDist) = Main.createBridgeRings(config, proxyList, self.key) # Should have an IPBasedDistributor ring, and an UnallocatedHolder ring: self.assertEqual(len(splitter.ringsByName.keys()), 2) self.assertNotIn('email', splitter.rings) self.assertNotIn(emailDist, splitter.ringsByName.values())
def test_Main_createBridgeRings_no_https_dist(self): """When HTTPS_DIST=False, Main.createBridgeRings() should add only two hashrings to the hashring. """ proxyList = Main.proxy.ProxySet() config = self.config config.HTTPS_DIST = False (hashring, emailDist, httpsDist) = Main.createBridgeRings(config, proxyList, self.key) # Should have an EmailDistributor ring, and an UnallocatedHolder ring: self.assertEqual(len(hashring.ringsByName.keys()), 2) self.assertNotIn('https', hashring.rings) self.assertNotIn(httpsDist, hashring.ringsByName.values())
def _cbCallUpdateBridgeHistory(self, d, splitter): """Fake some timestamps for the bridges in the splitter, and then call Main.updateBridgeHistory(). """ def timestamp(): return datetime.fromtimestamp(random.randint(1324285117, 1524285117)) bridges = splitter._bridges timestamps = {} for fingerprint, _ in bridges.items(): timestamps[fingerprint] = [timestamp(), timestamp(), timestamp()] return Main.updateBridgeHistory(bridges, timestamps)
def test_Main_createBridgeRings_with_proxyList(self): """Main.createBridgeRings() should add three hashrings to the splitter and add the proxyList to the IPBasedDistibutor. """ exitRelays = ['1.1.1.1', '2.2.2.2', '3.3.3.3'] proxyList = Main.proxy.ProxySet() proxyList.addExitRelays(exitRelays) (splitter, emailDist, httpsDist) = Main.createBridgeRings(self.config, proxyList, self.key) # Should have an IPBasedDistributor ring, an EmailDistributor ring, # and an UnallocatedHolder ring: self.assertEqual(len(splitter.ringsByName.keys()), 3) self.assertGreater(len(httpsDist.categories), 0) self.assertItemsEqual(exitRelays, httpsDist.categories[-1])
def _cbCallUpdateBridgeHistory(self, d, splitter): """Fake some timestamps for the bridges in the splitter, and then call Main.updateBridgeHistory(). """ def timestamp(): return datetime.fromtimestamp( random.randint(1324285117, 1524285117)) bridges = splitter._bridges timestamps = {} for fingerprint, _ in bridges.items(): timestamps[fingerprint] = [timestamp(), timestamp(), timestamp()] return Main.updateBridgeHistory(bridges, timestamps)
def test_Main_createBridgeRings_two_file_buckets(self): """When FILE_BUCKETS has two filenames in it, Main.createBridgeRings() should add three hashrings to the splitter, then add two "pseudo-rings". """ proxyList = Main.proxy.ProxySet() config = self.config config.FILE_BUCKETS = { 'bridges-for-support-desk': 10, 'bridges-for-ooni-tests': 10, } (splitter, emailDist, httpsDist) = Main.createBridgeRings(config, proxyList, self.key) # Should have an IPBasedDistributor ring, an EmailDistributor, and an # UnallocatedHolder ring: self.assertEqual(len(splitter.ringsByName.keys()), 3) # Should have two pseudoRings: self.assertEqual(len(splitter.pseudoRings), 2) self.assertIn('pseudo_bridges-for-support-desk', splitter.pseudoRings) self.assertIn('pseudo_bridges-for-ooni-tests', splitter.pseudoRings)
def test_Main_reloadFn(self): """Main._reloadFn() should return True.""" self.assertTrue(Main._reloadFn())
def test_Main_handleSIGHUP(self): """Main._handleSIGHUP() should return True.""" raise unittest.SkipTest("_handleSIGHUP touches the reactor.") self.assertTrue(Main._handleSIGHUP())