1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
14 #include <lib/errtest.h>
20 /*---------------------------------------------------------------------------
21 Automated test of the DMAPI functions:
28 test_dmattr [-v] [-n num] [-l length] [-s sid] directory
32 is the path to a specific copy of ls, important only for its size
34 is the pathname to a DMAPI filesystem
36 is the number of files to create for the test.
38 is the length of the attribute value for the test.
40 is the session ID whose attributes you are interested in.
42 ----------------------------------------------------------------------------*/
44 #define VALUE_LENGTH 22
45 #define NUM_ITERATIONS 50
47 extern char *sys_errlist[];
58 fprintf(stderr, "usage:\t%s [-v] [-n number] [-l length] "
59 "[-s sid] ls_path pathname\n", Progname);
69 dm_sessid_t sid = DM_NO_SESSION;
72 dm_attrname_t *attrnamep;
73 size_t buflen=VALUE_LENGTH;
84 int num_iter = NUM_ITERATIONS;
88 dm_size_t config_retval;
89 dm_token_t test_token;
91 struct stat *checkbuf;
93 Progname = strrchr(argv[0], '/');
100 /* Crack and validate the command line options. */
102 while ((opt = getopt(argc, argv, "vn:l:s:")) != EOF) {
108 num_iter = atoi(optarg);
111 buflen = atoi(optarg);
120 if (optind + 2 != argc)
122 ls_path = argv[optind];
123 dir_name = argv[optind+1];
126 (char *)(malloc (buflen * sizeof(char)));
128 (struct stat *)(malloc (num_iter * sizeof(struct stat)));
130 (struct stat *)(malloc (num_iter * sizeof(struct stat)));
132 (char **)(malloc (num_iter * sizeof(char *)));
133 if (bufp==NULL || test_array==NULL ||
134 statbuf==NULL || checkbuf==NULL) {
135 printf("Malloc failed\n");
138 for (i=0; i<num_iter; i++) {
140 (char*)(malloc (buflen * sizeof(char)));
141 if (test_array[i] == NULL) {
142 printf("Malloc failed\n");
147 if (dm_init_service(&name) == -1) {
148 fprintf(stderr, "Can't initialize the DMAPI\n");
151 if (sid == DM_NO_SESSION)
152 find_test_session(&sid);
154 printf("Attribute tests beginning...\n");
156 attrnamep = (dm_attrname_t *)("DMATTR");
158 /* File creation loop*/
159 for (i=0; i < num_iter; i++) {
160 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
162 sprintf(command, "cp %s %s \n", ls_path, test_file);
167 for (i=0; i < num_iter; i++) {
168 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
171 if (stat(test_file, &(statbuf[i]))){
173 "Error: unable to stat the test file; %s (before set)\n",
176 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
177 fprintf(stderr, "can't get handle for %s; bypassing test\n",
181 for (j=0; j < VALUE_LENGTH; j++) {
182 test_array[i][j]=(char)(rand()/128);;
184 /* buflen is already set (to VALUE_LENGTH) */
185 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep,
186 (i<num_iter/2)?0:1, buflen, test_array[i])) {
187 fprintf(stderr, "dm_set_dmattr failed on test %d, %s\n",
192 printf("Report: success with set #%d.\n", i);
199 for (i=0; i < num_iter; i++) {
200 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
203 if (stat(test_file, &(checkbuf[i]))){
205 "Error: unable to stat the test file; %s (before get)\n",
208 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
209 fprintf(stderr, "can't get handle for %s; bypassing test\n",
213 if (dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, buflen,
215 if (errno == E2BIG) {
216 fprintf(stderr, "dm_get_dmattr buffer too small, "
217 "should be %zd bytes\n", rlenp);
219 fprintf(stderr, "dm_get_dmattr failed (%s) for test file %d\n",
224 /* Compare bufp with test_array[i]: */
225 if (strncmp(test_array[i], bufp, buflen)){
226 printf("ERROR: failure on get test #%d.\n", i);
229 printf("Report: success with get #%d. "
230 "(output matches expectation)\n",i);
236 /* It's time for timestamp checking! */
237 for (i=0; i < num_iter; i++) {
239 if ((statbuf[i].st_atime == checkbuf[i].st_atime) &&
240 (statbuf[i].st_mtime == checkbuf[i].st_mtime) &&
241 (statbuf[i].st_ctime == checkbuf[i].st_ctime))
243 if ((statbuf[i].st_atim.tv_sec == checkbuf[i].st_atim.tv_sec) &&
244 (statbuf[i].st_atim.tv_nsec == checkbuf[i].st_atim.tv_nsec) &&
245 (statbuf[i].st_mtim.tv_sec == checkbuf[i].st_mtim.tv_sec) &&
246 (statbuf[i].st_mtim.tv_nsec == checkbuf[i].st_mtim.tv_nsec) &&
247 (statbuf[i].st_ctim.tv_sec == checkbuf[i].st_ctim.tv_sec) &&
248 (statbuf[i].st_ctim.tv_nsec == checkbuf[i].st_ctim.tv_nsec))
251 if (i < num_iter/2) {
252 /* Time stamp did not change, correctly */
254 fprintf(stdout, "Report: Time stamp was correctly "
255 "unchanged by test %d.\n", i);
259 /* Time stamp did not change, but should have */
260 fprintf(stdout, "Error: the time stamp should have "
261 "changed in test file %d\n", i);
265 /* Time stamp changed, but should not have. */
266 if (i < num_iter/2) {
267 fprintf(stdout, "Error: the time stamp should not"
268 "change in test file %d\n", i);
271 /* Time stamp changed, and should have. */
273 fprintf(stdout, "Report: Time stamp was correctly "
274 "changed by test %d.\n", i);
282 for (i=0; i < num_iter; i++) {
283 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
286 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
287 fprintf(stderr, "can't get handle for %s; bypassing test\n",
291 if (dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN, setdtime,
293 fprintf(stderr, "dm_remove_dmattr failed (%s) on test #%d\n",
298 printf("Report: success with remove test #%d.\n",i);
304 for (i=0; i < num_iter; i++) {
305 sprintf(test_file, "%s/DMAPI_attribute_test_file.%d",
307 sprintf(command, "rm %s \n", test_file);
311 /*************************************\
312 |* Correct-input testing complete. *|
313 |* Beginning improper-input testing. *|
314 \*************************************/
315 sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO",
317 sprintf(command, "cp %s %s\n", ls_path, test_file);
320 if (dm_path_to_handle(test_file, &hanp, &hlen)) {
321 fprintf(stderr, "can't get handle for %s; bypassing errno tests\n",
326 printf("\t(errno subtests beginning...)\n");
327 /**** SET tests ****/
328 /*---------------------------------------------------------*/
329 dm_get_config(hanp, hlen, DM_CONFIG_MAX_ATTRIBUTE_SIZE,
334 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
335 attrnamep, setdtime, (config_retval+1),
337 /*---------------------------------------------------------*/
338 EXCLTEST("set", hanp, hlen, test_token,
339 dm_set_dmattr(sid, hanp, hlen, test_token,
340 attrnamep, 0, buflen, "no right"))
341 /*---------------------------------------------------------*/
343 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
344 fprintf(stderr, "Cannot create a test handle (%s); "
345 "skipping EBADF test\n", ERR_NAME);
348 ((char *) test_vp)[hlen/2]++;
351 dm_set_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
352 attrnamep, 0, buflen, "EBADF"))
353 dm_handle_free(test_vp, hlen);
356 /*---------------------------------------------------------*/
359 dm_set_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
360 attrnamep, 0, buflen, "EBADF"))
361 /*---------------------------------------------------------*/
364 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
365 (dm_attrname_t*)(-1000), 0,
366 buflen, "EFAULT_test" ))
369 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
370 attrnamep, 0, buflen, (void*)(-1000)))
371 /*---------------------------------------------------------*/
374 dm_set_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
375 attrnamep, 0, buflen,
377 /*---------------------------------------------------------*/
379 "set (bad session id)",
380 dm_set_dmattr((dm_sessid_t)(-1000), hanp, hlen,
381 DM_NO_TOKEN, attrnamep, 0, buflen,
382 "EINVAL_bad_session_id"))
384 /**** GET tests ****/
385 /*---------------------------------------------------------*/
386 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
387 attrnamep, 0, buflen,
388 "ERRNO for GET_DMATTR");
389 /*---------------------------------------------------------*/
392 dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
393 attrnamep, 0, bufp, &rlenp))
394 /*---------------------------------------------------------*/
395 SHAREDTEST("get", hanp, hlen, test_token,
396 dm_get_dmattr(sid, hanp, hlen, test_token,
397 attrnamep, buflen, bufp, &rlenp))
398 /*---------------------------------------------------------*/
400 if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
401 fprintf(stderr, "Cannot create a test handle (%s); "
402 "skipping EBADF test\n", ERR_NAME);
405 ((char *) test_vp)[hlen/2]++;
408 dm_get_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
409 attrnamep, buflen, bufp, &rlenp))
410 dm_handle_free(test_vp, hlen);
413 /*---------------------------------------------------------*/
416 dm_get_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
417 attrnamep, buflen, bufp, &rlenp))
418 /*---------------------------------------------------------*/
420 "get (invalid session)",
421 dm_get_dmattr((dm_sessid_t)(-1000), hanp, hlen, DM_NO_TOKEN,
422 attrnamep, buflen, bufp, &rlenp))
423 /*---------------------------------------------------------*/
425 "get (invalid token)",
426 dm_get_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
427 attrnamep, buflen, bufp, &rlenp))
428 /*---------------------------------------------------------*/
431 dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
432 (dm_attrname_t *)("NO_SUCH_ENTRY"),
433 buflen, bufp, &rlenp))
434 /*---------------------------------------------------------*/
436 /**** REMOVE tests ****/
437 /*---------------------------------------------------------*/
438 dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
439 attrnamep, 0, buflen,
441 EXCLTEST("remove", hanp, hlen, test_token,
442 dm_remove_dmattr(sid, hanp, hlen, test_token,
444 /*---------------------------------------------------------*/
446 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep,
447 0, buflen, "ERRNO for DMATTR")) {
448 printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
451 else if ((test_vp = handle_clone(hanp, hlen)) == NULL) {
452 fprintf(stderr, "Cannot create a test handle (%s); "
453 "skipping EBADF test\n", ERR_NAME);
456 ((char *) test_vp)[hlen/2]++;
459 dm_remove_dmattr(sid, test_vp, hlen, DM_NO_TOKEN,
461 dm_handle_free(test_vp, hlen);
464 /*---------------------------------------------------------*/
465 if (dm_set_dmattr(sid, hanp, hlen, DM_NO_TOKEN, attrnamep, 0,
466 buflen, "ERRNO for DMATTR")) {
467 printf("ERROR in setting dmattr for remove_dmattr test. (%s)\n",
473 dm_remove_dmattr(sid, hanp, hlen-1, DM_NO_TOKEN,
476 /*---------------------------------------------------------*/
479 dm_remove_dmattr(sid, hanp, hlen, DM_NO_TOKEN,
481 /*---------------------------------------------------------*/
483 "remove (bad token)",
484 dm_remove_dmattr(sid, hanp, hlen, (dm_token_t)(-1000),
486 /*---------------------------------------------------------*/
488 "remove (bad session)",
489 dm_remove_dmattr(-1, hanp, hlen, DM_NO_TOKEN,
491 /*---------------------------------------------------------*/
494 sprintf(test_file, "%s/DMAPI_attribute_test_file.ERRNO",
496 sprintf(command, "rm %s\n", test_file);
498 printf("\t(errno subtests complete)\n");
500 /**********************************\
501 |* End of improper-input testing. *|
502 \**********************************/
505 printf("Attribute tests complete!\n");
507 dm_handle_free(hanp, hlen);