Debian Codegraph @

perl/agent/test.pl

raw
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
3
4 ######################### We start with some black magic to print on failure.
5
6 # Change 1..1 below to 1..last_test_to_print .
7 # (It may become useful if the test is moved to ./t subdirectory.)
8
9 BEGIN { $| = 1;
10 $ENV{'SNMPCONFPATH'} = 'nopath';
11 $ENV{'MIBS'} = '';
12 print "1..6\n";
13 }
14 END {print "not ok 1\n" unless $loaded;}
15 use NetSNMP::agent (':all');
16 use NetSNMP::default_store (':all');
17 use NetSNMP::agent::default_store (':all');
18 use NetSNMP::ASN (':all');
19 use NetSNMP::OID;
20 #use NetSNMP::agent (':all');
21 use SNMP;
22 $loaded = 1;
23 print "ok 1\n";
24
25 ######################### End of black magic.
26
27 sub it {
28 if ($_[0]) {
29 return "ok " . $_[1] . "\n";
30 } else {
31 return "not ok ". $_[1] ."\n";
32 }
33 }
34
35 # Insert your test code below (better if it prints "ok 13"
36 # (correspondingly "not ok 13") depending on the success of chunk 13
37 # of the test code):
38
39 print it((MODE_GET == 0xa0 &&
40 MODE_GETNEXT == 0xa1 &&
41 MODE_GETBULK == 0xa5 &&
42 MODE_SET_BEGIN == -1 &&
43 MODE_SET_RESERVE1 == 0 &&
44 MODE_SET_RESERVE2 == 1 &&
45 MODE_SET_ACTION == 2 &&
46 MODE_SET_COMMIT == 3 &&
47 MODE_SET_FREE == 4 &&
48 MODE_SET_UNDO == 5 &&
49 SNMP_ERR_NOERROR == 0 &&
50 SNMP_ERR_TOOBIG == 1 &&
51 SNMP_ERR_NOSUCHNAME == 2 &&
52 SNMP_ERR_BADVALUE == 3 &&
53 SNMP_ERR_READONLY == 4 &&
54 SNMP_ERR_GENERR == 5 &&
55 SNMP_ERR_NOACCESS == 6 &&
56 SNMP_ERR_WRONGTYPE == 7 &&
57 SNMP_ERR_WRONGLENGTH == 8 &&
58 SNMP_ERR_WRONGENCODING == 9 &&
59 SNMP_ERR_WRONGVALUE == 10 &&
60 SNMP_ERR_NOCREATION == 11 &&
61 SNMP_ERR_INCONSISTENTVALUE == 12 &&
62 SNMP_ERR_RESOURCEUNAVAILABLE == 13 &&
63 SNMP_ERR_COMMITFAILED == 14 &&
64 SNMP_ERR_UNDOFAILED == 15 &&
65 SNMP_ERR_AUTHORIZATIONERROR == 16 &&
66 SNMP_ERR_NOTWRITABLE == 17
67 ), 2);
68
69 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
70 NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
71 my $agent = new NetSNMP::agent('Name' => 'test',
72 'Ports' => '9161');
73 print it($agent, 3);
74
75 $regitem = $agent->register("test_reg", ".1.3.6.1.8888", \&testsub);
76 print it($regitem, 4);
77 #print STDERR $regitem,":",ref($regitem),"\n";
78 print it(ref($regitem) eq "NetSNMP::agent::netsnmp_handler_registration", 5);
79
80 my $uptime1 = $agent->uptime();
81 my $uptime2 = $agent->uptime(666);
82 my $uptime3 = $agent->uptime(555, 444);
83 print it($uptime1 <= $uptime2 && $uptime2 <= $uptime3, 6);
84
85 exit;
86
87 while(1) {
88 print netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
89 NETSNMP_DS_AGENT_PORTS), "\n";
90 $agent->agent_check_and_process(1);
91 print netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
92 NETSNMP_DS_AGENT_PORTS), "\n";
93 print "got something\n";
94 }
95 exit;
96
97 $x = NetSNMP::agent::handler_registration::new("hi",\&testsub,".1.3.6.1.999");
98 print ((ref($x) eq "handler_registrationPtr") ? "ok 2\n" : "not ok 2\n");
99
100 print (($x->register() == 0) ? "ok 3\n" : "not ok 3\n");
101
102 my $y = NetSNMP::agent::register_mib("me",\&testsub,".1.3.6.1.8888");
103 while(1) {
104 NetSNMP::agent::agent_check_and_process();
105 print "got something\n";
106 }
107
108 #use Data::Dumper;
109 sub testsub {
110 print STDERR "in perl handler sub\n";
111 print STDERR " args: ", join(", ", @_), "\n";
112 #print STDERR " dumped args: ", Dumper(@_);
113 $oid= $_[3]->getOID();
114 print STDERR " request oid: ", ref($oid), " -> ", $oid, "\n";
115 print STDERR " mode: ", $_[2]->getMode(),"\n";
116 $_[3]->setOID(".1.3.6.1.8888.1");
117 $_[3]->setValue(2, 42);
118 $_[3]->setValue(ASN_INTEGER, 42);
119 print STDERR " oid: ", $_[3]->getOID(),"\n";
120 print STDERR " ref: ", ref($_[3]),"\n";
121 print STDERR " val: ", $_[3]->getValue(),"\n";
122 }