1#!./perl -w 2use strict; 3 4use Test::More; 5use Config; 6use File::Temp 'tempdir'; 7use File::Spec; 8use Fcntl qw( :mode ); 9 10BEGIN { 11 plan(skip_all => "GDBM_File was not built") 12 unless $Config{extensions} =~ /\bGDBM_File\b/; 13 14 plan(tests => 7); 15 use_ok('GDBM_File'); 16} 17 18SKIP: { 19 skip "GDBM_File crash tolerance not available", 6, 20 unless GDBM_File->crash_tolerance_status; 21 22 my $wd = tempdir(CLEANUP => 1); 23 chdir $wd; 24 25 sub createdb { 26 my ($name, $type, $code) = @_; 27 my %h; 28 $type //= 0; 29 my $dbh = tie(%h, 'GDBM_File', $name, GDBM_NEWDB|$type, 0640); 30 if ($code) { 31 &{$code}($dbh, \%h); 32 } 33 untie %h 34 } 35 my $even = 'a.db'; 36 my $odd = 'b.db'; 37 my $time = time; 38 39 # 40 # Valid cases 41 # 42 43 # access modes 44 createdb($even); 45 createdb($odd); 46 chmod S_IRUSR, $even; 47 chmod S_IWUSR, $odd; 48 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 49 [ 'a.db', GDBM_SNAPSHOT_OK ], "different acess modes"); 50 51 # mtimes 52 chmod S_IRUSR, $odd; 53 utime($time, $time, $even); 54 utime($time, $time-5, $odd); 55 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 56 [ 'a.db', GDBM_SNAPSHOT_OK ], "different mtimes"); 57 unlink $even, $odd; 58 59 # numsync 60 createdb($even, GDBM_NUMSYNC); 61 createdb($odd, GDBM_NUMSYNC, sub { shift->sync }); 62 chmod S_IRUSR, $even, $odd; 63 utime($time, $time, $even, $odd); 64 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 65 [ 'b.db', GDBM_SNAPSHOT_OK ], "different numsync value"); 66 67 # 68 # Erroneous cases 69 # 70 71 unlink $even, $odd; 72 73 # Same snapshots 74 createdb($even); 75 createdb($odd); 76 chmod S_IRUSR, $even, $odd; 77 utime($time, $time, $even, $odd); 78 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 79 [ undef, GDBM_SNAPSHOT_SAME ], "GDBM_SNAPSHOT_SAME"); 80 81 # Both writable 82 chmod S_IWUSR, $even, $odd; 83 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 84 [ undef, GDBM_SNAPSHOT_BAD ], "GDBM_SNAPSHOT_BAD"); 85 86 # numsync difference > 1 87 unlink $even, $odd; 88 89 createdb($even, GDBM_NUMSYNC); 90 createdb($odd, GDBM_NUMSYNC, 91 sub { 92 my $dbh = shift; 93 $dbh->sync; 94 $dbh->sync; 95 }); 96 chmod S_IRUSR, $even, $odd; 97 utime($time, $time, $even, $odd); 98 is_deeply([GDBM_File->latest_snapshot($even, $odd)], 99 [ undef, GDBM_SNAPSHOT_SUSPICIOUS ], "GDBM_SNAPSHOT_SUSPICIOUS"); 100} 101