test-unix-socket-shutdown.c 2.81 KB
Newer Older
Jonas Termansen's avatar
Jonas Termansen committed
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
/*
 * Copyright (c) 2017 Jonas 'Sortie' Termansen.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * test-unix-socket-shutdown.c
 * Tests whether unix sockets shut down correctly.
 */

#include <sys/socket.h>
#include <sys/wait.h>

#include <signal.h>
#include <stdbool.h>
#include <unistd.h>

#include "test.h"

static void test(bool test_read,
                 bool parent_shutdown,
                 bool child_shutdown,
                 bool sigpipe)
{
	int fds[2];
	test_assert(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
	pid_t child_pid;
	test_assert(0 <= (child_pid = fork()));
	if ( child_pid == 0 )
	{
		if ( !test_read && !sigpipe )
			signal(SIGPIPE, SIG_IGN);
		if ( child_shutdown )
		{
			if ( test_read )
				test_assert(shutdown(fds[0], SHUT_RD) == 0);
			else
				test_assert(shutdown(fds[0], SHUT_WR) == 0);
		}
		close(fds[1]);
		char c;
		if ( test_read )
		{
Jonas Termansen's avatar
Jonas Termansen committed
53 54 55
			ssize_t amount;
			test_assert(0 <= (amount = read(fds[0], &c, 1)));
			test_assertx(amount == 0);
Jonas Termansen's avatar
Jonas Termansen committed
56 57 58 59 60 61 62 63 64 65
			_exit(0);
		}
		else
		{
			while ( true )
			{
				c = 'X';
				ssize_t amount = write(fds[0], &c, 1);
				if ( !sigpipe && amount == -1 && errno == EPIPE )
					_exit(0);
Jonas Termansen's avatar
Jonas Termansen committed
66 67
				test_assert(0 <= amount);
				test_assertx(amount == 1);
Jonas Termansen's avatar
Jonas Termansen committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
			}
		}
	}
	close(fds[0]);
	if ( parent_shutdown )
	{
		if ( test_read )
			test_assert(shutdown(fds[1], SHUT_WR) == 0);
		else
			test_assert(shutdown(fds[1], SHUT_RD) == 0);
	}
	else if ( !parent_shutdown && !child_shutdown )
		close(fds[1]);
	int status;
	test_assert(waitpid(child_pid, &status, 0) == child_pid);
	if ( test_read || !sigpipe )
Jonas Termansen's avatar
Jonas Termansen committed
84
		test_assertx(WIFEXITED(status) && WEXITSTATUS(status) == 0);
Jonas Termansen's avatar
Jonas Termansen committed
85
	else
Jonas Termansen's avatar
Jonas Termansen committed
86
		test_assertx(WIFSIGNALED(status) && WTERMSIG(status) == SIGPIPE);
Jonas Termansen's avatar
Jonas Termansen committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	if ( parent_shutdown || child_shutdown )
		close(fds[1]);
}

int main(void)
{
	test(false, false, false, false);
	test(true, false, false, false);
	test(true, false, false, true);
	test(false, true, false, false);
	test(true, true, false, false);
	test(true, true, false, true);
	test(false, false, true, false);
	test(true, false, true, false);
	test(true, false, true, true);
	test(false, true, true, false);
	test(true, true, true, false);
	test(true, true, true, true);
	return 0;
}