]>
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>
12 #include <mach/mach.h>
14 extern char g_target_path
[ PATH_MAX
];
16 #define XATTR_TEST_NAME "com.apple.xattr_test"
18 /* **************************************************************************************************************
19 * Test xattr system calls.
20 * **************************************************************************************************************
22 int xattr_tests( void * the_argp
)
26 char * my_pathp
= NULL
;
29 char my_xattr_data
[ ] = "xattr_foo";
33 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
34 if(my_kr
!= KERN_SUCCESS
){
35 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
36 goto test_failed_exit
;
40 strcat( my_pathp
, &g_target_path
[0] );
41 strcat( my_pathp
, "/" );
43 /* create a test file */
44 my_err
= create_random_name( my_pathp
, 1 );
46 goto test_failed_exit
;
49 /* use setxattr to add an attribute to our test file */
50 my_err
= setxattr( my_pathp
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
52 printf( "setxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
53 goto test_failed_exit
;
56 /* make sure it is there using listxattr and getxattr */
57 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
59 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
60 goto test_failed_exit
;
63 if ( my_result
< (strlen( XATTR_TEST_NAME
) + 1) ) {
64 printf( "listxattr did not get the attribute name length: my_result %d, strlen %zu \n", my_result
, (strlen(XATTR_TEST_NAME
)+1) );
65 goto test_failed_exit
;
68 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
70 my_result
= getxattr( my_pathp
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
72 printf( "getxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
73 goto test_failed_exit
;
76 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
77 strcmp(&my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
78 printf( "getxattr did not get the correct attribute data \n" );
79 goto test_failed_exit
;
82 /* use removexattr to remove an attribute to our test file */
83 my_err
= removexattr( my_pathp
, XATTR_TEST_NAME
, 0 );
85 printf( "removexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
86 goto test_failed_exit
;
89 /* make sure it is gone */
90 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
91 if ( my_result
== -1 ) {
92 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
93 goto test_failed_exit
;
96 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
97 my_result
= getxattr( my_pathp
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
98 if ( my_result
!= -1 && errno
!= ENOATTR
) {
99 printf( "getxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
100 goto test_failed_exit
;
105 /* repeat tests using file descriptor versions of the xattr system calls */
106 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
108 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
109 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
110 goto test_failed_exit
;
113 /* use fsetxattr to add an attribute to our test file */
114 my_err
= fsetxattr( my_fd
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
115 if ( my_err
== -1 ) {
116 printf( "fsetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
117 goto test_failed_exit
;
120 /* make sure it is there using flistxattr and fgetxattr */
121 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
122 if ( my_err
== -1 ) {
123 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
124 goto test_failed_exit
;
126 if ( my_result
< (strlen( XATTR_TEST_NAME
) + 1) ) {
127 printf( "flistxattr did not get the attribute name length \n" );
128 goto test_failed_exit
;
131 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
132 my_result
= fgetxattr( my_fd
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
133 if ( my_err
== -1 ) {
134 printf( "fgetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
135 goto test_failed_exit
;
137 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
138 strcmp( &my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
139 printf( "fgetxattr did not get the correct attribute data \n" );
140 goto test_failed_exit
;
143 /* use fremovexattr to remove an attribute to our test file */
144 my_err
= fremovexattr( my_fd
, XATTR_TEST_NAME
, 0 );
145 if ( my_err
== -1 ) {
146 printf( "fremovexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
147 goto test_failed_exit
;
150 /* make sure it is gone */
151 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
152 if ( my_result
== -1 ) {
153 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
154 goto test_failed_exit
;
157 memset( my_buffer
, 0x00, sizeof( my_buffer
) );
158 my_result
= fgetxattr( my_fd
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
159 if ( my_result
== -1 && (errno
!= ENOATTR
) ) {
160 printf( "fgetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
161 goto test_failed_exit
;
165 goto test_passed_exit
;
173 if ( my_pathp
!= NULL
) {
175 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);