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 => 40;
17}
18
19use File::Temp qw(tempfile);
20use File::Path;
21use File::ExtAttr::Tie;
22use File::ExtAttr qw(getfattr);
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
36foreach ( $filename, $dirname ) {
37    print "# using $_\n";
38
39    my %extattr;
40    my @ks;
41
42    tie %extattr, 'File::ExtAttr::Tie', $_, { namespace => 'nonuser' }; # ok()?
43
44    # Check there are no user extattrs.
45    @ks = keys(%extattr);
46    @ks = t::Support::filter_system_attrs(@ks);
47    ok(scalar(@ks) == 0);
48
49    # Test multiple attributes.
50    my %test_attrs = ( 'foo' => '123', 'bar' => '456' );
51    my $k;
52    my $err;
53
54    foreach $k (sort(keys(%test_attrs)))
55    {
56        my $v = $test_attrs{$k};
57
58        # Check that creation works.
59        $extattr{$k} = $v;
60        $err = int $!;
61        is ($err, $!{EOPNOTSUPP});
62        is(getfattr($_, "$k"), undef);
63
64        # Check that updating works.
65        $extattr{$k} = "$v$v";
66        $err = int $!;
67        is ($err, $!{EOPNOTSUPP});
68        is(getfattr($_, "$k"), undef);
69
70        $extattr{$k} = $v;
71        $err = int $!;
72        is ($err, $!{EOPNOTSUPP});
73        is(getfattr($_, "$k"), undef);
74
75        # Check that deletion works.
76        delete $extattr{$k};
77        is(getfattr($_, "$k"), undef);
78    }
79
80    # Recreate the keys and check that they're all in the hash.
81
82    foreach $k (sort(keys(%test_attrs)))
83    {
84        my $v = $test_attrs{$k};
85
86        # Check that creation works.
87        $extattr{$k} = $v;
88        $err = int $!;
89        is ($err, $!{EOPNOTSUPP});
90        is(getfattr($_, "$k"), undef);
91    }
92
93    # Check there are only our extattrs.
94    @ks = keys(%extattr);
95    @ks = t::Support::filter_system_attrs(@ks);
96    ok(scalar(@ks) == 0);
97    print '# '.join(' ', @ks)."\n";
98}
99
100END {
101    unlink $filename if $filename;
102    rmdir $dirname if $dirname;
103};
104