]> jfr.im git - solanum.git/blob - tests/sasl_abort1.c
Merge pull request #282 from edk0/propagate-oper
[solanum.git] / tests / sasl_abort1.c
1 /*
2 * sasl_abort1.c: Test SASL abort from the ircd to services
3 * Copyright 2019 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; aborted=%d, by_user=%d)", __FILE__, __LINE__, __FUNCTION__, aborted, by_user
38
39 static void common_sasl_test(bool aborted, bool by_user)
40 {
41 ircd_util_init(__FILE__);
42 client_util_init();
43
44 struct Client *user = make_local_unknown();
45 struct Client *server = make_remote_server(&me);
46 struct Client *remote = make_remote_person(server);
47
48 rb_inet_pton_sock(TEST_IP, &user->localClient->ip);
49 rb_strlcpy(user->host, TEST_HOSTNAME, sizeof(user->host));
50 rb_inet_ntop_sock((struct sockaddr *)&user->localClient->ip, user->sockhost, sizeof(user->sockhost));
51
52 strcpy(server->id, TEST_SERVER_ID);
53 strcpy(remote->id, TEST_REMOTE_ID);
54 add_to_id_hash(remote->id, remote);
55 server->localClient->caps = CAP_ENCAP | CAP_TS6;
56 remote->umodes |= UMODE_SERVICE;
57
58 client_util_parse(user, "CAP LS 302" CRLF);
59 const char *line;
60 while ((line = get_client_sendq(user)) && strcmp(line, "")) {
61 printf("%s", line);
62 }
63
64 client_util_parse(user, "NICK " TEST_NICK CRLF);
65 client_util_parse(user, "USER " TEST_USERNAME " 0 0 :" TEST_REALNAME CRLF);
66 is_client_sendq_empty(user, MSG);
67
68 user->tsinfo = 42;
69
70 client_util_parse(user, "CAP REQ :sasl" CRLF);
71 is_client_sendq(":" TEST_ME_NAME " CAP " TEST_NICK " ACK :sasl" CRLF, user, MSG);
72
73 client_util_parse(user, "AUTHENTICATE EXTERNAL" CRLF);
74 is_client_sendq_empty(user, MSG);
75
76 is_client_sendq_one(":" TEST_ME_ID " ENCAP " TEST_SERVER_NAME " SASL " TEST_ME_ID "AAAAAB " TEST_REMOTE_ID " H " TEST_HOSTNAME " " TEST_IP " P" CRLF, server, MSG);
77 is_client_sendq_one(":" TEST_ME_ID " ENCAP " TEST_SERVER_NAME " SASL " TEST_ME_ID "AAAAAB " TEST_REMOTE_ID " S EXTERNAL" CRLF, server, MSG);
78 is_client_sendq_empty(server, MSG);
79
80 if (aborted) {
81 if (by_user) {
82 // Explicit abort by user
83 client_util_parse(user, "AUTHENTICATE *" CRLF);
84 is_client_sendq(":" TEST_ME_NAME " 906 " TEST_NICK " :SASL authentication aborted" CRLF, user, MSG);
85
86 client_util_parse(user, "CAP END" CRLF);
87 ok(IsClient(user), MSG);
88 } else {
89 // Implicit abort by completing registration
90 client_util_parse(user, "CAP END" CRLF);
91 ok(IsClient(user), MSG);
92 is_client_sendq_one(":" TEST_ME_NAME " 906 " TEST_NICK " :SASL authentication aborted" CRLF, user, MSG);
93 }
94
95 is_client_sendq_one(":" TEST_ME_ID " ENCAP " TEST_SERVER_NAME " SASL " TEST_ME_ID "AAAAAB " TEST_REMOTE_ID " D A" CRLF, server, MSG);
96 is_client_sendq(":" TEST_ME_ID " UID " TEST_NICK " 1 42 +i ~" TEST_USERNAME " " TEST_HOSTNAME " " TEST_IP " " TEST_ME_ID "AAAAAB :" TEST_REALNAME CRLF, server, MSG);
97 } else {
98 // Return a successful auth
99 client_util_parse(server, ":" TEST_SERVER_NAME " ENCAP " TEST_ME_NAME " SASL " TEST_REMOTE_ID " " TEST_ME_ID "AAAAAB D S" CRLF);
100
101 // User should be authenticated
102 is_client_sendq_one(":" TEST_ME_NAME " 903 " TEST_NICK " :SASL authentication successful" CRLF, user, MSG);
103
104 client_util_parse(user, "CAP END" CRLF);
105 ok(IsClient(user), MSG);
106 }
107
108 is_client_sendq_one(":" TEST_ME_NAME " 001 " TEST_NICK " :Welcome to the Test Internet Relay Chat Network " TEST_NICK CRLF, user, MSG);
109 while ((line = get_client_sendq(user)) && strcmp(line, "")) {
110 printf("%s", line);
111 }
112
113 if (aborted) {
114 // Return a successful auth after auth was aborted
115 client_util_parse(server, ":" TEST_SERVER_NAME " ENCAP " TEST_ME_NAME " SASL " TEST_REMOTE_ID " " TEST_ME_ID "AAAAAB D S" CRLF);
116
117 // User should not be authenticated
118 is_client_sendq_empty(user, MSG);
119 }
120
121 remove_local_person(user);
122 remove_remote_person(remote);
123 remove_remote_server(server);
124
125 client_util_free();
126 ircd_util_free();
127 }
128
129 static void successful_login(void)
130 {
131 common_sasl_test(false, false);
132 }
133
134 static void successful_login_after_aborted_by_registration(void)
135 {
136 common_sasl_test(true, false);
137 }
138
139 static void successful_login_after_aborted_by_user(void)
140 {
141 common_sasl_test(true, true);
142 }
143
144 int main(int argc, char *argv[])
145 {
146 plan_lazy();
147
148 successful_login();
149 successful_login_after_aborted_by_registration();
150 successful_login_after_aborted_by_user();
151
152 return 0;
153 }