1 // -- Core testing commands
2 // REQUIRES: uclibc
3 // REQUIRES: posix-runtime
4 // RUN: rm -rf %t.out
5 // RUN: rm -f %t.bc
6 // RUN: mkdir -p %t.out
7 // RUN: echo -n aaaa > %t.out/aaaa.txt
8 // RUN: echo -n bbbb > %t.out/bbbb.txt
9 // RUN: echo -n ccc > %t.out/cccc.txt
10 // RUN: %clang %s -emit-llvm %O0opt -c -o %t.bc
11 // RUN: %klee-zesti -only-replay-seeds -libc=uclibc -posix-runtime %t.bc -o -p -q %t.out/bbbb.txt %t.out/cccc.txt < %t.out/aaaa.txt  &> %t.out/out.txt
12 // RUN: FileCheck --input-file=%t.out/out.txt %s
13 
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 
check_fd(int fd,const int file_size,const char * file_contents)21 int check_fd(int fd, const int file_size, const char *file_contents) {
22   struct stat fs;
23   char contents[10] = {0};
24 
25   if (fstat(fd, &fs) < 0)
26     return -1;
27 
28   if (fs.st_size != file_size)
29     return -1;
30 
31   read(fd, contents, 10);
32 
33   if (strcmp(contents, file_contents) != 0)
34     return -1;
35 
36   return 0;
37 }
38 
check_file(const char * file_name,const int file_size,const char * file_contents)39 int check_file(const char *file_name, const int file_size, const char *file_contents) {
40   int fd;
41 
42   if ((fd = open(file_name, O_RDONLY)) < 0)
43     return -1;
44 
45   if (check_fd(fd, file_size, file_contents) < 0)
46     return -1;
47 
48   close(fd);
49   return 0;
50 }
51 
main(int argc,char ** argv)52 int main(int argc, char **argv) {
53   if (argc == 6) {
54     // CHECK-DAG: Got 5 args
55     printf("Got 5 args\n");
56   }
57 
58   if (strcmp(argv[1], "-o") == 0) {
59     // CHECK-DAG: Got -o flag
60     printf("Got -o flag\n");
61   }
62 
63   if (strcmp(argv[2], "-p") == 0) {
64     // CHECK-DAG: Got -p flag
65     printf("Got -p flag\n");
66   }
67 
68   if (strcmp(argv[3], "-q") == 0) {
69     // CHECK-DAG: Got -q flag
70     printf("Got -q flag\n");
71   }
72 
73   if (strcmp(argv[4], "A") == 0) {
74     // CHECK-DAG: Got A file
75     printf("Got A file\n");
76   }
77 
78   if (check_file(argv[4], 4, "bbbb") == 0) {
79     // CHECK-DAG: Got A file size
80     printf("Got A file size\n");
81   }
82 
83   if (strcmp(argv[5], "B") == 0) {
84     // CHECK-DAG: Got B file
85     printf("Got B file\n");
86   }
87 
88   // File sizes get increased to the highest among files, so even B has file size 4.
89   // This is due to the limitaiton of posix-runtime API
90   if (check_file(argv[5], 4, "ccc") == 0) {
91     // CHECK-DAG: Got B file size
92     printf("Got B file size\n");
93   }
94 
95   if (check_fd(0, 4, "aaaa") == 0) {
96     // CHECK-DAG: Got stdin file size
97     printf("Got stdin file size\n");
98   }
99   // CHECK-DAG: KLEE: done: completed paths = 1
100   // CHECK-DAG: KLEE: done: generated tests = 1
101 
102   return 0;
103 }
104