<s> """<STR_LIT>""" <EOL> import socket <EOL> from grr . client import actions <EOL> from grr . lib import access_control <EOL> from grr . lib import aff4 <EOL> from grr . lib import client_fixture <EOL> from grr . lib import config_lib <EOL> from grr . lib import worker_mocks <EOL> from grr . lib . rdfvalues import client as rdf_client <EOL> from grr . lib . rdfvalues import flows as rdf_flows <EOL> from grr . lib . rdfvalues import protodict as rdf_protodict <EOL> class ActionMock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * action_names , ** kwargs ) : <EOL> self . client_id = kwargs . get ( "<STR_LIT>" ) <EOL> self . action_names = action_names <EOL> self . action_classes = dict ( <EOL> [ ( k , v ) for ( k , v ) in actions . ActionPlugin . classes . items ( ) <EOL> if k in action_names ] ) <EOL> self . action_counts = dict ( ( x , <NUM_LIT:0> ) for x in action_names ) <EOL> self . client_worker = worker_mocks . FakeClientWorker ( ) <EOL> def RecordCall ( self , action_name , action_args ) : <EOL> pass <EOL> def HandleMessage ( self , message ) : <EOL> """<STR_LIT>""" <EOL> message . auth_state = rdf_flows . GrrMessage . AuthorizationState . AUTHENTICATED <EOL> if hasattr ( self , message . name ) : <EOL> return getattr ( self , message . name ) ( message . payload ) <EOL> self . RecordCall ( message . name , message . payload ) <EOL> try : <EOL> suspended_action_id = message . payload . iterator . suspended_action <EOL> action = self . client_worker . suspended_actions [ suspended_action_id ] <EOL> except ( AttributeError , KeyError ) : <EOL> action_cls = self . action_classes [ message . name ] <EOL> action = action_cls ( grr_worker = self . client_worker ) <EOL> action . Execute ( message ) <EOL> self . action_counts [ message . name ] += <NUM_LIT:1> <EOL> return self . client_worker . Drain ( ) <EOL> class RecordingActionMock ( ActionMock ) : <EOL> def __init__ ( self , * action_names ) : <EOL> super ( RecordingActionMock , self ) . __init__ ( * action_names ) <EOL> self . recorded_args = { } <EOL> def RecordCall ( self , action_name , action_args ) : <EOL> self . recorded_args . setdefault ( action_name , [ ] ) . append ( action_args ) <EOL> class InvalidActionMock ( object ) : <EOL> """<STR_LIT>""" <EOL> def HandleMessage ( self , unused_message ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> class UnixVolumeClientMock ( ActionMock ) : <EOL> """<STR_LIT>""" <EOL> unix_local = rdf_client . UnixVolume ( mount_point = "<STR_LIT>" ) <EOL> unix_home = rdf_client . UnixVolume ( mount_point = "<STR_LIT:/>" ) <EOL> path_results = [ <EOL> rdf_client . Volume ( <EOL> unixvolume = unix_local , bytes_per_sector = <NUM_LIT> , <EOL> sectors_per_allocation_unit = <NUM_LIT:1> , actual_available_allocation_units = <NUM_LIT:50> , <EOL> total_allocation_units = <NUM_LIT:100> ) , <EOL> rdf_client . Volume ( <EOL> unixvolume = unix_home , bytes_per_sector = <NUM_LIT> , <EOL> sectors_per_allocation_unit = <NUM_LIT:1> , <EOL> actual_available_allocation_units = <NUM_LIT:10> , <EOL> total_allocation_units = <NUM_LIT:100> ) ] <EOL> def StatFS ( self , _ ) : <EOL> return self . path_results <EOL> class WindowsVolumeClientMock ( ActionMock ) : <EOL> """<STR_LIT>""" <EOL> windows_d = rdf_client . WindowsVolume ( drive_letter = "<STR_LIT>" ) <EOL> windows_c = rdf_client . WindowsVolume ( drive_letter = "<STR_LIT>" ) <EOL> path_results = [ <EOL> rdf_client . Volume ( <EOL> windowsvolume = windows_d , bytes_per_sector = <NUM_LIT> , <EOL> sectors_per_allocation_unit = <NUM_LIT:1> , <EOL> actual_available_allocation_units = <NUM_LIT:50> , total_allocation_units = <NUM_LIT:100> ) , <EOL> rdf_client . Volume ( <EOL> windowsvolume = windows_c , bytes_per_sector = <NUM_LIT> , <EOL> sectors_per_allocation_unit = <NUM_LIT:1> , actual_available_allocation_units = <NUM_LIT:10> , <EOL> total_allocation_units = <NUM_LIT:100> ) ] <EOL> def WmiQuery ( self , query ) : <EOL> if query . query == u"<STR_LIT>" : <EOL> return client_fixture . WMI_SAMPLE <EOL> else : <EOL> return None <EOL> class MemoryClientMock ( ActionMock ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( MemoryClientMock , self ) . __init__ ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , * args , ** kwargs ) <EOL> fd = aff4 . FACTORY . Create ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> mode = "<STR_LIT:w>" , token = access_control . ACLToken ( username = "<STR_LIT:test>" , <EOL> reason = "<STR_LIT>" ) ) <EOL> fd . Set ( fd . Schema . COMPONENT ( name = "<STR_LIT>" , version = "<STR_LIT>" ) ) <EOL> fd . Close ( ) <EOL> class InterrogatedClient ( ActionMock ) : <EOL> """<STR_LIT>""" <EOL> def InitializeClient ( self , system = "<STR_LIT>" , version = "<STR_LIT>" , <EOL> kernel = "<STR_LIT>" ) : <EOL> self . system = system <EOL> self . version = version <EOL> self . kernel = kernel <EOL> self . response_count = <NUM_LIT:0> <EOL> self . recorded_messages = [ ] <EOL> def HandleMessage ( self , message ) : <EOL> """<STR_LIT>""" <EOL> self . recorded_messages . append ( message ) <EOL> return super ( InterrogatedClient , self ) . HandleMessage ( message ) <EOL> def GetPlatformInfo ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_client . Uname ( <EOL> system = self . system , <EOL> node = "<STR_LIT>" , <EOL> release = "<STR_LIT:5>" , <EOL> version = self . version , <EOL> kernel = self . kernel , <EOL> machine = "<STR_LIT>" ) ] <EOL> def GetInstallDate ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_protodict . DataBlob ( integer = <NUM_LIT:100> ) ] <EOL> def EnumerateInterfaces ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_client . Interface ( <EOL> mac_address = "<STR_LIT>" , <EOL> addresses = [ <EOL> rdf_client . NetworkAddress ( <EOL> address_type = rdf_client . NetworkAddress . Family . INET , <EOL> human_readable = "<STR_LIT>" , <EOL> packed_bytes = socket . inet_pton ( socket . AF_INET , "<STR_LIT>" ) , <EOL> ) ] <EOL> ) ] <EOL> def EnumerateFilesystems ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_client . Filesystem ( device = "<STR_LIT>" , <EOL> mount_point = "<STR_LIT>" ) ] <EOL> def GetClientInfo ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_client . ClientInformation ( <EOL> client_name = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> client_version = int ( config_lib . CONFIG [ "<STR_LIT>" ] ) , <EOL> build_time = config_lib . CONFIG [ "<STR_LIT>" ] , <EOL> labels = [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ) ] <EOL> def GetUserInfo ( self , user ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> user . homedir = "<STR_LIT>" % user . username <EOL> user . full_name = user . username . capitalize ( ) <EOL> return [ user ] <EOL> def GetConfiguration ( self , _ ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> return [ rdf_protodict . Dict ( { <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> <EOL> } ) ] <EOL> def WmiQuery ( self , query ) : <EOL> if query . query == u"<STR_LIT>" : <EOL> self . response_count += <NUM_LIT:1> <EOL> return client_fixture . WMI_SAMPLE <EOL> elif query . query . startswith ( "<STR_LIT>" <EOL> "<STR_LIT>" ) : <EOL> self . response_count += <NUM_LIT:1> <EOL> rdf_dict = rdf_protodict . Dict ( ) <EOL> wmi_properties = ( client_fixture . WMIWin32NetworkAdapterConfigurationMock . <EOL> __dict__ . iteritems ( ) ) <EOL> for key , value in wmi_properties : <EOL> if not key . startswith ( "<STR_LIT>" ) : <EOL> try : <EOL> rdf_dict [ key ] = value <EOL> except TypeError : <EOL> rdf_dict [ key ] = "<STR_LIT>" % value <EOL> return [ rdf_dict ] <EOL> else : <EOL> return None </s>
