File: stat-helpers.t

package info (click to toggle)
liboverload-filecheck-perl 0.013-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,088 kB
  • sloc: perl: 2,030; ansic: 97; makefile: 8
file content (103 lines) | stat: -rw-r--r-- 2,916 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
#!/usr/bin/perl -w

# Copyright (c) 2018, cPanel, LLC.
# All rights reserved.
# http://cpanel.net
#
# This is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself. See L<perlartistic>.

use strict;
use warnings;

use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;

use Overload::FileCheck q{:stat};

use Fcntl (
    '_S_IFMT',     # bit mask for the file type bit field
                   #'S_IFPERMS',   # bit mask for file perms.
    'S_IFSOCK',    # socket
    'S_IFLNK',     # symbolic link
    'S_IFREG',     # regular file
    'S_IFBLK',     # block device
    'S_IFDIR',     # directory
    'S_IFCHR',     # character device
    'S_IFIFO',     # FIFO
);

is stat_as_directory(), [ 0, 0, S_IFDIR,  (0) x 10 ], 'stat_as_directory';
is stat_as_file(),      [ 0, 0, S_IFREG,  (0) x 10 ], 'stat_as_file';
is stat_as_symlink(),   [ 0, 0, S_IFLNK,  (0) x 10 ], 'stat_as_symlink';
is stat_as_socket(),    [ 0, 0, S_IFSOCK, (0) x 10 ], 'stat_as_socket';
is stat_as_chr(),       [ 0, 0, S_IFCHR,  (0) x 10 ], 'stat_as_chr';
is stat_as_block(),     [ 0, 0, S_IFBLK,  (0) x 10 ], 'stat_as_block';

if ( $> == 0 ) {
    my ($username, $gid) = (getpwuid $>)[0, 3];
    my $groupname = getgrgid $gid;

    diag "Running privileged ($username/$groupname)";

    my $got = stat_as_file( uid => $username, gid => $groupname );

    is(
        $got,
        [ 0, 0, S_IFREG, (0) x 10 ],
        "stat_as_file( uid => $username, gid => $groupname )",
        explain $got,
    );
}

SKIP: {
    my $daemon_uid = getpwnam('daemon');
    skip "daemon uid unknown" unless defined $daemon_uid;

    my $wheel_gid  = getgrnam('wheel');
    skip "wheel gid unknown" unless defined $wheel_gid;

    if ( $daemon_uid && $wheel_gid ) {
        is stat_as_file( uid => 'daemon', gid => 'wheel' ),
          [ 0, 0, S_IFREG, 0, int $daemon_uid, int $wheel_gid, (0) x 7 ],
          'stat_as_file( uid => daemon, gid => wheel )';
    }

}

is stat_as_file( uid => 98765, gid => 1234 ),
  [ 0, 0, S_IFREG, 0, 98765, 1234, (0) x 7 ],
  'stat_as_file( uid => 98765, gid => 1234 )';

my @regular_file = ( 0, 0, S_IFREG, (0) x 10 );

my $now = time();
my $expect;

$expect = [@regular_file];
$expect->[7] = 1234;

is stat_as_file( size => 1234 ), $expect, 'size';

$expect = [@regular_file];
$expect->[8] = $now;
is stat_as_file( atime => $now ), $expect, 'atime';

$expect = [@regular_file];
$expect->[9] = $now;
is stat_as_file( mtime => $now ), $expect, 'mtime';

$expect = [@regular_file];
$expect->[10] = $now;
is stat_as_file( ctime => $now ), $expect, 'ctime';

$expect       = [@regular_file];
$expect->[8]  = 8;
$expect->[9]  = 9;
$expect->[10] = 10;
is stat_as_file( atime => 8, mtime => 9, ctime => 10 ), $expect, 'atime + mtime + ctime';

is stat_as_file( perms => 0755 ), [ 0, 0, S_IFREG | 0755, (0) x 10 ], 'stat_as_file with perms 0755';

done_testing;