#!/usr/pkg/bin/perl
#
# SNMPTT Net-SNMP-Test v1.0
#
# Copyright 2002-2007 Alex Burger
# alex_b@users.sourceforge.net
#
# 4/13/2003
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
##############################################################################
#
# http://www.sourceforge.net/projects/snmptt
#
###############################################################################
#
# OPTIONS START
#
# Allows you to set the MIBS environment variable used by SNMPTT
# Leave blank or comment out to have the systems enviroment settings used
# To have all MIBS processed, set to ALL
$mibs_environment = "RFC1213-MIB";

# This sets the best_guess parameter used by the UCD-SNMP / NET-SNMP Perl module for 
# translating symbolic nams to OIDs and vice versa.
# For UCD-SNMP, and Net-SNMP 5.0.8 and previous versions, set this value to 0.
# For Net-SNMP 5.1, or any Net-SNMP with patch 722075 applied, set this value to 2.
# A value of 2 is equivalent to -IR on Net-SNMP command line utilities.
# UCD-SNMP and Net-SNMP 5.0.8 and previous may not be able to translate certain formats of
# symbolic names such as RFC1213-MIB::sysDescr.  Net-SNMP 5.1 or patch 722075 will allow
# all possibilities to be translated.
$net_snmp_perl_best_guess = 1;

#
# OPTIONS END
#
#############################################################################
#

$snmpttnet_snmp_test_version = "v1.0";

sub showversion
{
  print "\nSNMPTT Net-SNMP Test $snmpttnet_snmp_test_version\n";
  print "(c) 2003 Alex Burger\n";
  print "http://snmptt.sourceforge.net\n\n";
}

##############################################################################
# Process command line arguments

$| = 1;

use Getopt::Long;

$DEBUGGING = 0;

$version = 0;
$debug = 0;
$help = 0;

GetOptions 	('version' => \$version,
		'help' => \$help,
		'best_guess:i' => \$best_guess);
		
if ($version == 1)
{
  &showversion;
  exit(0);
}

if ($help == 1)
{
  $USAGE = qq/This program is used to perform various translations of numeric and symbolic
OIDS using the UCD-SNMP \/ NET-SNMP Perl module and the translateObj function.

The test results should assist with determining if the installed Perl module 
will function as expected with SNMPTT.

For UCD-SNMP, and Net-SNMP 5.0.8 and previous versions, use --best_guess=0.

For Net-SNMP 5.1, or any Net-SNMP with patch 722075 applied, use 
--best_guess=2.

Usage:

  net-snmp-test          [<options>]
Options:

  --help                 Display this message
  --version              Display author and version information
  --best_guess=n         Display author and version information
/;
  
  &showversion;
  print $USAGE . "\n";
  
  exit(0);
}

use SNMP;
&showversion;
if (defined ($mibs_environment))
{
  $ENV{'MIBS'} = $mibs_environment;
}

$SNMP::save_descriptions = 1;
&SNMP::initMib();

if (defined ($best_guess))
{
  $SNMP::best_guess = $best_guess;
}
else
{
  $SNMP::best_guess = $net_snmp_perl_best_guess;
}

$include_module   = 1;
my $test;
my $long_names;
my $include_module;

print "MIBS:$mibs_environment\n";
print "best_guess: $SNMP::best_guess\n";

print "\n\nTesting translateObj\n";
print "********************\n";

$test = '.1.3.6.1.2.1.1.1';
$expect = 'sysDescr';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

$test = '.1.3.6.1.2.1.1.1';
$expect = 'RFC1213-MIB::sysDescr';
$long_names = 0;
$include_module = 1;
&translate($test, $expect, $long_names, $include_module);

$test = '.1.3.6.1.2.1.1.1';
$expect = '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr';
$long_names = 1;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);
if ($SNMP::VERSION =~ /^4./)
{
  print "Note: UCD-SNMP v$SNMP::VERSION detected.  UCD-SNMP does not normally\n      return the entire tree when requesting a long name\n";
}

$test = '.1.3.6.1.2.1.1.1';
$expect = 'RFC1213-MIB::.iso.org.dod.internet.mgmt.mib-2.system.sysDescr';
$long_names = 1;
$include_module = 1;
&translate($test, $expect, $long_names, $include_module);

$test = 'sysDescr';
$expect = '.1.3.6.1.2.1.1.1';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

$test = 'RFC1213-MIB::sysDescr';
$expect = '.1.3.6.1.2.1.1.1';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

$test = 'system.sysDescr';
$expect = '.1.3.6.1.2.1.1.1';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

$test = 'RFC1213-MIB::system.sysDescr';
$expect = '.1.3.6.1.2.1.1.1';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

$test = '.iso.org.dod.internet.mgmt.mib-2.system.sysDescr';
$expect = '.1.3.6.1.2.1.1.1';
$long_names = 0;
$include_module = 0;
&translate($test, $expect, $long_names, $include_module);

print "\n\nTesting getType\n";
print "***************\n";

$test = '.1.3.6.1.2.1.4.1';		# ipForwarding
$expect = 'INTEGER';
&my_getType($test, $expect,);

$test = 'ipForwarding';		# ipForwarding
$expect = 'INTEGER';
&my_getType($test, $expect,);

print "\n\nTesting Description\n";
print "*******************\n";

$test = '.1.3.6.1.2.1.4.1';		# ipForwarding
$expect = 'The indication of whether this entity is acting';
&my_getDescription($test, $expect,);

print "\n";

sub translate
{
  my $test = shift;
  my $expect = shift;
  my $long_names = shift;
  my $include_module = shift;
  print "\nTesting: $test, long_names=" . ($long_names == 1 ? "enabled" : "disabled") . ", include_module=" . ($include_module == 1 ? "enabled" : "disabled") . "\n";
  $translated = &SNMP::translateObj("$test",$long_names,$include_module);
  if ($translated eq $expect)
  {
    print "Test passed.  Result: $translated\n";
  }
  else
  {
    print "Test FAILED!  Expected: $expect\n";
    print "              Received: $translated\n";
  }
}
  
sub my_getType
{
  my $test = shift;
  my $expect = shift;
  print "\nTesting: $test\n";
  $type = &SNMP::getType("$test");
  if ($type eq $expect)
  {
    print "Test passed.  Result: $type\n";
  }
  else
  {
    print "Test FAILED!  Expected: $expect\n";
    print "              Received: $type\n";
  }
}


sub my_getDescription
{
  my $test = shift;
  my $expect = shift;
  my $description_temp = $SNMP::MIB{$test}->{description};
  $description_temp =~ s/\n\s+/\n/g;

  if ($description_temp =~ $expect)
  {
    print "Test passed.  Result:\n";
    print "-------------------------------------------------\n";
    print "$description_temp\n";
    print "-------------------------------------------------\n";
  }
  else
  {
    print "Test FAILED!  Expected a string containing:\n";
    print "$expect\n\n";
    print "Received:\n";
    print "-------------------------------------------------\n";
    print "$description_temp\n";
    print "-------------------------------------------------\n";
  }
}

