]> jfr.im git - solanum.git/blame - tests/client_util.c
fix two headings to be toplevel
[solanum.git] / tests / client_util.c
CommitLineData
d2b5f411
SA
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
d2b5f411
SA
26#include "client_util.h"
27
60f1d711
SA
28#include "hash.h"
29#include "s_newconf.h"
30
d2b5f411
SA
31#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
32
33struct Client *make_local_person(void)
34{
35 return make_local_person_nick(TEST_NICK);
36}
37
38struct Client *make_local_person_nick(const char *nick)
39{
40 return make_local_person_full(nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
41}
42
43struct Client *make_local_person_full(const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
44{
45 struct Client *client;
46
47 client = make_client(NULL);
48 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &lclient_list);
49 client->servptr = &me;
50 rb_dlinkAdd(client, &client->lnode, &client->servptr->serv->users);
d2b5f411 51 make_user(client);
60f1d711 52 SetClient(client);
d2b5f411
SA
53
54 rb_inet_pton_sock(ip, (struct sockaddr *)&client->localClient->ip);
55 rb_strlcpy(client->name, nick, sizeof(client->name));
56 rb_strlcpy(client->username, username, sizeof(client->username));
57 rb_strlcpy(client->host, hostname, sizeof(client->host));
58 rb_inet_ntop_sock((struct sockaddr *)&client->localClient->ip, client->sockhost, sizeof(client->sockhost));
59 rb_strlcpy(client->info, realname, sizeof(client->info));
60
60f1d711
SA
61 add_to_client_hash(client->name, client);
62
d2b5f411
SA
63 return client;
64}
65
6af47466
SA
66void make_local_person_oper(struct Client *client)
67{
68 rb_dlinkAddAlloc(client, &local_oper_list);
69 rb_dlinkAddAlloc(client, &oper_list);
70 SetOper(client);
71}
72
d2b5f411
SA
73void remove_local_person(struct Client *client)
74{
75 exit_client(NULL, client, &me, "Test client removed");
76}
77
60f1d711
SA
78struct Client *make_remote_server(struct Client *uplink)
79{
80 return make_remote_server_name(uplink, TEST_SERVER_NAME);
81}
82
83struct Client *make_remote_server_name(struct Client *uplink, const char *name)
84{
85 return make_remote_server_full(uplink, name, "");
86}
87
88struct Client *make_remote_server_full(struct Client *uplink, const char *name, const char *id)
89{
90 struct Client *client;
91
92 client = make_client(NULL);
93 client->servptr = uplink;
94
95 attach_server_conf(client, find_server_conf(name));
96
97 rb_strlcpy(client->name, name, sizeof(client->name));
98 rb_strlcpy(client->id, id, sizeof(client->id));
99
100 rb_dlinkAdd(client, &client->lnode, &uplink->serv->servers);
101 rb_dlinkMoveNode(&client->localClient->tnode, &unknown_list, &serv_list);
102 rb_dlinkAddTailAlloc(client, &global_serv_list);
103
104 make_server(client);
105 SetServer(client);
106
107 add_to_client_hash(client->name, client);
e7010268
SA
108 if (strlen(id))
109 add_to_id_hash(client->id, client);
60f1d711
SA
110
111 return client;
112}
113
114struct Client *make_remote_person(struct Client *server)
115{
116 return make_remote_person_nick(server, TEST_REMOTE_NICK);
117}
118
119struct Client *make_remote_person_nick(struct Client *server, const char *nick)
120{
121 return make_remote_person_full(server, nick, TEST_USERNAME, TEST_HOSTNAME, TEST_IP, TEST_REALNAME);
122}
123
124struct Client *make_remote_person_full(struct Client *server, const char *nick, const char *username, const char *hostname, const char *ip, const char *realname)
125{
126 struct Client *client;
127 struct sockaddr addr;
128
129 client = make_client(server);
130 make_user(client);
131 SetRemoteClient(client);
132
133 client->servptr = server;
134 rb_dlinkAdd(server, &server->lnode, &server->servptr->serv->users);
135
136 rb_inet_pton_sock(ip, &addr);
137 rb_strlcpy(client->name, nick, sizeof(client->name));
138 rb_strlcpy(client->username, username, sizeof(client->username));
139 rb_strlcpy(client->host, hostname, sizeof(client->host));
140 rb_inet_ntop_sock(&addr, client->sockhost, sizeof(client->sockhost));
141 rb_strlcpy(client->info, realname, sizeof(client->info));
142
143 add_to_client_hash(nick, client);
144 add_to_hostname_hash(client->host, client);
145
146 return client;
147}
148
149void make_remote_person_oper(struct Client *client)
150{
151 rb_dlinkAddAlloc(client, &oper_list);
152 SetOper(client);
153}
154
155void remove_remote_person(struct Client *client)
156{
157 exit_client(client, client->servptr, client->servptr, "Test client removed");
158}
159
160void remove_remote_server(struct Client *server)
161{
162 exit_client(server, server, server->servptr, "Test server removed");
163}
164
54f75d36
SA
165struct Channel *make_channel(void)
166{
167 return allocate_channel(TEST_CHANNEL);
168}
60f1d711 169
d2b5f411
SA
170char *get_client_sendq(const struct Client *client)
171{
172 static char buf[EXT_BUFSIZE + sizeof(CRLF)];
173
174 if (rb_linebuf_len(&client->localClient->buf_sendq)) {
175 int ret;
176
177 memset(buf, 0, sizeof(buf));
178 ret = rb_linebuf_get(&client->localClient->buf_sendq, buf, sizeof(buf), 0, 1);
179
54f75d36 180 if (ok(ret > 0, MSG)) {
d2b5f411
SA
181 return buf;
182 } else {
183 return "<get_client_sendq error>";
184 }
185 }
186
187 return "";
188}
189
190void client_util_init(void)
191{
192}
193
194void client_util_free(void)
195{
196}