Browse Source

ports mostly done and testing in progress

tags/0.0.0
Zane C. B-H 1 month ago
parent
commit
8b45f4ba28

+ 26
- 0
Net-Connection-Match/lib/Net/Connection/Match/Ports.pm View File

@@ -213,6 +213,32 @@ sub match{
213 213
 		return 0;
214 214
 	}
215 215
 
216
+	my $lport=$object->local_port;
217
+	my $fport=$object->foreign_port;
218
+
219
+	# If either are non-numeric, resolve them if possible
220
+	if ( $lport !~ /^[0-9]+$/ ){
221
+		my $lport_number=(getservbyname( $lport , '' ))[2];
222
+		if ( defined( $lport_number ) ){
223
+			$lport=$lport_number;
224
+		}
225
+	}
226
+	if ( $fport !~ /^[0-9]+$/ ){
227
+		my $fport_number=(getservbyname( $fport , '' ))[2];
228
+		if ( defined( $fport_number ) ){
229
+			$fport=$fport_number;
230
+		}
231
+	}
232
+
233
+	# check if this is one of the ones we are looking for
234
+	if (
235
+		defined( $self->{ports}{ $lport } ) ||
236
+		defined( $self->{ports}{ $fport } ) ||
237
+		defined( $self->{lports}{ $lport } ) ||
238
+		defined( $self->{fports}{ $fport } )
239
+		){
240
+		return 1;
241
+	}
216 242
 
217 243
 	return 0;
218 244
 }

+ 81
- 0
Net-Connection-Match/t/Ports.t View File

@@ -0,0 +1,81 @@
1
+#!perl -T
2
+use 5.006;
3
+use strict;
4
+use warnings;
5
+use Test::More;
6
+use Net::Connection;
7
+
8
+BEGIN {
9
+    use_ok( 'Net::Connection::Match::Ports' ) || print "Bail out!\n";
10
+}
11
+
12
+my $connection_args={
13
+					  foreign_host=>'10.0.0.1',
14
+					  foreign_port=>'22',
15
+					  local_host=>'10.0.0.2',
16
+					  local_port=>'12322',
17
+					  proto=>'tcp4',
18
+					  state=>'LISTEN',
19
+					  };
20
+
21
+my %args=(
22
+		  ports=>[
23
+				   '22',
24
+				  ],
25
+		  lports=>[
26
+				  '53',
27
+				   ],
28
+		  fports=>[
29
+				   '12345',
30
+				   ],
31
+		  );
32
+my $checker;
33
+
34
+# makes sure we error with empty args
35
+my $worked=0;
36
+eval{
37
+	$checker=Net::Connection::Match::Ports->new();
38
+	$worked=1;
39
+};
40
+ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
41
+
42
+# makes sure we can init with good args
43
+$worked=0;
44
+eval{
45
+	$checker=Net::Connection::Match::Ports->new( \%args );
46
+	$worked=1;
47
+};
48
+ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::Ports->new resulted in... '.$@);
49
+
50
+# make sure it will not accept null input
51
+my $returned=1;
52
+eval{
53
+	$returned=$checker->match;
54
+};
55
+ok( $returned eq '0', 'ports undef check') or diag('match accepted undefined input');
56
+
57
+# make sure it will not accept a improper ref type
58
+$returned=1;
59
+eval{
60
+	$returned=$checker->match($checker);
61
+};
62
+ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
63
+
64
+# Create a connection with a matching protocol and see if it matches
65
+my $conn=Net::Connection->new( $connection_args );
66
+$returned=0;
67
+eval{
68
+	$returned=$checker->match( $conn );
69
+};
70
+ok( $returned eq '1', 'port match check') or diag('General port match failed');
71
+
72
+# Create a connection with a non-matching protocol and make sure it does not match
73
+$connection_args->{local_port}='53';
74
+$conn=Net::Connection->new( $connection_args );
75
+$returned=1;
76
+eval{
77
+	$returned=$checker->match( $conn );
78
+};
79
+ok( $returned eq '1', 'local port match check') or diag('Failed to matching local port');
80
+
81
+done_testing(7);

Loading…
Cancel
Save