summaryrefslogtreecommitdiff
path: root/perl/SNMP/t/bulkwalk.t
blob: 80acb101173ca38c6407fd6ab4c2997bef02390f (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!./perl
#
# $Id: bulkwalk.t 17708 2009-07-27 12:06:25Z hardaker $
#
# Test bulkwalk functionality.

BEGIN {
    unless(grep /blib/, @INC) {
        chdir 't' if -d 't';
        @INC = '../lib' if -d '../lib';
    }
    eval "use Cwd qw(abs_path)";
    $ENV{'SNMPCONFPATH'} = 'nopath';
    $ENV{'MIBDIRS'} = '+' . abs_path("../../mibs");
}
use Test;
BEGIN { $num = 62; plan test => $num; }

use SNMP;

require "t/startagent.pl";

use vars qw($agent_port $comm2 $agent_host);

$SNMP::debugging = 0;
$SNMP::verbose = 0;

#print "1..$num\n";

######################################################################
# Fire up a session.
$s1 = new SNMP::Session(
    'DestHost'   => $agent_host,
    'Community'  => $comm2,
    'RemotePort' => $agent_port,
    'Version'    => '2c',
    'UseNumeric' => 1,
    'UseEnum'    => 0,
    'UseLongNames' => 1
);
ok(defined($s1));

######################################################################
# 
# Attempt to use the bulkwalk method to get a few variables from the
# SNMP agent.
# test 1
$vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'], # NON-repeaters
			    ['ifSpeed'], ['ifDescr']);	 # Repeated variables.

$expect = scalar @$vars;
@list = $s1->bulkwalk(2, 16, $vars);

ok($s1->{ErrorNum} == 0);

# Did we get back the list of references to returned values?
#
ok(scalar @list == $expect);
if (defined($list[0][0])) {
  # Sanity check the returned values.  list[0] is sysUptime nonrepeater.
  ok($list[0][0]->tag eq ".1.3.6.1.2.1.1.3");	# check system.sysUptime OID
  ok($list[0][0]->iid eq "0");			# check system.sysUptime.0 IID
  ok($list[0][0]->val =~ m/^\d+$/);		# Uptime is numeric 
  ok($list[0][0]->type eq "TICKS");		# Uptime should be in ticks.
}
else {
  ok(0);
  ok(0);
  ok(0);
  ok(0);
}
if (defined($list[1][0])) {
  # Find out how many interfaces to expect.  list[1] is ifNumber nonrepeater.
  ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.1");	# Should be system.ifNumber OID.
  ok($list[1][0]->iid eq "0");			# system.ifNumber.0 IID.
  ok($list[1][0]->val =~ m/^\d+$/);		# Number is all numeric 
  #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
  #ok($list[1][0]->type eq "INTEGER32");		# Number should be integer.

  $ifaces = $list[1][0]->val;
}
else {
  ok(0);
  ok(0);
  ok(0);
}
    
# Make sure we got an ifSpeed for each interface.  list[2] is ifSpeed repeater.
ok(scalar @{$list[2]} == $ifaces);
# Make sure we got an ifDescr for each interface.  list[3] is ifDescr repeater.
ok(scalar @{$list[3]} == $ifaces);

if (defined($list[2][0])) {
  # Test for reasonable values from the agent.
  ok($list[2][0]->tag eq ".1.3.6.1.2.1.2.2.1.5");	# Should be system.ifSpeed OID.
  ok($list[2][0]->iid eq "1");			# Instance should be 1.
  ok($list[2][0]->val =~ m/^\d+$/);		# Number is all numeric 
  ok($list[2][0]->type eq "GAUGE");		# Number should be a gauge.
}
else {
  ok(0);
  ok(0);
  ok(0);
  ok(0);
}

