コード例 #1
0
ファイル: TestHydrusServer.py プロジェクト: HOZHENWAI/hydrus
 def setUpClass( cls ):
     
     cls._access_key = HydrusData.GenerateKey()
     
     cls._serverside_file_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo', HC.DEFAULT_SERVICE_PORT + 1 )
     cls._serverside_tag_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo', HC.DEFAULT_SERVICE_PORT )
     cls._serverside_admin_service = HydrusNetwork.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin', HC.DEFAULT_SERVER_ADMIN_PORT )
     
     cls._clientside_file_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.FILE_REPOSITORY, 'file repo' )
     cls._clientside_tag_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.TAG_REPOSITORY, 'tag repo' )
     cls._clientside_admin_service = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.SERVER_ADMIN, 'server admin' )
     
     cls._clientside_file_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVICE_PORT + 1, cls._access_key ) )
     cls._clientside_tag_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVICE_PORT, cls._access_key ) )
     cls._clientside_admin_service.SetCredentials( HydrusNetwork.Credentials( '127.0.0.1', HC.DEFAULT_SERVER_ADMIN_PORT, cls._access_key ) )
     
     cls._local_booru = ClientServices.GenerateService( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, 'local booru' )
     
     services_manager = HG.test_controller.services_manager
     
     services_manager._keys_to_services[ cls._clientside_file_service.GetServiceKey() ] = cls._clientside_file_service
     services_manager._keys_to_services[ cls._clientside_tag_service.GetServiceKey() ] = cls._clientside_tag_service
     services_manager._keys_to_services[ cls._clientside_admin_service.GetServiceKey() ] = cls._clientside_admin_service
     
     permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
     
     account_key = HydrusData.GenerateKey()
     account_type = HydrusNetwork.AccountType.GenerateAdminAccountType( HC.SERVER_ADMIN )
     created = HydrusData.GetNow() - 100000
     expires = None
     
     cls._account = HydrusNetwork.Account( account_key, account_type, created, expires )
     
     cls._file_hash = HydrusData.GenerateKey()
     
     def TWISTEDSetup():
         
         cls._ssl_cert_path = os.path.join( TestController.DB_DIR, 'server.crt' )
         cls._ssl_key_path = os.path.join( TestController.DB_DIR, 'server.key' )
         
         # if db test ran, this is still hanging around and read-only, so don't bother to fail overwriting
         if not os.path.exists( cls._ssl_cert_path ):
             
             HydrusEncryption.GenerateOpenSSLCertAndKeyFile( cls._ssl_cert_path, cls._ssl_key_path )
             
         
         context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory( cls._ssl_key_path, cls._ssl_cert_path )
         
         reactor.listenSSL( HC.DEFAULT_SERVER_ADMIN_PORT, ServerServer.HydrusServiceAdmin( cls._serverside_admin_service ), context_factory )
         reactor.listenSSL( HC.DEFAULT_SERVICE_PORT + 1, ServerServer.HydrusServiceRepositoryFile( cls._serverside_file_service ), context_factory )
         reactor.listenSSL( HC.DEFAULT_SERVICE_PORT, ServerServer.HydrusServiceRepositoryTag( cls._serverside_tag_service ), context_factory )
         
         reactor.listenTCP( 45866, ClientLocalServer.HydrusServiceBooru( cls._local_booru, allow_non_local_connections = False ) )
         
     
     reactor.callFromThread( TWISTEDSetup )
     
     time.sleep( 3 )
