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
13extern char  g_target_path[ PATH_MAX ];
14
15#define XATTR_TEST_NAME "com.apple.xattr_test"
16
17/*  **************************************************************************************************************
18 *	Test xattr system calls.
19 *  **************************************************************************************************************
20 */
21int xattr_tests( void * the_argp )
22{
23	int			my_err;
24	int			my_fd = -1;
25	char *		my_pathp = NULL;
26	ssize_t		my_result;
27	char		my_buffer[ 64 ];
28	char		my_xattr_data[ ] = "xattr_foo";
29
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;
34	}
35	*my_pathp = 0x00;
36	strcat( my_pathp, &g_target_path[0] );
37	strcat( my_pathp, "/" );
38
39	/* create a test file */
40	my_err = create_random_name( my_pathp, 1 );
41	if ( my_err != 0 ) {
42		goto test_failed_exit;
43	}
44
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 );
47	if ( my_err == -1 ) {
48		printf( "setxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
49		goto test_failed_exit;
50	}
51
52	/* make sure it is there using listxattr and getxattr */
53	my_result = listxattr( my_pathp, NULL, 0, 0 );
54	if ( my_err == -1 ) {
55		printf( "listxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
56		goto test_failed_exit;
57	}
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;
61	}
62
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 );
65	if ( my_err == -1 ) {
66		printf( "getxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
67		goto test_failed_exit;
68	}
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;
73	}
74
75	/* use removexattr to remove an attribute to our test file */
76	my_err = removexattr( my_pathp, XATTR_TEST_NAME, 0 );
77	if ( my_err == -1 ) {
78		printf( "removexattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
79		goto test_failed_exit;
80	}
81
82	/* make sure it is gone */
83	my_result = listxattr( my_pathp, NULL, 0, 0 );
84	if ( my_err == -1 ) {
85		printf( "listxattr failed with error %d - \"%s\" \n", errno, strerror( errno) );
86		goto test_failed_exit;
87	}
88	if ( my_result != 0 ) {
89		printf( "removexattr did not remove our test attribute \n" );
90		goto test_failed_exit;
91	}
92
93	/* repeat tests using file descriptor versions of the xattr system calls */
94	my_fd = open( my_pathp, O_RDONLY, 0 );
95	if ( my_fd == -1 ) {
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;
99	}
100
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;
106	}
107
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;
113	}
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;
117	}
118
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;
124	}
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;
129	}
130
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;
136	}
137
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;
143	}
144	if ( my_result != 0 ) {
145		printf( "fremovexattr did not remove our test attribute \n" );
146		goto test_failed_exit;
147	}
148
149	my_err = 0;
150	goto test_passed_exit;
151
152test_failed_exit:
153	my_err = -1;
154
155test_passed_exit:
156	if ( my_fd != -1 )
157		close( my_fd );
158	if ( my_pathp != NULL ) {
159		remove( my_pathp );
160		free( my_pathp );
161	 }
162	return( my_err );
163}
164
165