Changeset 1426

Show
Ignore:
Timestamp:
04/02/08 20:35:55 (4 months ago)
Author:
kindlund
Message:

Updated Agent unit tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeyclient/branches/exp/kindlund-simpler_agent/lib/HoneyClient/Agent.pm

    r1425 r1426  
    111111=begin testing 
    112112 
     113# Make sure Log::Log4perl loads 
     114BEGIN { use_ok('Log::Log4perl', qw(:nowarn)) 
     115        or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path."); 
     116 
     117        # Suppress all logging messages, since we need clean output for unit testing. 
     118        Log::Log4perl->init({ 
     119            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     120            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     121            "log4perl.appender.Buffer.min_level"                => "fatal", 
     122            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     123            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     124        }); 
     125} 
     126require_ok('Log::Log4perl'); 
     127use Log::Log4perl qw(:easy); 
     128 
     129# Make sure HoneyClient::Util::Config loads. 
     130BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) 
     131        or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path."); 
     132 
     133        # Suppress all logging messages, since we need clean output for unit testing. 
     134        Log::Log4perl->init({ 
     135            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     136            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     137            "log4perl.appender.Buffer.min_level"                => "fatal", 
     138            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     139            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     140        }); 
     141} 
     142require_ok('HoneyClient::Util::Config'); 
     143can_ok('HoneyClient::Util::Config', 'getVar'); 
     144use HoneyClient::Util::Config qw(getVar); 
     145 
     146# Suppress all logging messages, since we need clean output for unit testing. 
     147Log::Log4perl->init({ 
     148    "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     149    "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     150    "log4perl.appender.Buffer.min_level"                => "fatal", 
     151    "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     152    "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     153}); 
     154 
    113155# Make sure the module loads properly, with the exportable 
    114156# functions shared. 
     
    125167can_ok('HoneyClient::Util::SOAP', 'getClientHandle'); 
    126168use HoneyClient::Util::SOAP qw(getServerHandle getClientHandle); 
    127  
    128 # Make sure HoneyClient::Util::Config loads. 
    129 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path."); } 
    130 require_ok('HoneyClient::Util::Config'); 
    131 can_ok('HoneyClient::Util::Config', 'getVar'); 
    132 use HoneyClient::Util::Config qw(getVar); 
    133169 
    134170# Make sure Storable loads. 
     
    153189use Win32::Job; 
    154190 
     191# Make sure DateTime::HiRes loads. 
     192BEGIN { use_ok('DateTime::HiRes') or diag("Can't load DateTime::HiRes package.  Check to make sure the package library is correctly listed within the path."); } 
     193require_ok('DateTime::HiRes'); 
     194use DateTime::HiRes; 
     195 
    155196# Global test variables. 
    156197our $PORT = getVar(name      => "port", 
     
    188229use Win32::Job; 
    189230 
     231# Use ISO 8601 DateTime Libraries 
     232use DateTime::HiRes; 
     233 
    190234# Include Logging Library 
    191235use Log::Log4perl qw(:easy); 
     
    200244# The process ID of the SOAP server daemon, once created. 
    201245our $DAEMON_PID     = undef; 
    202  
    203 # Global array, to indicate which implemented Drivers the 
    204 # Agent is allowed to run. 
    205 our $ALLOWED_DRIVERS = getVar(name => 'allowed_drivers')->{name}; 
    206  
    207 # Global value, to indicate if the Agent should perform 
    208 # any integrity checks. 
    209 our $PERFORM_INTEGRITY_CHECKS = 
    210     getVar(name => "perform_integrity_checks"); 
    211246 
    212247####################################################################### 
     
    281316        Carp::croak "Error: " . __PACKAGE__ . " daemon is already running (PID = $DAEMON_PID)!\n"; 
    282317    } 
    283  
    284     # Reinitialize global constants (for dynamic updates). 
    285     $PERFORM_INTEGRITY_CHECKS = getVar(name => "perform_integrity_checks"); 
    286  
    287     # Figure out what our list of allowed Drivers are.  
    288     $ALLOWED_DRIVERS = getVar(name => 'allowed_drivers')->{name}; 
    289318 
    290319    my $argsExist = scalar(%args); 
     
    386415    } 
    387416    if ($ret) { 
    388         $DAEMON_PID           = undef; 
     417        $DAEMON_PID = undef; 
    389418    } 
    390419    return $ret; 
     
    394423# Private Methods Implemented                                         # 
    395424####################################################################### 
     425 
     426# Helper function designed to get a current timestamp from 
     427# the system OS. 
     428# 
     429# Note: This timestamp is in ISO 8601 format. 
     430# 
     431# Inputs: none 
     432# Outputs: timestamp 
     433sub _getTimestamp { 
     434    my $dt = DateTime::HiRes->now(time_zone => "local"); 
     435    return $dt->ymd('-') . " " . 
     436           $dt->hms(':') . "." . 
     437           $dt->nanosecond(); 
     438} 
    396439 
    397440####################################################################### 
     
    462505                            address   => "localhost"); 
    463506 
     507    diag("Driving HoneyClient::Agent::Driver::Browser::IE with no parameters..."); 
     508 
    464509    # Drive the Agent using IE. 
    465     $stub->drive(driver_name => "HoneyClient::Agent::Driver::Browser::IE", 
    466                  parameters => "http://www.google.com"); 
    467  
    468     # Make sure the test VM is stopped. 
    469     #$som = $stub->stopVM(config => $testVM); 
    470  
    471     # Test isRegisteredVM() method. 
    472     #$som = $stub->isRegisteredVM(config => $testVM); 
    473  
    474     # The test VM should be registered. 
    475     #ok($som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually."); 
    476  
    477     # Make sure the test VM is unregistered. 
    478     #$som = $stub->unregisterVM(config => $testVM); 
    479  
    480     # Test isRegisteredVM() method. 
    481     #$som = $stub->isRegisteredVM(config => $testVM); 
    482  
    483     # The test VM should not be registered. 
    484     #ok(!$som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually."); 
     510    $som = $stub->drive(driver_name => "HoneyClient::Agent::Driver::Browser::IE"); 
     511 
     512    # Verify changes. 
     513    my $changes = $som->result(); 
     514 
     515use Data::Dumper; 
     516diag(Dumper($changes)); 
     517    
     518    # Check to see if the drive operation completed properly.  
     519    ok($changes, "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     520    ok(exists($changes->{'status'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     521    ok(exists($changes->{'driven_at'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     522    ok(exists($changes->{'fingerprint'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     523 
     524    # Check that os_processes is empty. 
     525    ok(!scalar(@{$changes->{'fingerprint'}->{os_processes}}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     526 
    485527}; 
    486528 
     
    516558 
    517559        # Die if no valid argument is supplied. 
    518         $LOG->warn("No Driver name specified."); 
     560        $LOG->error("No Driver name specified."); 
    519561        die SOAP::Fault->faultcode(__PACKAGE__ . "->drive()") 
    520562                       ->faultstring("No Driver name specified."); 
     
    523565    # Sanity check.  Make sure the driver name specified is 
    524566    # on our allowed list. 
    525     my @drivers_found = grep(/^$args{'driver_name'}$/, @{$ALLOWED_DRIVERS}); 
     567    my @drivers_found = grep(/^$args{'driver_name'}$/, @{getVar(name => 'allowed_drivers')->{name}}); 
    526568    my $driverName = pop(@drivers_found); 
    527569    unless (defined($driverName)) { 
    528         $LOG->warn("Not allowed to run Driver (" . $args{'driver_name'} . ")."); 
     570        $LOG->error("Not allowed to run Driver (" . $args{'driver_name'} . ")."); 
    529571        die SOAP::Fault->faultcode(__PACKAGE__ . "->drive()") 
    530572                       ->faultstring("Not allowed to run Driver (" . $args{'driver_name'} . ")."); 
     
    544586    } 
    545587 
     588    # Construct the output hashtable. 
     589    my $ret = { 
     590        # Time when application was driven. 
     591        'driven_at'   => _getTimestamp(), 
     592 
     593        # Fingerprint information found (if any). 
     594        'fingerprint' => undef, 
     595 
     596        # Status information about the Win32::Job call. 
     597        'status'      => undef, 
     598    }; 
     599 
    546600    # Create a new Job. 
    547601    my $job = Win32::Job->new(); 
     
    567621                       ->faultstring("Error: Unable to execute '" . $processExec . "'"); 
    568622    } 
     623    $ret->{'status'} = $status; 
     624 
     625    $LOG->info($args{'driver_name'} . " - Driving To Resource: " . $args{'parameters'}); 
    569626 
    570627    # Run the job. 
     
    601658    } 
    602659 
    603     return 1; 
     660    # Perform an integrity check, if desired. 
     661    if (getVar(name => "perform_integrity_checks")) { 
     662        my $integrity = HoneyClient::Agent::Integrity->new(); 
     663        $ret->{'fingerprint'} = $integrity->check(); 
     664        if (scalar(@{$ret->{'fingerprint'}->{os_processes}})) { 
     665            $LOG->warn($args{'driver_name'} . " - Integrity Check: FAILED"); 
     666        } else { 
     667            $LOG->info($args{'driver_name'} . " - Integrity Check: PASSED"); 
     668        } 
     669    } 
     670  
     671    return $ret; 
    604672} 
    605673 
     
    616684=head1 BUGS & ASSUMPTIONS 
    617685 
    618 # XXX: Fill this in. 
    619 # XXX: If, at any time, the Manager's SOAP connection to the Agent 
     686If, at any time, the Manager's SOAP connection to the Agent 
    620687is disrupted during a drive() operation, then the Manager should assume 
    621688that the VM has been compromised and proceed to handle the VM as such. 
  • honeyclient/branches/exp/kindlund-simpler_agent/t/honeyclient_agent.t

    r1425 r1426  
    99# =begin testing 
    1010{ 
     11# Make sure Log::Log4perl loads 
     12BEGIN { use_ok('Log::Log4perl', qw(:nowarn)) 
     13        or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path."); 
     14 
     15        # Suppress all logging messages, since we need clean output for unit testing. 
     16        Log::Log4perl->init({ 
     17            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     18            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     19            "log4perl.appender.Buffer.min_level"                => "fatal", 
     20            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     21            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     22        }); 
     23} 
     24require_ok('Log::Log4perl'); 
     25use Log::Log4perl qw(:easy); 
     26 
     27# Make sure HoneyClient::Util::Config loads. 
     28BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) 
     29        or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path."); 
     30 
     31        # Suppress all logging messages, since we need clean output for unit testing. 
     32        Log::Log4perl->init({ 
     33            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     34            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     35            "log4perl.appender.Buffer.min_level"                => "fatal", 
     36            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     37            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     38        }); 
     39} 
     40require_ok('HoneyClient::Util::Config'); 
     41can_ok('HoneyClient::Util::Config', 'getVar'); 
     42use HoneyClient::Util::Config qw(getVar); 
     43 
     44# Suppress all logging messages, since we need clean output for unit testing. 
     45Log::Log4perl->init({ 
     46    "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     47    "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     48    "log4perl.appender.Buffer.min_level"                => "fatal", 
     49    "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     50    "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     51}); 
     52 
    1153# Make sure the module loads properly, with the exportable 
    1254# functions shared. 
     
    2365can_ok('HoneyClient::Util::SOAP', 'getClientHandle'); 
    2466use HoneyClient::Util::SOAP qw(getServerHandle getClientHandle); 
    25  
    26 # Make sure HoneyClient::Util::Config loads. 
    27 BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path."); } 
    28 require_ok('HoneyClient::Util::Config'); 
    29 can_ok('HoneyClient::Util::Config', 'getVar'); 
    30 use HoneyClient::Util::Config qw(getVar); 
    3167 
    3268# Make sure Storable loads. 
     
    5086require_ok('Win32::Job'); 
    5187use Win32::Job; 
     88 
     89# Make sure DateTime::HiRes loads. 
     90BEGIN { use_ok('DateTime::HiRes') or diag("Can't load DateTime::HiRes package.  Check to make sure the package library is correctly listed within the path."); } 
     91require_ok('DateTime::HiRes'); 
     92use DateTime::HiRes; 
    5293 
    5394# Global test variables. 
     
    93134                            address   => "localhost"); 
    94135 
     136    diag("Driving HoneyClient::Agent::Driver::Browser::IE with no parameters..."); 
     137 
    95138    # Drive the Agent using IE. 
    96     $stub->drive(driver_name => "HoneyClient::Agent::Driver::Browser::IE", 
    97                  parameters => "http://www.google.com"); 
     139    $som = $stub->drive(driver_name => "HoneyClient::Agent::Driver::Browser::IE"); 
    98140 
    99     # Make sure the test VM is stopped
    100     #$som = $stub->stopVM(config => $testVM); 
     141    # Verify changes
     142    my $changes = $som->result(); 
    101143 
    102     # Test isRegisteredVM() method. 
    103     #$som = $stub->isRegisteredVM(config => $testVM); 
     144use Data::Dumper; 
     145diag(Dumper($changes)); 
     146    
     147    # Check to see if the drive operation completed properly.  
     148    ok($changes, "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     149    ok(exists($changes->{'status'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     150    ok(exists($changes->{'driven_at'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
     151    ok(exists($changes->{'fingerprint'}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
    104152 
    105     # The test VM should be registered
    106     #ok($som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually."); 
     153    # Check that os_processes is empty
     154    ok(!scalar(@{$changes->{'fingerprint'}->{os_processes}}), "drive(driver_name => 'HoneyClient::Agent::Driver::Browser::IE')") or diag("The drive() call failed."); 
    107155 
    108     # Make sure the test VM is unregistered. 
    109     #$som = $stub->unregisterVM(config => $testVM); 
    110  
    111     # Test isRegisteredVM() method. 
    112     #$som = $stub->isRegisteredVM(config => $testVM); 
    113  
    114     # The test VM should not be registered. 
    115     #ok(!$som->result, "isRegisteredVM(config => '$testVM')") or diag("The isRegisteredVM() call failed.  If ($testVM) is still registered, be sure to unregister it manually."); 
    116156}; 
    117157 
  • honeyclient/branches/exp/kindlund-simpler_agent/t/honeyclient_manager_vm.t

    r1008 r1426  
    250250# Kill the child daemon, if it still exists. 
    251251HoneyClient::Manager::VM->destroy(); 
    252 # XXX: See if this is still needed. 
    253 #sleep (1); 
    254252 
    255253# Report any failure found. 
     
    295293# Kill the child daemon, if it still exists. 
    296294HoneyClient::Manager::VM->destroy(); 
    297 # XXX: See if this is still needed. 
    298 #sleep (1); 
    299295 
    300296# Report any failure found. 
     
    339335# Kill the child daemon, if it still exists. 
    340336HoneyClient::Manager::VM->destroy(); 
    341 # XXX: See if this is still needed. 
    342 #sleep (1); 
    343337 
    344338# Report any failure found. 
     
    395389# Kill the child daemon, if it still exists. 
    396390HoneyClient::Manager::VM->destroy(); 
    397 # XXX: See if this is still needed. 
    398 #sleep (1); 
    399391 
    400392# Report any failure found. 
     
    442434# Kill the child daemon, if it still exists. 
    443435HoneyClient::Manager::VM->destroy(); 
    444 # XXX: See if this is still needed. 
    445 #sleep (1); 
    446436 
    447437# Report any failure found. 
     
    499489# Kill the child daemon, if it still exists. 
    500490HoneyClient::Manager::VM->destroy(); 
    501 # XXX: See if this is still needed. 
    502 #sleep (1); 
    503491 
    504492# Report any failure found. 
     
    548536# Kill the child daemon, if it still exists. 
    549537HoneyClient::Manager::VM->destroy(); 
    550 # XXX: See if this is still needed. 
    551 #sleep (1); 
    552538 
    553539# Report any failure found. 
     
    618604# Kill the child daemon, if it still exists. 
    619605HoneyClient::Manager::VM->destroy(); 
    620 # XXX: See if this is still needed. 
    621 #sleep (1); 
    622606 
    623607# Report any failure found. 
     
    662646# Kill the child daemon, if it still exists. 
    663647HoneyClient::Manager::VM->destroy(); 
    664 # XXX: See if this is still needed. 
    665 #sleep (1); 
    666648 
    667649# Report any failure found. 
     
    715697# Kill the child daemon, if it still exists. 
    716698HoneyClient::Manager::VM->destroy(); 
    717 # XXX: See if this is still needed. 
    718 #sleep (1); 
    719699 
    720700# Report any failure found. 
     
    761741# Kill the child daemon, if it still exists. 
    762742HoneyClient::Manager::VM->destroy(); 
    763 # XXX: See if this is still needed. 
    764 #sleep (1); 
    765743 
    766744# Report any failure found. 
     
    813791# Kill the child daemon, if it still exists. 
    814792HoneyClient::Manager::VM->destroy(); 
    815 # XXX: See if this is still needed. 
    816 #sleep (1); 
    817793 
    818794# Report any failure found. 
     
    860836# Kill the child daemon, if it still exists. 
    861837HoneyClient::Manager::VM->destroy(); 
    862 # XXX: See if this is still needed. 
    863 #sleep (1); 
    864838 
    865839# Report any failure found. 
     
    904878# Kill the child daemon, if it still exists. 
    905879HoneyClient::Manager::VM->destroy(); 
    906 # XXX: See if this is still needed. 
    907 #sleep (1); 
    908880 
    909881# Report any failure found. 
     
    975947# Kill the child daemon, if it still exists. 
    976948HoneyClient::Manager::VM->destroy(); 
    977 # XXX: See if this is still needed. 
    978 #sleep (1); 
    979949 
    980950# Report any failure found. 
     
    10471017# Kill the child daemon, if it still exists. 
    10481018HoneyClient::Manager::VM->destroy(); 
    1049 # XXX: See if this is still needed. 
    1050 #sleep (1); 
    10511019 
    10521020# Report any failure found. 
     
    11321100# Kill the child daemon, if it still exists. 
    11331101HoneyClient::Manager::VM->destroy(); 
    1134 # XXX: See if this is still needed. 
    1135 #sleep (1); 
    11361102 
    11371103# Report any failure found. 
     
    12281194# Kill the child daemon, if it still exists. 
    12291195HoneyClient::Manager::VM->destroy(); 
    1230 # XXX: See if this is still needed. 
    1231 #sleep (1); 
    12321196 
    12331197# Report any failure found. 
     
    13411305# Kill the child daemon, if it still exists. 
    13421306HoneyClient::Manager::VM->destroy(); 
    1343 # XXX: See if this is still needed. 
    1344 #sleep (1); 
    13451307 
    13461308# Report any failure found. 
     
    14531415# Kill the child daemon, if it still exists. 
    14541416HoneyClient::Manager::VM->destroy(); 
    1455 # XXX: See if this is still needed. 
    1456 #sleep (1); 
    14571417 
    14581418# Report any failure found.