Skip to content

Commit

Permalink
Item11157: Fix Logger tests
Browse files Browse the repository at this point in the history
Item10637: Update tests for Obfuscating logger

The logger test - Logger.pm - needed to be renamed to LoggerTests.pm so
that it would get picked up by FoswikiSuite.  Also, it appears that the
PlainFile logger was making it's rotation decision on localtime, but
logging in gmt.   Updated Logger.pm to rotate on gmtime and fixed test
to check file rotation using gmt times.

Updated tests to further verify the hash and mask versions of the
obfuscating logger.

git-svn-id: http://svn.foswiki.org/trunk@12668 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Oct 1, 2011
1 parent 3d7d402 commit 11a8fdd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test/unit/HTMLValidationTests.pm 0644
test/unit/InitFormTests.pm 0644
test/unit/Iterator.pm 0755
test/unit/LoadedRevTests.pm 0755
test/unit/Logger.pm 0755
test/unit/LoggerTests.pm 0755
test/unit/ManageDotPmTests.pm 0644
test/unit/FoswikiPmFunctionsTests.pm 0644
test/unit/MergeTests.pm 0644
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tests for Foswiki::Logger

package Logger;
package LoggerTests;
use FoswikiTestCase;
our @ISA = qw( FoswikiTestCase );

Expand Down Expand Up @@ -48,6 +48,15 @@ sub PlainFileLogger {
$this->{logger} = new Foswiki::Logger::PlainFile();
}

sub ObfuscatingLogger {
my $this = shift;
require Foswiki::Logger::PlainFile::Obfuscating;
$Foswiki::cfg{Log}{Implementation} =
'Foswiki::Logger::PlainFile::Obfuscating';
$Foswiki::cfg{Log}{Obfuscating}{MaskIP} = 1;
$this->{logger} = new Foswiki::Logger::PlainFile::Obfuscating();
}

sub fixture_groups {
my %algs;
foreach my $dir (@INC) {
Expand All @@ -58,6 +67,13 @@ sub fixture_groups {
}
closedir(D);
}
if ( opendir( D, "$dir/Foswiki/Logger/PlainFile" ) ) {
foreach my $alg ( readdir D ) {
next unless $alg =~ /^(\w+)\.pm$/;
$algs{$1} = 1;
}
closedir(D);
}
}
my @groups;
foreach my $alg ( keys %algs ) {
Expand All @@ -69,23 +85,44 @@ sub fixture_groups {
}

sub verify_simpleWriteAndReplay {
my $this = shift;
my $time = time;
my $this = shift;
my $time = time;
my $ipaddr = '1.2.3.4';
my $tmpIP = $ipaddr;

# Verify the three levels used by Foswiki; debug, info and warning
foreach my $level (qw(debug info warning)) {
$this->{logger}->log( $level, $level, "Green", "Eggs", "and", "Ham" );

# For the obfuscating logger, have the warning record hash the IP addrss
if ( $Foswiki::cfg{Log}{Implementation} eq
'Foswiki::Logger::PlainFile::Obfuscating'
&& $level eq 'warning' )
{
$Foswiki::cfg{Log}{Obfuscating}{MaskIP} = 0;
}

$this->{logger}->log( $level, $level, "Green", "Eggs", "and", $tmpIP );
}

$ipaddr = 'x.x.x.x'
if ( $Foswiki::cfg{Log}{Implementation} eq
'Foswiki::Logger::PlainFile::Obfuscating' );

foreach my $level (qw(debug info warning)) {
my $it = $this->{logger}->eachEventSince( $time, $level );
$this->assert( $it->hasNext(), $level );
my $data = $it->next();
my $t = shift(@$data);
$this->assert( $t >= $time, "$t $time" );
$this->assert_str_equals( "$level.Green.Eggs.and.Ham",
$ipaddr = '109.104.118.183'
if ( $Foswiki::cfg{Log}{Implementation} eq
'Foswiki::Logger::PlainFile::Obfuscating'
&& $level eq 'warning' );
$this->assert_str_equals( "$level.Green.Eggs.and.$ipaddr",
join( '.', @$data ) );
$this->assert( !$it->hasNext() );
}

}

sub verify_eachEventSinceOnEmptyLog {
Expand Down Expand Up @@ -210,7 +247,7 @@ sub verify_rotate {

$Foswiki::Logger::PlainFile::dontRotate = 1;

my $then = Foswiki::Time::parseTime("2000-02-01");
my $then = Foswiki::Time::parseTime("2000-02-01T00:00Z");

$plainFileTestTime = $then;
$mode = 0777;
Expand All @@ -224,7 +261,7 @@ sub verify_rotate {
$this->assert( !-e $lfn );

# Create the log, the entry should be stamped at $then - 1000 (last month)
$plainFileTestTime = $then - 1000;
$plainFileTestTime = Foswiki::Time::parseTime("2000-01-31T23:59Z");
$logger->log( 'info', 'Nil carborundum illegitami' );

# fake the modify time
Expand Down Expand Up @@ -254,7 +291,7 @@ sub verify_rotate {
$this->assert( open( F, '<', $backup ) );
$e = <F>;
$this->assert_equals(
"| 2000-01-31T23:43:20Z info | Nil carborundum illegitami |\n", $e );
"| 2000-01-31T23:59:00Z info | Nil carborundum illegitami |\n", $e );
close(F);

*Foswiki::Logger::PlainFile::_time = $timecache;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/Foswiki/Logger/PlainFile.pm
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ sub _getLogForLevel {

sub _time2month {
my $time = shift;
my @t = localtime($time);
my @t = gmtime($time);
$t[5] += 1900;
return sprintf( '%0.4d%0.2d', $t[5], $t[4] + 1 );
}
Expand Down

0 comments on commit 11a8fdd

Please sign in to comment.