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