Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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
Example #4
0
    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
Example #5
0
 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)
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 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)
Example #9
0
 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())
Example #10
0
 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())
Example #11
0
 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())
Example #12
0
    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)
Example #13
0
 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])
Example #14
0
 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])
Example #15
0
    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)
Example #16
0
    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)
Example #17
0
    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)
Example #18
0
 def test_Main_reloadFn(self):
     """Main._reloadFn() should return True."""
     self.assertTrue(Main._reloadFn())
Example #19
0
    def test_Main_handleSIGHUP(self):
        """Main._handleSIGHUP() should return True."""
        raise unittest.SkipTest("_handleSIGHUP touches the reactor.")

        self.assertTrue(Main._handleSIGHUP())
Example #20
0
    def test_Main_handleSIGHUP(self):
        """Main._handleSIGHUP() should return True."""
        raise unittest.SkipTest("_handleSIGHUP touches the reactor.")

        self.assertTrue(Main._handleSIGHUP())
Example #21
0
 def test_Main_reloadFn(self):
     """Main._reloadFn() should return True."""
     self.assertTrue(Main._reloadFn())