[Rt-commit] r6251 - in rtir/branches/2.1-EXPERIMENTAL: . t
ruz at bestpractical.com
ruz at bestpractical.com
Mon Oct 23 20:01:06 EDT 2006
Author: ruz
Date: Mon Oct 23 20:01:06 2006
New Revision: 6251
Modified:
rtir/branches/2.1-EXPERIMENTAL/ (props changed)
rtir/branches/2.1-EXPERIMENTAL/lib/RT/IR.pm
rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t
Log:
r1744 at cubic-pc: cubic | 2006-10-24 03:31:51 +0400
* search by an IP is range search too, now
* fix range searches when left value is greater than right
* add more tests
Modified: rtir/branches/2.1-EXPERIMENTAL/lib/RT/IR.pm
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/lib/RT/IR.pm (original)
+++ rtir/branches/2.1-EXPERIMENTAL/lib/RT/IR.pm Mon Oct 23 20:01:06 2006
@@ -121,21 +121,20 @@
use Regexp::Common::net::CIDR;
require Net::CIDR;
-# limit formatting "%03d.%03d.%03d.%03d"
require RT::Tickets;
-wrap 'RT::Tickets::_CustomFieldLimit',
- pre => sub {
- return unless $_[3] =~ /^\s*($RE{net}{IPv4})\s*$/o;
- $_[3] = sprintf "%03d.%03d.%03d.%03d", split /\./, $1;
- };
+# limit formatting "%03d.%03d.%03d.%03d"
# "= 'sIP-eIP'" => "( >=sIP AND <=eIP)"
# "!= 'sIP-eIP'" => "( <sIP OR >eIP)"
# two ranges intercect when ( eIP1 >= sIP2 AND sIP1 <= eIP2 )
wrap 'RT::Tickets::_CustomFieldLimit',
pre => sub {
- return unless $_[3] =~ /^\s*($RE{net}{IPv4})\s*-\s*($RE{net}{IPv4})\s*$/o;
- my ($start_ip, $end_ip) = ($1, $2);
+ return if $_[2] && $_[2] =~ /^[<>]=?$/;
+ return unless $_[3] =~ /^\s*($RE{net}{IPv4})\s*(?:-\s*($RE{net}{IPv4})\s*)?$/o;
+ my ($start_ip, $end_ip) = ($1, ($2 || $1));
+ $_ = sprintf "%03d.%03d.%03d.%03d", split /\./, $_
+ for $start_ip, $end_ip;
+ ($start_ip, $end_ip) = ($end_ip, $start_ip) if $start_ip gt $end_ip;
my ($tickets, $field, $op, $value, %rest) = @_[0..($#_-1)];
$tickets->_OpenParen;
Modified: rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t
==============================================================================
--- rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t (original)
+++ rtir/branches/2.1-EXPERIMENTAL/t/013-custom-field-ip.t Mon Oct 23 20:01:06 2006
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 212;
+use Test::More tests => 252;
require "t/rtir-test.pl";
@@ -199,7 +199,7 @@
my $flag = 1;
while ( my $ticket = $tickets->Next ) {
my %has = map { $_->Content => 1 } @{ $ticket->CustomFieldValues('_RTIR_IP')->ItemsArrayRef };
- next if $has{'172.16.1.1'};
+ next if $has{'172.16.1.0-172.16.1.1'};
$flag = 0;
ok(0, "ticket #". $ticket->id ." has no IP 172.16.1.1, but should");
last;
@@ -224,15 +224,6 @@
ok(1, "all tickets have at least one IP from 172.16.2.0-172.16.2.255") if $flag;
}
-diag "search tickets within CIDR block" if $ENV{'TEST_VERBOSE'};
-{
- my $tickets = RT::Tickets->new( $rtir_user );
- $tickets->FromSQL("CF.{_RTIR_IP} = '172.16.2/24'");
- ok( $tickets->Count, "found tickets" );
- $tickets->FromSQL("CF.{_RTIR_IP} = '172.16/16'");
- ok( $tickets->Count, "found tickets" );
-}
-
diag "create two tickets with different IPs and check several searches" if $ENV{'TEST_VERBOSE'};
{
my $id1 = create_ir( $agent, { Subject => "test ip" }, { IP => '192.168.21.10' } );
@@ -277,16 +268,12 @@
# IP range, with start IP greater than end
$tickets = RT::Tickets->new( $rtir_user );
$tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.255-192.168.21.0'");
- TODO: { local $TODO = "not yet implemented";
- is( $tickets->Count, 1, "found one ticket" );
- #is( $tickets->First->FirstCustomFieldValue('_RTIR_IP'), '192.168.21.10', "correct value" );
- }
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('_RTIR_IP'), '192.168.21.10', "correct value" );
$tickets = RT::Tickets->new( $rtir_user );
$tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.22.255-192.168.22.0'");
- TODO: { local $TODO = "not yet implemented";
- is( $tickets->Count, 1, "found one ticket" );
- #is( $tickets->First->FirstCustomFieldValue('_RTIR_IP'), '192.168.22.10', "correct value" );
- }
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->FirstCustomFieldValue('_RTIR_IP'), '192.168.22.10', "correct value" );
# CIDR/24
$tickets = RT::Tickets->new( $rtir_user );
@@ -315,3 +302,82 @@
is( $tickets->Count, 2, "found both tickets" );
}
+diag "create two tickets with different IP ranges and check several searches" if $ENV{'TEST_VERBOSE'};
+{
+ my $id1 = create_ir( $agent, { Subject => "test ip" }, { IP => '192.168.21.0-192.168.21.127' } );
+ ok($id1, "created first ticket");
+ my $id2 = create_ir( $agent, { Subject => "test ip" }, { IP => '192.168.21.128-192.168.21.255' } );
+ ok($id2, "created second ticket");
+
+ my $tickets = RT::Tickets->new( $rtir_user );
+ $tickets->FromSQL("id = $id1 OR id = $id2");
+ is( $tickets->Count, 2, "found both tickets by 'id = x OR y'" );
+
+ # IP
+ $tickets = RT::Tickets->new( $rtir_user );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.0'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.64'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.127'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.128'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id2, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.191'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id2, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.255'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id2, "correct value" );
+
+ # IP/32 - one address
+ $tickets = RT::Tickets->new( $rtir_user );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.63/32'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.191/32'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id2, "correct value" );
+
+ # IP range, lower than both
+ $tickets = RT::Tickets->new( $rtir_user );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.20.0-192.168.20.255'");
+ is( $tickets->Count, 0, "didn't finnd ticket" ) or diag "but found ". $tickets->First->id;
+
+ # IP range, intersect with the first range
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.20.0-192.168.21.63'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+
+ # IP range, equal to the first range
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.0-192.168.21.127'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+
+ # IP range, lay inside the first range
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.31-192.168.21.63'");
+ is( $tickets->Count, 1, "found one ticket" );
+ is( $tickets->First->id, $id1, "correct value" );
+
+ # IP range, intersect with the ranges
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.31-192.168.21.191'");
+ is( $tickets->Count, 2, "found both tickets" );
+
+ # IP range, equal to range from the starting IP of the first ticket to the ending IP of the second
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.21.0-192.168.21.255'");
+ is( $tickets->Count, 2, "found both tickets" );
+
+ # IP range, has the both ranges inside it
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168/16'");
+ is( $tickets->Count, 2, "found both tickets" );
+
+ # IP range, greater than both
+ $tickets = RT::Tickets->new( $rtir_user );
+ $tickets->FromSQL("(id = $id1 OR id = $id2) AND CF.{_RTIR_IP} = '192.168.22/24'");
+ is( $tickets->Count, 0, "didn't finnd ticket" ) or diag "but found ". $tickets->First->id;
+}
+
More information about the Rt-commit
mailing list