]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/xattr_tests.c
5 * Created by Jerry Cottingham on 6/2/2005.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
11 #include <sys/xattr.h>
13 extern char g_target_path
[ PATH_MAX
];
15 #define XATTR_TEST_NAME "com.apple.xattr_test"
17 /* **************************************************************************************************************
18 * Test xattr system calls.
19 * **************************************************************************************************************
21 int xattr_tests( void * the_argp
)
25 char * my_pathp
= NULL
;
28 char my_xattr_data
[ ] = "xattr_foo";
30 my_pathp
= (char *) malloc( PATH_MAX
);
31 if ( my_pathp
== NULL
) {
32 printf( "malloc failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
33 goto test_failed_exit
;
36 strcat( my_pathp
, &g_target_path
[0] );
37 strcat( my_pathp
, "/" );
39 /* create a test file */
40 my_err
= create_random_name( my_pathp
, 1 );
42 goto test_failed_exit
;
45 /* use setxattr to add an attribute to our test file */
46 my_err
= setxattr( my_pathp
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
48 printf( "setxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
49 goto test_failed_exit
;
52 /* make sure it is there using listxattr and getxattr */
53 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
55 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
56 goto test_failed_exit
;
58 if ( my_result
!= (strlen( XATTR_TEST_NAME
) + 1) ) {
59 printf( "listxattr did not get the attribute name length \n" );
60 goto test_failed_exit
;
63 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
64 my_result
= getxattr( my_pathp
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
66 printf( "getxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
67 goto test_failed_exit
;
69 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
70 strcmp( &my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
71 printf( "getxattr did not get the correct attribute data \n" );
72 goto test_failed_exit
;
75 /* use removexattr to remove an attribute to our test file */
76 my_err
= removexattr( my_pathp
, XATTR_TEST_NAME
, 0 );
78 printf( "removexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
79 goto test_failed_exit
;
82 /* make sure it is gone */
83 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
85 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
86 goto test_failed_exit
;
88 if ( my_result
!= 0 ) {
89 printf( "removexattr did not remove our test attribute \n" );
90 goto test_failed_exit
;
93 /* repeat tests using file descriptor versions of the xattr system calls */
94 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
96 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
97 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
98 goto test_failed_exit
;
101 /* use fsetxattr to add an attribute to our test file */
102 my_err
= fsetxattr( my_fd
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
103 if ( my_err
== -1 ) {
104 printf( "fsetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
105 goto test_failed_exit
;
108 /* make sure it is there using flistxattr and fgetxattr */
109 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
110 if ( my_err
== -1 ) {
111 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
112 goto test_failed_exit
;
114 if ( my_result
!= (strlen( XATTR_TEST_NAME
) + 1) ) {
115 printf( "flistxattr did not get the attribute name length \n" );
116 goto test_failed_exit
;
119 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
120 my_result
= fgetxattr( my_fd
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
121 if ( my_err
== -1 ) {
122 printf( "fgetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
123 goto test_failed_exit
;
125 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
126 strcmp( &my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
127 printf( "fgetxattr did not get the correct attribute data \n" );
128 goto test_failed_exit
;
131 /* use fremovexattr to remove an attribute to our test file */
132 my_err
= fremovexattr( my_fd
, XATTR_TEST_NAME
, 0 );
133 if ( my_err
== -1 ) {
134 printf( "fremovexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
135 goto test_failed_exit
;
138 /* make sure it is gone */
139 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
140 if ( my_err
== -1 ) {
141 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
142 goto test_failed_exit
;
144 if ( my_result
!= 0 ) {
145 printf( "fremovexattr did not remove our test attribute \n" );
146 goto test_failed_exit
;
150 goto test_passed_exit
;
158 if ( my_pathp
!= NULL
) {