Browse Source

fix the PTR module and add tests for it

tags/0.0.0
Zane C. B-H 1 month ago
parent
commit
0e125b1b2f
2 changed files with 140 additions and 3 deletions
  1. 3
    3
      Net-Connection-Match/lib/Net/Connection/Match/PTR.pm
  2. 137
    0
      Net-Connection-Match/t/PTR.t

+ 3
- 3
Net-Connection-Match/lib/Net/Connection/Match/PTR.pm View File

@@ -145,7 +145,7 @@ sub new{
145 145
 	my $ptrs_int=0;
146 146
 	if ( defined( $args{ptrs} ) ){
147 147
 		while (defined( $args{ptrs}[$ptrs_int] )) {
148
-			$self->{ptrs}{ $ptrs_int }=lc( $args{ptrs}[$ptrs_int] );
148
+			$self->{ptrs}{ $args{ptrs}[$ptrs_int] }=lc( $args{ptrs}[$ptrs_int] );
149 149
 
150 150
 			$ptrs_int++;
151 151
 		}
@@ -155,7 +155,7 @@ sub new{
155 155
 	$ptrs_int=0;
156 156
 	if ( defined( $args{lptrs} ) ){
157 157
 		while (defined( $args{lptrs}[$ptrs_int] )) {
158
-			$self->{lptrs}{ $ptrs_int }=lc( $args{lptrs}[$ptrs_int] );
158
+			$self->{lptrs}{ $args{lptrs}[$ptrs_int] }=lc( $args{lptrs}[$ptrs_int] );
159 159
 
160 160
 			$ptrs_int++;
161 161
 		}
@@ -165,7 +165,7 @@ sub new{
165 165
 	$ptrs_int=0;
166 166
 	if ( defined( $args{fptrs} ) ){
167 167
 		while (defined( $args{fptrs}[$ptrs_int] )) {
168
-			$self->{fptrs}{ $ptrs_int }=lc( $args{fptrs}[$ptrs_int] );
168
+			$self->{fptrs}{ $args{fptrs}[$ptrs_int] }=lc( $args{fptrs}[$ptrs_int] );
169 169
 
170 170
 			$ptrs_int++;
171 171
 		}

+ 137
- 0
Net-Connection-Match/t/PTR.t View File

@@ -0,0 +1,137 @@
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::PTR' ) || 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
+					 local_ptr=>'foo.bar',
20
+					 foreign_ptr=>'test',
21
+					 };
22
+
23
+my %args=(
24
+		  ptrs=>[
25
+				 'foo.bar',
26
+				 ],
27
+		  );
28
+my %largs=(
29
+		  lptrs=>[
30
+				 'foo.bar',
31
+				 ],
32
+		   );
33
+my %fargs=(
34
+		  fptrs=>[
35
+				 'foo.bar',
36
+				 ],
37
+		  );
38
+my $checker;
39
+
40
+# makes sure we error with empty args
41
+my $worked=0;
42
+eval{
43
+	$checker=Net::Connection::Match::PTR->new();
44
+	$worked=1;
45
+};
46
+ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
47
+
48
+# makes sure we can init with general good args
49
+$worked=0;
50
+eval{
51
+	$checker=Net::Connection::Match::PTR->new( \%args );
52
+	$worked=1;
53
+};
54
+ok( $worked eq '1', 'init check, general') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
55
+
56
+# make sure it will not accept null input
57
+my $returned=1;
58
+eval{
59
+	$returned=$checker->match;
60
+};
61
+ok( $returned eq '0', 'proto undef check') or diag('match accepted undefined input');
62
+
63
+# make sure it will not accept a improper ref type
64
+$returned=1;
65
+eval{
66
+	$returned=$checker->match($checker);
67
+};
68
+ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
69
+
70
+# make sure the general PTR check works, testing local
71
+$returned=0;
72
+my $conn=Net::Connection->new( $connection_args );
73
+eval{
74
+	$returned=$checker->match( $conn );
75
+};
76
+ok( $returned eq '1', 'general PTR match check, local') or diag('failed to match a Net::Connection for a general PTR check when one of the two matches');
77
+
78
+# make sure the general PTR check works, testing foreign
79
+$connection_args->{local_ptr}='test';
80
+$connection_args->{foreign_ptr}='foo.bar';
81
+$conn=Net::Connection->new( $connection_args );
82
+eval{
83
+	$returned=$checker->match( $conn );
84
+};
85
+ok( $returned eq '1', 'general PTR match check, foriegn') or diag('failed to match a Net::Connection for a general PTR check when one of the two matches');
86
+
87
+# makes sure we can init with local good args
88
+$worked=0;
89
+eval{
90
+	$checker=Net::Connection::Match::PTR->new( \%largs );
91
+	$worked=1;
92
+};
93
+ok( $worked eq '1', 'init check, local') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
94
+
95
+# make sure the local PTR check works, testing foreign
96
+$returned=0;
97
+eval{
98
+	$returned=$checker->match( $conn );
99
+};
100
+ok( $returned eq '0', 'local PTR match check, foreign') or diag('matched a Net::Connection object when looking for local PTRs when the foreign matches');
101
+
102
+# make sure the local PTR check works, testing local
103
+$returned=0;
104
+$connection_args->{foreign_ptr}='test';
105
+$connection_args->{local_ptr}='foo.bar';
106
+$conn=Net::Connection->new( $connection_args );
107
+eval{
108
+	$returned=$checker->match( $conn );
109
+};
110
+ok( $returned eq '1', 'local PTR match check, local') or diag('did not match a Net::Connection object when looking for local PTRs when the local matches');
111
+
112
+# makes sure we can init with foreign good args
113
+$worked=0;
114
+eval{
115
+	$checker=Net::Connection::Match::PTR->new( \%fargs );
116
+	$worked=1;
117
+};
118
+ok( $worked eq '1', 'init check, foreign') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
119
+
120
+# make sure the foreign PTR check works, testing local
121
+$returned=0;
122
+eval{
123
+	$returned=$checker->match( $conn );
124
+};
125
+ok( $returned eq '0', 'foreign PTR match check, local') or diag('matched a Net::Connection object when looking for foreign PTRs when the local matches');
126
+
127
+# make sure the foreign PTR check works, testing foreign
128
+$returned=0;
129
+$connection_args->{local_ptr}='test';
130
+$connection_args->{foreign_ptr}='foo.bar';
131
+$conn=Net::Connection->new( $connection_args );
132
+eval{
133
+	$returned=$checker->match( $conn );
134
+};
135
+ok( $returned eq '1', 'local PTR match check, foreign') or diag('did not match a Net::Connection object when looking for foreign PTRs when the foreign matches');
136
+
137
+done_testing(13);

Loading…
Cancel
Save