Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (c) 2014-2015 Dmitry V. Levin <ldv (at) altlinux.org>
      3  * 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 
     28 #include <assert.h>
     29 #include <stddef.h>
     30 #include <stdio.h>
     31 #include <string.h>
     32 #include <unistd.h>
     33 #include <sys/wait.h>
     34 #include <sys/socket.h>
     35 #include <netinet/in.h>
     36 #include <arpa/inet.h>
     37 
     38 int main(void)
     39 {
     40 	static const char data[] = "data";
     41 	const size_t size = sizeof(data) - 1;
     42 	struct sockaddr_in addr;
     43 	socklen_t len = sizeof(addr);
     44 	pid_t pid;
     45 
     46 	memset(&addr, 0, sizeof(addr));
     47 	addr.sin_family = AF_INET;
     48 	addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     49 
     50 	close(0);
     51 	close(1);
     52 
     53 	if (socket(PF_INET, SOCK_STREAM, 0)) {
     54 		perror("socket");
     55 		return 77;
     56 	}
     57 	if (bind(0, (struct sockaddr *) &addr, len)) {
     58 		perror("bind");
     59 		return 77;
     60 	}
     61 	assert(listen(0, 5) == 0);
     62 
     63 	memset(&addr, 0, sizeof(addr));
     64 	assert(getsockname(0, (struct sockaddr *) &addr, &len) == 0);
     65 
     66 	assert((pid = fork()) >= 0);
     67 
     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 }
     88