コード例 #2
0
    def test_server(self):

        discard = HG.test_controller.GetWrite(
            'session')  # just to discard gumph from testserver

        session_key_1 = HydrusData.GenerateKey()
        service_key = HydrusData.GenerateKey()

        permissions = [
            HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS,
            HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES
        ]

        account_type = HydrusNetwork.AccountType.GenerateAdminAccountType(
            HC.SERVER_ADMIN)
        created = HydrusData.GetNow() - 100000
        expires = HydrusData.GetNow() + 300

        account_key_1 = HydrusData.GenerateKey()
        account_key_2 = HydrusData.GenerateKey()

        access_key_1 = HydrusData.GenerateKey()
        hashed_access_key_1 = hashlib.sha256(access_key_1).digest()

        access_key_2 = HydrusData.GenerateKey()
        hashed_access_key_2 = hashlib.sha256(access_key_2).digest()

        account = HydrusNetwork.Account(account_key_1, account_type, created,
                                        expires)
        account_2 = HydrusNetwork.Account(account_key_2, account_type, created,
                                          expires)

        # test timeout

        expires = HydrusData.GetNow() - 10

        HG.test_controller.SetRead('sessions', [
            (session_key_1, service_key, account, hashed_access_key_1, expires)
        ])

        session_manager = HydrusSessions.HydrusSessionManagerServer()

        with self.assertRaises(HydrusExceptions.SessionException):

            session_manager.GetAccount(service_key, session_key_1)

        # test missing

        with self.assertRaises(HydrusExceptions.SessionException):

            session_manager.GetAccount(service_key, HydrusData.GenerateKey())

        # test fetching a session already in db, after bootup

        expires = HydrusData.GetNow() + 300

        HG.test_controller.SetRead('sessions', [
            (session_key_1, service_key, account, hashed_access_key_1, expires)
        ])

        session_manager = HydrusSessions.HydrusSessionManagerServer()

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, account)

        # test too busy to add a new session for a new account it doesn't know about

        HG.server_busy.acquire()

        with self.assertRaises(HydrusExceptions.ServerBusyException):

            session_manager.AddSession(service_key, HydrusData.GenerateKey())

            session_manager.GetAccountFromAccessKey(service_key,
                                                    HydrusData.GenerateKey())

        # but ok to get for a session that already exists while busy

        session_manager.GetAccount(service_key, session_key_1)
        session_manager.GetAccountFromAccessKey(service_key, access_key_1)

        HG.server_busy.release()

        # test adding a session

        HG.test_controller.ClearWrites('session')

        expires = HydrusData.GetNow() + 300

        HG.test_controller.SetRead('account_key_from_access_key',
                                   account_key_2)
        HG.test_controller.SetRead('account', account_2)

        (session_key_2,
         expires_2) = session_manager.AddSession(service_key, access_key_2)

        [(args, kwargs)] = HG.test_controller.GetWrite('session')

        (written_session_key, written_service_key, written_account_key,
         written_expires) = args

        self.assertEqual(
            (session_key_2, service_key, account_key_2, expires_2),
            (written_session_key, written_service_key, written_account_key,
             written_expires))

        read_account = session_manager.GetAccount(service_key, session_key_2)

        self.assertIs(read_account, account_2)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_2)

        self.assertIs(read_account, account_2)

        # test adding a new session for an account already in the manager

        HG.test_controller.SetRead('account_key_from_access_key',
                                   account_key_1)
        HG.test_controller.SetRead('account', account)

        (session_key_3,
         expires_3) = session_manager.AddSession(service_key, access_key_1)

        [(args, kwargs)] = HG.test_controller.GetWrite('session')

        (written_session_key, written_service_key, written_account_key,
         written_expires) = args

        self.assertEqual(
            (session_key_3, service_key, account_key_1, expires_3),
            (written_session_key, written_service_key, written_account_key,
             written_expires))

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, account)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, account)

        # test individual account refresh

        expires = HydrusData.GetNow() + 300

        new_obj_account_1 = HydrusNetwork.Account(account_key_1, account_type,
                                                  created, expires)

        HG.test_controller.SetRead('account', new_obj_account_1)

        session_manager.RefreshAccounts(service_key, [account_key_1])

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, new_obj_account_1)

        # test all account refresh

        expires = HydrusData.GetNow() + 300

        new_obj_account_2 = HydrusNetwork.Account(account_key_2, account_type,
                                                  created, expires)

        HG.test_controller.SetRead(
            'sessions', [(session_key_1, service_key, new_obj_account_2,
                          hashed_access_key_2, expires),
                         (session_key_2, service_key, new_obj_account_1,
                          hashed_access_key_1, expires),
                         (session_key_3, service_key, new_obj_account_2,
                          hashed_access_key_2, expires)])

        session_manager.RefreshAllAccounts()

        read_account = session_manager.GetAccount(service_key, session_key_1)

        self.assertIs(read_account, new_obj_account_2)

        read_account = session_manager.GetAccount(service_key, session_key_2)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccount(service_key, session_key_3)

        self.assertIs(read_account, new_obj_account_2)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_1)

        self.assertIs(read_account, new_obj_account_1)

        read_account = session_manager.GetAccountFromAccessKey(
            service_key, access_key_2)

        self.assertIs(read_account, new_obj_account_2)