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