File: test_getpeername.c

package info (click to toggle)
torsocks 2.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 916 kB
  • sloc: ansic: 7,437; sh: 1,038; makefile: 105
file content (120 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2014 - David Goulet <dgoulet@ev0ke.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2 only, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>

#include <lib/torsocks.h>

#include <tap/tap.h>
#include "helpers.h"

#define NUM_TESTS 7

static void test_getpeername(void)
{
	int pipe_fds[2], ret, inet_sock = -1;
	char buf[INET_ADDRSTRLEN];
	struct sockaddr addr;
	struct sockaddr_in addrv4;
	struct sockaddr_storage ss;
	socklen_t addrlen;
	/* perdulce.torproject.org */
	const char *ip = "49.12.57.140";

	ret = pipe(pipe_fds);
	if (ret < 0) {
		fail("Unable to create pipe");
		goto error;
	}

	/* This test is to see if we go through the libc or not. */
	ret = getpeername(pipe_fds[0], NULL, NULL);
	ok(ret == -1 && errno == ENOTSOCK, "Invalid socket fd");

	close(pipe_fds[0]);
	close(pipe_fds[1]);

	/* Create inet socket. */
	inet_sock = socket(AF_INET, SOCK_STREAM, 0);
	ok(inet_sock >= 0, "Inet socket created");

	/* This test is to see if we go through the libc or not. */
	ret = getpeername(inet_sock, &addr, &addrlen);
	ok(ret == -1 && errno == ENOTCONN, "Socket not connected");

	/* Connect socket through Tor so we can test the wrapper. */
	addrv4.sin_family = AF_INET;
	addrv4.sin_port = htons(80);
	inet_pton(addrv4.sin_family, ip, &addrv4.sin_addr);
	memset(addrv4.sin_zero, 0, sizeof(addrv4.sin_zero));

	ret = connect(inet_sock, (struct sockaddr *) &addrv4, sizeof(addrv4));
	if (ret < 0) {
		fail("Unable to connect to %s", ip);
		goto error;
	}

	/* Invalid arguments */
	addrlen = sizeof(addr);
	ret = getpeername(inet_sock, NULL, &addrlen);
	ok(ret == -1 && errno == EFAULT, "Invalid addr ptr");

	ret = getpeername(inet_sock, &addr, NULL);
	ok(ret == -1 && errno == EFAULT, "Invalid addrlen ptr");

	addrlen = sizeof(addrv4);
	memset(&addrv4, 0, addrlen);
	ret = getpeername(inet_sock, (struct sockaddr *) &addrv4, &addrlen);

	/* Validate returned IP address. */
	memset(buf, 0, sizeof(buf));
	inet_ntop(addrv4.sin_family, &addrv4.sin_addr, buf, sizeof(buf));
	ok(ret == 0 && strncmp(buf, ip, strlen(ip)) == 0,
			"Valid returned IP address from getpeername()");

	/* Large but valid addrlen. */
	addrlen = sizeof(ss);
	ret = getpeername(inet_sock, (struct sockaddr *)&ss, &addrlen);
	ok(ret == 0 && addrlen == sizeof(addrv4),
			"Valid returned IP address from getpeername(), large addrlen");

error:
	if (inet_sock >= 0) {
		close(inet_sock);
	}
	return;
}

int main(int argc, char **argv)
{
	/* Try to connect to SocksPort localhost:9050 and if we can't skip. This is
	 * to avoid to have failing test if no tor daemon is available. */
	if (!helper_is_default_tor_running()) {
		goto end;
	}

	/* Libtap call for the number of tests planned. */
	plan_tests(NUM_TESTS);

	test_getpeername();

end:
	return exit_status();
}