From a4f89e21a6df0785f7e7714a7bf64d32f388e380 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sat, 14 Mar 2015 20:20:50 +0200 Subject: Whitebox test for get_interface_address6_via_udp_socket_hack(). Also, fix some whitespace mishaps. --- src/common/address.c | 8 ++++---- src/common/compat.c | 20 ++++++++++++++++++-- src/common/compat.h | 10 +++++++++- 3 files changed, 31 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/address.c b/src/common/address.c index 3e26576815..e4aad436fb 100644 --- a/src/common/address.c +++ b/src/common/address.c @@ -1529,13 +1529,14 @@ get_interface_address6_via_udp_socket_hack(int severity, goto err; } - if (connect(sock,(struct sockaddr *)&target_addr, addr_len) < 0) { + if (tor_connect_socket(sock,(struct sockaddr *)&target_addr, + addr_len) < 0) { int e = tor_socket_errno(sock); log_fn(severity, LD_NET, "connect() failed: %s", tor_socket_strerror(e)); goto err; } - if (getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) { + if (tor_getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) { int e = tor_socket_errno(sock); log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s", tor_socket_strerror(e)); @@ -1546,8 +1547,7 @@ get_interface_address6_via_udp_socket_hack(int severity, if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) { log_fn(severity, LD_NET, "Address that we determined via UDP socket" " magic is unsuitable for public comms."); - } - else { + } else { r=0; } } diff --git a/src/common/compat.c b/src/common/compat.c index fde65d9d15..18ab208aa8 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -1156,12 +1156,20 @@ mark_socket_open(tor_socket_t s) /** @} */ /** As socket(), but counts the number of open sockets. */ -tor_socket_t -tor_open_socket(int domain, int type, int protocol) +MOCK_IMPL(tor_socket_t, +tor_open_socket,(int domain, int type, int protocol)) { return tor_open_socket_with_extensions(domain, type, protocol, 1, 0); } +/** Mockable wrapper for connect(). */ +MOCK_IMPL(tor_socket_t, +tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address, + socklen_t address_len)) +{ + return connect(socket,address,address_len); +} + /** As socket(), but creates a nonblocking socket and * counts the number of open sockets. */ tor_socket_t @@ -1308,6 +1316,14 @@ get_n_open_sockets(void) return n; } +/** Mockable wrapper for getsockname(). */ +MOCK_IMPL(int, +tor_getsockname,(tor_socket_t socket, struct sockaddr *address, + socklen_t *address_len)) +{ + return getsockname(socket, address, address_len); +} + /** Turn socket into a nonblocking socket. Return 0 on success, -1 * on failure. */ diff --git a/src/common/compat.h b/src/common/compat.h index 23f8614196..18902e0928 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -451,7 +451,8 @@ int tor_close_socket(tor_socket_t s); tor_socket_t tor_open_socket_with_extensions( int domain, int type, int protocol, int cloexec, int nonblock); -tor_socket_t tor_open_socket(int domain, int type, int protocol); +MOCK_DECL(tor_socket_t, +tor_open_socket,(int domain, int type, int protocol)); tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol); tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len); @@ -462,8 +463,15 @@ tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len, int cloexec, int nonblock); +MOCK_DECL(tor_socket_t, +tor_connect_socket,(tor_socket_t socket,const struct sockaddr *address, + socklen_t address_len)); int get_n_open_sockets(void); +MOCK_DECL(int, +tor_getsockname,(tor_socket_t socket, struct sockaddr *address, + socklen_t *address_len)); + #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags) #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags) -- cgit v1.2.3-54-g00ecf