1#!perl -w
2
3# Before `make install' is performed this script should be runnable with
4# `make test'. After `make install' it should work as `perl Linux-xattr.t'
5
6##########################
7
8# Test an explicitly empty namespace
9
10use strict;
11use Test::More;
12
13BEGIN {
14  my $tlib = $0;
15  $tlib =~ s|/[^/]*$|/lib|;
16  push(@INC, $tlib);
17}
18use t::Support;
19
20if (t::Support::should_skip()) {
21  plan skip_all => 'Tests unsupported on this OS/filesystem';
22} else {
23  plan tests => 18;
24}
25
26use File::Temp qw(tempfile);
27use File::Path;
28use File::ExtAttr qw(setfattr getfattr delfattr);
29use IO::File;
30
31my $TESTDIR = ($ENV{ATTR_TEST_DIR} || '.');
32my ($fh, $filename) = tempfile( DIR => $TESTDIR );
33
34close $fh or die "can't close $filename $!";
35
36# Create a directory.
37my $dirname = "$filename.dir";
38eval { mkpath($dirname); };
39if ($@) {
40    warn "Couldn't create $dirname: $@";
41}
42
43#todo: try wierd characters in here?
44#     try unicode?
45my $key = "alskdfjadf2340zsdflksjdfa09eralsdkfjaldkjsldkfj";
46my $val = "ZZZadlf03948alsdjfaslfjaoweir12l34kealfkjalskdfas90d8fajdlfkj./.,f";
47
48##########################
49#  Filename-based tests  #
50##########################
51
52foreach ( $filename, $dirname ) {
53    print "# using $_\n";
54
55    #set it - should fail
56    my $ret = setfattr($_, "$key", $val, { namespace => '' });
57    my $err = int $!;
58    is ($ret, 0);
59    is ($err, $!{EOPNOTSUPP});
60
61    #read it back - should be missing
62    is (getfattr($_, "$key", { namespace => '' }), undef);
63
64    #delete it - should fail
65    $ret = delfattr($_, "$key", { namespace => '' });
66    $err = int $!;
67    is ($ret, 0);
68    is ($err, $!{EOPNOTSUPP});
69
70    #check that it's gone
71    is (getfattr($_, "$key", { namespace => '' }), undef);
72}
73
74##########################
75# IO::Handle-based tests #
76##########################
77
78$fh = new IO::File("<$filename") or die "Unable to open $filename";
79
80print "# using file descriptor ".$fh->fileno()."\n";
81
82my $ret = setfattr($fh, "$key", $val, { namespace => '' });
83my $err = int $!;
84is ($ret, 0);
85is ($err, $!{EOPNOTSUPP});
86
87#read it back - should be missing
88is (getfattr($fh, "$key", { namespace => '' }), undef);
89
90#delete it - should fail
91$ret = delfattr($fh, "$key", { namespace => '' });
92$err = int $!;
93is ($ret, 0);
94is ($err, $!{EOPNOTSUPP});
95
96#check that it's gone
97is (getfattr($fh, "$key", { namespace => '' }), undef);
98
99END {
100    unlink $filename if $filename;
101    rmdir $dirname if $dirname;
102};
103