| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test; |
| 6 |
|
| 7 |
BEGIN { |
| 8 |
eval "use Cwd qw(abs_path)"; |
| 9 |
plan tests => 11; |
| 10 |
} |
| 11 |
use SNMP; |
| 12 |
require 't/startagent.pl'; |
| 13 |
use vars qw($agent_host $agent_port $auth_pass $bad_name $comm $comm2 |
| 14 |
$priv_pass $sec_name $trap_port); |
| 15 |
$SNMP::debugging = 0; |
| 16 |
|
| 17 |
my $res; |
| 18 |
my $enterprise = '.1.3.6.1.2.1.1.1.0'; |
| 19 |
my $generic = 'specific'; |
| 20 |
|
| 21 |
# V1 trap testing |
| 22 |
######################## 1 ############################ |
| 23 |
# Fire up a trap session. |
| 24 |
my $s1 = |
| 25 |
new SNMP::Session (DestHost=>$agent_host,Version=>1,Community=>$comm,RemotePort=>$trap_port); |
| 26 |
ok(defined($s1)); |
| 27 |
|
| 28 |
######################## 2 ############################ |
| 29 |
# test v1 trap |
| 30 |
if (defined($s1)) { |
| 31 |
$res = $s1->trap(enterprise => $enterprise, agent=>$agent_host, |
| 32 |
generic => $generic, |
| 33 |
[['sysContact', 0, 'root@localhost'], |
| 34 |
['sysLocation', 0, 'here']] ); |
| 35 |
} |
| 36 |
ok($res =~ /^0 but true/); |
| 37 |
|
| 38 |
######################## 3 ############################ |
| 39 |
# test with wrong varbind |
| 40 |
undef $res; |
| 41 |
if (defined($s1)) { |
| 42 |
$res = $s1->trap([[$bad_name, 0, 'root@localhost'], |
| 43 |
['sysLocation', 0, 'here']] ); |
| 44 |
#print("res is $res\n"); |
| 45 |
} |
| 46 |
ok(!defined($res)); |
| 47 |
######################################################### |
| 48 |
|
| 49 |
# V2 testing |
| 50 |
######################## 4 ############################ |
| 51 |
# Fire up a v2 trap session. |
| 52 |
my $s2 = |
| 53 |
new SNMP::Session (Version=>2, DestHost=>$agent_host,Community=>$comm2,RemotePort=>$trap_port); |
| 54 |
ok(defined($s2)); |
| 55 |
######################## 5 ############################ |
| 56 |
# test v2 trap |
| 57 |
undef $res; |
| 58 |
if (defined($s2)) { |
| 59 |
$res = $s2->trap(uptime=>200, trapoid=>'coldStart', |
| 60 |
[['sysContact', 0, 'root@localhost'], |
| 61 |
['sysLocation', 0, 'here']] ); |
| 62 |
#print("res is $res\n"); |
| 63 |
} |
| 64 |
ok($res =~ /^0 but true/); |
| 65 |
######################## 6 ############################ |
| 66 |
# no trapoid and uptime given. Should take defaults... |
| 67 |
my $ret; |
| 68 |
if (defined($s2)) { |
| 69 |
$ret = $s2->trap([['sysContact', 0, 'root@localhost'], |
| 70 |
['sysLocation', 0, 'here']] ); |
| 71 |
#print("res is $ret\n"); |
| 72 |
} |
| 73 |
ok(defined($ret)); |
| 74 |
######################## 7 ############################ |
| 75 |
# no varbind list given. |
| 76 |
undef $res; |
| 77 |
if (defined($s2)) { |
| 78 |
$res = $s2->trap(trapoid=>'coldStart'); |
| 79 |
#print("res is $res\n"); |
| 80 |
} |
| 81 |
ok(defined($res) && $res =~ /^0 but true/); |
| 82 |
|
| 83 |
######################################################### |
| 84 |
|
| 85 |
# v3 testing |
| 86 |
######################## 8 ############################ |
| 87 |
# Fire up a v3 trap session. |
| 88 |
my $s3 = new SNMP::Session(Version=>3, DestHost=> $agent_host, RemotePort=>$trap_port, SecName => $sec_name); |
| 89 |
ok(defined($s3)); |
| 90 |
|
| 91 |
######################## 9 ############################ |
| 92 |
if (defined($s3)) { |
| 93 |
$res = $s3->inform(uptime=>111, trapoid=>'coldStart', |
| 94 |
[['sysContact', 0, 'root@localhost'], |
| 95 |
['sysLocation', 0, 'here']] ); |
| 96 |
} |
| 97 |
ok(defined($res) && $res =~ /^0 but true/); |
| 98 |
|
| 99 |
######################## 10 ############################ |
| 100 |
# Fire up a v3 trap session. |
| 101 |
$s3 = new SNMP::Session(Version => 3, DestHost=> $agent_host, |
| 102 |
RemotePort => $trap_port, SecName => $sec_name, |
| 103 |
SecLevel => 'authPriv', AuthPass => $auth_pass, |
| 104 |
PrivPass => $priv_pass); |
| 105 |
ok(defined($s3)); |
| 106 |
|
| 107 |
######################## 11 ############################ |
| 108 |
undef $res; |
| 109 |
if (defined($s3)) { |
| 110 |
$res = $s3->inform(uptime=>111, trapoid=>'coldStart', |
| 111 |
[['sysContact', 0, 'root@localhost'], |
| 112 |
['sysLocation', 0, 'here']] ); |
| 113 |
print "# res = " . ($res ? $res : "(undefined)") . "\n"; |
| 114 |
} |
| 115 |
|
| 116 |
ok(defined($res) && ($res =~ /^0 but true/)); |
| 117 |
|
| 118 |
snmptest_cleanup(); |