void test_ENOENT_nofile(char *name, int (*callback) (const char *), int expected) { remove_test_ENOENT_files(); temp = stderr; #ifdef DEBUG fprintf(temp, "TEST: ENOENT when file does not exist\n"); #endif if ((s2 = (*callback) (no_file)) == expected) { if (errno != ENOENT) { tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name, ENOENT, errno); cleanup_function(); fail_exit(); } } else { tst_resm(TFAIL, "%s did not return correct value; Expected=%d Received=%d", name, expected, s2); cleanup_function(); fail_exit(); } }
void test5A(void) { char path[] = "./tmp/data/d777"; int ret_val; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: Verify traversal without FTW_DEPTH set\n"); #endif visit = 0; if((ret_val = nftw64(path, test_func4, MAX_FD, 0)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if(ret_val != 999) { fprintf(temp, "ERROR: %s never visited\n", path); cleanup_function(); fail_exit(); } if (visit != 1) { fprintf(temp, "ERROR: Visited contents before directory\n"); cleanup_function(); fail_exit(); } }
void test6A(void) { char path[PATH_MAX + NAME_MAX]; int ret_val; if(getcwd(path, sizeof(path)) == NULL) { perror("getcwd"); cleanup_function(); fail_exit(); } (void)strcat(path, "/tmp/data/dirh"); temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 with FTW_CHDIR changes to each dir before reporting files in it\n"); #endif ret_val = nftw64(path, test_func5, MAX_FD, FTW_CHDIR); if (ret_val == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if ((ret_val == 998) || (ret_val == 999)) { cleanup_function(); fail_exit(); } }
void test3A(void) { int ret; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 without FTW_PHYS follows symbolic links\n"); #endif visit = 0; if((ret = nftw64("./tmp/data/dirl", test_func3, MAX_FD, 0)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if(ret == 999) { cleanup_function(); fail_exit(); } if (visit != LINK_CNT-1) { fprintf(temp, "ERROR: Expected %d files to be visited. nftw64() visited %d\n", LINK_CNT-1, visit); cleanup_function(); fail_exit(); } }
void test2A(void) { int i, ret; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 with FTW_PHYS does not follow symbolic links\n"); #endif visit = 0; if((ret = nftw64("./tmp/data/dirl", test_func1, MAX_FD, FTW_PHYS)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if(ret == 999) { cleanup_function(); fail_exit(); } if (visit != NO_LINK_CNT) { fprintf(temp, "ERROR: Expected %d files to be visited. nftw64() visited %d\n", NO_LINK_CNT, visit); cleanup_function(); fail_exit(); } for (i = 0; i < visit; i++) { if (dirlist[i] != (char *)NULL) free(dirlist[i]); } }
static void execute_function(char *name, int (*callback) (const char *), char *buffer, int expected) { int result; temp = stderr; #ifdef DEBUG fprintf(temp, "TEST: %s fails with ENAMETOOLONG\n", name); #endif errno = 0; result = (*callback) (buffer); /*callback found an error, fail */ if (result == -752) { tst_resm(TFAIL, "%s callback did not work correctly", name); cleanup_function(); fail_exit(); } if (result != expected) { tst_resm(TFAIL, "%s did not return value as expected; Expected=%d Received=%d", name, expected, result); cleanup_function(); fail_exit(); } if (errno != ENAMETOOLONG) { tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name, ENAMETOOLONG, errno); cleanup_function(); fail_exit(); } }
void test11A(void) { int i, ret; for (i = 0; i < nbads; i++) if (badlist[i].i == FTW_D) badlist[i].i = FTW_DP; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 passes FTW_DP when file is directory and subdirs already visited\n"); #endif if((ret = nftw64("./tmp/data/dirg", test_func11, MAX_FD, FTW_DEPTH | FTW_PHYS)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test_ENOENT_empty(char *name, int (*callback) (const char *), int expected) { char *empty_string; empty_string = ""; errno = 0; temp = stderr; #ifdef DEBUG fprintf(temp, "TEST: ENOENT when empty string is passed\n"); #endif if ((s2 = (*callback) (empty_string)) == expected) { if (errno != ENOENT) { tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name, ENOENT, errno); cleanup_function(); fail_exit(); } } else { tst_resm(TFAIL, "%s did not return correct value; Expected=%d Received=%d", name, expected, s2); cleanup_function(); fail_exit(); } }
void test18A(void) { int ret; unlink("./tmp/byebye"); visit=0; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 with FTW_PHYS does not pass FTW_SLN\n"); #endif if((ret = nftw64("./tmp/data/dirg", test_func18, MAX_FD, FTW_PHYS)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret == 999) { fprintf(temp, "ERROR: nftw64() passed FTW_SLN\n"); cleanup_function(); fail_exit(); } visit=0; #ifdef DEBUG fprintf(temp, "TEST: nftw64 without FTW_PHYS passes FTW_SLN\n"); #endif if((ret=nftw64("./tmp/data/dirg", test_func18, MAX_FD, 0)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (visit == 1) { if (ret == 999) { /* Test is passed */ return; } else { fprintf(temp, "ERROR: nftw64 passed FTW_SLN but did"); fprintf(temp, "not return value returned by fn()\n"); cleanup_function(); fail_exit(); } } else { fprintf(temp, "ERROR: nftw64() did not pass FTW_SLN\n"); cleanup_function(); fail_exit(); } }
void test1A(void) { int i, j, ret; temp = stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw() succeeds\n"); #endif visit = 0; if((ret = nftw("./tmp/data/dirh", test_func1, MAX_FD,0 )) == -1) { perror("ERROR: nftw failed"); cleanup_function(); fail_exit(); } if(ret == 999) { cleanup_function(); fail_exit(); } #ifdef DEBUG fprintf(temp, "TEST: Whole tree traversed\n"); #endif if (visit != ngoods) { fprintf(temp, "ERROR: Count of objects visited incorrect\n"); fprintf(temp, " Expected %d, Received %d\n", ngoods, visit); cleanup_function(); fail_exit(); } for (i = 0; i < visit; i++) { for (j = 0; j < ngoods; j++) { if (strcmp(dirlist[i], goodlist[j]) == 0) { free(dirlist[i]); dirlist[i] = (char *)NULL; break; } } } for (i = 0; i < visit; i++) { if (dirlist[i] != (char *)NULL) { free(dirlist[i]); fprintf(temp, "ERROR: Unexpected visit to %s\n", dirlist[i]); cleanup_function(); fail_exit(); } } }
void test_ENOTDIR(char *name, int (*callback) (const char *), int expected) { int fd; if ((fd = creat(is_a_file, (mode_t) (S_IRWXU | S_IRWXG | S_IRWXO))) == -1) { tst_resm(TFAIL, "creat(%s) failed: %s", is_a_file, strerror(errno)); cleanup_function(); fail_exit(); } if (close(fd) == -1) { tst_resm(TFAIL, "close(%i) failed: %s", fd, strerror(errno)); remove_test_ENOTDIR_files(); cleanup_function(); fail_exit(); } errno = 0; temp = stderr; #ifdef DEBUG fprintf(temp, "TEST: ENOTDIR when a component is not a directory\n"); #endif s2 = (*callback) ("./tmp/is_a_file/no_file"); /*callback found an error, bail */ if (s2 == -752) { remove_test_ENOTDIR_files(); cleanup_function(); fail_exit(); } if (s2 == expected) { if (errno != ENOTDIR) { tst_resm(TFAIL, "%s failed: errno should be %i but is %i", name, ENOTDIR, errno); cleanup_function(); fail_exit(); } } else { tst_resm(TFAIL, "%s did not return correct value; Expected=%d Received=%d", name, expected, s2); cleanup_function(); fail_exit(); } remove_test_ENOTDIR_files(); }
void test16A(void) { char path[PATH_MAX + NAME_MAX]; char orig[PATH_MAX]; if(getcwd(orig, sizeof(orig)) == NULL) { perror("getcwd on original wd"); cleanup_function(); fail_exit(); } strcpy(path, orig); (void)strcat(path, "/tmp/data/dirg"); #ifdef DEBUG fprintf(temp, "TEST: nftw with absolute pathname %s\n", path); #endif if((s2 = nftw(path, test_func16, MAX_FD, 0)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (s2 == 999) { cleanup_function(); fail_exit(); } (void)strcpy(path, "./tmp/data/dirg"); #ifdef DEBUG fprintf(temp, "TEST: nftw with relative pathname %s\n", path); #endif if((s2 = nftw(path, test_func16, MAX_FD, 0)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (s2 == 999) { cleanup_function(); fail_exit(); } }
signal_handler (int sig) { int killed; int status; assert (test_pid > 1); /* Kill the whole process group. */ kill (-test_pid, SIGKILL); /* In case setpgid failed in the child, kill it individually too. */ kill (test_pid, SIGKILL); /* Wait for it to terminate. */ int i; for (i = 0; i < 5; ++i) { killed = waitpid (test_pid, &status, WNOHANG|WUNTRACED); if (killed != 0) break; /* Delay, give the system time to process the kill. If the nanosleep() call return prematurely, all the better. We won't restart it since this probably means the child process finally died. */ struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = 100000000; nanosleep (&ts, NULL); } if (killed != 0 && killed != test_pid) { printf ("Failed to kill test process: %m\n"); exit (1); } if (cleanup_function != NULL) cleanup_function (); if (sig == SIGINT) { signal (sig, SIG_DFL); raise (sig); } if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL)) puts ("Timed out: killed the child process"); else if (WIFSTOPPED (status)) printf ("Timed out: the child process was %s\n", strsignal (WSTOPSIG (status))); else if (WIFSIGNALED (status)) printf ("Timed out: the child process got signal %s\n", strsignal (WTERMSIG (status))); else printf ("Timed out: killed the child process but it exited %d\n", WEXITSTATUS (status)); /* Exit with an error. */ exit (1); }
void test7A(void) { int ret; #ifdef DEBUG fprintf(temp, "TEST: nftw passes pathname as first argument to fn()\n"); #endif if ((ret = nftw("./tmp/data/dirg", test_func7, MAX_FD, 0)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test22A(void) { char path[] = "./tmp/data/dirh"; int ret_val, i; for (i = 0; i < 4; i++) { if((next_fd[i] = open(path, O_RDONLY)) == -1) { perror("open"); cleanup_function(); fail_exit(); } } for (i = 0; i < 4; i++) { if(close(next_fd[i]) == -1) { perror("close"); cleanup_function(); fail_exit(); } } visit = 0; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: No more than 1 fd per level is used in traversal\n"); #endif ret_val = nftw64(path, test_func22, MAX_FD, 0); if (ret_val == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret_val == 999) { cleanup_function(); fail_exit(); } }
void test15A(void) { int ret; #ifdef DEBUG fprintf(temp, "TEST: nftw(path, fn, depth, FTW_PHYS) passes FTW_NS when dir unsearchable\n"); #endif if((ret = nftw("./tmp/data/d666", test_func15, MAX_FD,FTW_PHYS)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test9A(void) { int ret; #ifdef DEBUG fprintf(temp, "TEST: nftw passes FTW_F as third arg to fn() for files\n"); #endif if ((ret = nftw("./tmp/data/dirg", test_func9, MAX_FD, FTW_PHYS)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test14A(void) { int ret; #ifdef DEBUG fprintf(temp, "TEST: nftw passes FTW_DNR when file is directory that cannot be read\n"); #endif if((ret = nftw("./tmp/data/d333", test_func14, MAX_FD, 0)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test17A(void) { int ret; visit = 0; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 with FTW_PHYS passes FTW_SL for symlink\n"); #endif if((ret = nftw64("./tmp/data/dirl", test_func17, MAX_FD, FTW_PHYS)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret != 999) { fprintf(temp, "ERROR: nftw64() failed to find symbolic link\n"); cleanup_function(); fail_exit(); } visit = 0; #ifdef DEBUG fprintf(temp, "TEST: nftw64 without FTW_PHYS does not pass FTW_SL for symlink\n"); #endif if((ret = nftw64("./tmp/data/dirl", test_func17, MAX_FD, 0)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret == 999) { fprintf(temp, "ERROR: nftw64() found symbolic link\n"); cleanup_function(); fail_exit(); } }
void test12A(void) { int ret; #ifdef DEBUG fprintf(temp, "TEST: nftw wth FTW_PHYS passes FTW_SL when file is symlink\n"); #endif if ((ret = nftw("./tmp/data/dirg", test_func12, MAX_FD, FTW_PHYS)) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test8A(void) { int ret; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 passes stat struct as second argument to fn()\n"); #endif if((ret = nftw64("./tmp/data/dirg", test_func8, MAX_FD, 0)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
void test30A(void) { if (chmod("./tmp/data/d333", (mode_t) S_IXUSR) == -1) { perror("chmod"); cleanup_function(); fail_exit(); } #ifdef DEBUG fprintf(temp, "TEST: [EACCES] && -1 returned by nftw\n"); #endif test_ENOTDIR("nftw", callback, -1); }
void Module::Remove() { char * error; void (*cleanup_function)(); cleanup_function = (void (*)())dlsym(this->module_handle, "cleanup_module"); if((error = dlerror()) != NULL){ std::cerr << std::string(error) << std::endl; return; } cleanup_function(); }
void test10A(void) { int ret; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: nftw64 passes FTW_D as third arg to fn() when file is directory\n"); #endif if ((ret = nftw64("./tmp/data/dirg", test_func10, MAX_FD, FTW_PHYS)) == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret == 999) { cleanup_function(); fail_exit(); } }
static char *get_long_name_buffer(size_t * length, size_t extra) { char *buffer; size_t path_length, name_length; temp = stderr; if ((path_length = pathconf(tmp_path, _PC_PATH_MAX)) == -1) { tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s", strerror(errno)); cleanup_function(); fail_exit(); } if ((name_length = pathconf(tmp_path, _PC_NAME_MAX)) == -1) { tst_resm(TFAIL, "pathconf(_PC_NAME_MAX) failed: %s", strerror(errno)); cleanup_function(); fail_exit(); } if ((strlen(tmp_path) + name_length + extra) > path_length) { tst_resm(TFAIL, "pathconf(_PC_NAME_MAX)[=%zi] too large relative to pathconf(_PC_PATH_MAX)[=%zi]", name_length, path_length); cleanup_function(); fail_exit(); } if ((buffer = malloc(path_length + extra)) == NULL) { tst_resm(TFAIL, "malloc(%zi) failed: %s", path_length + extra, strerror(errno)); cleanup_function(); fail_exit(); } *length = name_length; return buffer; }
void test19A(void) { int ret_val; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: Can not traverse directory with no read permission\n"); #endif visit = 0; ret_val = nftw64("./tmp/data/d333", test_func19, MAX_FD, 0); if (ret_val == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret_val == 999) { cleanup_function(); fail_exit(); } #ifdef DEBUG fprintf(temp, "TEST: fn only be called once\n"); #endif if (visit != 1) { fprintf(temp, "ERROR: %s","Directory without read permission allows traversing\n"); fprintf(temp, " Visited %d files\n", visit); cleanup_function(); fail_exit(); } }
void test21A(void) { char path[] = "./tmp/data/dirh"; int ret_val; temp=stderr; #ifdef DEBUG fprintf(temp, "TEST: No more than depth file descriptors used in traversal\n"); #endif /*this is the fd we expect if 0 are used*/ if((next_fd[0] = open(path, O_RDONLY)) == -1) { perror("open next_fd[0]"); cleanup_function(); fail_exit(); } /*this is the fd we expect if 1 is used*/ if((next_fd[1] = open(path, O_RDONLY)) == -1) { perror("open next_fd[1]"); cleanup_function(); fail_exit(); } if(close(next_fd[0]) == -1) { perror("close next_fd[0]"); cleanup_function(); fail_exit(); } if (close(next_fd[1]) == -1) { perror("close next_fd[1]"); cleanup_function(); fail_exit(); } visit=0; ret_val = nftw64(path, test_func21, 1, 0); if (ret_val == -1) { perror("nftw64"); cleanup_function(); fail_exit(); } if (ret_val == 999) { cleanup_function(); fail_exit(); } }
void test20A(void) { int fd, nfd; #ifdef DEBUG fprintf(temp, "TEST: File descriptors used in traversal are closed\n"); #endif if ((fd = open("./tmp/data/dirh", O_RDONLY)) == -1) { perror("close"); cleanup_function(); fail_exit(); } if (close(fd) == -1) { perror("close"); cleanup_function(); fail_exit(); } if (nftw("./tmp/data/dirh", test_func20, 1, 0) == -1) { perror("nftw"); cleanup_function(); fail_exit(); } if ((nfd = open("./tmp/data/dirh", O_RDONLY)) == -1) { perror("open"); cleanup_function(); fail_exit(); } if (nfd != fd) { fprintf(temp, "ERROR: %s,fd == %d ofd = %d", "nftw did not close all file descriptors used in traversal\n", nfd, fd); cleanup_function(); fail_exit(); } if (close(nfd) == -1) { perror("close"); cleanup_function(); fail_exit(); } }
void operator()(void* data) { cleanup_function(static_cast<T*>(data)); }
void test_ENAMETOOLONG_path(char *name, int (*callback) (const char *), int expected) { size_t pcPathMax; char *path, *tmpPtr; int pathLength, tempPathLength; temp = stderr; if ((pcPathMax = pathconf(tmp_path, _PC_PATH_MAX)) == -1) { tst_resm(TFAIL, "pathconf(_PC_PATH_MAX) failed: %s", strerror(errno)); cleanup_function(); fail_exit(); } #ifdef DEBUG fprintf(temp, "INFO: pathconf(_PC_PATH_MAX) for %s is %lu\n", tmp_path, pcPathMax); #endif if ((path = malloc(pcPathMax + 2)) == NULL) { tst_resm(TFAIL, "malloc(%zu) for path failed: %s", pcPathMax + 2, strerror(errno)); cleanup_function(); fail_exit(); } path = strcpy(path, tmp_path); pathLength = (int)strlen(path); tempPathLength = pathLength + 1; /* leave some chars for element that pushes path over PC_PATH_MAX */ pcPathMax = pcPathMax - tempPathLength - 5; tmpPtr = path + strlen(path); while (pathLength < pcPathMax) { tmpPtr += sprintf(tmpPtr, "/%s", tmp_path); pathLength += tempPathLength; } /* reinstate pcPathMax correct value */ pcPathMax = pcPathMax + tempPathLength + 5; tmpPtr = path + pathLength; *tmpPtr++ = '/'; pathLength++; while (pathLength <= pcPathMax) { *tmpPtr++ = 'z'; pathLength++; } *tmpPtr = '\0'; pathLength = (int)strlen(path); if (pathLength != pcPathMax + 1) { tst_resm(TFAIL, "test logic failure, path length is %d, should be %lu", pathLength, (long unsigned int)pcPathMax + 1); free(path); cleanup_function(); fail_exit(); } execute_function(name, callback, path, expected); free(path); }