File: file_listing.t

package info (click to toggle)
libfile-listing-perl 6.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: perl: 295; sh: 8; makefile: 2
file content (326 lines) | stat: -rw-r--r-- 8,031 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use strict;
use warnings;
use File::Listing;
use Test::More;

subtest 'unix' => sub {

  my $dir = do {
    open my $fh, '<', 'corpus/unix.txt';
    local $/;
    <$fh>;
  };

  {
    check_output( parse_dir($dir, undef, 'unix') );
  }

  {
    open LISTING, '<', 'corpus/unix.txt';  ## no critic
    check_output( parse_dir(\*LISTING, undef, 'unix') );
  }

  {
    open my $fh, '<', 'corpus/unix.txt';
    check_output( parse_dir($fh, undef, 'unix') );
  }

  sub check_output {
    my @dir = @_;

    ok(@dir, 'ok 25');

    for (@dir) {
       my ($name, $type, $size, $mtime, $mode) = @$_;
       $size ||= 0;  # ensure that it is defined
       printf "# %-25s $type %6d  ", $name, $size;
       print scalar(localtime($mtime));
       printf "  %06o", $mode;
       print "\n";
    }

    # Pick out the Socket.pm line as the sample we check carefully
    my ($name, $type, $size, $mtime, $mode) = @{$dir[9]};

    ok($name, "Socket.pm");
    ok($type, "f");
    ok($size, 'ok 8817');

    # Must be careful when checking the time stamps because we don't know
    # which year if this script lives for a long time.
    my $timestring = scalar(localtime($mtime));
    ok($timestring =~ /Mar\s+15\s+18:05/);

    ok($mode, 'mode 0100644');
  }

  {
    my @dir = parse_dir(<<'EOT');
drwxr-xr-x 21 root root 704 2007-03-22 21:48 dir
EOT

    ok(@dir, 'ok 1');
    ok($dir[0][0], "dir");
    ok($dir[0][1], "d");

    my $timestring = scalar(localtime($dir[0][3]));
    print "# $timestring\n";
    ok($timestring =~ /^Thu Mar 22 21:48/);
  }
};

subtest 'apache' => sub {


  {
    my %expected;
    foreach my $test_name (qw( fancy-indexing html-table html-table-with-icons old-date xhtml with-icons ))
    {
      subtest $test_name => sub {
        my $html = do {
          open my $fh, '<', "corpus/apache-$test_name.html";
          local $/;
          <$fh>;
        };

        my %actual = map { $_->[0] => $_ } parse_dir($html, undef, "apache");

        subtest 'dir' => sub {
          note join(':', map { defined $_ ? $_ : 'undef' } @{ $actual{'lib'} });
          is $actual{'lib'}->[0], 'lib';
          is $actual{'lib'}->[1], 'd';
          is $actual{'lib'}->[2], 0;
          cmp_ok $actual{'lib'}->[3], '>', 0;
          is $actual{'lib'}->[4], undef;
        };

        subtest 'file' => sub {
          note join(':', map { defined $_ ? $_ : 'undef' } @{ $actual{'dist.ini'} });
          is $actual{'dist.ini'}->[0], 'dist.ini';
          is $actual{'dist.ini'}->[1], 'f';
          cmp_ok $actual{'dist.ini'}->[2], '>', 0;
          cmp_ok $actual{'dist.ini'}->[3], '>', 0;
          is $actual{'dist.ini'}->[4], undef;
        };

        if(%expected)
        {
          is_deeply \%actual, \%expected;
        }
        else
        {
          %expected = %actual;
        }

      };
    }

    foreach my $file (values %expected)
    {
      $file->[2] = undef;
      $file->[3] = undef;
    }

    subtest 'default' => sub {
      my $html = do {
        open my $fh, '<', "corpus/apache-default.html";
        local $/;
        <$fh>;
      };

      my %actual = map { $_->[0] => $_ } parse_dir($html, undef, "apache");

      subtest 'dir' => sub {
        is_deeply $actual{lib}, ['lib','d',undef,undef,undef];
      };
      subtest 'file' => sub {
        is_deeply $actual{'dist.ini'}, ['dist.ini','f',undef,undef,undef];
      };

      is_deeply \%actual, \%expected;
    };
  }

  foreach my $num (1..3)
  {
    subtest "legacy $num" => sub {
      my @dir = do {
        open my $fh, '<', "corpus/apache-legacy$num.html";
        local $/;
        <$fh>;
      };

      my @listing = parse_dir(shift @dir, undef, "apache");
      ok(@listing);
   };
  }

  subtest 'year' => sub {

    my $parse = sub {
      my $date = shift;
      my $time = [
        parse_dir(
          qq{<img src="/icons/unknown.gif" alt="[   ]"> <a href="apache-modperl-1.3.6_1.19-0.i386.rpm">apache-modperl-1.3.6_1.19-0.i386.rpm</a>  $date  696K},
          undef,
          "apache",
        )
      ]->[0]->[3];

      [localtime($time)]->[5] + 1900;
    };

    ## hack to test the test of 2038 bug systems when
    ## you do not have one handy.
    #require Test2::Mock;
    #my $mock = Test2::Mock->new( class => 'Time::Local' );
    #$mock->around( 'timelocal' => sub {
    #  my $orig = shift;
    #  my(undef,undef,undef,undef,undef, $year) = @_;
    #  die "frooble bits" if $year >= 2038;
    #  $orig->(@_);
    #});

    my $max_year = 2500;

    local $@;
    eval {
      require Time::Local;
      Time::Local::timelocal(0, 30, 16, 29, 5, 2038);
    };

    if(my $error = $@)
    {
      diag '';
      diag "WARNING WARNING WARNING";
      diag '';
      diag "Your Perl / Operating System does not support dates in 2038.";
      diag "(probably due to 32 bit unix epoch).";
      diag "I will skip tests with these types of dates, but you should";
      diag "upgrade your Perl / Operating System if possible";
      diag '';
      diag "actual exception: $error";
      diag '';
      diag "WARNING WARNING WARNING";
      $max_year = 2037;
    }

    # Note: explicitly not tested are two digit years,
    # because the current behavior is probably wrong.
    # Right now we assume 9x is 199x and 0-89 is 20xx,
    # which is definitely wrong in the long term, but
    # I don't even have any examples where apache provides
    # a two digit date.
    foreach my $year (1970..$max_year) {
      local $@;

      my $got = eval { $parse->("$year-06-29 16:30") };
      my $error = $@;
      my $expected = $year;
      my $name = "year = $year";

      if($error)
      {
        fail($name);
        diag "parser threw exception: $error";
      }
      else
      {
        is( $got, $expected, $name );
      }
    }

  };

};

