def test_matching_referer_is_allowed(self): mock_conf_settings(self, {'api_host': 'the_api', 'api_port': 1111}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock( return_value='http://the_api:1111?settings') self.assertTrue(self.server._check_header_source(request, 'Referer')) request.getHeader.assert_called_with('Referer')
def setUp(self): mocks.mock_conf_settings(self) self.session = None self.stream_info_manager = None self.lbry_file_manager = None self.addCleanup(self.take_down_env) self.is_generous = True
def test_request_is_rejected_when_not_matching_allowed_origin_setting( self): mock_conf_settings(self, {'allowed_origin': 'http://example.com:1234'}) request = mock.Mock(['getHeader']) # note the ports don't match request.getHeader = mock.Mock(return_value='http://example.com:1235') self.assertFalse(self.server._check_header_source(request, 'Origin'))
def setUp(self): mocks.mock_conf_settings(self) self.server_processes = [] self.session = None self.lbry_file_manager = None self.is_generous = True self.addCleanup(self.take_down_env)
def setUp(self): mock_conf_settings(self) self.blob_manager = mock.Mock() self.payment_rate_manager = NegotiatedPaymentRateManager( BasePaymentRateManager(0.001), DummyBlobAvailabilityTracker()) self.handler = BlobRequestHandler.BlobRequestHandler( self.blob_manager, None, self.payment_rate_manager, None)
def test_any_origin_is_allowed(self): mock_conf_settings(self, {'api_host': '0.0.0.0', 'api_port': 80}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://example.com') self.assertTrue(self.server._check_header_source(request, 'Origin')) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://another-example.com') self.assertTrue(self.server._check_header_source(request, 'Origin'))
def setUp(self): mocks.mock_conf_settings(self) self.session = None self.lbry_file_manager = None self.is_generous = True self.db_dir = tempfile.mkdtemp() self.blob_dir = os.path.join(self.db_dir, "blobfiles") os.mkdir(self.blob_dir)
def setUp(self): def noop(): return None mock_conf_settings(self) util.resetTime(self) self.test_daemon = get_test_daemon() self.test_daemon.session.wallet.is_first_run = False self.test_daemon.session.wallet.get_best_blockhash = noop
def test_any_origin_is_allowed(self): mock_conf_settings(self, {'api_host': '0.0.0.0', 'api_port': 80}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://example.com') self.assertTrue(self.server._check_header_source(request, 'Origin')) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock( return_value='http://another-example.com') self.assertTrue(self.server._check_header_source(request, 'Origin'))
def setUp(self): def noop(): return None mock_conf_settings(self) util.resetTime(self) self.test_daemon = get_test_daemon() self.test_daemon.session.wallet = Wallet.LBRYumWallet(storage=Wallet.InMemoryStorage()) self.test_daemon.session.wallet.network = FakeNetwork() self.test_daemon.session.wallet.get_best_blockhash = noop
def setUp(self): mocks.mock_conf_settings(self) self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir() self.session = mock.Mock(spec=Session.Session)(None, None) self.session.payment_rate_manager.min_blob_data_payment_rate = 0 self.blob_manager = BlobManager.DiskBlobManager( self.tmp_blob_dir, SQLiteStorage(self.tmp_db_dir)) self.session.blob_manager = self.blob_manager self.session.storage = self.session.blob_manager.storage self.file_manager = EncryptedFileManager.EncryptedFileManager( self.session, object()) yield self.session.blob_manager.storage.setup() yield self.session.blob_manager.setup()
def setUp(self): self.reactor = Clock() mocks.mock_conf_settings(self) self.component_manager = ComponentManager( skip_components=[ DATABASE_COMPONENT, DHT_COMPONENT, HASH_ANNOUNCER_COMPONENT, STREAM_IDENTIFIER_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT, HEADERS_COMPONENT, PAYMENT_RATE_COMPONENT, RATE_LIMITER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT ], reactor=self.reactor, wallet=mocks.FakeDelayedWallet, file_manager=mocks.FakeDelayedFileManager, blob_manager=mocks.FakeDelayedBlobManager)
def setUp(self): mocks.mock_conf_settings(self) self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir() self.wallet = FakeWallet() self.peer_manager = PeerManager() self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2) self.rate_limiter = DummyRateLimiter() self.sd_identifier = StreamDescriptorIdentifier() self.storage = SQLiteStorage(self.tmp_db_dir) self.blob_manager = DiskBlobManager(self.tmp_blob_dir, self.storage) self.prm = OnlyFreePaymentsManager() self.lbry_file_manager = EncryptedFileManager( self.peer_finder, self.rate_limiter, self.blob_manager, self.wallet, self.prm, self.storage, self.sd_identifier) d = self.storage.setup() d.addCallback(lambda _: self.lbry_file_manager.setup()) return d
def setUp(self): mocks.mock_conf_settings(self) self.default_components_sort = [ [ Components.HeadersComponent, Components.DatabaseComponent, Components.ExchangeRateManagerComponent, Components.PaymentRateComponent, Components.RateLimiterComponent, Components.UPnPComponent ], [Components.DHTComponent, Components.WalletComponent], [Components.BlobComponent, Components.HashAnnouncerComponent], [ Components.PeerProtocolServerComponent, Components.StreamIdentifierComponent ], [Components.FileManagerComponent], [Components.ReflectorComponent] ] self.component_manager = ComponentManager()
def setUp(self): mock_conf_settings(self) util.resetTime(self) self.faker = Faker('en_US') self.faker.seed(66410) self.test_daemon = get_test_daemon() self.test_daemon.lbry_file_manager = mock.Mock(spec=EncryptedFileManager) self.test_daemon.lbry_file_manager.lbry_files = self._get_fake_lbry_files() # Pre-sorted lists of prices and file names in ascending order produced by # faker with seed 66410. This seed was chosen becacuse it produces 3 results # 'points_paid' at 6.0 and 2 results at 4.5 to test multiple sort criteria. self.test_points_paid = [0.2, 2.9, 4.5, 4.5, 6.0, 6.0, 6.0, 6.8, 7.1, 9.2] self.test_file_names = ['also.mp3', 'better.css', 'call.mp3', 'pay.jpg', 'record.pages', 'sell.css', 'strategy.pages', 'thousand.pages', 'town.mov', 'vote.ppt'] self.test_authors = ['angela41', 'edward70', 'fhart', 'johnrosales', 'lucasfowler', 'peggytorres', 'qmitchell', 'trevoranderson', 'xmitchell', 'zhangsusan']
def setUp(self): mocks.mock_conf_settings(self) self.db_dir, self.blob_dir = mk_db_and_blob_dir() self.wallet = FakeWallet() self.peer_manager = PeerManager() self.peer_finder = FakePeerFinder(5553, self.peer_manager, 1) self.rate_limiter = RateLimiter() self.prm = OnlyFreePaymentsManager() self.storage = SQLiteStorage(self.db_dir) self.blob_manager = DiskBlobManager(self.blob_dir, self.storage) self.sd_identifier = StreamDescriptorIdentifier() self.lbry_file_manager = EncryptedFileManager( self.peer_finder, self.rate_limiter, self.blob_manager, self.wallet, self.prm, self.storage, self.sd_identifier) self.uploader = LbryUploader(5209343) self.sd_hash = yield self.uploader.setup() yield self.storage.setup() yield self.blob_manager.setup() yield self.lbry_file_manager.setup() yield add_lbry_file_to_sd_identifier(self.sd_identifier)
def setUp(self): mocks.mock_conf_settings(self) self.session = None self.lbry_file_manager = None self.is_generous = True self.db_dir = tempfile.mkdtemp() self.blob_dir = os.path.join(self.db_dir, "blobfiles") os.mkdir(self.blob_dir) self.dht_node = FakeNode() self.wallet = FakeWallet() self.peer_manager = PeerManager() self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2) self.rate_limiter = DummyRateLimiter() self.sd_identifier = StreamDescriptorIdentifier() self.storage = SQLiteStorage(self.db_dir) self.blob_manager = DiskBlobManager(self.blob_dir, self.storage, self.dht_node._dataStore) self.prm = OnlyFreePaymentsManager() self.lbry_file_manager = EncryptedFileManager( self.peer_finder, self.rate_limiter, self.blob_manager, self.wallet, self.prm, self.storage, self.sd_identifier) d = self.storage.setup() d.addCallback(lambda _: self.lbry_file_manager.setup()) return d
def setUp(self): mock_conf_settings(self) util.resetTime(self) self.faker = Faker('en_US') self.faker.seed(66410) self.test_daemon = get_test_daemon() self.test_daemon.file_manager.lbry_files = self._get_fake_lbry_files() # Pre-sorted lists of prices and file names in ascending order produced by # faker with seed 66410. This seed was chosen becacuse it produces 3 results # 'points_paid' at 6.0 and 2 results at 4.5 to test multiple sort criteria. self.test_points_paid = [ 0.2, 2.9, 4.5, 4.5, 6.0, 6.0, 6.0, 6.8, 7.1, 9.2 ] self.test_file_names = [ 'alias.mp3', 'atque.css', 'commodi.mp3', 'nulla.jpg', 'praesentium.pages', 'quidem.css', 'rerum.pages', 'suscipit.pages', 'temporibus.mov', 'velit.ppt' ] self.test_authors = [ 'angela41', 'edward70', 'fhart', 'johnrosales', 'lucasfowler', 'peggytorres', 'qmitchell', 'trevoranderson', 'xmitchell', 'zhangsusan' ] return self.test_daemon.component_manager.setup()
def setUp(self): mock_conf_settings(self)
def setUp(self): mock_conf_settings(self) util.resetTime(self)
def test_matching_origin_is_allowed(self): mock_conf_settings(self, {'api_host': 'example.com', 'api_port': 1234}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://example.com:1234') self.assertTrue(self.server._check_header_source(request, 'Origin'))
def test_matching_referer_is_allowed(self): mock_conf_settings(self, {'api_host': 'the_api', 'api_port': 1111}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://the_api:1111?settings') self.assertTrue(self.server._check_header_source(request, 'Referer')) request.getHeader.assert_called_with('Referer')
def test_wrong_port_is_rejected(self): mock_conf_settings(self, {'api_port': 1234}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://localhost:9999') self.assertFalse(self.server._check_header_source(request, 'Origin'))
def test_foreign_origin_is_rejected(self): mock_conf_settings(self) # have to call this to generate Config mock request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://example.com') self.assertFalse(self.server._check_header_source(request, 'Origin'))
def test_request_is_rejected_when_not_matching_allowed_origin_setting(self): mock_conf_settings(self, {'allowed_origin': 'http://example.com:1234'}) request = mock.Mock(['getHeader']) # note the ports don't match request.getHeader = mock.Mock(return_value='http://example.com:1235') self.assertFalse(self.server._check_header_source(request, 'Origin'))
def setUp(self): mocks.mock_conf_settings(self) self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir()
def test_request_is_allowed_when_matching_allowed_origin_setting(self): mock_conf_settings(self, {'allowed_origin': 'http://example.com:1234'}) request = mock.Mock(['getHeader']) request.getHeader = mock.Mock(return_value='http://example.com:1234') self.assertTrue(self.server._check_header_source(request, 'Origin'))
def setUp(self): mocks.mock_conf_settings(self) self.session = None self.stream_info_manager = None self.lbry_file_manager = None self.server_blob_manager = None self.reflector_port = None self.port = None self.addCleanup(self.take_down_env) wallet = mocks.Wallet() peer_manager = PeerManager.PeerManager() peer_finder = mocks.PeerFinder(5553, peer_manager, 2) hash_announcer = mocks.Announcer() rate_limiter = RateLimiter.DummyRateLimiter() sd_identifier = StreamDescriptor.StreamDescriptorIdentifier() self.expected_blobs = [ ('dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b' '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152), ('f4067522c1b49432a2a679512e3917144317caa1abba0c04' '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152), ('305486c434260484fcb2968ce0e963b72f81ba56c11b08b1' 'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056), ] self.db_dir, self.blob_dir = mk_db_and_blob_dir() self.session = Session.Session( conf.settings['data_rate'], db_dir=self.db_dir, lbryid="abcd", peer_finder=peer_finder, hash_announcer=hash_announcer, blob_dir=self.blob_dir, peer_port=5553, use_upnp=False, rate_limiter=rate_limiter, wallet=wallet, blob_tracker_class=mocks.BlobAvailabilityTracker, dht_node_class=Node) self.stream_info_manager = EncryptedFileMetadataManager.DBEncryptedFileMetadataManager( self.db_dir) self.lbry_file_manager = EncryptedFileManager.EncryptedFileManager( self.session, self.stream_info_manager, sd_identifier) self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir() self.server_blob_manager = BlobManager.DiskBlobManager( hash_announcer, self.server_blob_dir, self.server_db_dir) self.server_stream_info_manager = \ EncryptedFileMetadataManager.DBEncryptedFileMetadataManager(self.server_db_dir) d = self.session.setup() d.addCallback(lambda _: self.stream_info_manager.setup()) d.addCallback(lambda _: EncryptedFileOptions. add_lbry_file_to_sd_identifier(sd_identifier)) d.addCallback(lambda _: self.lbry_file_manager.setup()) d.addCallback(lambda _: self.server_blob_manager.setup()) d.addCallback(lambda _: self.server_stream_info_manager.setup()) def verify_equal(sd_info): self.assertEqual(mocks.create_stream_sd_file, sd_info) def save_sd_blob_hash(sd_hash): self.sd_hash = sd_hash self.expected_blobs.append((sd_hash, 923)) def verify_stream_descriptor_file(stream_hash): self.stream_hash = stream_hash d = lbry_file.get_sd_info( self.lbry_file_manager.stream_info_manager, stream_hash, True) d.addCallback(verify_equal) d.addCallback(lambda _: lbry_file.publish_sd_blob( self.lbry_file_manager.stream_info_manager, self.session. blob_manager, stream_hash)) d.addCallback(save_sd_blob_hash) return d def create_stream(): test_file = mocks.GenFile( 5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) d = EncryptedFileCreator.create_lbry_file( self.session, self.lbry_file_manager, "test_file", test_file, key="0123456701234567", iv_generator=iv_generator()) return d def start_server(): server_factory = reflector.ServerFactory( peer_manager, self.server_blob_manager, self.server_stream_info_manager) from twisted.internet import reactor port = 8943 while self.reflector_port is None: try: self.reflector_port = reactor.listenTCP( port, server_factory) self.port = port except error.CannotListenError: port += 1 d.addCallback(lambda _: create_stream()) d.addCallback(verify_stream_descriptor_file) d.addCallback(lambda _: start_server()) return d
def setUp(self): mocks.mock_conf_settings(self) self.session = None self.stream_info_manager = None self.lbry_file_manager = None self.server_blob_manager = None self.reflector_port = None self.port = None self.addCleanup(self.take_down_env) wallet = mocks.Wallet() peer_manager = PeerManager.PeerManager() peer_finder = mocks.PeerFinder(5553, peer_manager, 2) hash_announcer = mocks.Announcer() sd_identifier = StreamDescriptor.StreamDescriptorIdentifier() self.expected_blobs = [ ( 'dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b' '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152 ), ( 'f4067522c1b49432a2a679512e3917144317caa1abba0c04' '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152 ), ( '305486c434260484fcb2968ce0e963b72f81ba56c11b08b1' 'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056 ), ] ## Setup reflector client classes ## self.db_dir, self.blob_dir = mk_db_and_blob_dir() self.session = Session.Session( conf.settings['data_rate'], db_dir=self.db_dir, node_id="abcd", peer_finder=peer_finder, hash_announcer=hash_announcer, blob_dir=self.blob_dir, peer_port=5553, use_upnp=False, wallet=wallet, blob_tracker_class=mocks.BlobAvailabilityTracker, external_ip="127.0.0.1" ) self.stream_info_manager = EncryptedFileMetadataManager.DBEncryptedFileMetadataManager( self.db_dir) self.lbry_file_manager = EncryptedFileManager.EncryptedFileManager( self.session, self.stream_info_manager, sd_identifier) ## Setup reflector server classes ## self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir() self.server_session = Session.Session( conf.settings['data_rate'], db_dir=self.server_db_dir, node_id="abcd", peer_finder=peer_finder, hash_announcer=hash_announcer, blob_dir=self.server_blob_dir, peer_port=5553, use_upnp=False, wallet=wallet, blob_tracker_class=mocks.BlobAvailabilityTracker, external_ip="127.0.0.1" ) self.server_blob_manager = BlobManager.DiskBlobManager( hash_announcer, self.server_blob_dir, self.server_db_dir) self.server_stream_info_manager = \ EncryptedFileMetadataManager.DBEncryptedFileMetadataManager(self.server_db_dir) self.server_lbry_file_manager = EncryptedFileManager.EncryptedFileManager( self.server_session, self.server_stream_info_manager, sd_identifier) d = self.session.setup() d.addCallback(lambda _: self.stream_info_manager.setup()) d.addCallback(lambda _: EncryptedFileOptions.add_lbry_file_to_sd_identifier(sd_identifier)) d.addCallback(lambda _: self.lbry_file_manager.setup()) d.addCallback(lambda _: self.server_session.setup()) d.addCallback(lambda _: self.server_blob_manager.setup()) d.addCallback(lambda _: self.server_stream_info_manager.setup()) d.addCallback(lambda _: self.server_lbry_file_manager.setup()) def verify_equal(sd_info): self.assertEqual(mocks.create_stream_sd_file, sd_info) def save_sd_blob_hash(sd_hash): self.sd_hash = sd_hash self.expected_blobs.append((sd_hash, 923)) def verify_stream_descriptor_file(stream_hash): self.stream_hash = stream_hash d = lbry_file.get_sd_info(self.lbry_file_manager.stream_info_manager, stream_hash, True) d.addCallback(verify_equal) d.addCallback( lambda _: lbry_file.publish_sd_blob( self.lbry_file_manager.stream_info_manager, self.session.blob_manager, stream_hash ) ) d.addCallback(save_sd_blob_hash) return d def create_stream(): test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) d = EncryptedFileCreator.create_lbry_file( self.session, self.lbry_file_manager, "test_file", test_file, key="0123456701234567", iv_generator=iv_generator() ) return d def start_server(): server_factory = reflector.ServerFactory( peer_manager, self.server_blob_manager, self.server_stream_info_manager, self.server_lbry_file_manager) from twisted.internet import reactor port = 8943 while self.reflector_port is None: try: self.reflector_port = reactor.listenTCP(port, server_factory) self.port = port except error.CannotListenError: port += 1 d.addCallback(lambda _: create_stream()) d.addCallback(verify_stream_descriptor_file) d.addCallback(lambda _: start_server()) return d
def setUp(self): self.reflector_port = None self.port = None mocks.mock_conf_settings(self) self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir() self.client_db_dir, self.client_blob_dir = mk_db_and_blob_dir() prm = OnlyFreePaymentsManager() wallet = mocks.Wallet() peer_manager = PeerManager.PeerManager() peer_finder = mocks.PeerFinder(5553, peer_manager, 2) self.server_storage = SQLiteStorage(self.server_db_dir) self.server_blob_manager = BlobManager.DiskBlobManager(self.server_blob_dir, self.server_storage) self.client_storage = SQLiteStorage(self.client_db_dir) self.client_blob_manager = BlobManager.DiskBlobManager(self.client_blob_dir, self.client_storage) self.server_lbry_file_manager = EncryptedFileManager( peer_finder, DummyRateLimiter(), self.server_blob_manager, wallet, prm, self.server_storage, StreamDescriptor.StreamDescriptorIdentifier() ) self.client_lbry_file_manager = EncryptedFileManager( peer_finder, DummyRateLimiter(), self.client_blob_manager, wallet, prm, self.client_storage, StreamDescriptor.StreamDescriptorIdentifier() ) self.expected_blobs = [ ( 'dc4708f76a5e7af0f1cae0ee96b824e2ed9250c9346c093b' '441f0a20d3607c17948b6fcfb4bc62020fe5286693d08586', 2097152 ), ( 'f4067522c1b49432a2a679512e3917144317caa1abba0c04' '1e0cd2cf9f635d4cf127ce1824fa04189b63916174951f70', 2097152 ), ( '305486c434260484fcb2968ce0e963b72f81ba56c11b08b1' 'af0789b55b44d78422600f9a38e3cf4f2e9569897e5646a9', 1015056 ), ] d = self.server_storage.setup() d.addCallback(lambda _: self.server_blob_manager.setup()) d.addCallback(lambda _: self.server_lbry_file_manager.setup()) d.addCallback(lambda _: self.client_storage.setup()) d.addCallback(lambda _: self.client_blob_manager.setup()) d.addCallback(lambda _: self.client_lbry_file_manager.setup()) @defer.inlineCallbacks def verify_equal(sd_info, stream_hash): self.assertDictEqual(mocks.create_stream_sd_file, sd_info) sd_hash = yield self.client_storage.get_sd_blob_hash_for_stream(stream_hash) defer.returnValue(sd_hash) def save_sd_blob_hash(sd_hash): self.sd_hash = sd_hash self.expected_blobs.append((sd_hash, 923)) def verify_stream_descriptor_file(stream_hash): self.stream_hash = stream_hash d = get_sd_info(self.client_storage, stream_hash, True) d.addCallback(verify_equal, stream_hash) d.addCallback(save_sd_blob_hash) return d def create_stream(): test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) d = EncryptedFileCreator.create_lbry_file( self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager, "test_file", test_file, key="0123456701234567", iv_generator=iv_generator() ) d.addCallback(lambda lbry_file: lbry_file.stream_hash) return d def start_server(): server_factory = reflector.ServerFactory(peer_manager, self.server_blob_manager, self.server_lbry_file_manager) from twisted.internet import reactor port = 8943 while self.reflector_port is None: try: self.reflector_port = reactor.listenTCP(port, server_factory) self.port = port except error.CannotListenError: port += 1 d.addCallback(lambda _: create_stream()) d.addCallback(verify_stream_descriptor_file) d.addCallback(lambda _: start_server()) return d