]>
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";
32 my_kr
= vm_allocate((vm_map_t
) mach_task_self(), (vm_address_t
*)&my_pathp
, PATH_MAX
, VM_FLAGS_ANYWHERE
);
33 if(my_kr
!= KERN_SUCCESS
){
34 printf( "vm_allocate failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
35 goto test_failed_exit
;
39 strcat( my_pathp
, &g_target_path
[0] );
40 strcat( my_pathp
, "/" );
42 /* create a test file */
43 my_err
= create_random_name( my_pathp
, 1 );
45 goto test_failed_exit
;
48 /* use setxattr to add an attribute to our test file */
49 my_err
= setxattr( my_pathp
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
51 printf( "setxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
52 goto test_failed_exit
;
55 /* make sure it is there using listxattr and getxattr */
56 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
58 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
59 goto test_failed_exit
;
61 if ( my_result
!= (strlen( XATTR_TEST_NAME
) + 1) ) {
62 printf( "listxattr did not get the attribute name length \n" );
63 goto test_failed_exit
;
66 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
67 my_result
= getxattr( my_pathp
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
69 printf( "getxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
70 goto test_failed_exit
;
72 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
73 strcmp( &my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
74 printf( "getxattr did not get the correct attribute data \n" );
75 goto test_failed_exit
;
78 /* use removexattr to remove an attribute to our test file */
79 my_err
= removexattr( my_pathp
, XATTR_TEST_NAME
, 0 );
81 printf( "removexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
82 goto test_failed_exit
;
85 /* make sure it is gone */
86 my_result
= listxattr( my_pathp
, NULL
, 0, 0 );
88 printf( "listxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
89 goto test_failed_exit
;
91 if ( my_result
!= 0 ) {
92 printf( "removexattr did not remove our test attribute \n" );
93 goto test_failed_exit
;
96 /* repeat tests using file descriptor versions of the xattr system calls */
97 my_fd
= open( my_pathp
, O_RDONLY
, 0 );
99 printf( "open call failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
100 printf( "\t file we attempted to open -> \"%s\" \n", my_pathp
);
101 goto test_failed_exit
;
104 /* use fsetxattr to add an attribute to our test file */
105 my_err
= fsetxattr( my_fd
, XATTR_TEST_NAME
, &my_xattr_data
[0], sizeof(my_xattr_data
), 0, 0 );
106 if ( my_err
== -1 ) {
107 printf( "fsetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
108 goto test_failed_exit
;
111 /* make sure it is there using flistxattr and fgetxattr */
112 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
113 if ( my_err
== -1 ) {
114 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
115 goto test_failed_exit
;
117 if ( my_result
!= (strlen( XATTR_TEST_NAME
) + 1) ) {
118 printf( "flistxattr did not get the attribute name length \n" );
119 goto test_failed_exit
;
122 memset( &my_buffer
[0], 0x00, sizeof( my_buffer
) );
123 my_result
= fgetxattr( my_fd
, XATTR_TEST_NAME
, &my_buffer
[0], sizeof(my_buffer
), 0, 0 );
124 if ( my_err
== -1 ) {
125 printf( "fgetxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
126 goto test_failed_exit
;
128 if ( my_result
!= (strlen( &my_xattr_data
[0] ) + 1) ||
129 strcmp( &my_buffer
[0], &my_xattr_data
[0] ) != 0 ) {
130 printf( "fgetxattr did not get the correct attribute data \n" );
131 goto test_failed_exit
;
134 /* use fremovexattr to remove an attribute to our test file */
135 my_err
= fremovexattr( my_fd
, XATTR_TEST_NAME
, 0 );
136 if ( my_err
== -1 ) {
137 printf( "fremovexattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
138 goto test_failed_exit
;
141 /* make sure it is gone */
142 my_result
= flistxattr( my_fd
, NULL
, 0, 0 );
143 if ( my_err
== -1 ) {
144 printf( "flistxattr failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
145 goto test_failed_exit
;
147 if ( my_result
!= 0 ) {
148 printf( "fremovexattr did not remove our test attribute \n" );
149 goto test_failed_exit
;
153 goto test_passed_exit
;
161 if ( my_pathp
!= NULL
) {
163 vm_deallocate(mach_task_self(), (vm_address_t
)my_pathp
, PATH_MAX
);