]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * serv_connect_exit_unknown_client1.c: Test serv_connect followed by exit_unknown_client | |
3 | * Copyright 2017 Simon Arlott | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
18 | * USA | |
19 | */ | |
20 | #define _GNU_SOURCE | |
21 | #include <dlfcn.h> | |
22 | #include <errno.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include "tap/basic.h" | |
28 | ||
29 | #include "ircd_util.h" | |
30 | #include "client_util.h" | |
31 | ||
32 | #include "s_serv.h" | |
33 | #include "s_conf.h" | |
34 | #include "s_newconf.h" | |
35 | #include "hash.h" | |
36 | ||
37 | #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__ | |
38 | ||
39 | static rb_fde_t *last_F = NULL; | |
40 | static CNCB *last_connect_callback = NULL; | |
41 | static void *last_connect_data = NULL; | |
42 | ||
43 | int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) | |
44 | { | |
45 | printf("# connect(%d, ...)\n", sockfd); | |
46 | errno = EINPROGRESS; | |
47 | return -1; | |
48 | } | |
49 | ||
50 | void rb_connect_tcp(rb_fde_t *F, struct sockaddr *dest, struct sockaddr *clocal, CNCB *callback, void *data, int timeout) | |
51 | { | |
52 | printf("# rb_connect_tcp(%p, ...)\n", F); | |
53 | ||
54 | last_F = F; | |
55 | last_connect_callback = callback; | |
56 | last_connect_data = data; | |
57 | ||
58 | void *(*func)() = dlsym(RTLD_NEXT, "rb_connect_tcp"); | |
59 | func(F, dest, clocal, callback, data, timeout); | |
60 | } | |
61 | ||
62 | static void basic_test(void) | |
63 | { | |
64 | struct server_conf *server = NULL; | |
65 | rb_dlink_node *ptr; | |
66 | ||
67 | RB_DLINK_FOREACH(ptr, server_conf_list.head) | |
68 | { | |
69 | server = ptr->data; | |
70 | } | |
71 | ||
72 | /* The server doesn't exist before */ | |
73 | ok(find_server(NULL, server->name) == NULL, MSG); | |
74 | ||
75 | /* The server doesn't exist during the connection attempt */ | |
76 | is_int(1, serv_connect(server, NULL), MSG); | |
77 | ok(find_server(NULL, server->name) == NULL, MSG); | |
78 | ||
79 | if (ok(last_connect_callback != NULL, MSG)) { | |
80 | last_connect_callback(last_F, RB_ERR_CONNECT, last_connect_data); | |
81 | } | |
82 | ||
83 | /* The server doesn't exist after the connection attempt fails */ | |
84 | ok(find_server(NULL, server->name) == NULL, MSG); | |
85 | } | |
86 | ||
87 | static void incoming_during_outgoing(void) | |
88 | { | |
89 | struct server_conf *server = NULL; | |
90 | struct Client *incoming = NULL; | |
91 | rb_dlink_node *ptr; | |
92 | ||
93 | RB_DLINK_FOREACH(ptr, server_conf_list.head) | |
94 | { | |
95 | server = ptr->data; | |
96 | } | |
97 | ||
98 | printf("# Test server = %s\n", server->name); | |
99 | ||
100 | /* The server doesn't exist before */ | |
101 | ok(find_server(NULL, server->name) == NULL, MSG); | |
102 | ||
103 | /* The server doesn't exist during the connection attempt */ | |
104 | is_int(1, serv_connect(server, NULL), MSG); | |
105 | ok(find_server(NULL, server->name) == NULL, MSG); | |
106 | ||
107 | /* The server makes its own incoming connection */ | |
108 | incoming = make_remote_server_full(&me, server->name, TEST_SERVER_ID); | |
109 | ok(find_server(NULL, server->name) == incoming, MSG); | |
110 | ok(find_id(TEST_SERVER_ID) == incoming, MSG); | |
111 | ||
112 | if (ok(last_connect_callback != NULL, MSG)) { | |
113 | /* This will call exit_unknown_client on our outgoing connection */ | |
114 | last_connect_callback(last_F, RB_ERR_CONNECT, last_connect_data); | |
115 | } | |
116 | ||
117 | /* The incoming server should still be here */ | |
118 | ok(find_server(NULL, server->name) == incoming, MSG); | |
119 | ok(find_id(TEST_SERVER_ID) == incoming, MSG); | |
120 | ||
121 | remove_remote_server(incoming); | |
122 | ||
123 | ok(find_server(NULL, server->name) == NULL, MSG); | |
124 | ok(find_id(TEST_SERVER_ID) == NULL, MSG); | |
125 | } | |
126 | ||
127 | int main(int argc, char *argv[]) | |
128 | { | |
129 | plan_lazy(); | |
130 | ||
131 | ircd_util_init(__FILE__); | |
132 | client_util_init(); | |
133 | ||
134 | basic_test(); | |
135 | incoming_during_outgoing(); | |
136 | ||
137 | client_util_free(); | |
138 | ircd_util_free(); | |
139 | return 0; | |
140 | } |