aboutsummaryrefslogtreecommitdiff
path: root/src/testspdy/test_proxies.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testspdy/test_proxies.c')
-rw-r--r--src/testspdy/test_proxies.c218
1 files changed, 218 insertions, 0 deletions
diff --git a/src/testspdy/test_proxies.c b/src/testspdy/test_proxies.c
new file mode 100644
index 00000000..3f7b554a
--- /dev/null
+++ b/src/testspdy/test_proxies.c
@@ -0,0 +1,218 @@
1/*
2 This file is part of libmicrospdy
3 Copyright (C) 2013 Andrey Uzunov
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file test_proxies.c
21 * @brief test curl > mhd2spdylay > microspdy2http > mhd
22 * @author Andrey Uzunov
23 */
24
25#include "platform.h"
26#include "microspdy.h"
27#include "common.h"
28#include <sys/wait.h>
29#include <stdio.h> /* printf, stderr, fprintf */
30#include <sys/types.h> /* pid_t */
31#include <unistd.h> /* _exit, fork */
32#include <stdlib.h> /* exit */
33#include <errno.h> /* errno */
34#include <sys/wait.h> /* pid_t */
35#include "common.h"
36
37#define EXPECTED_BODY "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
38
39
40pid_t parent;
41 pid_t child_mhd;
42 pid_t child_spdy2http;
43 pid_t child_mhd2spdy;
44 pid_t child_curl;
45
46 uint16_t mhd_port;
47 uint16_t spdy2http_port;
48 uint16_t mhd2spdy_port;
49
50void
51killproc(int pid, const char *message)
52{
53 printf("%s\nkilling %i\n",message,pid);
54 kill(pid, SIGKILL);
55}
56
57
58void killchildren()
59{
60 if(0 != child_mhd)
61 killproc(child_mhd,"kill mhd\n");
62 if(0 != child_spdy2http)
63 killproc(child_spdy2http,"kill spdy2http\n");
64 if(0 != child_mhd2spdy)
65 killproc(child_mhd2spdy,"kill mhd2spdy\n");
66 if(0 != child_curl)
67 killproc(child_curl,"kill curl\n");
68}
69
70pid_t au_fork()
71{
72 pid_t child = fork();
73 if (child == -1)
74 {
75 killchildren();
76
77 killproc(parent,"fork failed\n");
78 }
79
80 return child;
81}
82
83
84int main()
85{
86 //pid_t child;
87 int childstatus;
88
89 parent = getpid();
90 mhd_port = get_port(4000);
91 spdy2http_port = get_port(4100);
92 mhd2spdy_port = get_port(4200);
93
94 child_mhd = au_fork();
95 if (child_mhd == 0)
96 {
97 //run MHD
98 int devnull;
99 char *port_s;
100
101 close(1);
102 devnull = open("/dev/null", O_WRONLY);
103 if (1 != devnull)
104 {
105 dup2(devnull, 1);
106 close(devnull);
107 }
108 asprintf(&port_s, "%i", mhd_port);
109 execlp ("../examples/minimal_example", "minimal_example", port_s, NULL);
110 fprintf(stderr, "executing mhd failed\nFor this test 'make' must be run before 'make check'!\n");
111 //killchildren();
112 _exit(1);
113 }
114
115
116 child_spdy2http = au_fork();
117 if (child_spdy2http == 0)
118 {
119 //run spdy2http
120 int devnull;
121 char *port_s;
122 //char *url;
123
124 close(1);
125 devnull = open("/dev/null", O_WRONLY);
126 if (1 != devnull)
127 {
128 dup2(devnull, 1);
129 close(devnull);
130 }
131 //asprintf(&url, "127.0.0.1:%i", mhd_port);
132 asprintf(&port_s, "%i", spdy2http_port);
133 sleep(1);
134 execlp ("../spdy2http/microspdy2http", "microspdy2http", "-v4rtT", "10", "-p", port_s, NULL);
135 fprintf(stderr, "executing microspdy2http failed\n");
136 //killchildren();
137 _exit(1);
138 }
139
140 child_mhd2spdy = au_fork();
141 if (child_mhd2spdy == 0)
142 {
143 //run MHD2sdpy
144 int devnull;
145 char *port_s;
146 char *url;
147
148 close(1);
149 devnull = open("/dev/null", O_WRONLY);
150 if (1 != devnull)
151 {
152 dup2(devnull, 1);
153 close(devnull);
154 }
155 asprintf(&url, "http://127.0.0.1:%i", spdy2http_port);
156 asprintf(&port_s, "%i", mhd2spdy_port);
157 sleep(2);
158 execlp ("../examples/mhd2spdy", "mhd2spdy", "-vosb", url, "-p", port_s, NULL);
159 fprintf(stderr, "executing mhd2spdy failed\n");
160 //killchildren();
161 _exit(1);
162 }
163
164 child_curl = au_fork();
165 if (child_curl == 0)
166 {
167 //run curl
168 FILE *p;
169 int devnull;
170 char *cmd;
171 unsigned int i;
172 char buf[strlen(EXPECTED_BODY) + 1];
173
174 close(1);
175 devnull = open("/dev/null", O_WRONLY);
176 if (1 != devnull)
177 {
178 dup2(devnull, 1);
179 close(devnull);
180 }
181
182 asprintf (&cmd, "curl --proxy http://127.0.0.1:%i http://127.0.0.1:%i/", mhd2spdy_port, mhd_port);
183 sleep(3);
184 p = popen(cmd, "r");
185 if (p != NULL)
186 {
187 for (i = 0; i < strlen(EXPECTED_BODY) && !feof(p); i++)
188 {
189 buf[i] = fgetc(p);
190 }
191
192 pclose(p);
193 buf[i] = 0;
194 _exit(strcmp(EXPECTED_BODY, buf));
195 }
196 fprintf(stderr, "executing curl failed\n");
197 //killchildren();
198 _exit(1);
199 }
200
201 do
202 {
203 if(waitpid(child_mhd,&childstatus,WNOHANG) == child_mhd
204 || waitpid(child_spdy2http,&childstatus,WNOHANG) == child_spdy2http
205 || waitpid(child_mhd2spdy,&childstatus,WNOHANG) == child_mhd2spdy)
206 {
207 killchildren();
208 return 1;
209 }
210 if(waitpid(child_curl,&childstatus,WNOHANG) == child_curl)
211 {
212 killchildren();
213 return WEXITSTATUS(childstatus);
214 }
215 sleep(1);
216 }
217 while(true);
218}