summaryrefslogtreecommitdiff
path: root/src/bin/evil/evil_test_pipe.c
blob: e5cfdec4d1df7a06d668ce14f2df5a40641a4ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <stdio.h>

# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# undef WIN32_LEAN_AND_MEAN

#include <Evil.h>

#include "evil_suite.h"
#include "evil_test_pipe.h"


#define FDREAD  0
#define FDWRITE 1

typedef struct
{
  int val;
  int fd_write;
} data;


static DWORD WINAPI
thread (void *param)
{
   data *d;
   void *buf[1];

   Sleep (2 * 1000);
   d = (data *)param;
   buf[0] = d;
   send(d->fd_write, (char *)buf, sizeof(buf), 0);

   return 0;
}

static int
test_pipe_test(void)
{
   int            sockets[2];
   struct timeval t;
   fd_set         rfds;
   int            ret = 0;
   data          *d;
   DWORD          thread_id;
   HANDLE         h;

   FD_ZERO(&rfds);

   t.tv_sec = 5;
   t.tv_usec = 0;

   if (pipe(sockets) < 0)
     return 0;

   FD_SET(sockets[FDREAD], &rfds);
   fcntl(sockets[FDREAD], F_SETFL, O_NONBLOCK);

   d = (data *)malloc(sizeof (data));
   if (!d)
     return 0;

   d->val = 14;
   d->fd_write = sockets[FDWRITE];

   h = CreateThread(NULL, 0, thread, d, 0, &thread_id);
   if (!h)

   ret = select(sockets[FDREAD] + 1, &rfds, NULL, NULL, &t);

   if (ret < 0)
     goto free_d;
   else if (ret == 0)
     goto close_h;
   else /* ret > 0 */
     {
        void *buf[1];
        data *d2 = NULL;
        int   len;

        while ((len = recv(sockets[FDREAD], (char *)buf, sizeof(buf), 0)) > 0)
          {
             if (len == sizeof(buf))
               {
                  d2 = (data *)buf[0];
                  break;
               }
          }
        if (d2 && (d2->val == d->val))
          ret = 1;
        else
          ret = 0;
     }

   CloseHandle(h);
   free(d);

   return ret;

 close_h:
   CloseHandle(h);
 free_d:
   free(d);
   return 0;
}

static int
test_pipe_run(suite *s)
{
   int res;

   res = test_pipe_test();

   return res;
}

int
test_pipe(suite *s)
{
   return test_pipe_run(s);
}