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
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 => 15;
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    #create it
56    is (setfattr($_, "$key", $val, { create => 1 }), 1);
57
58    #replace it
59    is (setfattr($_, "$key", $val, { replace => 1 }), 1);
60
61    #read it back
62    is (getfattr($_, "$key"), $val);
63
64    #delete it
65    ok (delfattr($_, "$key"));
66
67    #check that it's gone
68    is (getfattr($_, "$key"), undef);
69}
70
71##########################
72# IO::Handle-based tests #
73##########################
74
75$fh = new IO::File("<$filename") or die "Unable to open $filename";
76
77print "# using file descriptor ".$fh->fileno()."\n";
78
79#create it
80is (setfattr($fh, "$key", $val, { create => 1 }), 1);
81
82#replace it
83is (setfattr($fh, "$key", $val, { replace => 1 }), 1);
84
85#read it back
86is (getfattr($fh, "$key"), $val);
87
88#delete it
89ok (delfattr($fh, "$key"));
90
91#check that it's gone
92is (getfattr($fh, "$key"), undef);
93
94END {
95    unlink $filename if $filename;
96    rmdir $dirname if $dirname;
97};
98