1 /* integration tests */
9 #include <sys/socket.h>
11 #include <netinet/ip.h>
12 #include <arpa/inet.h>
20 static void testsrv(char*, char*, int);
21 static void forksrv(int*, int*);
22 static int copy(int, int, int);
23 static int diff(char*, int);
24 static int diallocal(int);
25 static void cleanup(int sig
);
34 {"sh-tests/allow-underscore.commands", "sh-tests/allow-underscore.expected"},
35 {"sh-tests/delete_ready.commands", "sh-tests/delete_ready.expected"},
36 {"sh-tests/multi-tube.commands", "sh-tests/multi-tube.expected"},
37 {"sh-tests/no_negative_delays.commands", "sh-tests/no_negative_delays.expected"},
38 {"sh-tests/omit-time-left.commands", "sh-tests/omit-time-left.expected"},
39 {"sh-tests/pause-tube.commands", "sh-tests/pause-tube.expected"},
40 {"sh-tests/small_delay.commands", "sh-tests/small_delay.expected"},
41 {"sh-tests/too-big.commands", "sh-tests/too-big.expected"},
42 {"sh-tests/ttr-large.commands", "sh-tests/ttr-large.expected"},
43 {"sh-tests/zero_delay.commands", "sh-tests/zero_delay.expected"},
61 for (i
= 0; ts
[i
].cmd
; i
++) {
62 testsrv(ts
[i
].cmd
, ts
[i
].exp
, 4096);
63 testsrv(ts
[i
].cmd
, ts
[i
].exp
, 1);
75 testsrv(char *cmd
, char *exp
, int bufsiz
)
77 int status
, port
= 0, cfd
, tfd
, diffpid
;
78 struct sigaction sa
= {};
80 job_data_size_limit
= 10;
84 forksrv(&port
, &srvpid
);
85 if (port
== -1 || srvpid
== -1) {
86 puts("forksrv failed");
90 // Fail if this test takes more than 10 seconds.
91 // If we have trouble installing the timeout,
92 // just proceed anyway.
93 sa
.sa_handler
= cleanup
;
94 sigaction(SIGALRM
, &sa
, 0);
97 cfd
= diallocal(port
);
104 tfd
= open(cmd
, O_RDONLY
, 0);
111 if (copy(cfd
, tfd
, bufsiz
) == -1) {
117 diffpid
= diff(exp
, cfd
);
124 waitpid(diffpid
, &status
, 0);
126 // wait until after diff has finished to kill srvpid
129 printf("diff status %d\n", status
);
130 ASSERT(status
== 0, "diff status");
135 forksrv(int *port
, int *pid
)
138 struct sockaddr_in addr
;
139 struct event_base
*ev_base
;
141 srvfd
= make_server_socket("127.0.0.1", "0");
142 if (srvfd
== -1) return;
145 r
= getsockname(srvfd
, (struct sockaddr
*)&addr
, (socklen_t
*)&len
);
146 if (r
== -1 || len
> sizeof(addr
)) return;
148 *port
= addr
.sin_port
;
151 if (*pid
!= 0) return;
156 ev_base
= event_init();
158 srv(srvfd
); /* does not return */
159 exit(1); /* satisfy the compiler */
164 diff(char *f0
, int fd
)
169 if (pid
!= 0) return pid
;
176 execlp("diff", "diff", f0
, "-", (char*)0);
183 copy(int dst
, int src
, int bs
)
189 r
= read(src
, buf
, sizeof buf
);
190 if (r
== -1) return -1;
193 for (w
= 0; w
< r
; w
+= c
) {
194 c
= write(dst
, buf
+w
, r
-w
);
195 if (c
== -1) return -1;
206 struct sockaddr_in addr
= {AF_INET
, port
};
208 r
= inet_aton("127.0.0.1", &addr
.sin_addr
);
214 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
219 r
= connect(fd
, (struct sockaddr
*)&addr
, sizeof addr
);