| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Exporter; |
| 6 |
|
| 7 |
#Open the snmptest.cmd file and get the info |
| 8 |
require "t/readsnmptest.pl"; |
| 9 |
use vars qw($agent_host $agent_port $mibdir $snmpd_cmd $snmptrapd_cmd); |
| 10 |
|
| 11 |
our @ISA = 'Exporter'; |
| 12 |
our @EXPORT_OK = qw($agent_host $agent_port $mibdir |
| 13 |
$trap_port $comm $comm2 $comm3 $sec_name $oid $name |
| 14 |
$name_module $name_module2 $name_long $name_module_long $name_module_long2 |
| 15 |
$auth_pass $priv_pass $bad_comm $bad_name $bad_oid $bad_port $bad_host |
| 16 |
$bad_auth_pass $bad_priv_pass $bad_sec_name $bad_version); |
| 17 |
|
| 18 |
# common parameters used in SNMP::Session creation and tests |
| 19 |
$agent_host = 'localhost' if (!defined($agent_host)); |
| 20 |
$agent_port = 8765 if (!defined($agent_port)); |
| 21 |
our $trap_port = 8764; |
| 22 |
$mibdir = '/usr/local/share/snmp/mibs' if (!defined($mibdir)); |
| 23 |
our $comm = 'v1_private'; |
| 24 |
our $comm2 = 'v2c_private'; |
| 25 |
our $comm3 = 'v3_private'; |
| 26 |
our $sec_name = 'v3_user'; |
| 27 |
our $oid = '.1.3.6.1.2.1.1.1'; |
| 28 |
our $name = 'sysDescr'; |
| 29 |
our $name_module = 'RFC1213-MIB::sysDescr'; |
| 30 |
our $name_module2 = 'SNMPv2-MIB::sysDescr'; |
| 31 |
our $name_long = '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr'; |
| 32 |
our $name_module_long = 'RFC1213-MIB::.iso.org.dod.internet.mgmt.mib-2.system.sysDescr'; |
| 33 |
our $name_module_long2 = 'SNMPv2-MIB::.iso.org.dod.internet.mgmt.mib-2.system.sysDescr'; |
| 34 |
our $auth_pass = 'test_pass_auth'; |
| 35 |
our $priv_pass = 'test_pass_priv'; |
| 36 |
|
| 37 |
# erroneous input to test failure cases |
| 38 |
our $bad_comm = 'BAD_COMMUNITY'; |
| 39 |
our $bad_name = "badName"; |
| 40 |
our $bad_oid = ".1.3.6.1.2.1.1.1.1.1.1"; |
| 41 |
our $bad_host = 'bad.host.here'; |
| 42 |
our $bad_port = '9999999'; |
| 43 |
our $bad_auth_pass = 'bad_auth_pass'; |
| 44 |
our $bad_priv_pass = 'bad_priv_pass'; |
| 45 |
our $bad_sec_name = 'bad_sec_name'; |
| 46 |
our $bad_version = 7; |
| 47 |
|
| 48 |
if ($^O =~ /win32/i) { |
| 49 |
require Win32::Process; |
| 50 |
} |
| 51 |
|
| 52 |
# Variant of sleep that accepts a floating point number as argument. |
| 53 |
sub delay { |
| 54 |
my ($timeout) = @_; |
| 55 |
|
| 56 |
select(undef, undef, undef, $timeout); |
| 57 |
} |
| 58 |
|
| 59 |
sub run_async { |
| 60 |
my ($pidfile, $cmd, @args) = @_; |
| 61 |
if (-r "$cmd" and -x "$cmd") { |
| 62 |
if ($^O =~ /win32/i) { |
| 63 |
$cmd =~ s/\//\\/g; |
| 64 |
system "start \"$cmd\" /min cmd /c \"$cmd @args 2>&1\""; |
| 65 |
} else { |
| 66 |
system "$cmd @args 2>&1"; |
| 67 |
} |
| 68 |
# Wait at most three seconds for the pid file to appear. |
| 69 |
for (my $i = 0; ($i < 30) && ! (-r "$pidfile"); ++$i) { |
| 70 |
delay 0.1; |
| 71 |
} |
| 72 |
} else { |
| 73 |
warn "Couldn't run $cmd\n"; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
sub snmptest_cleanup { |
| 78 |
my $ignore_failures = shift; |
| 79 |
|
| 80 |
kill_by_pid_file("t/snmpd.pid", $ignore_failures); |
| 81 |
unlink("t/snmpd.pid"); |
| 82 |
kill_by_pid_file("t/snmptrapd.pid", $ignore_failures); |
| 83 |
unlink("t/snmptrapd.pid"); |
| 84 |
} |
| 85 |
|
| 86 |
sub kill_by_pid_file { |
| 87 |
my $pidfile = shift; |
| 88 |
my $ignore_failures = shift; |
| 89 |
|
| 90 |
if (!open(H, "<$pidfile")) { |
| 91 |
return; |
| 92 |
} |
| 93 |
my $pid = (<H>); |
| 94 |
close (H); |
| 95 |
if (!$pid) { |
| 96 |
defined($ignore_failures) or die "Reading $pidfile failed\n"; |
| 97 |
return; |
| 98 |
} |
| 99 |
if ($^O !~ /win32/i) { |
| 100 |
# Unix or Windows + Cygwin. |
| 101 |
system "kill $pid > /dev/null 2>&1"; |
| 102 |
} else { |
| 103 |
# Windows + MSVC or Windows + MinGW. |
| 104 |
Win32::Process::KillProcess($pid, 0); |
| 105 |
} |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
# Stop any processes started during a previous test. |
| 111 |
snmptest_cleanup(1); |
| 112 |
|
| 113 |
# Start snmpd and snmptrapd. |
| 114 |
|
| 115 |
#warn "\nStarting agents for test script $0\n"; |
| 116 |
|
| 117 |
my $scriptname = "snmptest"; |
| 118 |
if ($0 =~ /^t[\/\\](.*)\.t$/) { |
| 119 |
$scriptname = $1; |
| 120 |
} |
| 121 |
|
| 122 |
if ($snmpd_cmd) { |
| 123 |
run_async("t/snmpd.pid", "$snmpd_cmd", "-r -d -Lf t/snmpd-$scriptname.log -M+$mibdir -C -c t/snmptest.conf -p t/snmpd.pid ${agent_host}:${agent_port} >t/snmpd-$scriptname.stderr"); |
| 124 |
} |
| 125 |
if ($snmptrapd_cmd) { |
| 126 |
run_async("t/snmptrapd.pid", "$snmptrapd_cmd", "-d -Lf t/snmptrapd-$scriptname.log -p t/snmptrapd.pid -M+$mibdir -C -c t/snmptest.conf -C ${agent_host}:${trap_port} >t/snmptrapd-$scriptname.stderr"); |
| 127 |
} |
| 128 |
|
| 129 |
1; |
| 130 |
|