if (defined($list[3][0])) {
  ok($list[3][0]->tag eq ".1.3.6.1.2.1.2.2.1.2");	# Should be system.ifDescr OID.
  ok($list[3][0]->iid eq "1");			# Instance should be 1.

  # The first interface is probably loopback.  Check this.
  ok($list[3][0]->type eq "OCTETSTR");		# Description is a string.

  # This might fail on systems that don't have lo0/loopback as their first
  # interface. Please adjust accordingly.
  $loopback = $list[3][0]->val;
  if ($^O =~ /win32/i) {
    ok(($loopback =~ /loopback/i));
  } elsif ($^O =~ /(irix|hpux)/i) {
    # IRIX/HP-UX may have lo0 at the *end* of the interface list,
    # so just check for a non-empty string
    ok(($loopback ne ''));
  } elsif ($^O eq 'freebsd') {
    $loopback = $list[3][-1]->val;
    ok(($loopback =~ /^lo/));
  } else {
    ok(($loopback =~ /^lo/));
  }
}
else {
  ok(0);
  ok(0);
  ok(0);
  ok(0);
}
  
###############################################################################
# Attempt to use the bulkwalk method to get only non-repeaters
# test 2
$vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'] ); # NON-repeaters

$expect = scalar @$vars;
@list = $s1->bulkwalk(2, 0, $vars);
#@list = $s1->bulkwalk(2, 16, $vars);
ok($s1->{ErrorNum} == 0);

# Did we get back the list of references to returned values?
#
ok(scalar @list == $expect);

if (defined($list[0][0])) {
  # Sanity check the returned values.  list[0] is sysUptime nonrepeater.
  ok($list[0][0]->tag eq ".1.3.6.1.2.1.1.3");	# check system.sysUptime OID
  ok($list[0][0]->iid eq "0");			# check system.sysUptime.0 IID
  ok($list[0][0]->val =~ m/^\d+$/);		# Uptime is numeric 
  ok($list[0][0]->type eq "TICKS");		# Uptime should be in ticks.
}
else {
  ok(0);
  ok(0);
  ok(0);
  ok(0);
}

if (defined($list[1][0])) {
  # Find out how many interfaces to expect.  list[1] is ifNumber nonrepeater.
  ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.1");	# Should be system.ifNumber OID.
  ok($list[1][0]->iid eq "0");			# system.ifNumber.0 IID.
  ok($list[1][0]->val =~ m/^\d+$/);		# Number is all numeric 
  #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
  #ok($list[1][0]->type eq "INTEGER32");		# Number should be integer.
  $ifaces = $list[1][0]->val;
}
else {
  ok(0);
  ok(0);
  ok(0);
}

###############################################################################
# Attempt to use the bulkwalk method to get only repeated variables
# test 3
$vars = new SNMP::VarList ( ['ifIndex'], ['ifSpeed'] ); # repeaters

$expect = scalar @$vars;
@list = $s1->bulkwalk(0, 16, $vars);
ok($s1->{ErrorNum} == 0);

# Did we get back the list of references to returned values?
#
ok(scalar @list == $expect);

# Make sure we got an ifIndex for each interface.  list[0] is ifIndex repeater.
ok(scalar @{$list[0]} == $ifaces);

# Make sure we got an ifSpeed for each interface.  list[0] is ifSpeed repeater.
ok(scalar @{$list[1]} == $ifaces);

if (defined($list[0][0])) {
  # Test for reasonable values from the agent.
  ok($list[0][0]->tag eq ".1.3.6.1.2.1.2.2.1.1");	# Should be system.ifIndex OID.
  ok($list[0][0]->iid eq "1");			# Instance should be 1.
  ok($list[0][0]->val =~ m/^\d+$/);		# Number is all numeric 
  #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
  #ok($list[0][0]->type eq "INTEGER32");		# Number should be an integer.
}
else {
  ok(0);
  ok(0);
  ok(0);
}

if (defined($list[1][0])) {
  ok($list[1][0]->tag eq ".1.3.6.1.2.1.2.2.1.5");	# Should be system.ifSpeed OID.
  ok($list[1][0]->iid eq "1");			# Instance should be 1.
  ok($list[1][0]->val =~ m/^\d+$/);		# Number is all numeric 
  ok($list[1][0]->type eq "GAUGE");		# Number should be a gauge.
}
else {
  ok(0);
  ok(0);
  ok(0);
  ok(0);
}

