Browse Source

add a example and run tests with muliple methods

tags/0.0.0
Zane C. B-H 1 month ago
parent
commit
8f72345480
2 changed files with 124 additions and 6 deletions
  1. 58
    4
      Net-Connection-Match/lib/Net/Connection/Match.pm
  2. 66
    2
      Net-Connection-Match/t/Match.t

+ 58
- 4
Net-Connection-Match/lib/Net/Connection/Match.pm View File

@@ -22,6 +22,54 @@ our $VERSION = '0.0.0';
22 22
 
23 23
     use Net::Connection::Match;
24 24
     use Net::Connection;
25
+    
26
+    my $connection_args={
27
+                         foreign_host=>'10.0.0.1',
28
+                         foreign_port=>'22',
29
+                         local_host=>'10.0.0.2',
30
+                         local_port=>'12322',
31
+                         proto=>'tcp4',
32
+                         state=>'LISTEN',
33
+                        };
34
+    my $conn=Net::Connection->new( $connection_args );
35
+    
36
+    my %args=(
37
+              checks=>[
38
+                       {
39
+                        type=>'Ports',
40
+                        invert=>0,
41
+                        args=>{
42
+                               ports=>[
43
+                                       '22',
44
+                                      ],
45
+                               lports=>[
46
+                                        '53',
47
+                                       ],
48
+                               fports=>[
49
+                                        '12345',
50
+                                       ],
51
+                        }
52
+                       },
53
+                       {
54
+                        type=>'Protos',
55
+                        invert=>0,
56
+                        args=>{
57
+                               protos=>[
58
+                                        'tcp4',
59
+                                       ],
60
+                        }
61
+                       }
62
+                      ]
63
+             );
64
+    
65
+    my $checker;
66
+    eval{
67
+        $checker=Net::Connection::Match->new( \%args );
68
+    };
69
+    
70
+    if ( $check->match( $conn ) ){
71
+        print "It matched!\n";
72
+    }
25 73
 
26 74
 =head1 METHODS
27 75
 
@@ -32,6 +80,8 @@ This initializes a new check object.
32 80
 It takes one value and thht is a hash ref with the key checks.
33 81
 This is a array of hashes.
34 82
 
83
+If new fails, it will die.
84
+
35 85
 =head3 checks hash keys
36 86
 
37 87
 =head4 type
@@ -171,6 +221,14 @@ sub new{
171 221
 
172 222
 Checks if a single Net::Connection object matches the stack.
173 223
 
224
+One object is argument is taken and that is the Net::Connection to check.
225
+
226
+The return value is a boolean.
227
+
228
+    if ( $ncm->match( $conn ) ){
229
+        print "It matched.\n";
230
+    }
231
+
174 232
 =cut
175 233
 
176 234
 sub match{
@@ -232,10 +290,6 @@ sub match{
232 290
 
233 291
 Error handling is provided by L<Error::Helper>.
234 292
 
235
-=head2 1 / failedCheckInit
236
-
237
-Calling new for one or more of the checks failed.
238
-
239 293
 =head2 2 / notNCobj
240 294
 
241 295
 Not a Net::Connection object. Either is is not defined

+ 66
- 2
Net-Connection-Match/t/Match.t View File

@@ -18,6 +18,15 @@ my $connection_args={
18 18
 					  state=>'LISTEN',
19 19
 					  };
20 20
 
21
+my $nm_connection_args={
22
+					  foreign_host=>'10.0.0.1',
23
+					  foreign_port=>'80',
24
+					  local_host=>'10.0.0.2',
25
+					  local_port=>'12322',
26
+					  proto=>'tcp4',
27
+					  state=>'LISTEN',
28
+					  };
29
+
21 30
 my %args=(
22 31
 		  testing=>1,
23 32
 		  checks=>[
@@ -38,6 +47,38 @@ my %args=(
38 47
 					}
39 48
 				   ]
40 49
 		  );
50
+
51
+my %args2=(
52
+		  testing=>1,
53
+		  checks=>[
54
+				   {
55
+					type=>'Ports',
56
+					invert=>0,
57
+					args=>{
58
+						   ports=>[
59
+								   '22',
60
+								   ],
61
+						   lports=>[
62
+									'53',
63
+									],
64
+						   fports=>[
65
+									'12345',
66
+									],
67
+						   }
68
+					},
69
+				   {
70
+					type=>'Protos',
71
+					invert=>0,
72
+					args=>{
73
+						   protos=>[
74
+									'tcp4',
75
+									],
76
+						   }
77
+					}
78
+				   ]
79
+		   );
80
+
81
+
41 82
 my $checker;
42 83
 
43 84
 # makes sure we error with empty args
@@ -69,7 +110,7 @@ eval{
69 110
 };
70 111
 ok( $checker->error eq '2', 'match null input check') or diag('match accepted null input');
71 112
 
72
-# Create a connection with a matching general port and see if it matches
113
+# Create a matching connection and see if it matches
73 114
 my $conn=Net::Connection->new( $connection_args );
74 115
 $returned=0;
75 116
 eval{
@@ -77,4 +118,27 @@ eval{
77 118
 };
78 119
 ok( $returned eq '1', 'match good conn check') or diag('match failed on a connection that should match');
79 120
 
80
-done_testing(6);
121
+# Create a non-matching connection and see if it matches
122
+my $nm_conn=Net::Connection->new( $nm_connection_args );
123
+$returned=0;
124
+eval{
125
+	$returned=$checker->match($nm_conn);
126
+};
127
+ok( $returned eq '0', 'match bad conn check') or diag('match on a connection that should not match');
128
+
129
+# makes sure we can init with good args
130
+$worked=0;
131
+eval{
132
+	$checker=Net::Connection::Match->new( \%args2 );
133
+	$worked=1;
134
+};
135
+ok( $worked eq '1', 'init check2') or diag('Calling Net::Connection::Match::Ports->new resulted in... '.$@);
136
+
137
+# Create a matching connection and see if it matches
138
+$returned=0;
139
+eval{
140
+	$returned=$checker->match($conn);
141
+};
142
+ok( $returned eq '1', 'match good conn check2') or diag('match failed on a connection that should match');
143
+
144
+done_testing(9);

Loading…
Cancel
Save