]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/xnu_quick_test/xattr_tests.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / tools / tests / xnu_quick_test / xattr_tests.c
CommitLineData
2d21ac55
A
1/*
2 * xattr_tests.c
3 * xnu_quick_test
4 *
5 * Created by Jerry Cottingham on 6/2/2005.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
7 *
8 */
9
10#include "tests.h"
11#include <sys/xattr.h>
b0d623f7 12#include <mach/mach.h>
2d21ac55
A
13
14extern char g_target_path[ PATH_MAX ];
15
16#define XATTR_TEST_NAME "com.apple.xattr_test"
17
18/* **************************************************************************************************************
19 * Test xattr system calls.
20 * **************************************************************************************************************
21 */
22int xattr_tests( void * the_argp )
23{
24 int my_err;
25 int my_fd = -1;
26 char * my_pathp = NULL;
27 ssize_t my_result;
28 char my_buffer[ 64 ];
29 char my_xattr_data[ ] = "xattr_foo";
b0d623f7
A
30 kern_return_t my_kr;
31
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;
36 }
2d21ac55 37
2d21ac55
A
38 *my_pathp = 0x00;
39 strcat( my_pathp, &g_target_path[0] );
40 strcat( my_pathp, "/" );
41
42 /* create a test file */
43 my_err = create_random_name( my_pathp, 1 );
44 if ( my_err != 0 ) {
45 goto test_failed_exit;
46 }
47
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 );
50 if ( my_err == -1 ) {
51 printf( "setxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
52 goto test_failed_exit;
53 }
54
55 /* make sure it is there using listxattr and getxattr */
56 my_result = listxattr( my_pathp, NULL, 0, 0 );
57 if ( my_err == -1 ) {
58 printf( "listxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
59 goto test_failed_exit;
60 }
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;
64 }
65
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 );
68 if ( my_err == -1 ) {
69 printf( "getxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
70 goto test_failed_exit;
71 }
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;
76 }
77
78 /* use removexattr to remove an attribute to our test file */
79 my_err = removexattr( my_pathp, XATTR_TEST_NAME, 0 );
80 if ( my_err == -1 ) {
81 printf( "removexattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
82 goto test_failed_exit;
83 }
84
85 /* make sure it is gone */
86 my_result = listxattr( my_pathp, NULL, 0, 0 );
87 if ( my_err == -1 ) {
88 printf( "listxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
89 goto test_failed_exit;
90 }
91 if ( my_result != 0 ) {
92 printf( "removexattr did not remove our test attribute \n" );
93 goto test_failed_exit;
94 }
95
96 /* repeat tests using file descriptor versions of the xattr system calls */
97 my_fd = open( my_pathp, O_RDONLY, 0 );
98 if ( my_fd == -1 ) {
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;
102 }
103
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;
109 }
110
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;
116 }
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;
120 }
121
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;
127 }
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;
132 }
133
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;
139 }
140
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;
146 }
147 if ( my_result != 0 ) {
148 printf( "fremovexattr did not remove our test attribute \n" );
149 goto test_failed_exit;
150 }
151
152 my_err = 0;
153 goto test_passed_exit;
154
155test_failed_exit:
156 my_err = -1;
157
158test_passed_exit:
159 if ( my_fd != -1 )
160 close( my_fd );
161 if ( my_pathp != NULL ) {
b0d623f7
A
162 remove( my_pathp );
163 vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
2d21ac55
A
164 }
165 return( my_err );
166}
167