Ejemplo n.º 1
0
int main(void)
{
	struct data *d = malloc(sizeof(*d));
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(22);
	d->state = 0;
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(fd, init_conn, d);
	ok1(l);
	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		write_to_socket("hellothere", addrinfo);
		freeaddrinfo(addrinfo);
		free(d);
		exit(0);
	}
	ok1(io_loop() == d);
	ok1(d->state == 2);
	ok1(d->bytes > 0);
	ok1(d->bytes <= sizeof(d->buf));
	ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		write_to_socket("hi", addrinfo);
		freeaddrinfo(addrinfo);
		free(d);
		exit(0);
	}
	d->state = 0;
	ok1(io_loop() == d);
	ok1(d->state == 2);
	ok1(d->bytes > 0);
	ok1(d->bytes <= strlen("hi"));
	ok1(memcmp(d->buf, "hi", d->bytes) == 0);

	freeaddrinfo(addrinfo);
	free(d);
	io_close_listener(l);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 2
0
int main(void)
{
	struct packet *pkt = malloc(sizeof(*pkt));
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(13);
	pkt->state = 0;
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(fd, init_conn, pkt);
	ok1(l);
	fflush(stdout);
	if (!fork()) {
		struct {
			size_t len;
			char data[8];
		} data;

		io_close_listener(l);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		signal(SIGPIPE, SIG_IGN);

		data.len = sizeof(data.data);
		memcpy(data.data, "hithere!", sizeof(data.data));
		if (write(fd, &data, sizeof(data)) != sizeof(data))
			exit(3);

		close(fd);
		freeaddrinfo(addrinfo);
		free(pkt);
		exit(0);
	}
	freeaddrinfo(addrinfo);
	ok1(io_loop() == pkt);
	ok1(pkt->state == 4);
	ok1(pkt->len == 8);
	ok1(memcmp(pkt->contents, "hithere!", 8) == 0);
	free(pkt->contents);
	free(pkt);
	io_close_listener(l);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 3
0
int main(void)
{
	struct data *d = malloc(sizeof(*d));
	struct addrinfo *addrinfo;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(9);
	d->state = 0;
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	d->l = io_new_listener(NULL, fd, init_conn, d);
	ok1(d->l);
	fflush(stdout);
	if (!fork()) {
		int i;
		char buf[32];

		io_close_listener(d->l);
		free(d);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		signal(SIGPIPE, SIG_IGN);
		for (i = 0; i < 32; i++) {
			if (read(fd, buf+i, 1) != 1)
				break;
		}
		for (i = 0; i < strlen("hellothere"); i++) {
			if (write(fd, "hellothere" + i, 1) != 1)
				break;
		}
		close(fd);
		freeaddrinfo(addrinfo);
		exit(0);
	}
	freeaddrinfo(addrinfo);
	ok1(io_loop(NULL, NULL) == NULL);
	ok1(d->state == 4);
	ok1(memcmp(d->buf, "hellothere", sizeof(d->buf)) == 0);
	free(d);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 4
0
int net_bind(const struct addrinfo *addrinfo, int fds[2])
{
	const struct addrinfo *ipv6 = NULL;
	const struct addrinfo *ipv4 = NULL;
	unsigned int num;

	if (addrinfo->ai_family == AF_INET)
		ipv4 = addrinfo;
	else if (addrinfo->ai_family == AF_INET6)
		ipv6 = addrinfo;

	if (addrinfo->ai_next) {
		if (addrinfo->ai_next->ai_family == AF_INET)
			ipv4 = addrinfo->ai_next;
		else if (addrinfo->ai_next->ai_family == AF_INET6)
			ipv6 = addrinfo->ai_next;
	}

	num = 0;
	/* Take IPv6 first, since it might bind to IPv4 port too. */
	if (ipv6) {
		if ((fds[num] = make_listen_fd(ipv6)) >= 0)
			num++;
		else
			ipv6 = NULL;
	}
	if (ipv4) {
		if ((fds[num] = make_listen_fd(ipv4)) >= 0)
			num++;
		else
			ipv4 = NULL;
	}
	if (num == 0)
		return -1;

	return num;
}
Ejemplo n.º 5
0
int main(void)
{
	struct addrinfo *addrinfo = NULL;
	int fd, status;
	struct data d;

	/* This is how many tests you plan to run */
	plan_tests(8);
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	d.l = io_new_listener(NULL, fd, init_conn, &d);
	ok1(d.l);
	fflush(stdout);

	if (!fork()) {
		io_close_listener(d.l);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		signal(SIGPIPE, SIG_IGN);

		if (write(fd, "1hellothere", strlen("1hellothere")) != strlen("1hellothere"))
			exit(3);
		sleep(1);
		if (write(fd, "1helloagain", strlen("1helloagain")) != strlen("1helloagain"))
			exit(4);
		close(fd);
		freeaddrinfo(addrinfo);
		exit(0);
	}
	freeaddrinfo(addrinfo);

	d.pattern = tal_arrz(NULL, char, 1);
	ok1(io_loop(NULL, NULL) == NULL);
	/* No trace of writes */
	ok1(strcmp(d.pattern, "<1hellothere<1helloagain") == 0);
	tal_free(d.pattern);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 6
0
int main(void)
{
	int state = 0;
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd;

	/* This is how many tests you plan to run */
	plan_tests(12);
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(NULL, fd, init_conn, &state);
	ok1(l);
	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		close(fd);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(3);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(4);
		close(fd);
		freeaddrinfo(addrinfo);
		exit(0);
	}
	freeaddrinfo(addrinfo);
	ok1(io_loop(NULL, NULL) == &state + 1);
	ok1(state == 4);
	io_close_listener(l);
	ok1(wait(&state));
	ok1(WIFEXITED(state));
	ok1(WEXITSTATUS(state) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 7
0
int main(void)
{
	struct data *d = malloc(sizeof(*d));
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(9);
	d->state = 0;
	d->bytes = 1024*1024;
	d->buf = malloc(d->bytes);
	memset(d->buf, 'a', d->bytes);
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(fd, init_conn, d);
	ok1(l);
	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		read_from_socket(d->bytes, addrinfo);
		freeaddrinfo(addrinfo);
		free(d->buf);
		free(d);
		exit(0);
	}
	ok1(io_loop() == d);
	ok1(d->state == 2);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	freeaddrinfo(addrinfo);
	free(d->buf);
	free(d);
	io_close_listener(l);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 8
0
int main(void)
{
	struct io_listener *l;
	int fd;
	struct timers timers;
	struct timer timer, *expired;
	struct addrinfo *addrinfo;

	/* This is how many tests you plan to run */
	plan_tests(7);

	fake_time = time_now();

	timers_init(&timers, fake_time);
	timer_init(&timer);
	timer_add(&timers, &timer,
		  timeabs_add(fake_time, time_from_sec(1000)));

	fd = make_listen_fd(PORT, &addrinfo);
	freeaddrinfo(addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(NULL, fd, init_conn, NULL);
	ok1(l);

	fake_time.ts.tv_sec += 1000;
	ok1(io_time_override(get_fake_time) == time_now);
	ok1(io_loop(&timers, &expired) == NULL);

	ok1(expired == &timer);
	ok1(!timers_expire(&timers, fake_time));
	ok1(io_time_override(time_now) == get_fake_time);
	io_close_listener(l);

	timers_cleanup(&timers);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 9
0
int main(void)
{
	struct data *d = malloc(sizeof(*d));
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(20);
	d->state = 0;
	d->timed_out = false;
	d->timeout_usec = 100000;
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(fd, init_conn, d);
	ok1(l);
	fflush(stdout);

	if (!fork()) {
		int i;

		io_close_listener(l);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		signal(SIGPIPE, SIG_IGN);
		usleep(500000);
		for (i = 0; i < strlen("hellothere"); i++) {
			if (write(fd, "hellothere" + i, 1) != 1)
				break;
		}
		close(fd);
		freeaddrinfo(addrinfo);
		free(d);
		exit(i);
	}
	ok1(io_loop() == d);
	ok1(d->state == 3);
	ok1(d->timed_out == true);
	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) < sizeof(d->buf));

	/* This one shouldn't time out. */
	d->state = 0;
	d->timed_out = false;
	d->timeout_usec = 500000;
	fflush(stdout);

	if (!fork()) {
		int i;

		io_close_listener(l);
		fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
			    addrinfo->ai_protocol);
		if (fd < 0)
			exit(1);
		if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
			exit(2);
		signal(SIGPIPE, SIG_IGN);
		usleep(100000);
		for (i = 0; i < strlen("hellothere"); i++) {
			if (write(fd, "hellothere" + i, 1) != 1)
				break;
		}
		close(fd);
		freeaddrinfo(addrinfo);
		free(d);
		exit(i);
	}
	ok1(io_loop() == d);
	ok1(d->state == 3);
	ok1(d->timed_out == false);
	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) >= sizeof(d->buf));

	io_close_listener(l);
	freeaddrinfo(addrinfo);
	free(d);

	/* This exits depending on whether all tests passed */
	return exit_status();
}