BEGIN { use_ok
'POE::Component::SNMP::Session'
};
my
$CONF
=
do
"config.cache"
;
if
(1) {
$poe_kernel
->run();
plan
skip_all
=>
'todo'
;
}
if
(
$CONF
->{skip_all_tests} ) {
$poe_kernel
->run();
plan
skip_all
=>
'No SNMP data specified.'
;
}
else
{
plan
tests
=> 27;
}
POE::Session->create
(
inline_states
=>
{
_start
=> \
&snmp_run_tests
,
_stop
=> \
&stop_session
,
snmp_get_cb
=> \
&snmp_get_cb
,
},
);
$poe_kernel
->run;
ok 1;
exit
0;
my
$sysName
=
"1.3.6.1.2.1.1.5.0"
;
my
$system_base
=
"1.3.6.1.2.1.1"
;
sub
snmp_run_tests {
my
(
$kernel
,
$heap
) =
@_
[KERNEL, HEAP];
SKIP: {
skip
"parameter checks"
, 2
if
0
;
eval
{
POE::Component::SNMP::Session->create(
Alias
=>
'snmp_no_hostname'
,
Community
=>
$CONF
->{
'community'
},
);
};
ok $@,
'-hostname parameter required'
;
ok !
$kernel
->alias_resolve(
'snmp_no_hostname'
),
"session not created with missing hostname"
;
}
POE::Component::SNMP::Session->create(
Alias
=>
'snmp'
,
DestHost
=>
$CONF
->{
'hostname'
},
Community
=>
$CONF
->{
'community'
},
);
ok
$kernel
->alias_resolve(
'snmp'
),
"normal session create"
;
SKIP: {
skip
"dupe session check not safely trappable"
, 2
if
$POE::VERSION
<= 0.95 and POE::Kernel::ASSERT_DEFAULT;
eval
{
POE::Component::SNMP::Session->create(
Alias
=>
'snmp'
,
DestHost
=>
$CONF
->{
'hostname'
},
Community
=>
$CONF
->{
'community'
},
);
};
ok $@ =~ /
'snmp'
already
exists
|
'snmp'
is in
use
by another session/,
"duplicate alias is fatal"
;
ok
$kernel
->alias_resolve(
'snmp'
),
"existing session not affected"
;
}
SKIP: {
skip
"need secondary hostname"
, 3
unless
0!=0 and
$CONF
->{
'hostname2'
};
my
$LOCALPORT
= 43128;
eval
{
POE::Component::SNMP::Session->create(
Alias
=>
'snmp_localport'
,
DestHost
=>
$CONF
->{
'hostname'
},
Community
=>
$CONF
->{
'community'
},
localport
=>
$LOCALPORT
,
);
};
ok
$kernel
->alias_resolve(
'snmp_localport'
),
"session created with localport $LOCALPORT"
;
SKIP: {
skip
"not tested"
, 2;
eval
{
POE::Component::SNMP::Session->create(
Alias
=>
'snmp_localport_conflict'
,
DestHost
=>
$CONF
->{
'hostname2'
},
Community
=>
$CONF
->{
'community2'
},
localport
=>
$LOCALPORT
,
);
};
ok $@ =~ /Address already in
use
/,
'Eval returns Address in use'
;
my
$localport_close
= 0;
$localport_close
and
$kernel
->call(
snmp_localport
=>
'finish'
);
ok !
$kernel
->alias_resolve(
'snmp_localport_conflict'
),
"session NOT created with duplicate localport"
;
$localport_close
and ok !
$kernel
->alias_resolve(
'snmp_localport'
),
"session NOT created with duplicate localport"
;
}
}
$heap
->{done} = 0;
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
[
$system_base
],
);
get_sent(
$heap
);
SKIP: {
1 and
skip
"client stuff"
, 3;
$heap
->{planned}++;
$kernel
->post(
snmp
=>
walk
=>
'snmp_get_cb'
,
[
'sysName'
]
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
walk
=>
'snmp_get_cb'
,
0, 1,
''
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
0, 1, [
$system_base
],
);
get_sent(
$heap
);
}
SKIP: {
1 and
skip
"server stuff"
, 5;
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
undef
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
''
);
get_sent(
$heap
);
if
(0) {
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
$sysName
,
);
}
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
[
'9.9.9.9.9.9.9'
],
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
get
=>
'snmp_get_cb'
,
[
'1.9.9.9.9.9.9'
],
);
get_sent(
$heap
);
}
SKIP: {
1 and
skip
"write tests"
, 3;
skip
"no writeable SNMP device available"
, 3 + 6
if
not
length
$CONF
->{wcommunity};
my
$read_only_string_oid
=
".1.3.6.1.2.1.1.9.1.3.1"
;
$heap
->{planned}++;
$kernel
->post(
snmp
=>
set
=>
'snmp_get_cb'
,
[
'1.9.9.9.9.9.9'
],
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
set
=>
'snmp_get_cb'
,
[
$read_only_string_oid
,
'STRING'
,
'hi mom'
],
);
get_sent(
$heap
);
$heap
->{planned}++;
$kernel
->post(
snmp
=>
set
=>
'snmp_get_cb'
,
[
$read_only_string_oid
,
'OCTET_STRING'
,
'hi mom'
],
);
get_sent(
$heap
);
}
}
sub
snmp_get_cb {
my
(
$kernel
,
$heap
,
$request
,
$aref
) =
@_
[KERNEL, HEAP, ARG0, ARG1];
ok get_seen(
$heap
);
ok
ref
$aref
eq
'ARRAY'
;
my
$href
=
$aref
->[0];
if
(
ref
$href
) {
ok
ref
$href
eq
'SNMP::VarList'
;
if
(
$request
->[2] eq
'get'
and
$request
->[3] eq
'-varbindlist'
and
not
defined
$request
->[4]) {
ok
keys
%$href
== 0;
ok
@$aref
== 1;
ok ++
$heap
->{saw_empty},
"empty_request returned for undef -varbindlist"
;
}
foreach
my
$varlist
(
$href
) {
ok
ref
$varlist
eq
'SNMP::VarList'
,
ref
(
$varlist
) .
' e SNMP::VarList'
;
for
my
$var
(
@$varlist
) {
ok
ref
$var
eq
'SNMP::Varbind'
,
ref
(
$var
) .
' e SNMP::Varbind'
;
push
@{
$heap
->{results}{
$var
->[0]}},
$var
->[2];
}
}
}
elsif
(
defined
$href
) {
my
$message
=
$href
;
ok
$message
,
"received error: $message"
;
}
if
(check_done(
$heap
)) {
$kernel
->post(
snmp
=>
'finish'
);
ok check_done(
$heap
);
$kernel
->post(
snmp
=>
'finish'
);
}
}
sub
stop_session {
my
$r
=
$_
[HEAP]->{results};
ok 1;
if
(0) {
ok
$_
[HEAP]->{saw_empty};
ok !(
ref
$r
eq
'HASH'
);
ok !
keys
%$r
;
}
}