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>
12#include <mach/mach.h>
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";
30	kern_return_t   my_kr;
31	int xattr_len = 0;
32
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;
37	}
38
39	*my_pathp = 0x00;
40	strcat( my_pathp, &g_target_path[0] );
41	strcat( my_pathp, "/" );
42
43	/* create a test file */
44	my_err = create_random_name( my_pathp, 1 );
45	if ( my_err != 0 ) {
46		goto test_failed_exit;
47	}
48
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 );
51	if ( my_err == -1 ) {
52		printf( "setxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
53		goto test_failed_exit;
54	}
55
56	/* make sure it is there using listxattr and getxattr */
57	my_result = listxattr( my_pathp, NULL, 0, 0 );
58	if ( my_err == -1 ) {
59		printf( "listxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
60		goto test_failed_exit;
61	}
62
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;
66	}
67
68	memset( &my_buffer[0], 0x00, sizeof( my_buffer ) );
69
70	my_result = getxattr( my_pathp, XATTR_TEST_NAME, &my_buffer[0], sizeof(my_buffer), 0, 0 );
71	if ( my_err == -1 ) {
72		printf( "getxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
73		goto test_failed_exit;
74	}
75
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;
80	}
81
82	/* use removexattr to remove an attribute to our test file */
83	my_err = removexattr( my_pathp, XATTR_TEST_NAME, 0 );
84	if ( my_err == -1 ) {
85		printf( "removexattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
86		goto test_failed_exit;
87	}
88
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;
94	}
95
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;
101	}
102
103
104
105	/* repeat tests using file descriptor versions of the xattr system calls */
106	my_fd = open( my_pathp, O_RDONLY, 0 );
107	if ( my_fd == -1 ) {
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;
111	}
112
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;
118	}
119
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;
125	}
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;
129	}
130
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;
136	}
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;
141	}
142
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;
148	}
149
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;
155	}
156
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;
162	}
163
164	my_err = 0;
165	goto test_passed_exit;
166
167test_failed_exit:
168	my_err = -1;
169
170test_passed_exit:
171	if ( my_fd != -1 )
172		close( my_fd );
173	if ( my_pathp != NULL ) {
174		remove( my_pathp );
175		vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
176	}
177	return( my_err );
178}
179
180