Skip Menu |

This queue is for tickets about the Net-MAC CPAN distribution.

Report information
The Basics
Id: 34476
Status: resolved
Priority: 0/
Queue: Net-MAC

People
Owner: OLIVER [...] cpan.org
Requestors: kbrint [...] rufus.net
Cc: karlward [...] cpan.org
AdminCc:

Bug Information
Severity: Important
Broken in: 1.2
Fixed in: 1.3



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) {
Hi.. This patch fixes another issue.. The function that was installed after the first AUTOLOAD would not have the same value for $1, since it's a special variable. This causes the first as_* function to return the right value, but subsequent calls fail. This patch is based on the previous patch, but it fixes the $1 issue. Also, the as_* test cases are now run twice in a loop to verify that they return correct values on subsequent operations. Note that I bumped the version to 1.3 in this patch. Feel free to handle that however you like. Thanks. -kb
Index: t/Net-MAC.t =================================================================== --- t/Net-MAC.t (.../vendor/current) (revision 13) +++ t/Net-MAC.t (.../branches/fix-autoload) (revision 13) @@ -5,20 +5,30 @@ ######################### -use Test::More tests => 142; +use Test::More tests => 150; 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 +## also, check that the sub gets installed properly by running it twice +for my $round (1,2) +{ + is($hex_mac->as_Cisco, '0820.00ab.cdef', "as_Cisco (round $round)"); + is($hex_mac->as_IEEE, '08:20:00:AB:CD:EF', "as_IEEE (round $round)"); + is($hex_mac->as_Microsoft, '08-20-00-AB-CD-EF', "as_Microsoft (round $round)"); + is($hex_mac->as_Sun, '8:20:0:ab:cd:ef', "as_Sun (round $round)"); +} + # Converting a base 16 MAC to a base 10 MAC my $dec_mac = $hex_mac->convert( 'base' => 10, @@ -26,9 +36,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 +47,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 +58,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 +69,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 +91,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 +109,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 +126,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 +141,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 13) +++ lib/Net/MAC.pm (.../branches/fix-autoload) (revision 13) @@ -31,7 +31,7 @@ # RCS ident string #my $rcs_id = '$Id: MAC.pm,v 1.20 2007/01/27 05:40:47 karlward Exp $'; -our $VERSION = '1.2'; +our $VERSION = '1.3'; our $AUTOLOAD; # Constructor. @@ -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); @@ -156,9 +156,10 @@ $self->{$1} = $value; return; } - if ($AUTOLOAD =~ /.*::as_(\w+)/ && $_[0]->_format($1)) { - *{$AUTOLOAD} = sub { return $_[0]->convert($_[0]->_format($1)) }; - return($self->convert($_[0]->_format($1))); + if ( $AUTOLOAD =~ /.*::as_(\w+)/ && $_[0]->_format($1)) { + my $fmt = $1; + *{$AUTOLOAD} = sub { return $_[0]->convert($_[0]->_format($fmt)) }; + return($self->convert($_[0]->_format($fmt))); } croak "No such method: $AUTOLOAD"; } @@ -389,7 +390,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 +418,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) {
Subject: Re: [rt.cpan.org #34476] [patch] Net::MAC fix for as_* methods, case formatting, zero padding
Date: Mon, 31 Mar 2008 13:26:07 +0100
To: bug-Net-MAC [...] rt.cpan.org
From: Oliver Gorwits <oliver.gorwits [...] oucs.ox.ac.uk>
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Kevin, kevin brintnall via RT wrote: | Note that I bumped the version to 1.3 in this patch. Feel free | to handle that however you like. Many thanks for the patch! It looks good to me so I've committed it along with some small changes I needed to make to the distribution, and uploaded version 1.3 of Net::MAC to CPAN. Please let me know if you have any issues with the new release. If it helps you at all, a read-only subversion repository of the module source is available here: https://svn.oucs.ox.ac.uk/people/oliver/pub/libnet-mac-perl/trunk/ Once again many thanks for supporting this module. regards, oliver. - -- Oliver Gorwits, Network and Telecommunications Group, Oxford University Computing Services -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFH8Nhf2NPq7pwWBt4RAtFzAJ4ql40BpqR6JS8mPqJ3Nkoa5D7Z9wCbBljJ dH933xSQrAWCUS+6GcEcEXk= =tw24 -----END PGP SIGNATURE-----