aboutsummaryrefslogtreecommitdiff
path: root/src/testspdy/test_proxies.c
blob: 38d5f3cb6cc9ecca788d67e9011d47a483ecbd91 (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
    This file is part of libmicrospdy
    Copyright (C) 2013 Andrey Uzunov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file test_proxies.c
 * @brief  test curl > mhd2spdylay > microspdy2http > mhd
 * @author Andrey Uzunov
 */

#include "platform.h"
#include "microspdy.h"
#include "common.h"
#include <sys/wait.h>
#include <stdio.h>   /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h>  /* _exit, fork */
#include <stdlib.h>  /* exit */
#include <errno.h>   /* errno */
#include <sys/wait.h> /* pid_t */
#include "common.h"

#define EXPECTED_BODY "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"


pid_t parent;
	pid_t child_mhd;
	pid_t child_spdy2http;
	pid_t child_mhd2spdy;
	pid_t child_curl;

	uint16_t mhd_port;
	uint16_t spdy2http_port;
	uint16_t mhd2spdy_port;

void
killproc(int pid, const char *message)
{
	printf("%s\nkilling %i\n",message,pid);
	kill(pid, SIGKILL);
}


void killchildren()
{
    if(0 != child_mhd)
      killproc(child_mhd,"kill mhd\n");
    if(0 != child_spdy2http)
      killproc(child_spdy2http,"kill spdy2http\n");
    if(0 != child_mhd2spdy)
      killproc(child_mhd2spdy,"kill mhd2spdy\n");
    if(0 != child_curl)
      killproc(child_curl,"kill curl\n");
}

pid_t au_fork()
{
  pid_t child = fork();
	if (child == -1)
	{
    killchildren();
      
		killproc(parent,"fork failed\n");
	}

	return child;
}


int main()
{
  //pid_t child;
	int childstatus;
	pid_t wpid;
  
	parent = getpid();
	mhd_port = get_port(4000);
	spdy2http_port = get_port(4100);
	mhd2spdy_port = get_port(4200);

	child_mhd = au_fork();
	if (child_mhd == 0)
	{
    //run MHD
		pid_t devnull;
    char *port_s;

		close(1);
		devnull = open("/dev/null", O_WRONLY);
		if (1 != devnull)
		{
			dup2(devnull, 1);
			close(devnull);
		}
    asprintf(&port_s, "%i", mhd_port);
		execlp ("../examples/minimal_example", "minimal_example", port_s, NULL);
		fprintf(stderr, "executing mhd failed\nFor this test 'make' must be run before 'make check'!\n");
    //killchildren();
    _exit(1);
	}
  
  
	child_spdy2http = au_fork();
	if (child_spdy2http == 0)
	{
    //run spdy2http
		pid_t devnull;
    char *port_s;
    //char *url;

		close(1);
		devnull = open("/dev/null", O_WRONLY);
		if (1 != devnull)
		{
			dup2(devnull, 1);
			close(devnull);
		}
    //asprintf(&url, "127.0.0.1:%i", mhd_port);
    asprintf(&port_s, "%i", spdy2http_port);
    sleep(1);
		execlp ("../spdy2http/microspdy2http", "microspdy2http", "-v4rtT", "10", "-p", port_s, NULL);
		fprintf(stderr, "executing microspdy2http failed\n");
    //killchildren();
    _exit(1);
	}
  
	child_mhd2spdy = au_fork();
	if (child_mhd2spdy == 0)
	{
    //run MHD2sdpy
		pid_t devnull;
    char *port_s;
    char *url;

		close(1);
		devnull = open("/dev/null", O_WRONLY);
		if (1 != devnull)
		{
			dup2(devnull, 1);
			close(devnull);
		}
    asprintf(&url, "http://127.0.0.1:%i", spdy2http_port);
    asprintf(&port_s, "%i", mhd2spdy_port);
    sleep(2);
		execlp ("../examples/mhd2spdy", "mhd2spdy", "-vosb", url, "-p", port_s, NULL);
		fprintf(stderr, "executing mhd2spdy failed\n");
    //killchildren();
    _exit(1);
	}
  
	child_curl = au_fork();
	if (child_curl == 0)
	{
    //run curl
    FILE *p;
		pid_t devnull;
		char *cmd;
    unsigned int i;
    char buf[strlen(EXPECTED_BODY) + 1];

		close(1);
		devnull = open("/dev/null", O_WRONLY);
		if (1 != devnull)
		{
			dup2(devnull, 1);
			close(devnull);
		}
    
		asprintf (&cmd, "curl --proxy http://127.0.0.1:%i http://127.0.0.1:%i/", mhd2spdy_port, mhd_port);
    sleep(3);
    p = popen(cmd, "r");
    if (p != NULL)
    {
      for (i = 0; i < strlen(EXPECTED_BODY) && !feof(p); i++)
      {
          buf[i] = fgetc(p);
      }

      pclose(p);
      buf[i] = 0;
      _exit(strcmp(EXPECTED_BODY, buf));
    }
		fprintf(stderr, "executing curl failed\n");
    //killchildren();
    _exit(1);
	}
  
  do
  {
    wpid = waitpid(child_mhd,&childstatus,WNOHANG);
    if(wpid == child_mhd)
    {
      fprintf(stderr, "mhd died unexpectedly\n");
      killchildren();
      return 1;
    }
    
    wpid = waitpid(child_spdy2http,&childstatus,WNOHANG);
    if(wpid == child_spdy2http)
    {
      fprintf(stderr, "spdy2http died unexpectedly\n");
      killchildren();
      return 1;
    }
    
    wpid = waitpid(child_mhd2spdy,&childstatus,WNOHANG);
    if(wpid == child_mhd2spdy)
    {
      fprintf(stderr, "mhd2spdy died unexpectedly\n");
      killchildren();
      return 1;
    }
    
    if(waitpid(child_curl,&childstatus,WNOHANG) == child_curl)
    {
      killchildren();
      return WEXITSTATUS(childstatus);
    }
    sleep(1);
  }
	while(true);
}