2 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <sys/types.h>
25 #include <lib/errtest.h>
31 /*---------------------------------------------------------------------------
32 Automated test of the DMAPI functions:
39 test_dmattr [-v] [-n num] [-l length] [-s sid] directory
43 is the path to a specific copy of ls, important only for its size
45 is the pathname to a DMAPI filesystem
47 is the number of files to create for the test.
49 is the length of the attribute value for the test.
51 is the session ID whose attributes you are interested in.
53 ----------------------------------------------------------------------------*/
55 #define VALUE_LENGTH 22
56 #define NUM_ITERATIONS 50
58 extern char *sys_errlist[];
69 fprintf(stderr, "usage:\t%s [-v] [-n number] [-l length] "
70 "[-s sid] ls_path pathname\n", Progname);
80 dm_sessid_t sid = DM_NO_SESSION;
83 dm_attrname_t *attrnamep;
84 size_t buflen=VALUE_LENGTH;
95 int num_iter = NUM_ITERATIONS;
99 dm_size_t config_retval;
100 dm_token_t test_token;
101 struct stat *statbuf;
102 struct stat *checkbuf;
104 if (Progname = strrchr(argv[0], '/')) {
110 /* Crack and validate the command line options. */
112 while ((opt = getopt(argc, argv, "vn:l:s:")) != EOF) {
118 num_iter = atoi(optarg);
121 buflen = atoi(optarg);
130 if (optind + 2 != argc)
132 ls_path = argv[optind];
133 dir_name = argv[optind+1];
136 (char *)(malloc (buflen * sizeof(char)));
138 (struct stat *)(malloc (num_iter * sizeof(struct stat)));
140 (struct stat *)(malloc (num_iter * sizeof(struct stat)));
142 (char **)(malloc (num_iter * sizeof(char *)));
143 if (bufp==NULL || test_array==NULL ||
144 statbuf==NULL || checkbuf==NULL) {
145 printf("Malloc failed\n");
148 for (i=0; i<num_iter; i++) {
150 (char*)(malloc (buflen * sizeof(char)));
151 if (test_array[i] == NULL) {
152 printf("Malloc failed\n");
157 if (dm_init_service(&name) == -1) {
158 fprintf(stderr, "Can't initialize the DMAPI\n");
161 if (sid == DM_NO_SESSION)
162 find_test_session(&sid);
164 printf("Attribute tests beginning...\n");
166 attrnamep = (dm_attrname_t *)("DMATTR");
168 /* File creation loop*/
169 for (i=0; i < num_iter; i++) {
170 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
172 sprintf(command, "cp %s %s \n", ls_path, test_file);
177 for (i=0; i < num_iter; i++) {
178 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
181 if (stat(test_file, &(statbuf[i]))){
183 "Error: unable to stat the test file; %s (before set)\n",
186 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
187 fprintf(stderr, "can't get handle for %s; bypassing test\n",
191 for (j=0; j < VALUE_LENGTH; j++) {
192 test_array[i][j]=(char)(rand()/128);;
194 /* buflen is already set (to VALUE_LENGTH) */
195 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep,
196 (i<num_iter/2)?0:1, buflen, test_array[i])) {
197 fprintf(stderr, "dm_set_dmattr failed on test %d, %s\n",
202 printf("Report: success with set #%d.\n", i);
209 for (i=0; i < num_iter; i++) {
210 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
213 if (stat(test_file, &(checkbuf[i]))){
215 "Error: unable to stat the test file; %s (before get)\n",
218 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
219 fprintf(stderr, "can't get handle for %s; bypassing test\n",
223 if (dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, buflen,
225 if (errno == E2BIG) {
226 fprintf(stderr, "dm_get_dmattr buffer too small, "
227 "should be %d bytes\n", rlenp);
229 fprintf(stderr, "dm_get_dmattr failed (%s) for test file %d\n",
234 /* Compare bufp with test_array[i]: */
235 if (strncmp(test_array[i], bufp, buflen)){
236 printf("ERROR: failure on get test #%d.\n", i);
239 printf("Report: success with get #%d. "
240 "(output matches expectation)\n",i);
246 /* It's time for timestamp checking! */
247 for (i=0; i < num_iter; i++) {
249 if ((statbuf[i].st_atime == checkbuf[i].st_atime) &&
250 (statbuf[i].st_mtime == checkbuf[i].st_mtime) &&
251 (statbuf[i].st_ctime == checkbuf[i].st_ctime))
253 if ((statbuf[i].st_atim.tv_sec == checkbuf[i].st_atim.tv_sec) &&
254 (statbuf[i].st_atim.tv_nsec == checkbuf[i].st_atim.tv_nsec) &&
255 (statbuf[i].st_mtim.tv_sec == checkbuf[i].st_mtim.tv_sec) &&
256 (statbuf[i].st_mtim.tv_nsec == checkbuf[i].st_mtim.tv_nsec) &&
257 (statbuf[i].st_ctim.tv_sec == checkbuf[i].st_ctim.tv_sec) &&
258 (statbuf[i].st_ctim.tv_nsec == checkbuf[i].st_ctim.tv_nsec))
261 if (i < num_iter/2) {
262 /* Time stamp did not change, correctly */
264 fprintf(stdout, "Report: Time stamp was correctly "
265 "unchanged by test %d.\n", i);
269 /* Time stamp did not change, but should have */
270 fprintf(stdout, "Error: the time stamp should have "
271 "changed in test file %d\n", i);
275 /* Time stamp changed, but should not have. */
276 if (i < num_iter/2) {
277 fprintf(stdout, "Error: the time stamp should not"
278 "change in test file %d\n", i);
281 /* Time stamp changed, and should have. */
283 fprintf(stdout, "Report: Time stamp was correctly "
284 "changed by test %d.\n", i);
292 for (i=0; i < num_iter; i++) {
293 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
296 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
297 fprintf(stderr, "can't get handle for %s; bypassing test\n",
301 if (dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN, setdtime,
303 fprintf(stderr, "dm_remove_dmattr failed (%s) on test #%d\n",
308 printf("Report: success with remove test #%d.\n",i);
314 for (i=0; i < num_iter; i++) {
315 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
317 sprintf(command, "rm %s \n", test_file);
321 /*************************************\
322 |* Correct-input testing complete. *|
323 |* Beginning improper-input testing. *|
324 \*************************************/
325 sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO",
327 sprintf(command, "cp %s %s\n", ls_path, test_file);
330 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
331 fprintf(stderr, "can't get handle for %s; bypassing errno tests\n",
336 printf("\t(errno subtests beginning...)\n");
337 /**** SET tests ****/
338 /*---------------------------------------------------------*/
339 dm_get_config(hanp, hlen, DM_CONFIG_MAX_ATTRIBUTE_SIZE,
344 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
345 attrnamep, setdtime, (config_retval+1),
347 /*---------------------------------------------------------*/
348 EXCLTEST("set", hanp, hlen, test_token,
349 dm_set_dmattr(sid, hanp, hlen, test_token,
350 attrnamep, 0, buflen, "no right"))
351 /*---------------------------------------------------------*/
353 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
354 fprintf(stderr, "Cannot create a test handle (%s); "
355 "skipping EBADF test\n", ERR_NAME);
358 ((char *) test_vp)[hlen/2]++;
361 dm_set_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
362 attrnamep, 0, buflen, "EBADF"))
363 dm_handle_free(test_vp, hlen);
366 /*---------------------------------------------------------*/
369 dm_set_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
370 attrnamep, 0, buflen, "EBADF"))
371 /*---------------------------------------------------------*/
374 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
375 (dm_attrname_t*)(-1000), 0,
376 buflen, "EFAULT_test" ))
379 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
380 attrnamep, 0, buflen, (void*)(-1000)))
381 /*---------------------------------------------------------*/
384 dm_set_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
385 attrnamep, 0, buflen,
387 /*---------------------------------------------------------*/
389 "set (bad session id)",
390 dm_set_dmattr((dm_sessid_t)(-1000), hanp, hlen,
391 DM_NO_TOKEN, attrnamep, 0, buflen,
392 "EINVAL_bad_session_id"))
394 /**** GET tests ****/
395 /*---------------------------------------------------------*/
396 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
397 attrnamep, 0, buflen,
398 "ERRNO for GET_DMATTR");
399 /*---------------------------------------------------------*/
402 dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
403 attrnamep, 0, bufp, &rlenp))
404 /*---------------------------------------------------------*/
405 SHAREDTEST("get", hanp, hlen, test_token,
406 dm_get_dmattr(sid, hanp, hlen, test_token,
407 attrnamep, buflen, bufp, &rlenp))
408 /*---------------------------------------------------------*/
410 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
411 fprintf(stderr, "Cannot create a test handle (%s); "
412 "skipping EBADF test\n", ERR_NAME);
415 ((char *) test_vp)[hlen/2]++;
418 dm_get_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
419 attrnamep, buflen, bufp, &rlenp))
420 dm_handle_free(test_vp, hlen);
423 /*---------------------------------------------------------*/
426 dm_get_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
427 attrnamep, buflen, bufp, &rlenp))
428 /*---------------------------------------------------------*/
430 "get (invalid session)",
431 dm_get_dmattr((dm_sessid_t)(-1000), hanp, hlen, DM_NO_TOKEN,
432 attrnamep, buflen, bufp, &rlenp))
433 /*---------------------------------------------------------*/
435 "get (invalid token)",
436 dm_get_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
437 attrnamep, buflen, bufp, &rlenp))
438 /*---------------------------------------------------------*/
441 dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
442 (dm_attrname_t *)("NO_SUCH_ENTRY"),
443 buflen, bufp, &rlenp))
444 /*---------------------------------------------------------*/
446 /**** REMOVE tests ****/
447 /*---------------------------------------------------------*/
448 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
449 attrnamep, 0, buflen,
451 EXCLTEST("remove", hanp, hlen, test_token,
452 dm_remove_dmattr(sid, hanp, hlen, test_token,
454 /*---------------------------------------------------------*/
456 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep,
457 0, buflen, "ERRNO for DMATTR")) {
458 printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
461 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
462 fprintf(stderr, "Cannot create a test handle (%s); "
463 "skipping EBADF test\n", ERR_NAME);
466 ((char *) test_vp)[hlen/2]++;
469 dm_remove_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
471 dm_handle_free(test_vp, hlen);
474 /*---------------------------------------------------------*/
475 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, 0,
476 buflen, "ERRNO for DMATTR")) {
477 printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
483 dm_remove_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
486 /*---------------------------------------------------------*/
489 dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
491 /*---------------------------------------------------------*/
493 "remove (bad token)",
494 dm_remove_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
496 /*---------------------------------------------------------*/
498 "remove (bad session)",
499 dm_remove_dmattr(-1, hanp, hlen, DM_NO_TOKEN,
501 /*---------------------------------------------------------*/
504 sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO",
506 sprintf(command, "rm %s\n", test_file);
508 printf("\t(errno subtests complete)\n");
510 /**********************************\
511 |* End of improper-input testing. *|
512 \**********************************/
515 printf("Attribute tests complete!\n");
517 dm_handle_free(hanp, hlen);