From e7010268374db34489ffd8365a55edd732111f95 Mon Sep 17 00:00:00 2001 From: Simon Arlott Date: Thu, 24 Aug 2017 20:03:51 +0100 Subject: [PATCH] tests: add serv_connect1 (exit_unknown_client) Test serv_connect followed by exit_unknown_client. An outgoing connection should not delete an incoming connection that occurs during the connection attempt. --- .gitignore | 1 + tests/Makefile.am | 4 +- tests/TESTS | 1 + tests/client_util.c | 2 + tests/serv_connect1.c | 140 +++++++++++++++++++++++++++++++++++++++ tests/serv_connect1.conf | 17 +++++ 6 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 tests/serv_connect1.c create mode 100644 tests/serv_connect1.conf diff --git a/.gitignore b/.gitignore index 34aafeb9..39347b7e 100644 --- a/.gitignore +++ b/.gitignore @@ -63,6 +63,7 @@ tests/rb_dictionary1 tests/rb_snprintf_append1 tests/rb_snprintf_try_append1 tests/send1 +tests/serv_connect1 tests/substitution1 tests/runtests tests/*.c.ban.db diff --git a/tests/Makefile.am b/tests/Makefile.am index 500fca16..3318c876 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,11 +5,12 @@ check_PROGRAMS = runtests \ rb_snprintf_append1 \ rb_snprintf_try_append1 \ send1 \ + serv_connect1 \ substitution1 AM_CFLAGS=$(WARNFLAGS) AM_CPPFLAGS = $(DEFAULT_INCLUDES) -I../librb/include -I.. AM_LDFLAGS = -no-install -LDADD = tap/libtap.a ../librb/src/librb.la ../ircd/libircd.la +LDADD = tap/libtap.a ../librb/src/librb.la ../ircd/libircd.la -ldl # Override -rpath or programs will be linked to installed libraries libdir=$(abs_top_builddir) @@ -26,6 +27,7 @@ rb_dictionary1_SOURCES = rb_dictionary1.c rb_snprintf_append1_SOURCES = rb_snprintf_append1.c rb_snprintf_try_append1_SOURCES = rb_snprintf_try_append1.c send1_SOURCES = send1.c ircd_util.c client_util.c +serv_connect1_SOURCES = serv_connect1.c ircd_util.c client_util.c substitution1_SOURCES = substitution1.c check-local: $(check_PROGRAMS) \ diff --git a/tests/TESTS b/tests/TESTS index b6e1ded6..ce1b3e7d 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -4,4 +4,5 @@ rb_dictionary1 rb_snprintf_append1 rb_snprintf_try_append1 send1 +serv_connect1 substitution1 diff --git a/tests/client_util.c b/tests/client_util.c index 2778f56d..04c22e0b 100644 --- a/tests/client_util.c +++ b/tests/client_util.c @@ -105,6 +105,8 @@ struct Client *make_remote_server_full(struct Client *uplink, const char *name, SetServer(client); add_to_client_hash(client->name, client); + if (strlen(id)) + add_to_id_hash(client->id, client); return client; } diff --git a/tests/serv_connect1.c b/tests/serv_connect1.c new file mode 100644 index 00000000..df89c57b --- /dev/null +++ b/tests/serv_connect1.c @@ -0,0 +1,140 @@ +/* + * serv_connect_exit_unknown_client1.c: Test serv_connect followed by exit_unknown_client + * Copyright 2017 Simon Arlott + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "tap/basic.h" + +#include "ircd_util.h" +#include "client_util.h" + +#include "s_serv.h" +#include "s_conf.h" +#include "s_newconf.h" +#include "hash.h" + +#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ + +static rb_fde_t *last_F = NULL; +static CNCB *last_connect_callback = NULL; +static void *last_connect_data = NULL; + +int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + printf("# connect(%d, ...)\n", sockfd); + errno = EINPROGRESS; + return -1; +} + +void rb_connect_tcp(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, CNCB *callback, void *data, int timeout) +{ + printf("# rb_connect_tcp(%p, ...)\n", F); + + last_F = F; + last_connect_callback = callback; + last_connect_data = data; + + void *(*func)() = dlsym(RTLD_NEXT, "rb_connect_tcp"); + func(F, dest, clocal, callback, data, timeout); +} + +static void basic_test(void) +{ + struct server_conf *server = NULL; + rb_dlink_node *ptr; + + RB_DLINK_FOREACH(ptr, server_conf_list.head) + { + server = ptr->data; + } + + /* The server doesn't exist before */ + ok(find_server(NULL, server->name) == NULL, MSG); + + /* The server doesn't exist during the connection attempt */ + is_int(1, serv_connect(server, NULL), MSG); + ok(find_server(NULL, server->name) == NULL, MSG); + + if (ok(last_connect_callback != NULL, MSG)) { + last_connect_callback(last_F, RB_ERR_CONNECT, last_connect_data); + } + + /* The server doesn't exist after the connection attempt fails */ + ok(find_server(NULL, server->name) == NULL, MSG); +} + +static void incoming_during_outgoing(void) +{ + struct server_conf *server = NULL; + struct Client *incoming = NULL; + rb_dlink_node *ptr; + + RB_DLINK_FOREACH(ptr, server_conf_list.head) + { + server = ptr->data; + } + + printf("# Test server = %s\n", server->name); + + /* The server doesn't exist before */ + ok(find_server(NULL, server->name) == NULL, MSG); + + /* The server doesn't exist during the connection attempt */ + is_int(1, serv_connect(server, NULL), MSG); + ok(find_server(NULL, server->name) == NULL, MSG); + + /* The server makes its own incoming connection */ + incoming = make_remote_server_full(&me, server->name, TEST_SERVER_ID); + ok(find_server(NULL, server->name) == incoming, MSG); + ok(find_id(TEST_SERVER_ID) == incoming, MSG); + + if (ok(last_connect_callback != NULL, MSG)) { + /* This will call exit_unknown_client on our outgoing connection */ + last_connect_callback(last_F, RB_ERR_CONNECT, last_connect_data); + } + + /* The incoming server should still be here */ + ok(find_server(NULL, server->name) == incoming, MSG); + ok(find_id(TEST_SERVER_ID) == incoming, MSG); + + remove_remote_server(incoming); + + ok(find_server(NULL, server->name) == NULL, MSG); + ok(find_id(TEST_SERVER_ID) == NULL, MSG); +} + +int main(int argc, char *argv[]) +{ + plan_lazy(); + + ircd_util_init(__FILE__); + client_util_init(); + + basic_test(); + incoming_during_outgoing(); + + client_util_free(); + ircd_util_free(); + return 0; +} diff --git a/tests/serv_connect1.conf b/tests/serv_connect1.conf new file mode 100644 index 00000000..8981e67b --- /dev/null +++ b/tests/serv_connect1.conf @@ -0,0 +1,17 @@ +serverinfo { + sid = "0AA"; + name = "me.test"; + description = "Test server"; + network_name = "Test network"; +}; + +connect "remote.test" { + host = "::1"; + fingerprint = "test"; + class = "default"; +}; + +privset "admin" { + privs = oper:admin; +}; +