Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / perl / SNMP / t / bulkwalk.t
1 #!/usr/local/bin/perl
2 #
3 # $Id: bulkwalk.t,v 5.1.2.1 2003/02/25 18:10:03 rstory Exp $
4 #
5 # Test bulkwalk functionality.
6 use Data::Dumper;
7
8 BEGIN {
9     unless(grep /blib/, @INC) {
10         chdir 't' if -d 't';
11         @INC = '../lib' if -d '../lib';
12     }
13 }
14 use Test;
15 BEGIN { $num = 62; plan test => $num; }
16
17 use SNMP;
18
19 require "t/startagent.pl";
20
21 use vars qw($agent_port $comm2 $agent_host);
22
23 $SNMP::debugging = 0;
24 $SNMP::verbose = 0;
25
26 #print "1..$num\n";
27
28 ######################################################################
29 # Fire up a session.
30 $s1 = new SNMP::Session(
31     'DestHost'   => $agent_host,
32     'Community'  => $comm2,
33     'RemotePort' => $agent_port,
34     'Version'    => '2c',
35     'UseNumeric' => 1,
36     'UseEnum'    => 0,
37     'UseLongNames' => 1
38 );
39 ok(defined($s1));
40
41 ######################################################################
42
43 # Attempt to use the bulkwalk method to get a few variables from the
44 # SNMP agent.
45 # test 1
46 $vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'], # NON-repeaters
47                             ['ifSpeed'], ['ifDescr']);   # Repeated variables.
48
49 $expect = scalar @$vars;
50 @list = $s1->bulkwalk(2, 16, $vars);
51
52 ok($s1->{ErrorNum} == 0);
53
54 # Did we get back the list of references to returned values?
55 #
56 ok(scalar @list == $expect);
57
58 # Sanity check the returned values.  list[0] is sysUptime nonrepeater.
59 ok($list[0][0]->tag eq ".1.3.6.1.2.1.1.3");     # check system.sysUptime OID
60 ok($list[0][0]->iid eq "0");                    # check system.sysUptime.0 IID
61 ok($list[0][0]->val =~ m/^\d+$/);               # Uptime is numeric 
62 ok($list[0][0]->type eq "TICKS");               # Uptime should be in ticks.
63
64 # Find out how many interfaces to expect.  list[1] is ifNumber nonrepeater.
65 ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.1");     # Should be system.ifNumber OID.
66 ok($list[1][0]->iid eq "0");                    # system.ifNumber.0 IID.
67 ok($list[1][0]->val =~ m/^\d+$/);               # Number is all numeric 
68 #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
69 #ok($list[1][0]->type eq "INTEGER32");          # Number should be integer.
70
71 $ifaces = $list[1][0]->val;
72
73 # Make sure we got an ifSpeed for each interface.  list[2] is ifSpeed repeater.
74 ok(scalar @{$list[2]} == $ifaces);
75
76 # Make sure we got an ifDescr for each interface.  list[3] is ifDescr repeater.
77 ok(scalar @{$list[3]} == $ifaces);
78
79 # Test for reasonable values from the agent.
80 ok($list[2][0]->tag eq ".1.3.6.1.2.1.2.2.1.5"); # Should be system.ifSpeed OID.
81 ok($list[2][0]->iid eq "1");                    # Instance should be 1.
82 ok($list[2][0]->val =~ m/^\d+$/);               # Number is all numeric 
83 ok($list[2][0]->type eq "GAUGE");               # Number should be a gauge.
84
85 ok($list[3][0]->tag eq ".1.3.6.1.2.1.2.2.1.2"); # Should be system.ifDescr OID.
86 ok($list[3][0]->iid eq "1");                    # Instance should be 1.
87
88 # The first interface is probably loopback.  Check this.
89 ok($list[3][0]->type eq "OCTETSTR");            # Description is a string.
90
91 # This might fail for some weird (Windows?) systems.  Can be safely ignored.
92 $loopback = $list[3][0]->val;
93 ok(($loopback =~ /^lo/));
94
95 ###############################################################################
96 # Attempt to use the bulkwalk method to get only non-repeaters
97 # test 2
98 $vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'] ); # NON-repeaters
99
100 $expect = scalar @$vars;
101 @list = $s1->bulkwalk(2, 16, $vars);
102 ok($s1->{ErrorNum} == 0);
103
104 # Did we get back the list of references to returned values?
105 #
106 ok(scalar @list == $expect);
107
108 # Sanity check the returned values.  list[0] is sysUptime nonrepeater.
109 ok($list[0][0]->tag eq ".1.3.6.1.2.1.1.3");     # check system.sysUptime OID
110 ok($list[0][0]->iid eq "0");                    # check system.sysUptime.0 IID
111 ok($list[0][0]->val =~ m/^\d+$/);               # Uptime is numeric 
112 ok($list[0][0]->type eq "TICKS");               # Uptime should be in ticks.
113
114 # Find out how many interfaces to expect.  list[1] is ifNumber nonrepeater.
115 ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.1");     # Should be system.ifNumber OID.
116 ok($list[1][0]->iid eq "0");                    # system.ifNumber.0 IID.
117 ok($list[1][0]->val =~ m/^\d+$/);               # Number is all numeric 
118 #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
119 #ok($list[1][0]->type eq "INTEGER32");          # Number should be integer.
120 $ifaces = $list[1][0]->val;
121
122
123 ###############################################################################
124 # Attempt to use the bulkwalk method to get only repeated variables
125 # test 3
126 $vars = new SNMP::VarList ( ['ifIndex'], ['ifSpeed'] ); # repeaters
127
128 $expect = scalar @$vars;
129 @list = $s1->bulkwalk(0, 16, $vars);
130 ok($s1->{ErrorNum} == 0);
131
132 # Did we get back the list of references to returned values?
133 #
134 ok(scalar @list == $expect);
135
136 # Make sure we got an ifIndex for each interface.  list[0] is ifIndex repeater.
137 ok(scalar @{$list[0]} == $ifaces);
138
139 # Make sure we got an ifSpeed for each interface.  list[0] is ifSpeed repeater.
140 ok(scalar @{$list[1]} == $ifaces);
141
142 # Test for reasonable values from the agent.
143 ok($list[0][0]->tag eq ".1.3.6.1.2.1.2.2.1.1"); # Should be system.ifIndex OID.
144 ok($list[0][0]->iid eq "1");                    # Instance should be 1.
145 ok($list[0][0]->val =~ m/^\d+$/);               # Number is all numeric 
146 #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
147 #ok($list[0][0]->type eq "INTEGER32");          # Number should be an integer.
148
149 ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.2.1.5"); # Should be system.ifSpeed OID.
150 ok($list[1][0]->iid eq "1");                    # Instance should be 1.
151 ok($list[1][0]->val =~ m/^\d+$/);               # Number is all numeric 
152 ok($list[1][0]->type eq "GAUGE");               # Number should be a gauge.
153
154 ######################################################################
155 #  Asynchronous Bulkwalk Methods
156 ######################################################################
157
158 # Attempt to use the bulkwalk method to get a few variables from the
159 # SNMP agent.
160 # test 4
161 sub async_cb1 {
162     my ($vars, $list) = @_;
163     ok(defined $list && ref($list) =~ m/ARRAY/);
164     ok(defined $vars && ref($vars) =~ m/SNMP::VarList/);
165
166     ok(scalar @$list == scalar @$vars);
167
168     my $vbr;
169
170     # Sanity check the returned values.  First is sysUptime nonrepeater.
171     $vbr = $list->[0][0];
172     ok($vbr->tag eq ".1.3.6.1.2.1.1.3");        # check system.sysUptime OID
173     ok($vbr->iid eq "0");                       # check system.sysUptime.0 IID
174     ok($vbr->val =~ m/^\d+$/);                  # Uptime is numeric 
175     ok($vbr->type eq "TICKS");                  # Uptime should be in ticks.
176
177     # Find out how many interfaces to expect.  Next is ifNumber nonrepeater.
178     $vbr = $list->[1][0];
179     ok($vbr->tag eq ".1.3.6.1.2.1.2.1");        # Should be system.ifNumber OID.
180     ok($vbr->iid eq "0");                       # system.ifNumber.0 IID.
181     ok($vbr->val =~ m/^\d+$/);                  # Number is all numeric 
182 #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
183 #    ok($vbr->type eq "INTEGER32");             # Number should be integer.
184     $ifaces = $vbr->[2];
185
186     # Test for reasonable values from the agent.
187     ok(scalar @{$list->[2]} == $ifaces);
188     $vbr = $list->[2][0];
189     ok($vbr->tag eq ".1.3.6.1.2.1.2.2.1.5");    # Should be ifSpeed OID
190     ok($vbr->iid eq "1");                       # Instance should be 1.
191     ok($vbr->val =~ m/^\d+$/);                  # Number is all numeric 
192     ok($vbr->type eq "GAUGE");                  # Should be a gauge.
193
194     ok(scalar @{$list->[3]} == $ifaces);
195     $vbr = $list->[3][0];
196     ok($vbr->tag eq ".1.3.6.1.2.1.2.2.1.2");    # Should be ifDescr OID
197     ok($vbr->iid eq "1");                       # Instance should be 1.
198
199     # The first interface is probably loopback.  Check this.
200     ok($vbr->type eq "OCTETSTR");
201
202     # This might fail for some weird (Windows?) systems.  Can be safely ignored.
203     ok(($vbr->val =~ /^lo/));
204
205     SNMP::finish();
206 }
207
208 $vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'], # NON-repeaters
209                             ['ifSpeed'], ['ifDescr']);   # Repeated variables.
210
211 @list = $s1->bulkwalk(2, 16, $vars, [ \&async_cb1, $vars ] );
212 ok($s1->{ErrorNum} == 0);
213 SNMP::MainLoop();
214 ok(1);