subtest 'dosftp' => sub {

  my $list = parse_dir(<<EOT, undef, 'dosftp');
02-05-96  10:48AM                 1415 src.slf
09-10-96  09:18AM       <DIR>          sl_util
EOT

  is scalar @$list, 2, "is 2";
  ok $list->[0][0], "src.slf";
  ok $list->[0][1], "f";
  ok $list->[1][1], "d";

  $list = parse_dir(<<EOT, undef, 'dosftp');
02-05-2022  10:48AM                 1415 src.slf
09-10-2022  09:18AM       <DIR>          sl_util
EOT

  is scalar @$list, 2, "is 2";
  ok $list->[0][0], "src.slf";
  ok $list->[0][1], "f";
  ok $list->[1][1], "d";
};

subtest 'perms' => sub {

  my $generator = sub {
    my($filename) = @_;
    open my $fh, '<', $filename or die "unable to open $filename $!";
    my $line = 0;
    return sub {
      while(my $text = <$fh>)
      {
        next if $text =~ /^#/;
        chomp $text;
        return ($line++, $text);
      }
      return;
    };
  };

  foreach my $type (qw( solaris ucb xpg4 gnu ))
  {
    subtest $type => sub {

      my $iter = $generator->("corpus/perms-$type.txt");

      while(my($expected, $line) = $iter->())
      {
        # This version of `ls' does not show whether the sticky bit (file mode bit
        # 01000 ) is set, so remove it from the expected output.
        $expected &= 06777 unless $type eq 'gnu';

        # Information text.
        my $text = sprintf('"%s" -> "%05o"', $line, $expected);

        # Get output and keep only permission (no file type info).
        my $got = File::Listing::file_mode($line);
        $got &= 07777;
        cmp_ok($got, '==', $expected, $text);
      }
    };
  };

};

subtest 'win32-openssh' => sub {
  my $txt = do {
    open my $fh, '<', "corpus/win32-openssh.txt";
    local $/;
    <$fh>;
  };

  my %actual = map { $_->[0] => $_ } parse_dir($txt, undef);

  subtest 'dir' => sub {
    is $actual{'.ssh'}->[0], '.ssh';
    is $actual{'.ssh'}->[1], 'd';
    like $actual{'.ssh'}->[3], qr/^[0-9]+$/;
    is $actual{'.ssh'}->[4], '16832';
  };
  subtest 'file' => sub {
    is $actual{'.bash_history'}->[0], '.bash_history';
    is $actual{'.bash_history'}->[1], 'f';
    is $actual{'.bash_history'}->[2], '2090';
    like $actual{'.bash_history'}->[3], qr/^[0-9]+$/;
    is $actual{'.bash_history'}->[4], '33152';
  };
};

done_testing;