######################################################################
#  Asynchronous Bulkwalk Methods
######################################################################
# 
# Attempt to use the bulkwalk method to get a few variables from the
# SNMP agent.
# test 4
sub async_cb1 {
    my ($vars, $list) = @_;
    ok(defined $list && ref($list) =~ m/ARRAY/);
    ok(defined $vars && ref($vars) =~ m/SNMP::VarList/);

    ok(scalar @$list == scalar @$vars);

    my $vbr;

    if (defined($list->[0][0])) {
      # Sanity check the returned values.  First is sysUptime nonrepeater.
      $vbr = $list->[0][0];
      ok($vbr->tag eq ".1.3.6.1.2.1.1.3");	# check system.sysUptime OID
      ok($vbr->iid eq "0");			# check system.sysUptime.0 IID
      ok($vbr->val =~ m/^\d+$/);			# Uptime is numeric 
      ok($vbr->type eq "TICKS");			# Uptime should be in ticks.
    }
    else {
      ok(0);
      ok(0);
      ok(0);
      ok(0);
    }

    if (defined($list->[1][0])) {
      # Find out how many interfaces to expect.  Next is ifNumber nonrepeater.
      $vbr = $list->[1][0];
      ok($vbr->tag eq ".1.3.6.1.2.1.2.1");	# Should be system.ifNumber OID.
      ok($vbr->iid eq "0");			# system.ifNumber.0 IID.
      ok($vbr->val =~ m/^\d+$/);			# Number is all numeric 
      #XXX: test fails due SMIv1 codes being returned intstead of SMIv2...
      #    ok($vbr->type eq "INTEGER32");		# Number should be integer.
      $ifaces = $vbr->[2];
    }
    else {
      ok(0);
      ok(0);
      ok(0);
    }

    # Test for reasonable values from the agent.
    ok(scalar @{$list->[2]} == $ifaces);
    
    if (defined($list->[2][0])) {
      $vbr = $list->[2][0];
      ok($vbr->tag eq ".1.3.6.1.2.1.2.2.1.5");	# Should be ifSpeed OID
      ok($vbr->iid eq "1");			# Instance should be 1.
      ok($vbr->val =~ m/^\d+$/);			# Number is all numeric 
      ok($vbr->type eq "GAUGE");			# Should be a gauge.

      ok(scalar @{$list->[3]} == $ifaces);
    }
    else {
      ok(0);
      ok(0);
      ok(0);
      ok(0);
      ok(0);
    }
  
    if (defined($list->[3][0])) {
      $vbr = $list->[3][0];
      ok($vbr->tag eq ".1.3.6.1.2.1.2.2.1.2");	# Should be ifDescr OID
      ok($vbr->iid eq "1");			# Instance should be 1.

      # The first interface is probably loopback.  Check this.
      ok($vbr->type eq "OCTETSTR");

      # This might fail on systems that don't have lo0/loopback as their first
      # interface. Please adjust accordingly.
      if ($^O =~ /(irix|hpux)/i) {
        # IRIX/HP-UX may have lo0 at the *end* of the interface list,
        # so just check for a non-empty string
        ok(($vbr->val ne ''));
      } elsif ($^O eq 'freebsd') {
        $vbr = $list->[3][-1];
        ok(($vbr->val =~ /^lo/));
      } else {
        ok(($vbr->val =~ /^lo/));
      }
    }
    else {
      ok(0);
      ok(0);
      ok(0);
      ok(0);
    }

    SNMP::finish();
}

$vars = new SNMP::VarList ( ['sysUpTime'], ['ifNumber'], # NON-repeaters
			    ['ifSpeed'], ['ifDescr']);	 # Repeated variables.

if ($^O =~ /win32/i) {
  warn "Win32 detected - skipping and failing async calls\n";
  for (my $i=1;$i <= 21; $i++) {
    ok(0);
  }
}
else {
  @list = $s1->bulkwalk(2, 16, $vars, [ \&async_cb1, $vars ] );
  ok($s1->{ErrorNum} == 0);
  SNMP::MainLoop();
}
ok(1);

snmptest_cleanup();