blob: 334aeb7b0c4b648c6aacd51d12ae009664876937 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levinaf364e12016-01-05 23:04:30 +00002 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Dmitry V. Levinaf364e12016-01-05 23:04:30 +000028#include "tests.h"
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000029#include <assert.h>
30#include <stddef.h>
Dmitry V. Levin552bc3a2015-01-14 11:32:38 +000031#include <stdio.h>
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000032#include <string.h>
33#include <unistd.h>
34#include <sys/wait.h>
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
38
39int main(void)
40{
41 static const char data[] = "data";
42 const size_t size = sizeof(data) - 1;
43 struct sockaddr_in addr;
44 socklen_t len = sizeof(addr);
45 pid_t pid;
46
47 memset(&addr, 0, sizeof(addr));
48 addr.sin_family = AF_INET;
49 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
50
51 close(0);
52 close(1);
53
Dmitry V. Levinaf364e12016-01-05 23:04:30 +000054 if (socket(PF_INET, SOCK_STREAM, 0))
55 perror_msg_and_skip("socket");
56 if (bind(0, (struct sockaddr *) &addr, len))
57 perror_msg_and_skip("bind");
58 if (listen(0, 5))
59 perror_msg_and_skip("listen");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000060
61 memset(&addr, 0, sizeof(addr));
62 assert(getsockname(0, (struct sockaddr *) &addr, &len) == 0);
63
Dmitry V. Levinaf364e12016-01-05 23:04:30 +000064 pid = fork();
65 if (pid < 0)
66 perror_msg_and_fail("fork");
Dmitry V. Levinfdfa7222014-09-23 00:14:04 +000067
68 if (pid) {
69 char buf[sizeof(data)];
70 int status;
71
72 assert(accept(0, (struct sockaddr *) &addr, &len) == 1);
73 assert(close(0) == 0);
74 assert(recv(1, buf, sizeof(buf), MSG_WAITALL) == (int) size);
75 assert(waitpid(pid, &status, 0) == pid);
76 assert(status == 0);
77 assert(close(1) == 0);
78 } else {
79 assert(close(0) == 0);
80 assert(socket(PF_INET, SOCK_STREAM, 0) == 0);
81 assert(connect(0, (struct sockaddr *) &addr, len) == 0);
82 assert(send(0, data, size, MSG_DONTROUTE) == (int) size);
83 assert(close(0) == 0);
84 }
85
86 return 0;
87}