From: rkinyon Date: Sun, 28 Jan 2007 06:37:21 +0000 (+0000) Subject: Added tests for wrong file versions X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e0fc635483fbb272770076301aa7b56cbc4bc8a4;p=dbsrgits%2FDBM-Deep.git Added tests for wrong file versions --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 29d4f4f..b47a065 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -552,7 +552,7 @@ sub clear_entries { { my $header_fixed = length( SIG_FILE ) + 1 + 4 + 4; - my $this_header_version = 2; + my $this_file_version = 2; sub _write_file_header { my $self = shift; @@ -566,8 +566,8 @@ sub clear_entries { $self->storage->print_at( $loc, SIG_FILE, SIG_HEADER, - pack('N', $this_header_version), # At this point, we're at 9 bytes - pack('N', $header_var), # header size + pack('N', $this_file_version), # At this point, we're at 9 bytes + pack('N', $header_var), # header size # --- Above is $header_fixed. Below is $header_var pack('C', $self->byte_size), pack('C', $self->max_buckets), @@ -592,7 +592,7 @@ sub clear_entries { my $buffer = $self->storage->read_at( 0, $header_fixed ); return unless length($buffer); - my ($file_signature, $sig_header, $header_version, $size) = unpack( + my ($file_signature, $sig_header, $file_version, $size) = unpack( 'A4 A N N', $buffer ); @@ -606,11 +606,11 @@ sub clear_entries { DBM::Deep->_throw_error( "Pre-1.00 file version found" ); } - unless ( $header_version == $this_header_version ) { + unless ( $file_version == $this_file_version ) { $self->storage->close; DBM::Deep->_throw_error( - "Wrong file version found - " . $header_version . - " - expected " . $this_header_version + "Wrong file version found - " . $file_version . + " - expected " . $this_file_version ); } diff --git a/t/06_error.t b/t/06_error.t index 39c57b6..d343d23 100644 --- a/t/06_error.t +++ b/t/06_error.t @@ -3,23 +3,26 @@ ## $|++; use strict; -use Test::More tests => 6; +use Test::More tests => 8; use Test::Exception; use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); -my ($fh, $filename) = new_fh(); - ## # test a corrupted file ## -open FH, ">$filename"; -print FH 'DPDB'; -close FH; -throws_ok { - DBM::Deep->new( $filename ); -} qr/DBM::Deep: Pre-1.00 file version found/, "Fail if there's a bad header"; +{ + my ($fh, $filename) = new_fh(); + + open FH, ">$filename"; + print FH 'DPDB'; + close FH; + + throws_ok { + DBM::Deep->new( $filename ); + } qr/DBM::Deep: Pre-1.00 file version found/, "Fail if there's a bad header"; +} { my ($fh, $filename) = new_fh(); @@ -52,3 +55,15 @@ throws_ok { DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_HASH ) } qr/DBM::Deep: File type mismatch/, "Fail if we try and open an array file with a hash"; } + +{ + throws_ok { + DBM::Deep->new( 't/old_versions/db.0.983' ); + } qr/DBM::Deep: Pre-1.00 file version found/, "Fail if opening a pre-1.00 file"; +} + +{ + throws_ok { + DBM::Deep->new( 't/old_versions/db.0.99_04' ); + } qr/DBM::Deep: Wrong file version found - 1 - expected 2/, "Fail if opening a file version 1"; +} diff --git a/t/old_versions/db.0.983 b/t/old_versions/db.0.983 new file mode 100644 index 0000000..1505fa6 Binary files /dev/null and b/t/old_versions/db.0.983 differ diff --git a/t/old_versions/db.0.99_04 b/t/old_versions/db.0.99_04 new file mode 100644 index 0000000..9268167 Binary files /dev/null and b/t/old_versions/db.0.99_04 differ