]> jfr.im git - solanum.git/blob - tests/client_util.c
tests: Check sendto_wallops_flags works with format strings
[solanum.git] / tests / client_util.c
1 /*
2 * client_util.c: Utility functions for making test clients
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 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "tap/basic.h"
25
26 #include "client_util.h"
27
28 #include "hash.h"
29 #include "s_newconf.h"
30 #include "parse.h"
31 #include "listener.h"
32
33 #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
34
35 static struct Listener fake_listener = {
36 .next = NULL,
37 .name = "fake",
38 .F = NULL,
39 .ref_count = 0,
40 .active = 1,
41 .ssl = 1,
42 .defer_accept = 0,
43 .sctp = false,
44 .wsock = 0,
45 .addr = {
46 { .ss_family = AF_INET6 },
47 { .ss_family = AF_INET6 },
48 },
49 .vhost = { "fake" },
50 };
51
52 struct Client *make_local_unknown(void)
53 {
54 struct Client *client;
55
56 client = make_client(NULL);
57 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &lclient_list);
58 client->servptr = &me;
59 rb_dlinkAdd(client, &client->lnode, &client->servptr->serv->users);
60 client->localClient->listener = &fake_listener;
61 client->preClient->auth.accepted = true;
62
63 return client;
64 }
65
66 struct Client *make_local_person(void)
67 {
68 return make_local_person_nick(TEST_NICK);
69 }
70
71 struct Client *make_local_person_nick(const char *nick)
72 {
73 return make_local_person_full(nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
74 }
75
76 struct Client *make_local_person_full(const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
77 {
78 struct Client *client;
79
80 client = make_local_unknown();
81 make_user(client);
82 SetClient(client);
83
84 rb_inet_pton_sock(ip, &client->localClient->ip);
85 rb_strlcpy(client->name, nick, sizeof(client->name));
86 rb_strlcpy(client->username, username, sizeof(client->username));
87 rb_strlcpy(client->host, hostname, sizeof(client->host));
88 rb_inet_ntop_sock((struct sockaddr *)&client->localClient->ip, client->sockhost, sizeof(client->sockhost));
89 rb_strlcpy(client->info, realname, sizeof(client->info));
90
91 add_to_client_hash(client->name, client);
92
93 return client;
94 }
95
96 void make_local_person_oper(struct Client *client)
97 {
98 rb_dlinkAddAlloc(client, &local_oper_list);
99 rb_dlinkAddAlloc(client, &oper_list);
100 SetOper(client);
101 }
102
103 void remove_local_person(struct Client *client)
104 {
105 exit_client(NULL, client, &me, "Test client removed");
106 }
107
108 struct Client *make_remote_server(struct Client *uplink)
109 {
110 return make_remote_server_name(uplink, TEST_SERVER_NAME);
111 }
112
113 struct Client *make_remote_server_name(struct Client *uplink, const char *name)
114 {
115 return make_remote_server_full(uplink, name, "");
116 }
117
118 struct Client *make_remote_server_full(struct Client *uplink, const char *name, const char *id)
119 {
120 struct Client *client;
121
122 client = make_client(NULL);
123 client->servptr = uplink;
124
125 attach_server_conf(client, find_server_conf(name));
126
127 rb_strlcpy(client->name, name, sizeof(client->name));
128 rb_strlcpy(client->id, id, sizeof(client->id));
129
130 rb_dlinkAdd(client, &client->lnode, &uplink->serv->servers);
131 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &serv_list);
132 rb_dlinkAddTailAlloc(client, &global_serv_list);
133
134 make_server(client);
135 SetServer(client);
136
137 add_to_client_hash(client->name, client);
138 if (strlen(id))
139 add_to_id_hash(client->id, client);
140
141 return client;
142 }
143
144 struct Client *make_remote_person(struct Client *server)
145 {
146 return make_remote_person_nick(server, TEST_REMOTE_NICK);
147 }
148
149 struct Client *make_remote_person_nick(struct Client *server, const char *nick)
150 {
151 return make_remote_person_full(server, nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
152 }
153
154 struct Client *make_remote_person_full(struct Client *server, const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
155 {
156 struct Client *client;
157 struct sockaddr_storage addr;
158
159 client = make_client(server);
160 make_user(client);
161 SetRemoteClient(client);
162
163 client->servptr = server;
164 rb_dlinkAdd(server, &server->lnode, &server->servptr->serv->users);
165
166 rb_inet_pton_sock(ip, &addr);
167 rb_strlcpy(client->name, nick, sizeof(client->name));
168 rb_strlcpy(client->username, username, sizeof(client->username));
169 rb_strlcpy(client->host, hostname, sizeof(client->host));
170 rb_inet_ntop_sock((struct sockaddr *)&addr, client->sockhost, sizeof(client->sockhost));
171 rb_strlcpy(client->info, realname, sizeof(client->info));
172
173 add_to_client_hash(nick, client);
174 add_to_hostname_hash(client->host, client);
175
176 return client;
177 }
178
179 void make_remote_person_oper(struct Client *client)
180 {
181 rb_dlinkAddAlloc(client, &oper_list);
182 SetOper(client);
183 }
184
185 void remove_remote_person(struct Client *client)
186 {
187 exit_client(client, client->servptr, client->servptr, "Test client removed");
188 }
189
190 void remove_remote_server(struct Client *server)
191 {
192 exit_client(server, server, server->servptr, "Test server removed");
193 }
194
195 struct Channel *make_channel(void)
196 {
197 return allocate_channel(TEST_CHANNEL);
198 }
199
200 char *get_client_sendq(const struct Client *client)
201 {
202 static char buf[EXT_BUFSIZE + sizeof(CRLF)];
203
204 if (rb_linebuf_len(&client->localClient->buf_sendq)) {
205 int ret;
206
207 memset(buf, 0, sizeof(buf));
208 ret = rb_linebuf_get(&client->localClient->buf_sendq, buf, sizeof(buf), 0, 1);
209
210 if (ok(ret > 0, MSG)) {
211 return buf;
212 } else {
213 return "<get_client_sendq error>";
214 }
215 }
216
217 return "";
218 }
219
220 void client_util_parse(struct Client *client, const char *message)
221 {
222 char *copy = rb_strdup(message);
223
224 parse(client, copy, copy+strlen(copy));
225
226 rb_free(copy);
227 }
228
229 void client_util_init(void)
230 {
231 }
232
233 void client_util_free(void)
234 {
235 }