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.
This commit is contained in:
Simon Arlott 2017-08-24 20:03:51 +01:00
parent 4ce1cac4b9
commit e701026837
No known key found for this signature in database
GPG Key ID: C8975F2043CA5D24
6 changed files with 164 additions and 1 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -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) \

View File

@ -4,4 +4,5 @@ rb_dictionary1
rb_snprintf_append1
rb_snprintf_try_append1
send1
serv_connect1
substitution1

View File

@ -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;
}

140
tests/serv_connect1.c Normal file
View File

@ -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 <dlfcn.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#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;
}

17
tests/serv_connect1.conf Normal file
View File

@ -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;
};