]>
jfr.im git - solanum.git/blob - tests/client_util.c
2 * client_util.c: Utility functions for making test clients
3 * Copyright 2017 Simon Arlott
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.
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.
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
24 #include "tap/basic.h"
26 #include "client_util.h"
29 #include "s_newconf.h"
31 #define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__
33 struct Client
*make_local_person(void)
35 return make_local_person_nick(TEST_NICK
);
38 struct Client
*make_local_person_nick(const char *nick
)
40 return make_local_person_full(nick
, TEST_USERNAME
, TEST_HOSTNAME
, TEST_IP
, TEST_REALNAME
);
43 struct Client
*make_local_person_full(const char *nick
, const char *username
, const char *hostname
, const char *ip
, const char *realname
)
45 struct Client
*client
;
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
);
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
));
61 add_to_client_hash(client
->name
, client
);
66 void make_local_person_oper(struct Client
*client
)
68 rb_dlinkAddAlloc(client
, &local_oper_list
);
69 rb_dlinkAddAlloc(client
, &oper_list
);
73 void remove_local_person(struct Client
*client
)
75 exit_client(NULL
, client
, &me
, "Test client removed");
78 struct Client
*make_remote_server(struct Client
*uplink
)
80 return make_remote_server_name(uplink
, TEST_SERVER_NAME
);
83 struct Client
*make_remote_server_name(struct Client
*uplink
, const char *name
)
85 return make_remote_server_full(uplink
, name
, "");
88 struct Client
*make_remote_server_full(struct Client
*uplink
, const char *name
, const char *id
)
90 struct Client
*client
;
92 client
= make_client(NULL
);
93 client
->servptr
= uplink
;
95 attach_server_conf(client
, find_server_conf(name
));
97 rb_strlcpy(client
->name
, name
, sizeof(client
->name
));
98 rb_strlcpy(client
->id
, id
, sizeof(client
->id
));
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
);
107 add_to_client_hash(client
->name
, client
);
109 add_to_id_hash(client
->id
, client
);
114 struct Client
*make_remote_person(struct Client
*server
)
116 return make_remote_person_nick(server
, TEST_REMOTE_NICK
);
119 struct Client
*make_remote_person_nick(struct Client
*server
, const char *nick
)
121 return make_remote_person_full(server
, nick
, TEST_USERNAME
, TEST_HOSTNAME
, TEST_IP
, TEST_REALNAME
);
124 struct Client
*make_remote_person_full(struct Client
*server
, const char *nick
, const char *username
, const char *hostname
, const char *ip
, const char *realname
)
126 struct Client
*client
;
127 struct sockaddr addr
;
129 client
= make_client(server
);
131 SetRemoteClient(client
);
133 client
->servptr
= server
;
134 rb_dlinkAdd(server
, &server
->lnode
, &server
->servptr
->serv
->users
);
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
));
143 add_to_client_hash(nick
, client
);
144 add_to_hostname_hash(client
->host
, client
);
149 void make_remote_person_oper(struct Client
*client
)
151 rb_dlinkAddAlloc(client
, &oper_list
);
155 void remove_remote_person(struct Client
*client
)
157 exit_client(client
, client
->servptr
, client
->servptr
, "Test client removed");
160 void remove_remote_server(struct Client
*server
)
162 exit_client(server
, server
, server
->servptr
, "Test server removed");
165 struct Channel
*make_channel(void)
167 return allocate_channel(TEST_CHANNEL
);
170 char *get_client_sendq(const struct Client
*client
)
172 static char buf
[EXT_BUFSIZE
+ sizeof(CRLF
)];
174 if (rb_linebuf_len(&client
->localClient
->buf_sendq
)) {
177 memset(buf
, 0, sizeof(buf
));
178 ret
= rb_linebuf_get(&client
->localClient
->buf_sendq
, buf
, sizeof(buf
), 0, 1);
180 if (ok(ret
> 0, MSG
)) {
183 return "<get_client_sendq error>";
190 void client_util_init(void)
194 void client_util_free(void)