CC: | karlward [...] cpan.org |
Subject: | [patch] Net::MAC fix for as_* methods, case formatting, zero padding |
Patch fixes the following problems:
* The as_* methods were not being invoked by AUTOLOAD because the arguments
in _format() function were wrong.
* updated formatting in as_Sun to match POD documentation (delim ':', fixed padding.
* added test cases for as_* routines
* modified t/Net-MAC.t to use is(a,b) instead of ok(a==b).
* zero padding logic was reversed
* case folding was not working properly
Subject: | net-mac.patch |
Index: t/Net-MAC.t
===================================================================
--- t/Net-MAC.t (.../vendor/current) (revision 10)
+++ t/Net-MAC.t (.../branches/fix-autoload) (revision 10)
@@ -5,20 +5,26 @@
#########################
-use Test::More tests => 142;
+use Test::More tests => 146;
BEGIN { use_ok('Net::MAC') };
# Creating base 16 Net::MAC objects
my @macs = ();
my $hex_mac = Net::MAC->new('mac' => '08:20:00:AB:CD:EF');
ok($hex_mac);
-ok($hex_mac->get_mac() eq '08:20:00:AB:CD:EF');
-ok($hex_mac->get_bit_group() == 8);
-ok($hex_mac->get_base() == 16);
-ok($hex_mac->get_delimiter() eq ':');
+is($hex_mac->get_mac(), '08:20:00:AB:CD:EF');
+is($hex_mac->get_bit_group(), 8);
+is($hex_mac->get_base(), 16);
+is($hex_mac->get_delimiter(), ':');
#ok($hex_mac->get_internal_mac() eq '082000ABCDEF');
-ok($hex_mac->get_internal_mac() eq '082000abcdef');
+is($hex_mac->get_internal_mac(), '082000abcdef');
+## check AUTOLOAD as_* methods
+is($hex_mac->as_Cisco, '0820.00ab.cdef', "as_Cisco");
+is($hex_mac->as_IEEE, '08:20:00:AB:CD:EF', "as_IEEE");
+is($hex_mac->as_Microsoft, '08-20-00-AB-CD-EF', "as_Microsoft");
+is($hex_mac->as_Sun, '8:20:0:ab:cd:ef', "as_Sun");
+
# Converting a base 16 MAC to a base 10 MAC
my $dec_mac = $hex_mac->convert(
'base' => 10,
@@ -26,9 +32,9 @@
'delimiter' => '.'
);
ok($dec_mac);
-ok($dec_mac->get_mac() eq '8.32.0.171.205.239');
-ok($dec_mac->get_bit_group() == 8);
-ok($dec_mac->get_base() == 10);
+is($dec_mac->get_mac(), '8.32.0.171.205.239');
+is($dec_mac->get_bit_group(), 8);
+is($dec_mac->get_base(), 10);
# Converting a base 10 MAC to a base 16 MAC
my $hex_mac_2 = $dec_mac->convert(
@@ -37,10 +43,10 @@
'delimiter' => ':'
);
ok($hex_mac_2);
-ok($hex_mac_2->get_mac() eq '0820:00ab:cdef');
-ok($hex_mac_2->get_bit_group() == 16);
-ok($hex_mac_2->get_base() == 16);
-ok($hex_mac_2->get_internal_mac() eq '082000abcdef');
+is($hex_mac_2->get_mac(), '0820:00ab:cdef');
+is($hex_mac_2->get_bit_group(), 16);
+is($hex_mac_2->get_base(), 16);
+is($hex_mac_2->get_internal_mac(), '082000abcdef');
# Creating a base 10 Net::MAC object
my $dec_mac_2 = Net::MAC->new(
@@ -48,10 +54,10 @@
'base' => 10
);
ok($dec_mac_2);
-ok($dec_mac_2->get_mac() eq '0.7.14.6.43.3');
-ok($dec_mac_2->get_bit_group() == 8);
-ok($dec_mac_2->get_base() == 10);
-ok($dec_mac_2->get_internal_mac() eq '00070e062b03');
+is($dec_mac_2->get_mac(), '0.7.14.6.43.3');
+is($dec_mac_2->get_bit_group(), 8);
+is($dec_mac_2->get_base(), 10);
+is($dec_mac_2->get_internal_mac(), '00070e062b03');
my $hex_mac_3 = $dec_mac_2->convert(
'base' => 16,
@@ -59,18 +65,18 @@
'delimiter' => '.'
);
ok($hex_mac_3);
-ok($hex_mac_3->get_mac() eq '0007.0e06.2b03');
-ok($hex_mac_3->get_bit_group() == 16);
-ok($hex_mac_3->get_base() == 16);
-ok($hex_mac_3->get_internal_mac() eq '00070e062b03');
+is($hex_mac_3->get_mac(), '0007.0e06.2b03');
+is($hex_mac_3->get_bit_group(), 16);
+is($hex_mac_3->get_base(), 16);
+is($hex_mac_3->get_internal_mac(), '00070e062b03');
# Creating a base 16 dash delimited Net::MAC object
my $hex_mac_4 = Net::MAC->new('mac' => '12-23-34-45-a4-ff');
ok($hex_mac_4);
-ok($hex_mac_4->get_mac() eq '12-23-34-45-a4-ff');
-ok($hex_mac_4->get_bit_group() == 8);
-ok($hex_mac_4->get_base() == 16);
-ok($hex_mac_4->get_internal_mac() eq '12233445a4ff');
+is($hex_mac_4->get_mac(), '12-23-34-45-a4-ff');
+is($hex_mac_4->get_bit_group(), 8);
+is($hex_mac_4->get_base(), 16);
+is($hex_mac_4->get_internal_mac(),'12233445a4ff');
my (%delim_mac) = (
@@ -81,18 +87,16 @@
'none' => ['080020abcdef', '080020ABCDEF'],
);
foreach my $delim (keys %delim_mac) {
- diag "testing delimiter \"$delim\"";
foreach my $test_mac (@{$delim_mac{$delim}}) {
my $mac = Net::MAC->new('mac' => $test_mac);
- ok($mac, $test_mac);
+ is($mac, $test_mac, "test with delimiter '$delim'");
my $test_delim = $mac->get_delimiter();
if ($delim eq 'none') {
#diag "null delimiter";
ok(!defined($test_delim), 'delimiter \"none\"');
}
else {
- #diag "delimiter \"$delim\" MAC \"$test_mac\"";
- ok($test_delim eq $delim, "delimiter \"$delim\"");
+ is($test_delim, $delim, "delimiter '$delim'");
}
}
}
@@ -101,16 +105,14 @@
'16' => ['08.00.20.ab.cd.ef', '8:0:20:ab:cd:ef', '8:0:20:AB:CD:EF']
);
foreach my $base (keys %base_mac) {
- diag "testing base \"$base\"";
foreach my $test_mac_2 (@{$base_mac{$base}}) {
my $mac = Net::MAC->new(
'mac' => $test_mac_2,
'base' => $base
);
- ok($mac, $test_mac_2);
+ is($mac, $test_mac_2, "mac correct for base '$base'");
my $mac_base = $mac->get_base();
- #diag "base is $mac_base, MAC is $test_mac_2";
- ok($mac_base == $base, "base $base");
+ is($mac_base, $base, "base $base");
}
}
@@ -120,12 +122,11 @@
8 => ['80.80.ab.e4.c9.ff', '80:80:ab:e4:c9:ff', '80-80-ab-e4-c9-ff', '80 80 AB E4 C9 FF']
);
foreach my $bit (keys %bit_mac) {
- diag "testing bit grouping \"$bit\"";
foreach my $test_mac_3 (@{$bit_mac{$bit}}) {
my $mac = Net::MAC->new('mac' => $test_mac_3);
- ok($mac, $test_mac_3);
+ is($mac, $test_mac_3, "mac correct for grouping '$bit'");
my $mac_bit = $mac->get_bit_group();
- ok($mac_bit == $bit, "bit grouping $bit");
+ is($mac_bit, $bit, "bit grouping correct $bit");
}
}
@@ -136,15 +137,12 @@
ok(Net::MAC->new('mac' => $test_mac));
}
-diag("testing some invalid MAC addresses");
no warnings;
my @invalid_mac = (':::::', ' : : : : : ', '..', '\s\s\s\s\s', '-----', '---', ' - - ', ' ', '99.6', '888:76.12', '1', '000000000000000000111111', '256.256.256.256.256.256', '128.123.123.234.345.456', 'abcdefghijkl');
foreach my $invalid_mac (@invalid_mac) {
my $no_die = Net::MAC->new(mac => $invalid_mac, die => 0);
- ok($no_die, "testing 'die' attribute");
- diag("testing invalid MAC $invalid_mac\n");
- ok($no_die->get_error(), "testing get_error() method");
- #diag($no_die->get_error());
+ ok($no_die, "testing 'die' attribute for invalid mac '$invalid_mac'");
+ ok($no_die->get_error(), "testing get_error() method for invalid mac '$invalid_mac'");
}
use warnings;
Index: lib/Net/MAC.pm
===================================================================
--- lib/Net/MAC.pm (.../vendor/current) (revision 10)
+++ lib/Net/MAC.pm (.../branches/fix-autoload) (revision 10)
@@ -126,10 +126,10 @@
Cisco => {base => 16, bit_group => 16, delimiter => '.' },
IEEE => { base => 16, bit_group => 8, delimiter => ':', zero_padded => 1, case => 'upper' },
Microsoft => { base => 16, bit_group => 8, delimiter => '-', case => 'upper' },
- Sun => { base => 16, bit_group => 8, delimiter => '.', zero_padding => 0, case => 'lower' }
+ Sun => { base => 16, bit_group => 8, delimiter => ':', zero_padded => 0, case => 'lower' }
);
sub _format {
- my ($identifier) = @_;
+ my ($self, $identifier) = @_;
my $format = $_format_for{$identifier};
if ((defined $format) && (%$format)) {
return(%$format);
@@ -389,7 +389,7 @@
my $group = substr($imac, $offset, $size);
if ( ($bit_group == 8)
&& (exists $arg{zero_padded})
- && ($arg{zero_padded} != 0)
+ && ($arg{zero_padded} == 0)
) {
$group =~ s/^0//;
}
@@ -417,6 +417,15 @@
else {
$mac_string = join('', @groups);
}
+
+ if (exists $arg{case} && $arg{case} =~ /^(upper|lower)$/)
+ {
+ for ($mac_string)
+ {
+ $_ = $arg{case} eq 'upper' ? uc : lc ;
+ }
+ }
+
# Construct the argument list for the new Net::MAC object
$arg{'mac'} = $mac_string;
# foreach my $test (keys %arg) {