5bbb73cbb411b935ea47e07ceb9eafb05148bf7e
[vsys.git] / tests / vsys_conctest.c
1 #include <stdio.h>
2 #include <sys/select.h>
3 #include <sys/time.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <errno.h>
9
10 int main()
11 {
12   FILE *fp = NULL, *fp_in = NULL;
13   FILE *out_fp = NULL, *diff_fp = NULL;
14   const char* topcmd = "feg/test.out";
15   const char* top_in_file = "feg/test.in";
16   char buf[4096];
17   int fd_in = -1, fd_out;
18   int res;
19   int flag;
20   int count = 1;
21   struct timeval tv={.tv_sec=5,.tv_usec=0};
22
23   while (count < 10000) {
24     fd_set readSet;
25     int res;
26     int nlines=0;
27
28     printf("(%d)", count);
29
30     if ((fd_out = open(topcmd, O_RDONLY | O_NONBLOCK)) < 0) {
31       fprintf(stderr, "error executing top\n");
32       exit(-1);
33     }
34
35     if ((fd_in = open(top_in_file, O_WRONLY)) < 0) {
36       fprintf(stderr, "error opening %s\n", top_in_file);
37       exit(-1);
38     }
39
40     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
41       printf("fcntl get failed\n");
42       exit(-1);
43     }
44
45     FD_ZERO(&readSet);
46     FD_SET(fd_out, &readSet);
47
48     res = select(fd_out + 1, &readSet, NULL, NULL, &tv);
49     if (res < 1) {
50       printf("select failed: %d,%s\n",fd_out,strerror(errno));
51       exit(-1);
52     }
53
54     if (fcntl(fd_out, F_SETFL, flag & ~O_NONBLOCK) == -1) {
55       printf("fcntl set failed\n");
56       exit(-1);
57     }
58
59     if ((flag = fcntl(fd_out, F_GETFL)) == -1) {
60       printf("fcntl get failed\n");
61       exit(-1);
62     }
63
64     if (flag & O_NONBLOCK == 0) {
65       printf("fd_out still nonblocking\n");
66       exit(-1);
67     }
68
69     if ((fp = fdopen(fd_out, "r")) == NULL) {
70       printf("fdopen failed\n");
71       exit(-1);
72     }
73
74     if ((out_fp = fopen("/tmp/vsys_passwd_test", "w")) == NULL) {
75             printf("could not create tmp file for test\n");
76             exit(-1);
77     }
78
79     while (fgets(buf, sizeof(buf), fp) != NULL) {
80         fprintf(out_fp, "%s",buf);
81     }
82
83     fflush(out_fp);
84     fclose(out_fp);
85
86     if ((diff_fp = popen("/usr/bin/diff -u /tmp/vsys_passwd_test /etc/passwd","r")) == NULL) {
87             printf("Could not diff results\n");
88             exit(-1);
89     }
90
91     while (fgets(buf, sizeof(buf), diff_fp) != NULL) {
92             nlines++;
93     }
94
95     if (nlines) {
96             printf("Test returned different results - run again to verify\n");
97             exit(-1);
98     }
99
100     pclose (diff_fp);
101     fclose(fp);
102     close(fd_in);
103     close(fd_out);
104     count++;
105   }
106   printf("test successful.\n");
107   exit(0);
108
109 }