1#!perl -w
2
3use strict;
4use Test::More;
5
6BEGIN {
7  my $tlib = $0;
8  $tlib =~ s|/[^/]*$|/lib|;
9  push(@INC, $tlib);
10}
11use t::Support;
12
13if (t::Support::should_skip()) {
14  plan skip_all => 'Tests unsupported on this OS/filesystem';
15} else {
16  plan tests => 213;
17}
18
19use File::Temp qw(tempfile);
20use File::Path;
21use File::ExtAttr qw(setfattr getfattr delfattr listfattr);
22use IO::File;
23
24my $TESTDIR = ($ENV{ATTR_TEST_DIR} || '.');
25my ($fh, $filename) = tempfile( DIR => $TESTDIR );
26
27close $fh or die "can't close $filename $!";
28
29# Create a directory.
30my $dirname = "$filename.dir";
31eval { mkpath($dirname); };
32if ($@) {
33    warn "Couldn't create $dirname: $@";
34}
35
36my %vals;
37for (my $i = 0; $i < 10; ++$i)
38{
39    $vals{"key$i"} = "val$i";
40}
41
42##########################
43#  Filename-based tests  #
44##########################
45
46foreach ( $filename, $dirname ) {
47    print "# using $_\n";
48
49    foreach my $k (keys %vals)
50    {
51        # create it
52        is (setfattr($_, $k, $vals{$k}, { create => 1 }), 1);
53
54        # create it again -- should fail
55        my $ret = setfattr($_, $k, $vals{$k}, { create => 1 });
56        my $err = int $!;
57        is ($ret, 0);
58        is ($err, $!{EEXIST});
59
60        # read it back
61        is (getfattr($_, $k), $vals{$k});
62    }
63    
64    # Check that the list contains all the attributes.
65    my @attrs = listfattr($_);
66    @attrs = sort(t::Support::filter_system_attrs(@attrs));
67    my @ks = sort keys %vals;
68
69    check_attrs(\@attrs, \@ks);
70
71    # Clean up for next round of testing
72    foreach my $k (keys %vals)
73    {
74        # delete it
75        ok (delfattr($_, $k));
76
77        # check that it's gone
78        is (getfattr($_, $k), undef);
79    }
80}
81
82##########################
83# IO::Handle-based tests #
84##########################
85
86$fh = new IO::File("<$filename") or die "Unable to open $filename";
87
88print "# using file descriptor ".$fh->fileno()."\n";
89
90foreach (keys %vals)
91{
92    # create it
93    is (setfattr($fh, $_, $vals{$_}, { create => 1 }), 1);
94
95    # create it again -- should fail
96    my $ret = setfattr($fh, $_, $vals{$_}, { create => 1 });
97    my $err = int $!;
98    is ($ret, 0);
99    is ($err, $!{EEXIST});
100
101    # read it back
102    is (getfattr($fh, $_), $vals{$_});
103}
104
105# Check that the list contains all the attributes.
106my @attrs = listfattr($fh);
107@attrs = sort(t::Support::filter_system_attrs(@attrs));
108my @ks = sort keys %vals;
109
110check_attrs(\@attrs, \@ks);
111
112# Clean up for next round of testing
113foreach (keys %vals)
114{
115    # delete it
116    ok (delfattr($filename, $_));
117
118    # check that it's gone
119    is (getfattr($filename, $_), undef);
120}
121
122END {
123    unlink $filename if $filename;
124    rmdir $dirname if $dirname;
125};
126
127sub check_attrs
128{
129    my @attrs = @{ $_[0] };
130    my @ks = @{ $_[1] };
131
132    is(scalar @attrs, scalar @ks);
133    for (my $i = 0; $i < scalar @attrs; ++$i)
134    {
135        is($attrs[$i], $ks[$i]);
136    }
137}
138