File: test.pl

package info (click to toggle)
libnet-ifconfig-wrapper-perl 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 128 kB
  • sloc: perl: 773; makefile: 2
file content (172 lines) | stat: -rw-r--r-- 4,382 bytes parent folder | download | duplicates (5)
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
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use Test;
BEGIN { plan tests => 5 };

$^W++;

my %IfCfgCmd = ('freebsd' => '/sbin/ifconfig -a',
                'solaris' => '/sbin/ifconfig -a',
                'openbsd' => '/sbin/ifconfig -A',
                'linux'   => '/sbin/ifconfig -a',
                'darwin'  => '/sbin/ifconfig -a',
               );

print 'Loading Net::Ifconfig::Wrapper........';

use Net::Ifconfig::Wrapper;

ok(1); # If we made it this far, we're ok.



print 'Geting information about interfaces...';

my $Info = Net::Ifconfig::Wrapper::Ifconfig('list')
	or die $@;

ok(1);

if ($IfCfgCmd{$^O})
	{
	print "\n== '".$IfCfgCmd{$^O}. "' command output ==\n";
	system($IfCfgCmd{$^O});
	print "======================================\n";
	};

print "\n== Net\:\:Ifconfig\:\:Wrapper info output ==\n";
foreach (sort(keys(%{$Info})))
	{ print IfaceInfo($Info, $_); };
print "======================================\n";

print "Is Net\:\:Ifconfig\:\:Wrapper info output correct? Y/N:";

while (<STDIN>)
	{
	$_ =~ m/\A\s*y(es)?\s*\n?\Z/i
		and last;
	$_ =~ m/\A\s*n(o)?\s*\n?\Z/i
		and die "Net\:\:Ifconfig\:\:Wrapper info is incorrect!";
	print "Is Net\:\:Ifconfig\:\:Wrapper info output correct? Y/N:";
	};

print "\nInformation displayed correctly.......";

ok(1);



if (($^O eq "MSWin32") || ($> == 0))
	{
	print "\nPlease choose the interface for add/remove alias function test\n";
	
	my @Ifaces = ('skip test');
	
	push(@Ifaces, sort(keys(%{$Info})));
	
	my $DefIface = '';
	
	for (my $RI = 0; defined($Ifaces[$RI]); $RI++)
		{
		(($Ifaces[$RI] =~ m/\Alo/i) && !length($DefIface))
			and $DefIface = $RI;
		print "$RI:\t".$Ifaces[$RI]."\n";
		};
	
	print "($DefIface):";
	
	while (<STDIN>)
		{
		if    (($_ =~ m/\A\s*\n?\Z/i) && length($DefIface))
			{ last; }
		elsif (($_ =~ m/\A\s*(\d+)\s*\n?\Z/i) && defined($Ifaces[$1]))
			{
			$DefIface = $1;
			last;
			};
		print "Please choose the interface for add/remove alias function tests\n";
		for (my $RI = 0; defined($Ifaces[$RI]); $RI++)
			{ print "$RI:\t".$Ifaces[$RI]."\n"; };
		print "($DefIface):";
		};
	
	if (!$DefIface)
		{
		print "add/remove alias function tests (4,5) skipped\n";
		exit 0;
		};
	
	my $Addr = '192.168.192.168';
	my $Mask = '255.255.255.0';
	
	print "Please choose address and mask for test alias\n($Addr:$Mask):";
	
	while (<STDIN>)
		{
		$_ =~ m/\A\s*\n?\Z/i
			and last;
		if ($_ =~ m/\A\s*(\d{1,3}(?:\.\d{1,3}){3})\s*\:?\s*(\d{1,3}(?:\.\d{1,3}){3})\s*\n?\Z/i)
			{
			$Addr = $1;
			$Mask = $2;
			last;
			};
		print "Please choose address and mask for test alias\n($Addr:$Mask):";
		};
	print "\nAdding   alias '$Addr:$Mask' to   interface ".$Ifaces[$DefIface]."...";
	
	Net::Ifconfig::Wrapper::Ifconfig('+alias', $Ifaces[$DefIface], $Addr, $Mask)
		or die $@;
	
	$Info = Net::Ifconfig::Wrapper::Ifconfig('list')
		or die $@;
	
	defined($Info->{$Ifaces[$DefIface]}->{'inet'}->{$Addr}) &&
		($Info->{$Ifaces[$DefIface]}->{'inet'}->{$Addr} eq $Mask)
		or die "Can not find recently added address '$Addr:$Mask' on interface ".$Ifaces[$DefIface];
	
	ok(1);
	
	print "Removing alias '$Addr:$Mask' from interface ".$Ifaces[$DefIface]."...";
	
	Net::Ifconfig::Wrapper::Ifconfig('-alias', $Ifaces[$DefIface], $Addr, '')
		or die $@;
	
	$Info = Net::Ifconfig::Wrapper::Ifconfig('list')
		or die $@;
	
	defined($Info->{$Ifaces[$DefIface]}->{'inet'}->{$Addr})
		and die "Can not remove recently added address '$Addr:$Mask' from interface ".$Ifaces[$DefIface];
	
	ok(1);
	}
else
	{
	print "add/remove alias function tests (4,5) skipped: insufficient privileges\n";
	exit 0;
	};

#exit 0;

sub IfaceInfo
	{
	my ($Info, $Iface) = @_;

	my $Res = "$Iface:\t".($Info->{$Iface}->{'status'} ? 'UP' : 'DOWN')."\n";

	while (my ($Addr, $Mask) = each(%{$Info->{$Iface}->{'inet'}}))
		{ $Res .= sprintf("\tinet %-15s mask $Mask\n", $Addr); };
	
	$Info->{$Iface}->{'ether'}
		and $Res .= "\tether ".$Info->{$Iface}->{'ether'}."\n";

	$Info->{$Iface}->{'descr'}
		and $Res .= "\tdescr '".$Info->{$Iface}->{'descr'}."'\n";
	
	return $Res;
	};