hydrus/include/TestHydrusServer.py

802 lines
28 KiB
Python
Raw Normal View History

2019-01-09 22:59:03 +00:00
from . import ClientLocalServer
from . import ClientMedia
from . import ClientRatings
from . import ClientServices
2013-12-04 22:44:16 +00:00
import hashlib
2019-01-09 22:59:03 +00:00
import http.client
from . import HydrusConstants as HC
from . import HydrusEncryption
from . import HydrusNetwork
from . import HydrusPaths
from . import HydrusServer
from . import HydrusServerResources
2013-11-20 21:12:21 +00:00
import os
2017-03-29 19:39:34 +00:00
import random
2019-01-09 22:59:03 +00:00
from . import ServerFiles
from . import ServerServer
2017-01-25 22:56:55 +00:00
import ssl
2019-01-09 22:59:03 +00:00
from . import TestConstants
2013-11-20 21:12:21 +00:00
import time
import unittest
from twisted.internet import reactor
2019-01-09 22:59:03 +00:00
#from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
2013-11-27 18:27:11 +00:00
from twisted.internet.defer import deferredGenerator, waitForDeferred
2017-01-25 22:56:55 +00:00
import twisted.internet.ssl
2019-01-09 22:59:03 +00:00
from . import HydrusData
from . import HydrusGlobals as HG
2013-11-20 21:12:21 +00:00
2016-12-07 22:12:52 +00:00
with open( os.path.join( HC.STATIC_DIR, 'hydrus.png' ), 'rb' ) as f:
EXAMPLE_FILE = f.read()
with open( os.path.join( HC.STATIC_DIR, 'hydrus_small.png' ), 'rb' ) as f:
EXAMPLE_THUMBNAIL = f.read()
2013-11-20 21:12:21 +00:00
class TestServer( unittest.TestCase ):
@classmethod
2017-04-05 21:16:40 +00:00
def setUpClass( cls ):
2013-11-20 21:12:21 +00:00
2018-03-07 22:48:29 +00:00
cls._access_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
services = []
2017-04-05 21:16:40 +00:00
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 )
2017-03-29 19:39:34 +00:00
2017-04-05 21:16:40 +00:00
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' )
2017-03-29 19:39:34 +00:00
2018-03-07 22:48:29 +00:00
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 ) )
2017-03-08 23:23:12 +00:00
2017-04-05 21:16:40 +00:00
cls._local_booru = ClientServices.GenerateService( HydrusData.GenerateKey(), HC.LOCAL_BOORU, 'local booru' )
2014-08-27 22:15:22 +00:00
2017-06-28 20:23:21 +00:00
services_manager = HG.test_controller.services_manager
2014-08-27 22:15:22 +00:00
2017-04-05 21:16:40 +00:00
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
2013-11-20 21:12:21 +00:00
permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
2015-07-01 22:02:07 +00:00
account_key = HydrusData.GenerateKey()
2018-03-07 22:48:29 +00:00
account_type = HydrusNetwork.AccountType.GenerateAdminAccountType( HC.SERVER_ADMIN )
2015-03-25 22:04:19 +00:00
created = HydrusData.GetNow() - 100000
2014-10-01 22:58:32 +00:00
expires = None
2013-11-20 21:12:21 +00:00
2018-03-07 22:48:29 +00:00
cls._account = HydrusNetwork.Account( account_key, account_type, created, expires )
2013-11-20 21:12:21 +00:00
2017-04-05 21:16:40 +00:00
cls._file_hash = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
def TWISTEDSetup():
2017-04-05 21:16:40 +00:00
cls._ssl_cert_path = os.path.join( TestConstants.DB_DIR, 'server.crt' )
cls._ssl_key_path = os.path.join( TestConstants.DB_DIR, 'server.key' )
2017-01-25 22:56:55 +00:00
# if db test ran, this is still hanging around and read-only, so don't bother to fail overwriting
2017-04-05 21:16:40 +00:00
if not os.path.exists( cls._ssl_cert_path ):
2017-01-25 22:56:55 +00:00
2017-04-05 21:16:40 +00:00
HydrusEncryption.GenerateOpenSSLCertAndKeyFile( cls._ssl_cert_path, cls._ssl_key_path )
2017-01-25 22:56:55 +00:00
2017-04-05 21:16:40 +00:00
context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory( cls._ssl_key_path, cls._ssl_cert_path )
2017-01-25 22:56:55 +00:00
2017-04-05 21:16:40 +00:00
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 )
2017-01-25 22:56:55 +00:00
2017-04-05 21:16:40 +00:00
reactor.listenTCP( HC.DEFAULT_LOCAL_BOORU_PORT, ClientLocalServer.HydrusServiceBooru( cls._local_booru ) )
2013-11-20 21:12:21 +00:00
reactor.callFromThread( TWISTEDSetup )
time.sleep( 1 )
2017-01-25 22:56:55 +00:00
def _test_basics( self, host, port, https = True ):
2015-11-04 22:30:28 +00:00
2017-01-25 22:56:55 +00:00
if https:
context = ssl.SSLContext( ssl.PROTOCOL_SSLv23 )
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
2019-01-09 22:59:03 +00:00
connection = http.client.HTTPSConnection( host, port, timeout = 10, context = context )
2017-01-25 22:56:55 +00:00
else:
2019-01-09 22:59:03 +00:00
connection = http.client.HTTPConnection( host, port, timeout = 10 )
2017-01-25 22:56:55 +00:00
2013-11-20 21:12:21 +00:00
#
connection.request( 'GET', '/' )
response = connection.getresponse()
data = response.read()
2017-03-29 19:39:34 +00:00
self.assertEqual( response.status, 200 )
2013-11-20 21:12:21 +00:00
#
2019-01-09 22:59:03 +00:00
with open( os.path.join( HC.STATIC_DIR, 'hydrus.ico' ), 'rb' ) as f:
favicon = f.read()
2013-11-20 21:12:21 +00:00
connection.request( 'GET', '/favicon.ico' )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, favicon )
2017-03-29 19:39:34 +00:00
def _test_file_repo( self, service ):
2013-11-20 21:12:21 +00:00
# file
2016-06-08 20:27:22 +00:00
path = ServerFiles.GetExpectedFilePath( self._file_hash )
2013-11-20 21:12:21 +00:00
2017-01-25 22:56:55 +00:00
HydrusPaths.MakeSureDirectoryExists( os.path.dirname( path ) )
2019-01-09 22:59:03 +00:00
with open( path, 'wb' ) as f:
f.write( EXAMPLE_FILE )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'file', { 'hash' : self._file_hash } )
2013-11-20 21:12:21 +00:00
2016-12-07 22:12:52 +00:00
self.assertEqual( response, EXAMPLE_FILE )
2013-11-20 21:12:21 +00:00
try: os.remove( path )
except: pass
2015-11-04 22:30:28 +00:00
path = os.path.join( HC.STATIC_DIR, 'hydrus.png' )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
with open( path, 'rb' ) as f:
file_bytes = f.read()
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
service.Request( HC.POST, 'file', { 'file' : file_bytes } )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
written = HG.test_controller.GetWrite( 'file' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_account, written_file_dict ) = args
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
self.assertEqual( written_file_dict[ 'hash' ], b'\xadm5\x99\xa6\xc4\x89\xa5u\xeb\x19\xc0&\xfa\xce\x97\xa9\xcdey\xe7G(\xb0\xce\x94\xa6\x01\xd22\xf3\xc3' )
2013-11-20 21:12:21 +00:00
self.assertEqual( written_file_dict[ 'ip' ], '127.0.0.1' )
self.assertEqual( written_file_dict[ 'height' ], 200 )
self.assertEqual( written_file_dict[ 'width' ], 200 )
self.assertEqual( written_file_dict[ 'mime' ], 2 )
self.assertEqual( written_file_dict[ 'size' ], 5270 )
# ip
2015-03-25 22:04:19 +00:00
( ip, timestamp ) = ( '94.45.87.123', HydrusData.GetNow() - 100000 )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'ip', ( ip, timestamp ) )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'ip', { 'hash' : self._file_hash } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'ip' ], ip )
self.assertEqual( response[ 'timestamp' ], timestamp )
# thumbnail
2016-06-08 20:27:22 +00:00
path = ServerFiles.GetExpectedThumbnailPath( self._file_hash )
2013-11-20 21:12:21 +00:00
2017-01-25 22:56:55 +00:00
HydrusPaths.MakeSureDirectoryExists( os.path.dirname( path ) )
2019-01-09 22:59:03 +00:00
with open( path, 'wb' ) as f:
f.write( EXAMPLE_THUMBNAIL )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'thumbnail', { 'hash' : self._file_hash } )
2013-11-20 21:12:21 +00:00
2016-12-07 22:12:52 +00:00
self.assertEqual( response, EXAMPLE_THUMBNAIL )
2013-11-20 21:12:21 +00:00
try: os.remove( path )
except: pass
2014-07-16 20:50:18 +00:00
def _test_local_booru( self, host, port ):
2014-09-10 22:37:38 +00:00
#
2019-01-09 22:59:03 +00:00
connection = http.client.HTTPConnection( host, port, timeout = 10 )
2014-07-16 20:50:18 +00:00
#
2015-11-04 22:30:28 +00:00
with open( os.path.join( HC.STATIC_DIR, 'local_booru_style.css' ), 'rb' ) as f:
css = f.read()
2014-07-16 20:50:18 +00:00
connection.request( 'GET', '/style.css' )
response = connection.getresponse()
data = response.read()
self.assertEqual( data, css )
#
2015-07-01 22:02:07 +00:00
share_key = HydrusData.GenerateKey()
hashes = [ HydrusData.GenerateKey() for i in range( 5 ) ]
2014-07-16 20:50:18 +00:00
2016-10-12 21:52:50 +00:00
client_files_default = os.path.join( TestConstants.DB_DIR, 'client_files' )
2016-06-01 20:04:15 +00:00
2019-01-09 22:59:03 +00:00
hash_encoded = hashes[0].hex()
2016-06-08 20:27:22 +00:00
prefix = hash_encoded[:2]
2016-07-27 21:53:34 +00:00
file_path = os.path.join( client_files_default, 'f' + prefix, hash_encoded + '.jpg' )
thumbnail_path = os.path.join( client_files_default, 't' + prefix, hash_encoded + '.thumbnail' )
2016-06-08 20:27:22 +00:00
2019-01-09 22:59:03 +00:00
with open( file_path, 'wb' ) as f:
f.write( EXAMPLE_FILE )
with open( thumbnail_path, 'wb' ) as f:
f.write( EXAMPLE_THUMBNAIL )
2014-07-16 20:50:18 +00:00
2017-12-06 22:06:56 +00:00
local_booru_manager = HG.client_controller.local_booru_manager
2014-07-16 20:50:18 +00:00
#
self._test_local_booru_requests( connection, share_key, hashes[0], 404 )
#
info = {}
info[ 'name' ] = 'name'
info[ 'text' ] = 'text'
info[ 'timeout' ] = 0
info[ 'hashes' ] = hashes
2018-08-08 20:29:54 +00:00
file_info_manager = ClientMedia.FileInfoManager( 1, hashes[0], 500, HC.IMAGE_JPEG, 640, 480 )
2014-07-16 20:50:18 +00:00
2018-12-05 22:35:30 +00:00
file_viewing_stats_manager = ClientMedia.FileViewingStatsManager.STATICGenerateEmptyManager()
media_results = [ ClientMedia.MediaResult( file_info_manager, ClientMedia.TagsManager( {} ), ClientMedia.LocationsManager( set(), set(), set(), set() ), ClientRatings.RatingsManager( {} ), file_viewing_stats_manager ) for hash in hashes ]
2014-07-16 20:50:18 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'local_booru_share_keys', [ share_key ] )
HG.test_controller.SetRead( 'local_booru_share', info )
HG.test_controller.SetRead( 'media_results', media_results )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 403 )
#
info[ 'timeout' ] = None
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'local_booru_share', info )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 200 )
#
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'local_booru_share_keys', [] )
2014-07-16 20:50:18 +00:00
local_booru_manager.RefreshShares()
#
self._test_local_booru_requests( connection, share_key, hashes[0], 404 )
def _test_local_booru_requests( self, connection, share_key, hash, expected_result ):
requests = []
2019-01-09 22:59:03 +00:00
requests.append( '/gallery?share_key=' + share_key.hex() )
requests.append( '/page?share_key=' + share_key.hex() + '&hash=' + hash.hex() )
requests.append( '/file?share_key=' + share_key.hex() + '&hash=' + hash.hex() )
requests.append( '/thumbnail?share_key=' + share_key.hex() + '&hash=' + hash.hex() )
2014-07-16 20:50:18 +00:00
for request in requests:
connection.request( 'GET', request )
response = connection.getresponse()
data = response.read()
self.assertEqual( response.status, expected_result )
2017-03-29 19:39:34 +00:00
def _test_repo( self, service ):
2014-02-12 23:09:38 +00:00
2014-09-17 21:28:26 +00:00
service_key = service.GetServiceKey()
2013-11-20 21:12:21 +00:00
# num_petitions
2018-05-02 20:45:20 +00:00
num_petitions = [ [ HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_STATUS_PETITIONED, 23 ], [ HC.CONTENT_TYPE_TAG_PARENTS, HC.CONTENT_STATUS_PENDING, 0 ] ]
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'num_petitions', num_petitions )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'num_petitions' )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'num_petitions' ], num_petitions )
# petition
2015-10-14 21:02:25 +00:00
action = HC.CONTENT_UPDATE_PETITION
2017-03-29 19:39:34 +00:00
petitioner_account = HydrusNetwork.Account.GenerateUnknownAccount()
2015-10-14 21:02:25 +00:00
reason = 'it sucks'
2017-03-02 02:14:56 +00:00
contents = [ HydrusNetwork.Content( HC.CONTENT_TYPE_FILES, [ HydrusData.GenerateKey() for i in range( 10 ) ] ) ]
2015-10-14 21:02:25 +00:00
2017-03-29 19:39:34 +00:00
petition = HydrusNetwork.Petition( action, petitioner_account, reason, contents )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'petition', petition )
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
response = service.Request( HC.GET, 'petition', { 'content_type' : HC.CONTENT_TYPE_FILES, 'status' : HC.CONTENT_UPDATE_PETITION } )
2013-11-20 21:12:21 +00:00
2017-03-29 19:39:34 +00:00
self.assertEqual( response[ 'petition' ].GetSerialisableTuple(), petition.GetSerialisableTuple() )
# definitions
2013-11-20 21:12:21 +00:00
2017-03-29 19:39:34 +00:00
definitions_update = HydrusNetwork.DefinitionsUpdate()
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
for i in range( 100, 200 ):
2017-03-29 19:39:34 +00:00
definitions_update.AddRow( ( HC.DEFINITIONS_TYPE_TAGS, i, 'series:test ' + str( i ) ) )
definitions_update.AddRow( ( HC.DEFINITIONS_TYPE_HASHES, i + 500, HydrusData.GenerateKey() ) )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
definitions_update_network_bytes = definitions_update.DumpToNetworkBytes()
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
definitions_update_hash = hashlib.sha256( definitions_update_network_bytes ).digest()
2013-11-20 21:12:21 +00:00
2017-03-29 19:39:34 +00:00
path = ServerFiles.GetExpectedFilePath( definitions_update_hash )
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
HydrusPaths.MakeSureDirectoryExists( path )
with open( path, 'wb' ) as f:
2019-01-09 22:59:03 +00:00
f.write( definitions_update_network_bytes )
2018-05-02 20:45:20 +00:00
2015-06-03 21:05:13 +00:00
2017-03-29 19:39:34 +00:00
response = service.Request( HC.GET, 'update', { 'update_hash' : definitions_update_hash } )
2013-11-20 21:12:21 +00:00
try: os.remove( path )
except: pass
2019-01-09 22:59:03 +00:00
self.assertEqual( response, definitions_update_network_bytes )
2017-03-29 19:39:34 +00:00
# content
rows = [ ( random.randint( 100, 1000 ), [ random.randint( 100, 1000 ) for i in range( 50 ) ] ) for j in range( 20 ) ]
2015-06-03 21:05:13 +00:00
2017-03-29 19:39:34 +00:00
content_update = HydrusNetwork.ContentUpdate()
2015-06-03 21:05:13 +00:00
2017-03-29 19:39:34 +00:00
for row in rows:
content_update.AddRow( ( HC.CONTENT_TYPE_MAPPINGS, HC.CONTENT_UPDATE_ADD, row ) )
2019-01-09 22:59:03 +00:00
content_update_network_bytes = content_update.DumpToNetworkBytes()
2017-03-29 19:39:34 +00:00
2019-01-09 22:59:03 +00:00
content_update_hash = hashlib.sha256( content_update_network_bytes ).digest()
2015-06-03 21:05:13 +00:00
2017-03-29 19:39:34 +00:00
path = ServerFiles.GetExpectedFilePath( content_update_hash )
2015-06-03 21:05:13 +00:00
2019-01-09 22:59:03 +00:00
with open( path, 'wb' ) as f:
f.write( content_update_network_bytes )
2015-06-03 21:05:13 +00:00
2017-03-29 19:39:34 +00:00
response = service.Request( HC.GET, 'update', { 'update_hash' : content_update_hash } )
2015-06-03 21:05:13 +00:00
try: os.remove( path )
except: pass
2019-01-09 22:59:03 +00:00
self.assertEqual( response, content_update_network_bytes )
2017-03-29 19:39:34 +00:00
# metadata
metadata = HydrusNetwork.Metadata()
metadata.AppendUpdate( [ definitions_update_hash, content_update_hash ], HydrusData.GetNow() - 101000, HydrusData.GetNow() - 1000, HydrusData.GetNow() + 100000 )
service._metadata = metadata
response = service.Request( HC.GET, 'metadata_slice', { 'since' : 0 } )
self.assertEqual( response[ 'metadata_slice' ].GetSerialisableTuple(), metadata.GetSerialisableTuple() )
# post content
2017-04-05 21:16:40 +00:00
raise NotImplementedError()
2017-09-20 19:47:31 +00:00
'''
2015-06-03 21:05:13 +00:00
update = HydrusData.ClientToServerContentUpdatePackage( {}, hash_ids_to_hashes )
service.Request( HC.POST, 'content_update_package', { 'update' : update } )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
written = HG.test_controller.GetWrite( 'update' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2014-08-27 22:15:22 +00:00
( written_service_key, written_account, written_update ) = args
2013-11-20 21:12:21 +00:00
2015-06-03 21:05:13 +00:00
self.assertEqual( update.GetHashes(), written_update.GetHashes() )
2017-09-20 19:47:31 +00:00
'''
2013-11-20 21:12:21 +00:00
2017-03-29 19:39:34 +00:00
def _test_restricted( self, service ):
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
# access_key
2013-11-20 21:12:21 +00:00
2015-07-01 22:02:07 +00:00
registration_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'access_key', self._access_key )
2013-11-20 21:12:21 +00:00
2017-03-02 02:14:56 +00:00
response = service.Request( HC.GET, 'access_key', { 'registration_key' : registration_key } )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
self.assertEqual( response[ 'access_key' ], self._access_key )
2013-12-11 22:09:25 +00:00
2013-11-20 21:12:21 +00:00
# set up session
last_error = 0
account = self._account
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'service', service )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'account_key_from_access_key', HydrusData.GenerateKey() )
HG.test_controller.SetRead( 'account', self._account )
2013-11-20 21:12:21 +00:00
# account
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'account' )
2013-11-20 21:12:21 +00:00
self.assertEqual( repr( response[ 'account' ] ), repr( self._account ) )
# account_info
account_info = { 'message' : 'hello' }
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'account_info', account_info )
HG.test_controller.SetRead( 'account_key_from_identifier', HydrusData.GenerateKey() )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'account_info', { 'subject_account_key' : HydrusData.GenerateKey() } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'account_info' ], account_info )
2018-12-05 22:35:30 +00:00
# this not working for now, screw it m8
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
#response = service.Request( HC.GET, 'account_info', { 'subject_hash' : HydrusData.GenerateKey() } )
2013-11-20 21:12:21 +00:00
2018-12-05 22:35:30 +00:00
#self.assertEqual( response[ 'account_info' ], account_info )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
#response = service.Request( HC.GET, 'account_info', { 'subject_hash' : HydrusData.GenerateKey(), 'subject_tag' : 'hello' } )
2018-12-05 22:35:30 +00:00
#self.assertEqual( response[ 'account_info' ], account_info )
2013-11-20 21:12:21 +00:00
# account_types
2018-05-02 20:45:20 +00:00
account_types = [ HydrusNetwork.AccountType.GenerateAdminAccountType( service.GetServiceType() ) ]
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'account_types', account_types )
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.GET, 'account_types' )
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
self.assertEqual( response[ 'account_types' ][0].GetAccountTypeKey(), account_types[0].GetAccountTypeKey() )
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
service.Request( HC.POST, 'account_types', { 'account_types' : account_types, 'deletee_account_type_keys_to_new_account_type_keys' : {} } )
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
written = HG.test_controller.GetWrite( 'account_types' )
2013-11-20 21:12:21 +00:00
[ ( args, kwargs ) ] = written
2018-05-02 20:45:20 +00:00
( written_service_key, written_account, written_account_types, written_deletee_account_type_keys_to_new_account_type_keys ) = args
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
self.assertEqual( written_account_types[0].GetAccountTypeKey(), account_types[0].GetAccountTypeKey() )
self.assertEqual( written_deletee_account_type_keys_to_new_account_type_keys, {} )
2013-11-20 21:12:21 +00:00
# registration_keys
2015-07-01 22:02:07 +00:00
registration_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'registration_keys', [ registration_key ] )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'registration_keys', { 'num' : 1, 'account_type_key' : os.urandom( 32 ), 'expires' : HydrusData.GetNow() + 1200 } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'registration_keys' ], [ registration_key ] )
2017-03-29 19:39:34 +00:00
def _test_server_admin( self, service ):
2013-11-20 21:12:21 +00:00
# init
2015-07-01 22:02:07 +00:00
access_key = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2017-05-10 21:33:58 +00:00
HG.test_controller.SetRead( 'access_key', access_key )
2013-11-20 21:12:21 +00:00
2019-01-09 22:59:03 +00:00
response = service.Request( HC.GET, 'access_key', { 'registration_key' : b'init' } )
2013-11-20 21:12:21 +00:00
self.assertEqual( response[ 'access_key' ], access_key )
2014-02-12 23:09:38 +00:00
#
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
## backup
2013-11-20 21:12:21 +00:00
2014-02-12 23:09:38 +00:00
response = service.Request( HC.POST, 'backup' )
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
#
2013-11-20 21:12:21 +00:00
2018-05-02 20:45:20 +00:00
# add some new services info
2013-11-20 21:12:21 +00:00
2017-03-29 19:39:34 +00:00
def _test_tag_repo( self, service ):
2013-11-20 21:12:21 +00:00
pass
def test_repository_file( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVICE_PORT
self._test_basics( host, port )
2017-03-29 19:39:34 +00:00
self._test_restricted( self._clientside_file_service )
2018-08-15 20:40:30 +00:00
# broke since service rewrite
#self._test_repo( self._clientside_file_service )
#self._test_file_repo( self._clientside_file_service )
2013-11-20 21:12:21 +00:00
def test_repository_tag( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVICE_PORT + 1
self._test_basics( host, port )
2017-03-29 19:39:34 +00:00
self._test_restricted( self._clientside_tag_service )
2018-08-15 20:40:30 +00:00
# broke since service rewrite
#self._test_repo( self._clientside_tag_service )
#self._test_tag_repo( self._clientside_tag_service )
2013-11-20 21:12:21 +00:00
def test_server_admin( self ):
host = '127.0.0.1'
port = HC.DEFAULT_SERVER_ADMIN_PORT
self._test_basics( host, port )
2017-03-29 19:39:34 +00:00
self._test_restricted( self._clientside_admin_service )
self._test_server_admin( self._clientside_admin_service )
2013-11-20 21:12:21 +00:00
2014-07-16 20:50:18 +00:00
def test_local_booru( self ):
host = '127.0.0.1'
port = HC.DEFAULT_LOCAL_BOORU_PORT
2017-01-25 22:56:55 +00:00
self._test_basics( host, port, https = False )
2014-07-16 20:50:18 +00:00
self._test_local_booru( host, port )
2014-12-25 00:07:20 +00:00
'''
2013-11-20 21:12:21 +00:00
class TestAMP( unittest.TestCase ):
@classmethod
2017-04-05 21:16:40 +00:00
def setUpClass( cls ):
2013-11-20 21:12:21 +00:00
2017-04-05 21:16:40 +00:00
cls._alice = HydrusData.GenerateKey()
cls._bob = HydrusData.GenerateKey()
2013-11-20 21:12:21 +00:00
2017-04-05 21:16:40 +00:00
cls._server_port = HC.DEFAULT_SERVICE_PORT + 10
2013-11-20 21:12:21 +00:00
2017-04-05 21:16:40 +00:00
cls._service_key = HydrusData.GenerateKey()
2013-11-27 18:27:11 +00:00
def TWISTEDSetup():
2017-04-05 21:16:40 +00:00
cls._factory = HydrusServer.MessagingServiceFactory( cls._service_key )
2013-11-27 18:27:11 +00:00
2017-04-05 21:16:40 +00:00
reactor.listenTCP( cls._server_port, cls._factory )
2013-11-27 18:27:11 +00:00
reactor.callFromThread( TWISTEDSetup )
time.sleep( 1 )
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
def _get_deferred_result( self, deferred ):
def err( failure ):
failure.trap( Exception )
return failure.type( failure.value )
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
deferred.addErrback( err )
2013-11-20 21:12:21 +00:00
2014-11-12 23:33:13 +00:00
before = time.time()
while not deferred.called:
time.sleep( 0.1 )
if time.time() - before > 10: raise Exception( 'Trying to get deferred timed out!' )
2013-11-27 18:27:11 +00:00
result = deferred.result
if issubclass( type( result ), Exception ): raise result
return result
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
def _get_client_protocol( self ):
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
point = TCP4ClientEndpoint( reactor, '127.0.0.1', self._server_port )
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
deferred = connectProtocol( point, HydrusServerAMP.MessagingClientProtocol() )
2013-11-27 18:27:11 +00:00
protocol = self._get_deferred_result( deferred )
return protocol
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
def _make_persistent_connection( self, protocol, access_key, name ):
2013-11-20 21:12:21 +00:00
2013-12-04 22:44:16 +00:00
identifier = hashlib.sha256( access_key ).digest()
2013-11-20 21:12:21 +00:00
2013-11-27 18:27:11 +00:00
HC.app.SetRead( 'im_identifier', identifier )
2013-12-04 22:44:16 +00:00
permissions = [ HC.GET_DATA, HC.POST_DATA, HC.POST_PETITIONS, HC.RESOLVE_PETITIONS, HC.MANAGE_USERS, HC.GENERAL_ADMIN, HC.EDIT_SERVICES ]
2015-07-01 22:02:07 +00:00
account_key = HydrusData.GenerateKey()
2013-12-04 22:44:16 +00:00
account_type = HC.AccountType( 'account', permissions, ( None, None ) )
created = HC.GetNow() - 100000
2014-10-01 22:58:32 +00:00
expires = None
used_bytes = 0
used_requests = 0
2013-12-04 22:44:16 +00:00
2014-10-01 22:58:32 +00:00
account = HC.Account( account_key, account_type, created, expires, used_bytes, used_requests )
2013-12-04 22:44:16 +00:00
2015-07-01 22:02:07 +00:00
HC.app.SetRead( 'account_key_from_access_key', HydrusData.GenerateKey() )
2013-12-04 22:44:16 +00:00
HC.app.SetRead( 'account', account )
2013-11-27 18:27:11 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMSessionKey, access_key = access_key, name = name )
result = self._get_deferred_result( deferred )
session_key = result[ 'session_key' ]
2013-12-04 22:44:16 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMLoginPersistent, network_version = HC.NETWORK_VERSION, session_key = session_key )
2013-11-27 18:27:11 +00:00
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
def _make_temporary_connection( self, protocol, identifier, name ):
2013-12-04 22:44:16 +00:00
deferred = protocol.callRemote( HydrusServerAMP.IMLoginTemporary, network_version = HC.NETWORK_VERSION, identifier = identifier, name = name )
2013-11-27 18:27:11 +00:00
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
2013-11-20 21:12:21 +00:00
def test_connections( self ):
2013-11-27 18:27:11 +00:00
persistent_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
persistent_access_key = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
persistent_identifier = hashlib.sha256( persistent_access_key ).digest()
2013-11-27 18:27:11 +00:00
persistent_name = 'persistent'
2013-12-04 22:44:16 +00:00
self._make_persistent_connection( persistent_protocol, persistent_access_key, persistent_name )
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self.assertIn( persistent_identifier, self._factory._persistent_connections )
self.assertIn( persistent_name, self._factory._persistent_connections[ persistent_identifier ] )
temp_protocol_1 = self._get_client_protocol()
temp_protocol_2 = self._get_client_protocol()
temp_name_1 = 'temp_1'
2015-07-01 22:02:07 +00:00
temp_identifier = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
temp_name_2 = 'temp_2'
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self._make_temporary_connection( temp_protocol_1, temp_identifier, temp_name_1 )
self._make_temporary_connection( temp_protocol_2, temp_identifier, temp_name_2 )
2013-11-20 21:12:21 +00:00
2013-12-11 22:09:25 +00:00
self.assertIn( temp_identifier, self._factory._temporary_connections )
self.assertIn( temp_name_1, self._factory._temporary_connections[ temp_identifier ] )
self.assertIn( temp_name_2, self._factory._temporary_connections[ temp_identifier ] )
2013-11-20 21:12:21 +00:00
def test_status( self ):
# some of this is UDP, so get that working!
# add two bobs
# ask for status of the bobs
# test that we get both, online
# now disconnect a bob
# ask for bob status
# test that we only have one bob
# now disconnect other bob
# repeat for nothing
pass
def test_message( self ):
2013-12-11 22:09:25 +00:00
persistent_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
persistent_access_key = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
persistent_identifier = hashlib.sha256( persistent_access_key ).digest()
persistent_name = 'persistent'
self._make_persistent_connection( persistent_protocol, persistent_access_key, persistent_name )
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
temp_protocol = self._get_client_protocol()
2015-07-01 22:02:07 +00:00
temp_identifier = HydrusData.GenerateKey()
2013-12-11 22:09:25 +00:00
temp_name = 'temp'
2013-11-27 18:27:11 +00:00
2013-12-11 22:09:25 +00:00
self._make_temporary_connection( temp_protocol, temp_identifier, temp_name )
#
HC.pubsub.ClearPubSubs()
message = 'hello temp'
deferred = persistent_protocol.callRemote( HydrusServerAMP.IMMessageServer, identifier_to = temp_identifier, name_to = temp_name, message = message )
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
result = HC.pubsub.GetPubSubs( 'im_message_received' )
[ ( args, kwargs ) ] = result
self.assertEqual( args, ( persistent_identifier, persistent_name, temp_identifier, temp_name, message ) )
#
HC.pubsub.ClearPubSubs()
message = 'hello persistent'
deferred = temp_protocol.callRemote( HydrusServerAMP.IMMessageServer, identifier_to = persistent_identifier, name_to = persistent_name, message = message )
result = self._get_deferred_result( deferred )
self.assertEqual( result, {} )
result = HC.pubsub.GetPubSubs( 'im_message_received' )
[ ( args, kwargs ) ] = result
self.assertEqual( args, ( temp_identifier, temp_name, persistent_identifier, persistent_name, message ) )
2016-12-07 22:12:52 +00:00
'''