t/01-updateserial-pm.t
changeset 130 5578cb7933c1
equal deleted inserted replaced
129:642a27894c86 130:5578cb7933c1
       
     1 #! /usr/bin/perl
       
     2 
       
     3 use strict;
       
     4 use warnings;
       
     5 use Test::More qw(no_plan);
       
     6 use File::Temp;
       
     7 
       
     8 sub mydie(@);
       
     9 sub mk_tmp_zones($@);
       
    10 
       
    11 # is the module usable?
       
    12 BEGIN {
       
    13     use_ok "DNStools::UpdateSerial";
       
    14 }
       
    15 
       
    16 # does it have all the functions?
       
    17 can_ok("DNStools::UpdateSerial" => qw(uniq zones changed_zones update_index
       
    18     signature_expired need_rollover done_rollover begin_rollover end_rollover
       
    19     unlink_unused_keys include_keys sign update_serial mk_zone_conf file_entry
       
    20     server_reload dnssec_enabled));
       
    21 
       
    22 # uniq
       
    23 ok(@{[sort (uniq(qw(a a b)))]} ~~ @{[qw(a b)]}, 'uniq');
       
    24 
       
    25 # changed zones
       
    26 {
       
    27     my $m = File::Temp::tempdir(CLEANUP => 1) or mydie "Can't create master dir";
       
    28     $config{master_dir} = $m;
       
    29     my @z = qw(nostamp.tld);
       
    30     mk_tmp_zones($m, @z) or mydie "Can't make zones";
       
    31     ok(@{[changed_zones]} ~~ @{[@z]}, "'changed_zones' detects missing .stamp");
       
    32 }
       
    33 
       
    34 {
       
    35     my $m = File::Temp::tempdir(CLEANUP => 1) or mydie "Can't create master dir";
       
    36     $config{master_dir} = $m;
       
    37     my @z = qw(newstamp.tld);
       
    38     mk_tmp_zones($m, @z) or mydie "Can't make zones";
       
    39     my $sf = "$m/$z[0]/.stamp";
       
    40     open F, ">$sf" or mydie "Can't create stamp '$sf': $!";
       
    41     ok(@{[changed_zones]} ~~ @{[()]}, "'changed_zones' skips zones unless .stamp is older");
       
    42 }
       
    43 
       
    44 {
       
    45     my $m = File::Temp::tempdir(CLEANUP => 1) or mydie "Can't create master dir";
       
    46     $config{master_dir} = $m;
       
    47     my @z = qw(newzone.tld);
       
    48     mk_tmp_zones($m, @z) or mydie "Can't make zones";
       
    49     my $sf = "$m/$z[0]/.stamp";
       
    50     open F, ">$sf" or mydie "Can't create stamp file '$sf': $!";
       
    51     my $zf = "$m/$z[0]/$z[0]";
       
    52     my $atime = (stat "$zf")[8] or mydie "Can't stat zone file '$zf': $!";
       
    53     my $utime = time;
       
    54     utime $atime, $utime - 1, $sf or mydie "Can't utime .stamp file '$sf': $!";
       
    55     utime $atime, $utime, $zf or mydie "Can't utime zone file '$zf': $!";
       
    56     ok(@{[changed_zones]} ~~ @{[@z]}, "'changed_zones' detects zones with older .stamp");
       
    57 }
       
    58 
       
    59 # update_index
       
    60 {
       
    61     eval { update_index 'foo' };
       
    62     ok($@, "'update_index' dies on missing index zone file");
       
    63 }
       
    64 
       
    65 ## signature_expired
       
    66 #$m = File::Temp::tempdir(CLEANUP => 1) or mydie "Can't create master dir";
       
    67 #@z = qw(sigtest.tld);
       
    68 #mk_tmp_zones($m, @z) or mydie "Can't make zones";
       
    69 #
       
    70 
       
    71 # maybe skip some tests
       
    72 SKIP: {
       
    73 
       
    74     skip 'idn broken or not installed?', 2 unless eval {
       
    75         local $SIG{__WARN__} = sub {};
       
    76         chomp(my $r = qx(idn --quiet sub.tld));
       
    77         $r eq 'sub.tld'
       
    78     };
       
    79 
       
    80     # zones
       
    81     eval { zones 'foo' };
       
    82     ok($@, "'zones' dies on missing zone file");
       
    83 
       
    84     my @z = qw(föö.bär föö.bäz);
       
    85     my @zi = qw(xn--f-1gaa.xn--br-via xn--f-1gaa.xn--bz-via);
       
    86     my $m = File::Temp::tempdir(CLEANUP => 1) or mydie "Can't create master dir";
       
    87     mk_tmp_zones($m, @zi) or mydie "Can't make zones";
       
    88     $config{master_dir} = $m;
       
    89     ok(@{[zones @z]} ~~ @{[@zi]}, "'zones' internationalizes the zone name");
       
    90 
       
    91 }
       
    92 
       
    93 sub mydie(@) { 
       
    94     my ($p, $f, $l) = caller;
       
    95     die "\n\n", @_, " at $f line $l in package $p\n\n";
       
    96 }
       
    97 # we expect the master dir and the internationalized domain names as argument
       
    98 sub mk_tmp_zones($@) {
       
    99 
       
   100     my ($m, @z) = @_;
       
   101     return unless $m and @z;
       
   102 
       
   103     for (@z) {
       
   104         mkdir "$m/$_" or mydie "Can't create zone dir";
       
   105         open F, ">$m/$_/$_" or mydie "Can't create zone file";
       
   106         close F;
       
   107     }
       
   108 
       
   109     return 1;
       
   110 
       
   111 }
       
   112 
       
   113 __END__
       
   114 
       
   115 eval { get_config("xxx|xxx", "yyy|yyy") };
       
   116 ok($@, "dies on missing config");
       
   117 
       
   118 my ($tmp, %cf);
       
   119 
       
   120 # prepare some simple sample config
       
   121 $tmp = File::Temp->new();
       
   122 print {$tmp} <<__EOF;
       
   123 # comment
       
   124 abc = xyz
       
   125 other =    value with space
       
   126 __EOF
       
   127 close($tmp);
       
   128 
       
   129 # the files is specified, it should find the first 
       
   130 # existing
       
   131 %cf = get_config("xxx|xxx", $tmp->filename);
       
   132 ok(%cf, "got config");
       
   133 is($cf{abc} => "xyz", "simple value");
       
   134 is($cf{other} => "valuewithspace", "spaced value");
       
   135 
       
   136 # it should find the file specified in $ENV{DNSTOOLS_CONF}
       
   137 $ENV{DNSTOOLS_CONF} = $tmp->filename;
       
   138 %cf = ();
       
   139 %cf = get_config("xxx|xxx", $tmp->filename);
       
   140 ok(%cf, "got config from \$DNSTOOLS_CONF");
       
   141 is($cf{abc} => "xyz", "simple value");
       
   142 is($cf{other} => "valuewithspace", "spaced value");