summaryrefslogtreecommitdiff
path: root/third_party/heimdal/lib/roken/test-mini_inetd.c
blob: 079ace266a1f63bd24282102cb42f13663fed776 (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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/***********************************************************************
 * Copyright (c) 2009, Secure Endpoints Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **********************************************************************/

#include <config.h>
#include <roken.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define PORT 8013
#define PORT_S "8013"

char * prog = "Master";
int is_client = 0;

static int
get_address(int flags, struct addrinfo ** ret)
{
    struct addrinfo ai;
    int rv;

    memset(&ai, 0, sizeof(ai));

    ai.ai_flags = flags | AI_NUMERICHOST;
    ai.ai_family = AF_INET;
    ai.ai_socktype = SOCK_STREAM;
    ai.ai_protocol = PF_UNSPEC;

    rv = getaddrinfo("127.0.0.1", PORT_S, &ai, ret);
    if (rv)
	warnx("getaddrinfo: %s", gai_strerror(rv));
    return rv;
}

static int
get_connected_socket(rk_socket_t * s_ret)
{
    struct addrinfo * ai = NULL;
    int rv = 0;
    rk_socket_t s = rk_INVALID_SOCKET;

    rv = get_address(0, &ai);
    if (rv)
	return rv;

    s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
    if (rk_IS_BAD_SOCKET(s)) {
	rv = 1;
	goto done;
    }

    rv = connect(s, ai->ai_addr, ai->ai_addrlen);
    if (rk_IS_SOCKET_ERROR(rv))
	goto done;

    *s_ret = s;
    s = rk_INVALID_SOCKET;
    rv = 0;

 done:
    if (!rk_IS_BAD_SOCKET(s))
	rk_closesocket(s);

    if (ai)
	freeaddrinfo(ai);

    return (rv) ? rk_SOCK_ERRNO : 0;
}

const char * test_strings[] = {
    "Hello",
    "01234566789012345689012345678901234567890123456789",
    "Another test",
    "exit"
};

static int
test_simple_echo_client(void)
{
    rk_socket_t s = rk_INVALID_SOCKET;
    int rv;
    char buf[81];
    int i;

    fprintf(stderr, "[%s] Getting connected socket...", getprogname());
    rv = get_connected_socket(&s);
    if (rv) {
	fprintf(stderr, "\n[%s] get_connected_socket() failed (%s)\n",
		getprogname(), strerror(rk_SOCK_ERRNO));
	return 1;
    }

    fprintf(stderr, "[%s] done\n", getprogname());

    for (i=0; i < sizeof(test_strings)/sizeof(test_strings[0]); i++) {
	rv = send(s, test_strings[i], strlen(test_strings[i]), 0);
	if (rk_IS_SOCKET_ERROR(rv)) {
	    fprintf(stderr, "[%s] send() failure (%s)\n",
		    getprogname(), strerror(rk_SOCK_ERRNO));
	    rk_closesocket(s);
	    return 1;
	}

	rv = recv(s, buf, sizeof(buf), 0);
	if (rk_IS_SOCKET_ERROR(rv)) {
	    fprintf (stderr, "[%s] recv() failure (%s)\n",
		     getprogname(), strerror(rk_SOCK_ERRNO));
	    rk_closesocket(s);
	    return 1;
	}

	if (rv == 0) {
	    fprintf (stderr, "[%s] No data received\n", prog);
	    rk_closesocket(s);
	    return 1;
	}

	if (rv != strlen(test_strings[i])) {
	    fprintf (stderr, "[%s] Data length mismatch %d != %d\n", prog, rv, strlen(test_strings[i]));
	    rk_closesocket(s);
	    return 1;
	}
    }

    fprintf (stderr, "[%s] Done\n", prog);
    rk_closesocket(s);
    return 0;
}

static int
test_simple_echo_socket(void)
{
    fprintf (stderr, "[%s] Process ID %d\n", prog, GetCurrentProcessId());
    fprintf (stderr, "[%s] Starting echo test with sockets\n", prog);

    if (is_client) {
	return test_simple_echo_client();
    } else {

	rk_socket_t s = rk_INVALID_SOCKET;

	fprintf (stderr, "[%s] Listening for connections...\n", prog);
	mini_inetd(htons(PORT), &s);
	if (rk_IS_BAD_SOCKET(s)) {
	    fprintf (stderr, "[%s] Connect failed (%s)\n",
		     getprogname(), strerror(rk_SOCK_ERRNO));
	} else {
	    fprintf (stderr, "[%s] Connected\n", prog);
	}

	{
	    char buf[81];
	    int rv, srv;

	    while ((rv = recv(s, buf, sizeof(buf), 0)) != 0 && !rk_IS_SOCKET_ERROR(rv)) {
		buf[rv] = 0;
		fprintf(stderr, "[%s] Received [%s]\n", prog, buf);

		/* simple echo */
		srv = send(s, buf, rv, 0);
		if (srv != rv) {
		    if (rk_IS_SOCKET_ERROR(srv))
			fprintf(stderr, "[%s] send() error [%s]\n",
				getprogname(), strerror(rk_SOCK_ERRNO));
		    else
			fprintf(stderr, "[%s] send() size mismatch %d != %d",
				getprogname(), srv, rv);
		}

		if (strcmp(buf, "exit") == 0) {
		    fprintf(stderr, "[%s] Exiting...\n", prog);
		    shutdown(s, SD_SEND);
		    rk_closesocket(s);
		    return 0;
		}
	    }

	    fprintf(stderr, "[%s] recv() failed (%s)\n",
		    getprogname(),
		    strerror(rk_SOCK_ERRNO));
	}

	rk_closesocket(s);
    }

    return 1;
}

static int
test_simple_echo(void)
{
    fprintf (stderr, "[%s] Starting echo test\n", prog);

    if (is_client) {

	return test_simple_echo_client();

    } else {

	fprintf (stderr, "[%s] Listening for connections...\n", prog);
	mini_inetd(htons(PORT), NULL);
	fprintf (stderr, "[%s] Connected\n", prog);

	{
	    char buf[81];
	    while (gets(buf)) {
		fprintf(stderr, "[%s] Received [%s]\n", prog, buf);

		if (strcmp(buf, "exit") == 0)
		    return 0;

		/* simple echo */
		puts(buf);
	    }

	    fprintf(stderr, "[%s] gets() failed (%s)\n", prog, _strerror("gets"));
	}
    }

    return 1;
}

static int
do_client(void)
{
    int rv = 0;

    if (rk_SOCK_INIT())
	errx(1, "Failed to initialize sockets (%s)", strerror(rk_SOCK_ERRNO));

    prog = "Client";
    is_client = 1;

    fprintf(stderr, "Starting client...\n");

    rv = test_simple_echo_socket();

    rk_SOCK_EXIT();

    return rv;
}

static int
do_server(void)
{
    int rv = 0;

    if (rk_SOCK_INIT())
	errx(1, "Failed to initialize sockets (%s)", strerror(rk_SOCK_ERRNO));

    prog = "Server";

    fprintf(stderr, "Starting server...\n");

    rv = test_simple_echo_socket();

    rk_SOCK_EXIT();

    return rv;
}

static time_t
wait_callback(void *p)
{
    return (time_t)-1;
}

static int
do_test(char * path)
{
    intptr_t p_server;
    intptr_t p_client;
    int client_rv;
    int server_rv;

    p_server = _spawnl(_P_NOWAIT, path, path, "--server", NULL);
    if (p_server <= 0) {
	fprintf(stderr, "%s: %s", path, _strerror("Can't start server process"));
	return 1;
    }
#ifdef _WIN32
    /* On Windows, the _spawn*() functions return a process handle on
       success.  We need a process ID for use with
       wait_for_process_timed(). */

    p_server = GetProcessId((HANDLE) p_server);
#endif
    fprintf(stderr, "Created server process ID %d\n", p_server);

    p_client = _spawnl(_P_NOWAIT, path, path, "--client", NULL);
    if (p_client <= 0) {
	fprintf(stderr, "%s: %s", path, _strerror("Can't start client process"));
	fprintf(stderr, "Waiting for server process to terminate ...");
	wait_for_process_timed(p_server, wait_callback, NULL, 5);
	fprintf(stderr, "DONE\n");
	return 1;
    }
#ifdef _WIN32
    p_client = GetProcessId((HANDLE) p_client);
#endif
    fprintf(stderr, "Created client process ID %d\n", p_client);

    fprintf(stderr, "Waiting for client process to terminate ...");
    client_rv = wait_for_process_timed(p_client, wait_callback, NULL, 5);
    if (SE_IS_ERROR(client_rv)) {
	fprintf(stderr, "\nwait_for_process_timed() failed for client. rv=%d\n", client_rv);
    } else {
	fprintf(stderr, "DONE\n");
    }

    fprintf(stderr, "Waiting for server process to terminate ...");
    server_rv = wait_for_process_timed(p_server, wait_callback, NULL, 5);
    if (SE_IS_ERROR(server_rv)) {
	fprintf(stderr, "\nwait_for_process_timed() failed for server. rv=%d\n", server_rv);
    } else {
	fprintf(stderr, "DONE\n");
    }

    if (client_rv == 0 && server_rv == 0) {
	fprintf(stderr, "PASS\n");
	return 0;
    } else {
	fprintf(stderr, "FAIL: Client rv=%d, Server rv=%d\n", client_rv, server_rv);
	return 1;
    }
}

int main(int argc, char ** argv)
{
    setprogname(argv[0]);

    if (argc == 2 && strcmp(argv[1], "--client") == 0)
	return do_client();
    else if (argc == 2 && strcmp(argv[1], "--server") == 0)
	return do_server();
    else if (argc == 1)
	return do_test(argv[0]);
    else {
	printf ("%s: Test mini_inetd() function.  Run with no arguments to start test\n",
		argv[0]);
	return 1;
    }
}