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# change 'tests => 2' to 'tests => last_test_to_print';
9
10# XXX: Refactor the common bits between this and 11basic.t
11# into Test::Class classes?
12
13use strict;
14use Test::More;
15
16BEGIN {
17  my $tlib = $0;
18  $tlib =~ s|/[^/]*$|/lib|;
19  push(@INC, $tlib);
20}
21use t::Support;
22
23if (t::Support::should_skip()) {
24  plan skip_all => 'Tests unsupported on this OS/filesystem';
25} else {
26  plan tests => 24;
27}
28
29use File::Temp qw(tempfile);
30use File::Path;
31use File::ExtAttr qw(setfattr getfattr delfattr);
32use IO::File;
33
34my $TESTDIR = ($ENV{ATTR_TEST_DIR} || '.');
35my ($fh, $filename) = tempfile( DIR => $TESTDIR );
36close $fh or die "can't close $filename $!";
37
38# Create a directory.
39my $dirname = "$filename.dir";
40eval { mkpath($dirname); };
41if ($@) {
42    warn "Couldn't create $dirname: $@";
43}
44
45#todo: try wierd characters in here?
46#     try unicode?
47my $key = "alskdfjadf2340zsdflksjdfa09eralsdkfjaldkjsldkfj";
48my $longval = 'A' x $File::ExtAttr::MAX_INITIAL_VALUELEN;
49my $longval2 = 'A' x ($File::ExtAttr::MAX_INITIAL_VALUELEN + 11);
50
51##########################
52#  Filename-based tests  #
53##########################
54
55foreach ( $filename, $dirname ) {
56    print "# using $_\n";
57
58#for (1..30000) { #checking memory leaks
59   #check a really big one, bigger than $File::ExtAttr::MAX_INITIAL_VALUELEN
60   #Hmmm, 3991 is the biggest number that doesn't generate "no space left on device"
61   #on my /var partition, and 920 is the biggest for my loopback partition.
62   #What's up with that?
63   #setfattr($_, "$key-2", ('x' x 3991)) or die "setfattr failed on $_: $!"; 
64    setfattr($_, "$key", $longval, { namespace => 'user' })
65        or die "setfattr failed on $_: $!"; 
66
67    #set it
68    is (setfattr($_, "$key", $longval, { namespace => 'user' }), 1);
69
70    #read it back
71    is (getfattr($_, "$key", { namespace => 'user' }), $longval);
72
73    #delete it
74    ok (delfattr($_, "$key", { namespace => 'user' }));
75
76    #check that it's gone
77    is (getfattr($_, "$key", { namespace => 'user' }), undef);
78
79    #set it
80    is (setfattr($_, "$key", $longval2, { namespace => 'user' }), 1);
81
82    #read it back
83    is (getfattr($_, "$key", { namespace => 'user' }), $longval2);
84
85    #delete it
86    ok (delfattr($_, "$key", { namespace => 'user' }));
87
88    #check that it's gone
89    is (getfattr($_, "$key", { namespace => 'user' }), undef);
90#}
91}
92
93##########################
94# IO::Handle-based tests #
95##########################
96
97$fh = new IO::File("<$filename") or die "Unable to open $filename";
98
99print "# using file descriptor ".$fh->fileno()."\n";
100
101#for (1..30000) { #checking memory leaks
102   #check a really big one, bigger than $File::ExtAttr::MAX_INITIAL_VALUELEN
103   #Hmmm, 3991 is the biggest number that doesn't generate "no space left on device"
104   #on my /var partition, and 920 is the biggest for my loopback partition.
105   #What's up with that?
106   #setfattr($filename, "$key-2", ('x' x 3991)) or die "setfattr failed on $filename: $!"; 
107   setfattr($fh, "$key", $longval, { namespace => 'user' })
108    or die "setfattr failed on file descriptor ".$fh->fileno().": $!"; 
109
110   #set it
111   is (setfattr($fh, "$key", $longval, { namespace => 'user' }), 1);
112
113   #read it back
114   is (getfattr($fh, "$key", { namespace => 'user' }), $longval);
115
116   #delete it
117   ok (delfattr($fh, "$key", { namespace => 'user' }));
118
119   #check that it's gone
120   is (getfattr($fh, "$key", { namespace => 'user' }), undef);
121
122   #set it
123   is (setfattr($fh, "$key", $longval2, { namespace => 'user' }), 1);
124
125   #read it back
126   is (getfattr($fh, "$key", { namespace => 'user' }), $longval2);
127
128   #delete it
129   ok (delfattr($fh, "$key", { namespace => 'user' }));
130
131   #check that it's gone
132   is (getfattr($fh, "$key", { namespace => 'user' }), undef);
133#}
134#print STDERR "done\n";
135#<STDIN>;
136
137END {
138    unlink $filename if $filename;
139    rmdir $dirname if $dirname;
140};
141