blob: 53b2ec8e44ed3740ee0a78852462901c21c4b045 [file] [log] [blame]
Juraj Vijtiukd7989422020-09-15 13:54:00 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <dirent.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <sys/stat.h>
8#include<fcntl.h>
9
10int main(int argc, char **argv)
11{
12 DIR *d;
13 struct dirent *dir;
14 pid_t p = 0;
15 int input_fd = 0;
16 int status = 0;
17 int rc = 0;
18 struct stat path_stat;
19 int i = 0;
20
21 if (argc != 3) {
22 fprintf(stderr, "invalid number of arguments. Call like this ./fuzz_regression_test fuzz_harness corpus_dir\n");
23 return EXIT_FAILURE;
24 }
25
26 d = opendir(argv[2]);
27 if (!d) {
28 fprintf(stderr, "error opening dir %s\n", argv[2]);
29 return EXIT_FAILURE;
30 }
31
32 while ((dir = readdir(d)) != NULL) {
33 stat(dir->d_name, &path_stat);
34 if (!S_ISREG(path_stat.st_mode)) {
35 continue;
36 }
37
38 p = fork();
39 if (p == -1) {
40 fprintf(stderr, "fork failed\n");
41 return EXIT_FAILURE;
42 } else if (p == 0) {
43 input_fd = open(dir->d_name, O_RDONLY);
44 if (input_fd == -1) {
45 fprintf(stderr, "error opening input file %s\n", dir->d_name);
46 return EXIT_FAILURE;
47 }
48
49 dup2(input_fd, STDIN_FILENO);
50 execl(argv[1], argv[1], NULL);
51 return EXIT_SUCCESS;
52 }
53
54 rc = waitpid(p, &status, 0);
55 if (rc == -1) {
56 fprintf(stderr, "waitpid failed\n");
57 return EXIT_FAILURE;
58 }
59
60 if (!WIFEXITED(status)) {
61 fprintf(stderr, "test %s - %s failed\n", argv[1], argv[2]);
62 return EXIT_FAILURE;
63 }
64
65 printf("test %s - %s successful\n", argv[1], dir->d_name);
66 }
67
68 return EXIT_SUCCESS;
69}