Debian Codegraph @

tests/unittests/torture_bind_config.c

raw
1 /*
2 * torture_bind_config.c - Tests for server side configuration
3 *
4 * This file is part of the SSH Library
5 *
6 * Copyright (c) 2019 by Red Hat, Inc.
7 *
8 * Author: Anderson Toshiyuki Sasaki <[email protected]>
9 *
10 * The SSH Library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
14 *
15 * The SSH Library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the SSH Library; see the file COPYING. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
24 */
25
26 #include "config.h"
27
28 #define LIBSSH_STATIC
29
30 #include "torture.h"
31 #include "torture_key.h"
32
33 #include <libssh/bind_config.h>
34 #include <libssh/bind.h>
35
36 extern LIBSSH_THREAD int ssh_log_level;
37
38 #define LOGLEVEL "verbose"
39 #define LOGLEVEL2 "fatal"
40 #define LOGLEVEL3 "DEBUG1"
41 #define LOGLEVEL4 "DEBUG2"
42 #define LISTEN_ADDRESS "::1"
43 #define LISTEN_ADDRESS2 "::2"
44 #define KEXALGORITHMS "ecdh-sha2-nistp521,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1"
45 #define KEXALGORITHMS2 "ecdh-sha2-nistp521"
46 #define CIPHERS "aes128-ctr,aes192-ctr,aes256-ctr"
47 #define CIPHERS2 "aes256-ctr"
48 #define HOSTKEYALGORITHMS "ssh-ed25519,ecdsa-sha2-nistp521,ssh-rsa"
49 #define HOSTKEYALGORITHMS_UNKNOWN "ssh-ed25519,ecdsa-sha2-nistp521,unknown,ssh-rsa"
50 #define HOSTKEYALGORITHMS2 "rsa-sha2-256"
51 #define PUBKEYACCEPTEDTYPES "rsa-sha2-512,ssh-rsa,ecdsa-sha2-nistp521"
52 #define PUBKEYACCEPTEDTYPES_UNKNOWN "rsa-sha2-512,ssh-rsa,unknown,ecdsa-sha2-nistp521"
53 #define PUBKEYACCEPTEDTYPES2 "rsa-sha2-256,ssh-rsa"
54 #define MACS "hmac-sha1,hmac-sha2-256,hmac-sha2-512,[email protected],[email protected],[email protected]"
55 #define MACS2 "hmac-sha1"
56
57 #define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
58 #define LIBSSH_ED25519_TESTKEY "libssh_testkey.id_ed25519"
59 #ifdef HAVE_ECC
60 #define LIBSSH_ECDSA_521_TESTKEY "libssh_testkey.id_ecdsa521"
61 #endif
62
63 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS "libssh_test_bind_config_listenaddress"
64 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_STRING "ListenAddress "LISTEN_ADDRESS"\n"
65 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2 "libssh_test_bind_config_listenaddress2"
66 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2_STRING "ListenAddress "LISTEN_ADDRESS2"\n"
67 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE "libssh_test_bind_config_listenaddress_twice"
68 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_STRING \
69 "ListenAddress "LISTEN_ADDRESS"\n" \
70 "ListenAddress "LISTEN_ADDRESS2"\n"
71 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC "libssh_test_bind_config_listenaddress_twice_rec"
72 #define LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC_STRING \
73 "ListenAddress "LISTEN_ADDRESS"\n" \
74 "Include "LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2"\n"
75 #define LIBSSH_TEST_BIND_CONFIG_PORT "libssh_test_bind_config_port"
76 #define LIBSSH_TEST_BIND_CONFIG_PORT_STRING "Port 123\n"
77 #define LIBSSH_TEST_BIND_CONFIG_PORT2 "libssh_test_bind_config_port2"
78 #define LIBSSH_TEST_BIND_CONFIG_PORT2_STRING "Port 456\n"
79 #define LIBSSH_TEST_BIND_CONFIG_PORT_TWICE "libssh_test_bind_config_port_twice"
80 #define LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_STRING \
81 "Port 123\n" \
82 "Port 456\n"
83 #define LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC "libssh_test_bind_config_port_twice_rec"
84 #define LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC_STRING \
85 "Port 123\n" \
86 "Include "LIBSSH_TEST_BIND_CONFIG_PORT2"\n"
87 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY "libssh_test_bind_config_hostkey"
88 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_STRING "HostKey "LIBSSH_ECDSA_521_TESTKEY"\n"
89 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY2 "libssh_test_bind_config_hostkey2"
90 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY2_STRING "HostKey "LIBSSH_RSA_TESTKEY"\n"
91 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE "libssh_test_bind_config_hostkey_twice"
92 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_STRING \
93 "HostKey "LIBSSH_ECDSA_521_TESTKEY"\n" \
94 "HostKey "LIBSSH_RSA_TESTKEY"\n"
95 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC "libssh_test_bind_config_hostkey_twice_rec"
96 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC_STRING \
97 "HostKey "LIBSSH_ECDSA_521_TESTKEY"\n" \
98 "Include "LIBSSH_TEST_BIND_CONFIG_HOSTKEY2"\n"
99 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL "libssh_test_bind_config_loglevel"
100 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_STRING "LogLevel "LOGLEVEL"\n"
101 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1 "libssh_test_bind_config_loglevel2"
102 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1_STRING "LogLevel "LOGLEVEL2"\n"
103 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE "libssh_test_bind_config_loglevel_twice"
104 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_STRING \
105 "LogLevel "LOGLEVEL"\n" \
106 "LogLevel "LOGLEVEL2"\n"
107 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC "libssh_test_bind_config_loglevel_twice_rec"
108 #define LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC_STRING \
109 "LogLevel "LOGLEVEL"\n" \
110 "Include "LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1"\n"
111 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS "libssh_test_bind_config_ciphers"
112 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS_STRING "Ciphers "CIPHERS"\n"
113 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS2 "libssh_test_bind_config_ciphers2"
114 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS2_STRING "Ciphers "CIPHERS2"\n"
115 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE "libssh_test_bind_config_ciphers_twice"
116 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_STRING \
117 "Ciphers "CIPHERS"\n" \
118 "Ciphers "CIPHERS2"\n"
119 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC "libssh_test_bind_config_ciphers_twice_rec"
120 #define LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC_STRING \
121 "Ciphers "CIPHERS"\n" \
122 "Include "LIBSSH_TEST_BIND_CONFIG_CIPHERS2"\n"
123 #define LIBSSH_TEST_BIND_CONFIG_MACS "libssh_test_bind_config_macs"
124 #define LIBSSH_TEST_BIND_CONFIG_MACS_STRING "MACs "MACS"\n"
125 #define LIBSSH_TEST_BIND_CONFIG_MACS2 "libssh_test_bind_config_macs2"
126 #define LIBSSH_TEST_BIND_CONFIG_MACS2_STRING "MACs "MACS2"\n"
127 #define LIBSSH_TEST_BIND_CONFIG_MACS_TWICE "libssh_test_bind_config_macs_twice"
128 #define LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_STRING \
129 "MACs "MACS"\n" \
130 "MACs "MACS2"\n"
131 #define LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC "libssh_test_bind_config_macs_twice_rec"
132 #define LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC_STRING \
133 "MACs "MACS"\n" \
134 "Include "LIBSSH_TEST_BIND_CONFIG_MACS2"\n"
135 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS "libssh_test_bind_config_kexalgorithms"
136 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_STRING "KexAlgorithms "KEXALGORITHMS"\n"
137 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2 "libssh_test_bind_config_kexalgorithms2"
138 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2_STRING "KexAlgorithms "KEXALGORITHMS2"\n"
139 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE "libssh_test_bind_config_kexalgorithms_twice"
140 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_STRING \
141 "KexAlgorithms "KEXALGORITHMS"\n" \
142 "KexAlgorithms "KEXALGORITHMS2"\n"
143 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC "libssh_test_bind_config_kexalgorithms_twice_rec"
144 #define LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC_STRING \
145 "KexAlgorithms "KEXALGORITHMS"\n" \
146 "Include "LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2"\n"
147
148 #define LIBSSH_TEST_BIND_CONFIG_REQUIRED_RSA_SIZE "libssh_test_bind_config_required_rsa_size"
149 #define LIBSSH_TEST_BIND_CONFIG_REQUIRED_RSA_SIZE_STRING "RequiredRsaSize 2233\n"
150
151 #define LIBSSH_TEST_BIND_CONFIG_FULL "libssh_test_bind_config_full"
152 #define LIBSSH_TEST_BIND_CONFIG_INCLUDE "libssh_test_bind_config_include"
153 #define LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE "libssh_test_bind_config_include_recursive"
154 #define LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE_LOOP "libssh_test_bind_config_include_recursive_loop"
155 #define LIBSSH_TEST_BIND_CONFIG_CORNER_CASES "libssh_test_bind_config_corner_cases"
156
157 #define LIBSSH_TEST_BIND_CONFIG_MATCH_ALL "libssh_test_bind_config_match_all"
158 #define LIBSSH_TEST_BIND_CONFIG_MATCH_TWICE "libssh_test_bind_config_match_twice"
159 #define LIBSSH_TEST_BIND_CONFIG_MATCH_UNSUPPORTED "libssh_test_bind_config_match_unsupported"
160 #define LIBSSH_TEST_BIND_CONFIG_MATCH_NOT_ALLOWED "libssh_test_bind_config_match_not_allowed"
161 #define LIBSSH_TEST_BIND_CONFIG_MATCH_CORNER_CASES "libssh_test_bind_config_match_corner_cases"
162 #define LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID "libssh_test_bind_config_match_invalid"
163 #define LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID2 "libssh_test_bind_config_match_invalid2"
164
165 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED "libssh_test_bind_config_pubkey"
166 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_STRING "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES"\n"
167 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2 "libssh_test_bind_config_pubkey2"
168 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2_STRING "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES2"\n"
169 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE "libssh_test_bind_config_pubkey_twice"
170 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_STRING \
171 "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES"\n" \
172 "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES2"\n"
173 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC "libssh_test_bind_config_pubkey_twice_rec"
174 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC_STRING \
175 "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES2"\n" \
176 "Include "LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS"\n"
177 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN "libssh_test_bind_config_pubkey_unknown"
178 #define LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN_STRING "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES_UNKNOWN"\n"
179
180 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS "libssh_test_bind_config_hostkey_alg"
181 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_STRING "HostKeyAlgorithms "HOSTKEYALGORITHMS"\n"
182 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2 "libssh_test_bind_config_hostkey_alg2"
183 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2_STRING "HostKeyAlgorithms "HOSTKEYALGORITHMS2"\n"
184 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE "libssh_test_bind_config_hostkey_alg_twice"
185 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_STRING \
186 "HostKeyAlgorithms "HOSTKEYALGORITHMS"\n" \
187 "HostKeyAlgorithms "HOSTKEYALGORITHMS2"\n"
188 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC "libssh_test_bind_config_hostkey_alg_twice_rec"
189 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC_STRING \
190 "HostKeyAlgorithms "HOSTKEYALGORITHMS2"\n" \
191 "Include "LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS"\n"
192 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN "libssh_test_bind_config_hostkey_alg_unknown"
193 #define LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN_STRING "HostKeyAlgorithms "HOSTKEYALGORITHMS_UNKNOWN"\n"
194
195 const char template[] = "temp_dir_XXXXXX";
196
197 struct bind_st {
198 char *cwd;
199 char *temp_dir;
200 ssh_bind bind;
201 };
202
203 static int setup_config_files(void **state)
204 {
205 struct bind_st *test_state = NULL;
206 char *cwd = NULL;
207 char *tmp_dir = NULL;
208 int rc = 0;
209
210 test_state = (struct bind_st *)malloc(sizeof(struct bind_st));
211 assert_non_null(test_state);
212
213 cwd = torture_get_current_working_dir();
214 assert_non_null(cwd);
215
216 tmp_dir = torture_make_temp_dir(template);
217 assert_non_null(tmp_dir);
218
219 test_state->cwd = cwd;
220 test_state->temp_dir = tmp_dir;
221
222 *state = test_state;
223
224 rc = torture_change_dir(tmp_dir);
225 assert_int_equal(rc, 0);
226
227 printf("Changed directory to: %s\n", tmp_dir);
228
229 /* For ed25519 the test keys are not available in legacy PEM format. Using
230 * the new OpenSSH format for all algorithms */
231 torture_write_file(LIBSSH_RSA_TESTKEY,
232 torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 0));
233
234 torture_write_file(LIBSSH_ED25519_TESTKEY,
235 torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0));
236 #ifdef HAVE_ECC
237 torture_write_file(LIBSSH_ECDSA_521_TESTKEY,
238 torture_get_openssh_testkey(SSH_KEYTYPE_ECDSA_P521, 0));
239 #endif
240
241 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS,
242 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_STRING);
243 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2,
244 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2_STRING);
245 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE,
246 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_STRING);
247 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC,
248 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC_STRING);
249
250 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PORT,
251 LIBSSH_TEST_BIND_CONFIG_PORT_STRING);
252 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PORT2,
253 LIBSSH_TEST_BIND_CONFIG_PORT2_STRING);
254 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PORT_TWICE,
255 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_STRING);
256 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC,
257 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC_STRING);
258
259 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY,
260 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_STRING);
261 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY2,
262 LIBSSH_TEST_BIND_CONFIG_HOSTKEY2_STRING);
263 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE,
264 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_STRING);
265 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC,
266 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC_STRING);
267
268 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LOGLEVEL,
269 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_STRING);
270 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1,
271 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1_STRING);
272 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE,
273 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_STRING);
274 torture_write_file(LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC,
275 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC_STRING);
276
277 torture_write_file(LIBSSH_TEST_BIND_CONFIG_CIPHERS,
278 LIBSSH_TEST_BIND_CONFIG_CIPHERS_STRING);
279 torture_write_file(LIBSSH_TEST_BIND_CONFIG_CIPHERS2,
280 LIBSSH_TEST_BIND_CONFIG_CIPHERS2_STRING);
281 torture_write_file(LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE,
282 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_STRING);
283 torture_write_file(LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC,
284 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC_STRING);
285
286 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MACS,
287 LIBSSH_TEST_BIND_CONFIG_MACS_STRING);
288 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MACS2,
289 LIBSSH_TEST_BIND_CONFIG_MACS2_STRING);
290 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MACS_TWICE,
291 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_STRING);
292 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC,
293 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC_STRING);
294
295 torture_write_file(LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS,
296 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_STRING);
297 torture_write_file(LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2,
298 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2_STRING);
299 torture_write_file(LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE,
300 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_STRING);
301 torture_write_file(LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC,
302 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC_STRING);
303
304 torture_write_file(LIBSSH_TEST_BIND_CONFIG_REQUIRED_RSA_SIZE,
305 LIBSSH_TEST_BIND_CONFIG_REQUIRED_RSA_SIZE_STRING);
306
307 torture_write_file(LIBSSH_TEST_BIND_CONFIG_FULL,
308 "ListenAddress "LISTEN_ADDRESS"\n"
309 "Port 123\n"
310 "HostKey "LIBSSH_ECDSA_521_TESTKEY"\n"
311 "LogLevel "LOGLEVEL"\n"
312 "Ciphers "CIPHERS"\n"
313 "MACs "MACS"\n"
314 "KexAlgorithms "KEXALGORITHMS"\n"
315 "RequiredRsaSize 2233\n");
316
317 torture_write_file(LIBSSH_TEST_BIND_CONFIG_INCLUDE,
318 "Include "LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS"\n"
319 "Include "LIBSSH_TEST_BIND_CONFIG_PORT"\n"
320 "Include "LIBSSH_TEST_BIND_CONFIG_HOSTKEY"\n"
321 "Include "LIBSSH_TEST_BIND_CONFIG_LOGLEVEL"\n"
322 "Include "LIBSSH_TEST_BIND_CONFIG_CIPHERS"\n"
323 "Include "LIBSSH_TEST_BIND_CONFIG_MACS"\n"
324 "Include "LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS"\n"
325 "Include "LIBSSH_TEST_BIND_CONFIG_REQUIRED_RSA_SIZE"\n");
326
327 torture_write_file(LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE,
328 "Include "LIBSSH_TEST_BIND_CONFIG_INCLUDE"\n");
329
330 torture_write_file(LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE_LOOP,
331 "Include "LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE_LOOP"\n");
332
333 /* Unsupported options and corner cases */
334 torture_write_file(LIBSSH_TEST_BIND_CONFIG_CORNER_CASES,
335 "\n" /* empty line */
336 "# comment line\n"
337 " # comment line not starting with hash\n"
338 "UnknownConfigurationOption yes\n"
339 "Ciphers "CIPHERS2"\n");
340
341 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_ALL,
342 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
343 "Match All\n"
344 "\tLogLevel "LOGLEVEL2"\n");
345 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_TWICE,
346 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
347 "Match All\n"
348 "\tLogLevel "LOGLEVEL2"\n"
349 "Match All\n"
350 "\tLogLevel "LOGLEVEL3"\n");
351 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_UNSUPPORTED,
352 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
353 "Match User alice\n"
354 "\tLogLevel "LOGLEVEL2"\n"
355 "Match Group sftp_users\n"
356 "\tLogLevel "LOGLEVEL2"\n"
357 "Match Host 192.168.0.*\n"
358 "\tLogLevel "LOGLEVEL2"\n"
359 "Match LocalAddress 172.30.1.5\n"
360 "\tLogLevel "LOGLEVEL2"\n"
361 "Match LocalPort 42\n"
362 "\tLogLevel "LOGLEVEL2"\n"
363 "Match Rdomain 4\n"
364 "\tLogLevel "LOGLEVEL2"\n"
365 "Match Address 10.0.0.10\n"
366 "\tLogLevel "LOGLEVEL2"\n");
367 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_NOT_ALLOWED,
368 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
369 "Match All\n"
370 "\tListenAddress "LISTEN_ADDRESS2"\n"
371 "\tPort 456\n"
372 "\tHostKey "LIBSSH_RSA_TESTKEY"\n"
373 "\tCiphers "CIPHERS2"\n"
374 "\tMACs "MACS2"\n"
375 "\tKexAlgorithms "KEXALGORITHMS2"\n");
376 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_CORNER_CASES,
377 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
378 "Match User alice\n"
379 "\tLogLevel "LOGLEVEL2"\n"
380 "Match All\n"
381 "\tLogLevel "LOGLEVEL3"\n"
382 "Match All\n"
383 "\tLogLevel "LOGLEVEL"\n");
384 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID,
385 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
386 "Match User alice All\n"
387 "\tLogLevel "LOGLEVEL2"\n"
388 "Match All\n"
389 "\tLogLevel "LOGLEVEL3"\n"
390 "Match All\n"
391 "\tLogLevel "LOGLEVEL4"\n");
392 torture_write_file(LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID2,
393 "Include "LIBSSH_TEST_BIND_CONFIG_FULL"\n"
394 "Match All User alice\n"
395 "\tLogLevel "LOGLEVEL2"\n"
396 "Match All\n"
397 "\tLogLevel "LOGLEVEL3"\n"
398 "Match All\n"
399 "\tLogLevel "LOGLEVEL4"\n");
400
401 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED,
402 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_STRING);
403 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2,
404 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2_STRING);
405 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE,
406 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_STRING);
407 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC,
408 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC_STRING);
409 torture_write_file(LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN,
410 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN_STRING);
411
412 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS,
413 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_STRING);
414 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2,
415 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2_STRING);
416 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE,
417 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_STRING);
418 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC,
419 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC_STRING);
420 torture_write_file(LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN,
421 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN_STRING);
422 return 0;
423 }
424
425 static int sshbind_setup(void **state)
426 {
427 int rc;
428 struct bind_st *test_state = NULL;
429
430 rc = setup_config_files((void **)&test_state);
431 assert_int_equal(rc, 0);
432 assert_non_null(test_state);
433
434 test_state->bind = ssh_bind_new();
435 assert_non_null(test_state->bind);
436
437 *state = test_state;
438
439 return 0;
440 }
441
442 static int sshbind_teardown(void **state)
443 {
444 struct bind_st *test_state = NULL;
445 int rc;
446
447 assert_non_null(state);
448 test_state = *((struct bind_st **)state);
449
450 assert_non_null(test_state);
451 assert_non_null(test_state->cwd);
452 assert_non_null(test_state->temp_dir);
453 assert_non_null(test_state->bind);
454
455 rc = torture_change_dir(test_state->cwd);
456 assert_int_equal(rc, 0);
457
458 rc = torture_rmdirs(test_state->temp_dir);
459 assert_int_equal(rc, 0);
460
461 SAFE_FREE(test_state->temp_dir);
462 SAFE_FREE(test_state->cwd);
463 ssh_bind_free(test_state->bind);
464 SAFE_FREE(test_state);
465
466 return 0;
467 }
468
469 /**
470 * @brief helper function loading configuration from either file or string
471 */
472 static void
473 _parse_config(ssh_bind bind,
474 const char *file,
475 const char *string,
476 int expected)
477 {
478 int ret = -1;
479
480 /* make sure either config file or config string is given,
481 * not both */
482 assert_int_not_equal(file == NULL, string == NULL);
483
484 if (file != NULL) {
485 ret = ssh_bind_config_parse_file(bind, file);
486 } else if (string != NULL) {
487 ret = ssh_bind_config_parse_string(bind, string);
488 } else {
489 /* should not happen */
490 fail();
491 }
492
493 /* make sure parsing went as expected */
494 assert_return_code(ret, expected);
495 }
496
497
498 static void
499 torture_bind_config_listen_address(void **state,
500 const char *file,
501 const char *string,
502 const char *expect)
503 {
504 struct bind_st *test_state;
505 ssh_bind bind;
506
507 assert_non_null(state);
508 test_state = *((struct bind_st **)state);
509 assert_non_null(test_state);
510 assert_non_null(test_state->bind);
511 bind = test_state->bind;
512
513 _parse_config(bind, file, string, SSH_OK);
514
515 assert_non_null(bind->bindaddr);
516 assert_string_equal(bind->bindaddr, expect);
517 }
518
519 static void torture_bind_config_listen_address_file(void **state)
520 {
521 torture_bind_config_listen_address(state,
522 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS,
523 NULL,
524 LISTEN_ADDRESS);
525 }
526
527 static void torture_bind_config_listen_address_string(void **state)
528 {
529 torture_bind_config_listen_address(state,
530 NULL,
531 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_STRING,
532 LISTEN_ADDRESS);
533 }
534
535 static void torture_bind_config_listen_address2_file(void **state)
536 {
537 torture_bind_config_listen_address(state,
538 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2,
539 NULL,
540 LISTEN_ADDRESS2);
541 }
542
543 static void torture_bind_config_listen_address2_string(void **state)
544 {
545 torture_bind_config_listen_address(state,
546 NULL,
547 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS2_STRING,
548 LISTEN_ADDRESS2);
549 }
550
551 static void torture_bind_config_listen_address_twice_file(void **state)
552 {
553 torture_bind_config_listen_address(state,
554 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE,
555 NULL,
556 LISTEN_ADDRESS);
557 }
558
559 static void torture_bind_config_listen_address_twice_string(void **state)
560 {
561 torture_bind_config_listen_address(state,
562 NULL,
563 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_STRING,
564 LISTEN_ADDRESS);
565 }
566
567 static void torture_bind_config_listen_address_twice_rec_file(void **state)
568 {
569 torture_bind_config_listen_address(state,
570 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC,
571 NULL,
572 LISTEN_ADDRESS);
573 }
574
575 static void torture_bind_config_listen_address_twice_rec_string(void **state)
576 {
577 torture_bind_config_listen_address(state,
578 NULL,
579 LIBSSH_TEST_BIND_CONFIG_LISTENADDRESS_TWICE_REC_STRING,
580 LISTEN_ADDRESS);
581 }
582
583 static void
584 torture_bind_config_port(void **state,
585 const char *file,
586 const char *string,
587 int expect)
588 {
589 struct bind_st *test_state;
590 ssh_bind bind;
591
592 assert_non_null(state);
593 test_state = *((struct bind_st **)state);
594 assert_non_null(test_state);
595 assert_non_null(test_state->bind);
596 bind = test_state->bind;
597
598 _parse_config(bind, file, string, SSH_OK);
599
600 assert_int_equal(bind->bindport, expect);
601 }
602
603 static void torture_bind_config_port_file(void **state)
604 {
605 torture_bind_config_port(state,
606 LIBSSH_TEST_BIND_CONFIG_PORT,
607 NULL,
608 123);
609 }
610
611 static void torture_bind_config_port_string(void **state)
612 {
613 torture_bind_config_port(state,
614 NULL,
615 LIBSSH_TEST_BIND_CONFIG_PORT_STRING,
616 123);
617 }
618
619 static void torture_bind_config_port2_file(void **state)
620 {
621 torture_bind_config_port(state,
622 LIBSSH_TEST_BIND_CONFIG_PORT2,
623 NULL,
624 456);
625 }
626
627 static void torture_bind_config_port2_string(void **state)
628 {
629 torture_bind_config_port(state,
630 NULL,
631 LIBSSH_TEST_BIND_CONFIG_PORT2_STRING,
632 456);
633 }
634
635 static void torture_bind_config_port_twice_file(void **state)
636 {
637 torture_bind_config_port(state,
638 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE,
639 NULL,
640 123);
641 }
642
643 static void torture_bind_config_port_twice_string(void **state)
644 {
645 torture_bind_config_port(state,
646 NULL,
647 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_STRING,
648 123);
649 }
650
651 static void torture_bind_config_port_twice_rec_file(void **state)
652 {
653 torture_bind_config_port(state,
654 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC,
655 NULL,
656 123);
657 }
658
659 static void torture_bind_config_port_twice_rec_string(void **state)
660 {
661 torture_bind_config_port(state,
662 NULL,
663 LIBSSH_TEST_BIND_CONFIG_PORT_TWICE_REC_STRING,
664 123);
665 }
666
667 static void
668 torture_bind_config_hostkey(void **state,
669 const char *file,
670 const char *string)
671 {
672 struct bind_st *test_state;
673 ssh_bind bind;
674
675 assert_non_null(state);
676 test_state = *((struct bind_st **)state);
677 assert_non_null(test_state);
678 assert_non_null(test_state->bind);
679 bind = test_state->bind;
680
681 _parse_config(bind, file, string, SSH_OK);
682
683 assert_non_null(bind->ecdsakey);
684 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
685 }
686
687 static void
688 torture_bind_config_hostkey2(void **state,
689 const char *file,
690 const char *string)
691 {
692 struct bind_st *test_state;
693 ssh_bind bind;
694
695 assert_non_null(state);
696 test_state = *((struct bind_st **)state);
697 assert_non_null(test_state);
698 assert_non_null(test_state->bind);
699 bind = test_state->bind;
700
701 _parse_config(bind, file, string, SSH_OK);
702
703 assert_non_null(bind->ecdsakey);
704 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
705 assert_non_null(bind->rsakey);
706 assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
707 }
708
709 static void torture_bind_config_hostkey_file(void **state)
710 {
711 torture_bind_config_hostkey(state, LIBSSH_TEST_BIND_CONFIG_HOSTKEY, NULL);
712 }
713
714 static void torture_bind_config_hostkey_string(void **state)
715 {
716 torture_bind_config_hostkey(state, NULL, LIBSSH_TEST_BIND_CONFIG_HOSTKEY_STRING);
717 }
718
719 static void torture_bind_config_hostkey_twice_file(void **state)
720 {
721 torture_bind_config_hostkey2(state, LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE, NULL);
722 }
723
724 static void torture_bind_config_hostkey_twice_string(void **state)
725 {
726 torture_bind_config_hostkey2(state, NULL, LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_STRING);
727 }
728
729 static void torture_bind_config_hostkey_twice_rec_file(void **state)
730 {
731 torture_bind_config_hostkey2(state, LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC, NULL);
732 }
733
734 static void torture_bind_config_hostkey_twice_rec_string(void **state)
735 {
736 torture_bind_config_hostkey2(state, NULL, LIBSSH_TEST_BIND_CONFIG_HOSTKEY_TWICE_REC_STRING);
737 }
738
739 static void torture_bind_config_hostkey_separately(void **state)
740 {
741 struct bind_st *test_state;
742 ssh_bind bind;
743 int rc;
744
745 assert_non_null(state);
746 test_state = *((struct bind_st **)state);
747 assert_non_null(test_state);
748 assert_non_null(test_state->bind);
749 bind = test_state->bind;
750
751 rc = ssh_bind_config_parse_file(bind, LIBSSH_TEST_BIND_CONFIG_HOSTKEY);
752 assert_int_equal(rc, 0);
753 assert_non_null(bind->ecdsakey);
754 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
755
756 rc = ssh_bind_config_parse_file(bind, LIBSSH_TEST_BIND_CONFIG_HOSTKEY2);
757 assert_int_equal(rc, 0);
758 assert_non_null(bind->rsakey);
759 assert_string_equal(bind->rsakey, LIBSSH_RSA_TESTKEY);
760 assert_non_null(bind->ecdsakey);
761 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
762 }
763
764 static void
765 torture_bind_config_loglevel(void **state,
766 const char *file,
767 const char *string,
768 int expect)
769 {
770 struct bind_st *test_state;
771 ssh_bind bind;
772 int previous_level, new_level, rc;
773
774 previous_level = ssh_get_log_level();
775
776 assert_non_null(state);
777 test_state = *((struct bind_st **)state);
778 assert_non_null(test_state);
779 assert_non_null(test_state->bind);
780 bind = test_state->bind;
781
782 _parse_config(bind, file, string, SSH_OK);
783
784 new_level = ssh_get_log_level();
785 assert_int_equal(new_level, expect);
786
787 rc = ssh_set_log_level(previous_level);
788 assert_int_equal(rc, SSH_OK);
789 }
790
791 static void torture_bind_config_loglevel_file(void **state)
792 {
793 torture_bind_config_loglevel(state,
794 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL,
795 NULL,
796 2);
797 }
798
799 static void torture_bind_config_loglevel_string(void **state)
800 {
801 torture_bind_config_loglevel(state,
802 NULL,
803 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_STRING,
804 2);
805 }
806
807 static void torture_bind_config_loglevel1_file(void **state)
808 {
809 torture_bind_config_loglevel(state,
810 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1,
811 NULL,
812 1);
813 }
814
815 static void torture_bind_config_loglevel1_string(void **state)
816 {
817 torture_bind_config_loglevel(state,
818 NULL,
819 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL1_STRING,
820 1);
821 }
822
823 static void torture_bind_config_loglevel_twice_file(void **state)
824 {
825 torture_bind_config_loglevel(state,
826 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE,
827 NULL,
828 2);
829 }
830
831 static void torture_bind_config_loglevel_twice_string(void **state)
832 {
833 torture_bind_config_loglevel(state,
834 NULL,
835 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_STRING,
836 2);
837 }
838
839 static void torture_bind_config_loglevel_twice_rec_file(void **state)
840 {
841 torture_bind_config_loglevel(state,
842 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC,
843 NULL,
844 2);
845 }
846
847 static void torture_bind_config_loglevel_twice_rec_string(void **state)
848 {
849 torture_bind_config_loglevel(state,
850 NULL,
851 LIBSSH_TEST_BIND_CONFIG_LOGLEVEL_TWICE_REC_STRING,
852 2);
853 }
854
855 static void
856 torture_bind_config_ciphers(void **state,
857 const char *file,
858 const char *string,
859 const char *expect)
860 {
861 struct bind_st *test_state;
862 ssh_bind bind;
863 char *fips_ciphers = NULL;
864
865 assert_non_null(state);
866 test_state = *((struct bind_st **)state);
867 assert_non_null(test_state);
868 assert_non_null(test_state->bind);
869 bind = test_state->bind;
870
871 if (ssh_fips_mode()) {
872 fips_ciphers = ssh_keep_fips_algos(SSH_CRYPT_C_S, expect);
873 assert_non_null(fips_ciphers);
874 }
875
876 _parse_config(bind, file, string, SSH_OK);
877
878 assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
879 assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
880 if (ssh_fips_mode()) {
881 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S], fips_ciphers);
882 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C], fips_ciphers);
883 SAFE_FREE(fips_ciphers);
884 } else {
885 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S], expect);
886 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C], expect);
887 }
888 }
889
890 static void torture_bind_config_ciphers_file(void **state)
891 {
892 torture_bind_config_ciphers(state,
893 LIBSSH_TEST_BIND_CONFIG_CIPHERS,
894 NULL,
895 CIPHERS);
896 }
897
898 static void torture_bind_config_ciphers_string(void **state)
899 {
900 torture_bind_config_ciphers(state,
901 NULL,
902 LIBSSH_TEST_BIND_CONFIG_CIPHERS_STRING,
903 CIPHERS);
904 }
905
906 static void torture_bind_config_ciphers2_file(void **state)
907 {
908 torture_bind_config_ciphers(state,
909 LIBSSH_TEST_BIND_CONFIG_CIPHERS2,
910 NULL,
911 CIPHERS2);
912 }
913
914 static void torture_bind_config_ciphers2_string(void **state)
915 {
916 torture_bind_config_ciphers(state,
917 NULL,
918 LIBSSH_TEST_BIND_CONFIG_CIPHERS2_STRING,
919 CIPHERS2);
920 }
921
922 static void torture_bind_config_ciphers_twice_file(void **state)
923 {
924 torture_bind_config_ciphers(state,
925 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE,
926 NULL,
927 CIPHERS);
928 }
929
930 static void torture_bind_config_ciphers_twice_string(void **state)
931 {
932 torture_bind_config_ciphers(state,
933 NULL,
934 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_STRING,
935 CIPHERS);
936 }
937
938 static void torture_bind_config_ciphers_twice_rec_file(void **state)
939 {
940 torture_bind_config_ciphers(state,
941 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC,
942 NULL,
943 CIPHERS);
944 }
945
946 static void torture_bind_config_ciphers_twice_rec_string(void **state)
947 {
948 torture_bind_config_ciphers(state,
949 NULL,
950 LIBSSH_TEST_BIND_CONFIG_CIPHERS_TWICE_REC_STRING,
951 CIPHERS);
952 }
953
954 static void
955 torture_bind_config_macs(void **state,
956 const char *file,
957 const char *string,
958 const char *expect)
959 {
960 struct bind_st *test_state;
961 ssh_bind bind;
962
963 assert_non_null(state);
964 test_state = *((struct bind_st **)state);
965 assert_non_null(test_state);
966 assert_non_null(test_state->bind);
967 bind = test_state->bind;
968
969 _parse_config(bind, file, string, SSH_OK);
970
971 assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
972 assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
973 assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], expect);
974 assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], expect);
975 }
976
977 static void torture_bind_config_macs_file(void **state)
978 {
979 torture_bind_config_macs(state,
980 LIBSSH_TEST_BIND_CONFIG_MACS,
981 NULL,
982 MACS);
983 }
984
985 static void torture_bind_config_macs_string(void **state)
986 {
987 torture_bind_config_macs(state,
988 NULL,
989 LIBSSH_TEST_BIND_CONFIG_MACS_STRING,
990 MACS);
991 }
992
993 static void torture_bind_config_macs2_file(void **state)
994 {
995 torture_bind_config_macs(state,
996 LIBSSH_TEST_BIND_CONFIG_MACS2,
997 NULL,
998 MACS2);
999 }
1000
1001 static void torture_bind_config_macs2_string(void **state)
1002 {
1003 torture_bind_config_macs(state,
1004 NULL,
1005 LIBSSH_TEST_BIND_CONFIG_MACS2_STRING,
1006 MACS2);
1007 }
1008
1009 static void torture_bind_config_macs_twice_file(void **state)
1010 {
1011 torture_bind_config_macs(state,
1012 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE,
1013 NULL,
1014 MACS);
1015 }
1016
1017 static void torture_bind_config_macs_twice_string(void **state)
1018 {
1019 torture_bind_config_macs(state,
1020 NULL,
1021 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_STRING,
1022 MACS);
1023 }
1024
1025 static void torture_bind_config_macs_twice_rec_file(void **state)
1026 {
1027 torture_bind_config_macs(state,
1028 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC,
1029 NULL,
1030 MACS);
1031 }
1032
1033 static void torture_bind_config_macs_twice_rec_string(void **state)
1034 {
1035 torture_bind_config_macs(state,
1036 NULL,
1037 LIBSSH_TEST_BIND_CONFIG_MACS_TWICE_REC_STRING,
1038 MACS);
1039 }
1040
1041 static void
1042 torture_bind_config_kexalgorithms(void **state,
1043 const char *file,
1044 const char *string,
1045 const char *expect)
1046 {
1047 struct bind_st *test_state;
1048 ssh_bind bind;
1049 char *fips_kex = NULL;
1050
1051 assert_non_null(state);
1052 test_state = *((struct bind_st **)state);
1053 assert_non_null(test_state);
1054 assert_non_null(test_state->bind);
1055 bind = test_state->bind;
1056
1057 if (ssh_fips_mode()) {
1058 fips_kex = ssh_keep_fips_algos(SSH_KEX, expect);
1059 assert_non_null(fips_kex);
1060 }
1061
1062 _parse_config(bind, file, string, SSH_OK);
1063
1064 assert_non_null(bind->wanted_methods[SSH_KEX]);
1065 if (ssh_fips_mode()) {
1066 assert_string_equal(bind->wanted_methods[SSH_KEX], fips_kex);
1067 SAFE_FREE(fips_kex);
1068 } else {
1069 assert_string_equal(bind->wanted_methods[SSH_KEX], expect);
1070 }
1071 }
1072
1073 static void torture_bind_config_kexalgorithms_file(void **state)
1074 {
1075 torture_bind_config_kexalgorithms(state,
1076 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS,
1077 NULL,
1078 KEXALGORITHMS);
1079 }
1080
1081 static void torture_bind_config_kexalgorithms_string(void **state)
1082 {
1083 torture_bind_config_kexalgorithms(state,
1084 NULL,
1085 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_STRING,
1086 KEXALGORITHMS);
1087 }
1088
1089 static void torture_bind_config_kexalgorithms2_file(void **state)
1090 {
1091 torture_bind_config_kexalgorithms(state,
1092 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2,
1093 NULL,
1094 KEXALGORITHMS2);
1095 }
1096
1097 static void torture_bind_config_kexalgorithms2_string(void **state)
1098 {
1099 torture_bind_config_kexalgorithms(state,
1100 NULL,
1101 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS2_STRING,
1102 KEXALGORITHMS2);
1103 }
1104
1105 static void torture_bind_config_kexalgorithms_twice_file(void **state)
1106 {
1107 torture_bind_config_kexalgorithms(state,
1108 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE,
1109 NULL,
1110 KEXALGORITHMS);
1111 }
1112
1113 static void torture_bind_config_kexalgorithms_twice_string(void **state)
1114 {
1115 torture_bind_config_kexalgorithms(state,
1116 NULL,
1117 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_STRING,
1118 KEXALGORITHMS);
1119 }
1120
1121 static void torture_bind_config_kexalgorithms_twice_rec_file(void **state)
1122 {
1123 torture_bind_config_kexalgorithms(state,
1124 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC,
1125 NULL,
1126 KEXALGORITHMS);
1127 }
1128
1129 static void torture_bind_config_kexalgorithms_twice_rec_string(void **state)
1130 {
1131 torture_bind_config_kexalgorithms(state,
1132 NULL,
1133 LIBSSH_TEST_BIND_CONFIG_KEXALGORITHMS_TWICE_REC_STRING,
1134 KEXALGORITHMS);
1135 }
1136
1137 static void
1138 torture_bind_config_pubkey_accepted(void **state,
1139 const char *file,
1140 const char *string,
1141 const char *expect)
1142 {
1143 struct bind_st *test_state;
1144 ssh_bind bind;
1145 char *fips_pubkeys = NULL;
1146
1147 assert_non_null(state);
1148 test_state = *((struct bind_st **)state);
1149 assert_non_null(test_state);
1150 assert_non_null(test_state->bind);
1151 bind = test_state->bind;
1152
1153 if (ssh_fips_mode()) {
1154 fips_pubkeys = ssh_keep_fips_algos(SSH_HOSTKEYS, expect);
1155 assert_non_null(fips_pubkeys);
1156 }
1157
1158 _parse_config(bind, file, string, SSH_OK);
1159
1160 assert_non_null(bind->pubkey_accepted_key_types);
1161 if (ssh_fips_mode()) {
1162 assert_string_equal(bind->pubkey_accepted_key_types, fips_pubkeys);
1163 SAFE_FREE(fips_pubkeys);
1164 } else {
1165 assert_string_equal(bind->pubkey_accepted_key_types, expect);
1166 }
1167 }
1168
1169 static void torture_bind_config_pubkey_accepted_file(void **state)
1170 {
1171 torture_bind_config_pubkey_accepted(state,
1172 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED,
1173 NULL,
1174 PUBKEYACCEPTEDTYPES);
1175 }
1176
1177 static void torture_bind_config_pubkey_accepted_string(void **state)
1178 {
1179 torture_bind_config_pubkey_accepted(state,
1180 NULL,
1181 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_STRING,
1182 PUBKEYACCEPTEDTYPES);
1183 }
1184
1185 static void torture_bind_config_pubkey_accepted_twice_file(void **state)
1186 {
1187 torture_bind_config_pubkey_accepted(state,
1188 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE,
1189 NULL,
1190 PUBKEYACCEPTEDTYPES);
1191 }
1192
1193 static void torture_bind_config_pubkey_accepted_twice_string(void **state)
1194 {
1195 torture_bind_config_pubkey_accepted(state,
1196 NULL,
1197 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_STRING,
1198 PUBKEYACCEPTEDTYPES);
1199 }
1200
1201 static void torture_bind_config_pubkey_accepted_twice_rec_file(void **state)
1202 {
1203 torture_bind_config_pubkey_accepted(state,
1204 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC,
1205 NULL,
1206 PUBKEYACCEPTEDTYPES2);
1207 }
1208
1209 static void torture_bind_config_pubkey_accepted_twice_rec_string(void **state)
1210 {
1211 torture_bind_config_pubkey_accepted(state,
1212 NULL,
1213 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_TWICE_REC_STRING,
1214 PUBKEYACCEPTEDTYPES2);
1215 }
1216
1217 static void torture_bind_config_pubkey_accepted_unknown_file(void **state)
1218 {
1219 torture_bind_config_pubkey_accepted(state,
1220 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN,
1221 NULL,
1222 PUBKEYACCEPTEDTYPES);
1223 }
1224
1225 static void torture_bind_config_pubkey_accepted_unknown_string(void **state)
1226 {
1227 torture_bind_config_pubkey_accepted(state,
1228 NULL,
1229 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED_UNKNOWN_STRING,
1230 PUBKEYACCEPTEDTYPES);
1231 }
1232
1233 static void torture_bind_config_pubkey_accepted2_file(void **state)
1234 {
1235 torture_bind_config_pubkey_accepted(state,
1236 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2,
1237 NULL,
1238 PUBKEYACCEPTEDTYPES2);
1239 }
1240
1241 static void torture_bind_config_pubkey_accepted2_string(void **state)
1242 {
1243 torture_bind_config_pubkey_accepted(state,
1244 NULL,
1245 LIBSSH_TEST_BIND_CONFIG_PUBKEY_ACCEPTED2_STRING,
1246 PUBKEYACCEPTEDTYPES2);
1247 }
1248
1249 static void
1250 torture_bind_config_hostkey_algorithms(void **state,
1251 const char *file,
1252 const char *string,
1253 const char *expect)
1254 {
1255 struct bind_st *test_state;
1256 ssh_bind bind;
1257 char *fips_hostkey = NULL;
1258
1259 assert_non_null(state);
1260 test_state = *((struct bind_st **)state);
1261 assert_non_null(test_state);
1262 assert_non_null(test_state->bind);
1263 bind = test_state->bind;
1264
1265 if (ssh_fips_mode()) {
1266 fips_hostkey = ssh_keep_fips_algos(SSH_HOSTKEYS, expect);
1267 assert_non_null(fips_hostkey);
1268 }
1269
1270 _parse_config(bind, file, string, SSH_OK);
1271
1272 assert_non_null(bind->wanted_methods[SSH_HOSTKEYS]);
1273 if (ssh_fips_mode()) {
1274 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS], fips_hostkey);
1275 SAFE_FREE(fips_hostkey);
1276 } else {
1277 assert_string_equal(bind->wanted_methods[SSH_HOSTKEYS], expect);
1278 }
1279 }
1280
1281 static void torture_bind_config_hostkey_algorithms_file(void **state)
1282 {
1283 torture_bind_config_hostkey_algorithms(state,
1284 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS,
1285 NULL,
1286 HOSTKEYALGORITHMS);
1287 }
1288
1289 static void torture_bind_config_hostkey_algorithms_string(void **state)
1290 {
1291 torture_bind_config_hostkey_algorithms(state,
1292 NULL,
1293 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_STRING,
1294 HOSTKEYALGORITHMS);
1295 }
1296
1297 static void torture_bind_config_hostkey_algorithms_twice_file(void **state)
1298 {
1299 torture_bind_config_hostkey_algorithms(state,
1300 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE,
1301 NULL,
1302 HOSTKEYALGORITHMS);
1303 }
1304
1305 static void torture_bind_config_hostkey_algorithms_twice_string(void **state)
1306 {
1307 torture_bind_config_hostkey_algorithms(state,
1308 NULL,
1309 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_STRING,
1310 HOSTKEYALGORITHMS);
1311 }
1312
1313 static void torture_bind_config_hostkey_algorithms_twice_rec_file(void **state)
1314 {
1315 torture_bind_config_hostkey_algorithms(state,
1316 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC,
1317 NULL,
1318 HOSTKEYALGORITHMS2);
1319 }
1320
1321 static void torture_bind_config_hostkey_algorithms_twice_rec_string(void **state)
1322 {
1323 torture_bind_config_hostkey_algorithms(state,
1324 NULL,
1325 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_TWICE_REC_STRING,
1326 HOSTKEYALGORITHMS2);
1327 }
1328
1329 static void torture_bind_config_hostkey_algorithms2_file(void **state)
1330 {
1331 torture_bind_config_hostkey_algorithms(state,
1332 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2,
1333 NULL,
1334 HOSTKEYALGORITHMS2);
1335 }
1336
1337 static void torture_bind_config_hostkey_algorithms2_string(void **state)
1338 {
1339 torture_bind_config_hostkey_algorithms(state,
1340 NULL,
1341 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS2_STRING,
1342 HOSTKEYALGORITHMS2);
1343 }
1344
1345 static void torture_bind_config_hostkey_algorithms_unknown_file(void **state)
1346 {
1347 torture_bind_config_hostkey_algorithms(state,
1348 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN,
1349 NULL,
1350 HOSTKEYALGORITHMS);
1351 }
1352
1353 static void torture_bind_config_hostkey_algorithms_unknown_string(void **state)
1354 {
1355 torture_bind_config_hostkey_algorithms(state,
1356 NULL,
1357 LIBSSH_TEST_BIND_CONFIG_HOSTKEY_ALGORITHMS_UNKNOWN_STRING,
1358 HOSTKEYALGORITHMS);
1359 }
1360
1361 static int assert_full_bind_config(void **state)
1362 {
1363 struct bind_st *test_state;
1364 ssh_bind bind;
1365 int new_level;
1366
1367 char *fips_ciphers = NULL;
1368 char *fips_kex = NULL;
1369
1370 if (ssh_fips_mode()) {
1371 fips_ciphers = ssh_keep_fips_algos(SSH_CRYPT_C_S, CIPHERS);
1372 assert_non_null(fips_ciphers);
1373 fips_kex = ssh_keep_fips_algos(SSH_KEX, KEXALGORITHMS);
1374 assert_non_null(fips_kex);
1375 }
1376
1377 assert_non_null(state);
1378 test_state = *((struct bind_st **)state);
1379 assert_non_null(test_state);
1380 assert_non_null(test_state->bind);
1381 bind = test_state->bind;
1382
1383 new_level = ssh_get_log_level();
1384 assert_int_equal(new_level, 2);
1385
1386 assert_non_null(bind->bindaddr);
1387 assert_string_equal(bind->bindaddr, LISTEN_ADDRESS);
1388
1389 assert_int_equal(bind->bindport, 123);
1390
1391 assert_non_null(bind->ecdsakey);
1392 assert_string_equal(bind->ecdsakey, LIBSSH_ECDSA_521_TESTKEY);
1393
1394 assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
1395 if (ssh_fips_mode()) {
1396 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S], fips_ciphers);
1397 } else {
1398 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S], CIPHERS);
1399 }
1400
1401 assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
1402 if (ssh_fips_mode()) {
1403 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C], fips_ciphers);
1404 } else {
1405 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C], CIPHERS);
1406 }
1407
1408 assert_non_null(bind->wanted_methods[SSH_MAC_S_C]);
1409 assert_string_equal(bind->wanted_methods[SSH_MAC_S_C], MACS);
1410
1411 assert_non_null(bind->wanted_methods[SSH_MAC_C_S]);
1412 assert_string_equal(bind->wanted_methods[SSH_MAC_C_S], MACS);
1413
1414 assert_non_null(bind->wanted_methods[SSH_KEX]);
1415 if (ssh_fips_mode()) {
1416 assert_string_equal(bind->wanted_methods[SSH_KEX], fips_kex);
1417 } else {
1418 assert_string_equal(bind->wanted_methods[SSH_KEX], KEXALGORITHMS);
1419 }
1420
1421 assert_int_equal(bind->rsa_min_size, 2233);
1422
1423 SAFE_FREE(fips_ciphers);
1424 SAFE_FREE(fips_kex);
1425
1426 return 0;
1427 }
1428
1429 static void torture_bind_config_full(void **state)
1430 {
1431 struct bind_st *test_state;
1432 ssh_bind bind;
1433 int rc;
1434 int previous_level;
1435
1436 assert_non_null(state);
1437 test_state = *((struct bind_st **)state);
1438 assert_non_null(test_state);
1439 assert_non_null(test_state->bind);
1440 bind = test_state->bind;
1441
1442 previous_level = ssh_get_log_level();
1443
1444 rc = ssh_bind_config_parse_file(bind, LIBSSH_TEST_BIND_CONFIG_FULL);
1445 assert_int_equal(rc, 0);
1446
1447 rc = assert_full_bind_config(state);
1448 assert_int_equal(rc, 0);
1449
1450 rc = ssh_set_log_level(previous_level);
1451 assert_int_equal(rc, SSH_OK);
1452 }
1453
1454 static void torture_bind_config_include(void **state)
1455 {
1456 struct bind_st *test_state;
1457 ssh_bind bind;
1458 int rc;
1459 int previous_level;
1460
1461 assert_non_null(state);
1462 test_state = *((struct bind_st **)state);
1463 assert_non_null(test_state);
1464 assert_non_null(test_state->bind);
1465 bind = test_state->bind;
1466
1467 previous_level = ssh_get_log_level();
1468
1469 rc = ssh_bind_config_parse_file(bind, LIBSSH_TEST_BIND_CONFIG_INCLUDE);
1470 assert_int_equal(rc, 0);
1471
1472 rc = assert_full_bind_config(state);
1473 assert_int_equal(rc, 0);
1474
1475 rc = ssh_set_log_level(previous_level);
1476 assert_int_equal(rc, SSH_OK);
1477 }
1478
1479 static void torture_bind_config_include_recursive(void **state)
1480 {
1481 struct bind_st *test_state;
1482 ssh_bind bind;
1483 int rc;
1484 int previous_level;
1485
1486 assert_non_null(state);
1487 test_state = *((struct bind_st **)state);
1488 assert_non_null(test_state);
1489 assert_non_null(test_state->bind);
1490 bind = test_state->bind;
1491
1492 previous_level = ssh_get_log_level();
1493
1494 rc = ssh_bind_config_parse_file(bind,
1495 LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE);
1496 assert_int_equal(rc, 0);
1497
1498 rc = assert_full_bind_config(state);
1499 assert_int_equal(rc, 0);
1500
1501 rc = ssh_set_log_level(previous_level);
1502 assert_int_equal(rc, SSH_OK);
1503 }
1504
1505 static void torture_bind_config_include_recursive_loop(void **state)
1506 {
1507 struct bind_st *test_state;
1508 ssh_bind bind;
1509 int rc;
1510
1511 assert_non_null(state);
1512 test_state = *((struct bind_st **)state);
1513 assert_non_null(test_state);
1514 assert_non_null(test_state->bind);
1515 bind = test_state->bind;
1516
1517 rc = ssh_bind_config_parse_file(bind,
1518 LIBSSH_TEST_BIND_CONFIG_INCLUDE_RECURSIVE_LOOP);
1519 assert_int_equal(rc, 0);
1520 }
1521
1522 /**
1523 * @brief Verify the configuration parser does not choke on unknown
1524 * or unsupported configuration options
1525 */
1526 static void torture_bind_config_corner_cases(void **state)
1527 {
1528 struct bind_st *test_state;
1529 ssh_bind bind;
1530 int rc;
1531
1532 assert_non_null(state);
1533 test_state = *((struct bind_st **)state);
1534 assert_non_null(test_state);
1535 assert_non_null(test_state->bind);
1536 bind = test_state->bind;
1537
1538 rc = ssh_bind_config_parse_file(bind, LIBSSH_TEST_BIND_CONFIG_CORNER_CASES);
1539 assert_int_equal(rc, 0);
1540
1541 assert_non_null(bind->wanted_methods[SSH_CRYPT_C_S]);
1542 assert_string_equal(bind->wanted_methods[SSH_CRYPT_C_S], CIPHERS2);
1543
1544 assert_non_null(bind->wanted_methods[SSH_CRYPT_S_C]);
1545 assert_string_equal(bind->wanted_methods[SSH_CRYPT_S_C], CIPHERS2);
1546 }
1547
1548 static void torture_bind_config_match_all(void **state)
1549 {
1550 struct bind_st *test_state;
1551 ssh_bind bind;
1552 int rc;
1553 int previous_level, new_level;
1554
1555 assert_non_null(state);
1556 test_state = *((struct bind_st **)state);
1557 assert_non_null(test_state);
1558 assert_non_null(test_state->bind);
1559 bind = test_state->bind;
1560
1561 previous_level = ssh_get_log_level();
1562
1563 rc = ssh_bind_config_parse_file(bind,
1564 LIBSSH_TEST_BIND_CONFIG_MATCH_ALL);
1565 assert_int_equal(rc, 0);
1566
1567 new_level = ssh_get_log_level();
1568 assert_int_equal(new_level, 1);
1569
1570 rc = ssh_set_log_level(previous_level);
1571 assert_int_equal(rc, SSH_OK);
1572 }
1573
1574 static void torture_bind_config_match_twice(void **state)
1575 {
1576 struct bind_st *test_state;
1577 ssh_bind bind;
1578 int rc;
1579 int previous_level, new_level;
1580
1581 assert_non_null(state);
1582 test_state = *((struct bind_st **)state);
1583 assert_non_null(test_state);
1584 assert_non_null(test_state->bind);
1585 bind = test_state->bind;
1586
1587 previous_level = ssh_get_log_level();
1588
1589 rc = ssh_bind_config_parse_file(bind,
1590 LIBSSH_TEST_BIND_CONFIG_MATCH_TWICE);
1591 assert_int_equal(rc, 0);
1592
1593 new_level = ssh_get_log_level();
1594 assert_int_equal(new_level, 1);
1595
1596 rc = ssh_set_log_level(previous_level);
1597 assert_int_equal(rc, SSH_OK);
1598 }
1599
1600 static void torture_bind_config_match_unsupported(void **state)
1601 {
1602 struct bind_st *test_state;
1603 ssh_bind bind;
1604 int rc;
1605 int previous_level;
1606
1607 assert_non_null(state);
1608 test_state = *((struct bind_st **)state);
1609 assert_non_null(test_state);
1610 assert_non_null(test_state->bind);
1611 bind = test_state->bind;
1612
1613 previous_level = ssh_get_log_level();
1614
1615 rc = ssh_bind_config_parse_file(bind,
1616 LIBSSH_TEST_BIND_CONFIG_MATCH_UNSUPPORTED);
1617 assert_int_equal(rc, 0);
1618
1619 rc = assert_full_bind_config(state);
1620 assert_int_equal(rc, 0);
1621
1622 rc = ssh_set_log_level(previous_level);
1623 assert_int_equal(rc, SSH_OK);
1624 }
1625
1626 static void torture_bind_config_match_not_allowed(void **state)
1627 {
1628 struct bind_st *test_state;
1629 ssh_bind bind;
1630 int rc;
1631 int previous_level;
1632
1633 assert_non_null(state);
1634 test_state = *((struct bind_st **)state);
1635 assert_non_null(test_state);
1636 assert_non_null(test_state->bind);
1637 bind = test_state->bind;
1638
1639 previous_level = ssh_get_log_level();
1640
1641 rc = ssh_bind_config_parse_file(bind,
1642 LIBSSH_TEST_BIND_CONFIG_MATCH_NOT_ALLOWED);
1643 assert_int_equal(rc, 0);
1644
1645 rc = assert_full_bind_config(state);
1646 assert_int_equal(rc, 0);
1647
1648 rc = ssh_set_log_level(previous_level);
1649 assert_int_equal(rc, SSH_OK);
1650 }
1651
1652 static void torture_bind_config_match_corner_cases(void **state)
1653 {
1654 struct bind_st *test_state;
1655 ssh_bind bind;
1656 int rc;
1657 int previous_level, new_level;
1658
1659 assert_non_null(state);
1660 test_state = *((struct bind_st **)state);
1661 assert_non_null(test_state);
1662 assert_non_null(test_state->bind);
1663 bind = test_state->bind;
1664
1665 previous_level = ssh_get_log_level();
1666
1667 rc = ssh_bind_config_parse_file(bind,
1668 LIBSSH_TEST_BIND_CONFIG_MATCH_CORNER_CASES);
1669 assert_int_equal(rc, 0);
1670
1671 new_level = ssh_get_log_level();
1672 assert_int_equal(new_level, 3);
1673
1674 rc = ssh_set_log_level(previous_level);
1675 assert_int_equal(rc, SSH_OK);
1676 }
1677
1678 static void torture_bind_config_match_invalid(void **state)
1679 {
1680 struct bind_st *test_state;
1681 ssh_bind bind;
1682 int rc;
1683 int previous_level;
1684
1685 assert_non_null(state);
1686 test_state = *((struct bind_st **)state);
1687 assert_non_null(test_state);
1688 assert_non_null(test_state->bind);
1689 bind = test_state->bind;
1690
1691 previous_level = ssh_get_log_level();
1692
1693 rc = ssh_bind_config_parse_file(bind,
1694 LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID);
1695 assert_int_equal(rc, -1);
1696
1697 rc = ssh_bind_config_parse_file(bind,
1698 LIBSSH_TEST_BIND_CONFIG_MATCH_INVALID2);
1699 assert_int_equal(rc, -1);
1700
1701 rc = ssh_set_log_level(previous_level);
1702 assert_int_equal(rc, SSH_OK);
1703 }
1704
1705 int torture_run_tests(void)
1706 {
1707 int rc;
1708 struct CMUnitTest tests[] = {
1709 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_file,
1710 sshbind_setup, sshbind_teardown),
1711 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_string,
1712 sshbind_setup, sshbind_teardown),
1713 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address2_file,
1714 sshbind_setup, sshbind_teardown),
1715 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address2_string,
1716 sshbind_setup, sshbind_teardown),
1717 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_twice_file,
1718 sshbind_setup, sshbind_teardown),
1719 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_twice_string,
1720 sshbind_setup, sshbind_teardown),
1721 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_twice_rec_file,
1722 sshbind_setup, sshbind_teardown),
1723 cmocka_unit_test_setup_teardown(torture_bind_config_listen_address_twice_rec_string,
1724 sshbind_setup, sshbind_teardown),
1725 cmocka_unit_test_setup_teardown(torture_bind_config_port_file,
1726 sshbind_setup, sshbind_teardown),
1727 cmocka_unit_test_setup_teardown(torture_bind_config_port_string,
1728 sshbind_setup, sshbind_teardown),
1729 cmocka_unit_test_setup_teardown(torture_bind_config_port2_file,
1730 sshbind_setup, sshbind_teardown),
1731 cmocka_unit_test_setup_teardown(torture_bind_config_port2_string,
1732 sshbind_setup, sshbind_teardown),
1733 cmocka_unit_test_setup_teardown(torture_bind_config_port_twice_file,
1734 sshbind_setup, sshbind_teardown),
1735 cmocka_unit_test_setup_teardown(torture_bind_config_port_twice_string,
1736 sshbind_setup, sshbind_teardown),
1737 cmocka_unit_test_setup_teardown(torture_bind_config_port_twice_rec_file,
1738 sshbind_setup, sshbind_teardown),
1739 cmocka_unit_test_setup_teardown(torture_bind_config_port_twice_rec_string,
1740 sshbind_setup, sshbind_teardown),
1741 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_file,
1742 sshbind_setup, sshbind_teardown),
1743 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_string,
1744 sshbind_setup, sshbind_teardown),
1745 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_twice_file,
1746 sshbind_setup, sshbind_teardown),
1747 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_twice_string,
1748 sshbind_setup, sshbind_teardown),
1749 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_twice_rec_file,
1750 sshbind_setup, sshbind_teardown),
1751 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_twice_rec_string,
1752 sshbind_setup, sshbind_teardown),
1753 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_separately,
1754 sshbind_setup, sshbind_teardown),
1755 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_file,
1756 sshbind_setup, sshbind_teardown),
1757 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_string,
1758 sshbind_setup, sshbind_teardown),
1759 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel1_file,
1760 sshbind_setup, sshbind_teardown),
1761 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel1_string,
1762 sshbind_setup, sshbind_teardown),
1763 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_twice_file,
1764 sshbind_setup, sshbind_teardown),
1765 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_twice_string,
1766 sshbind_setup, sshbind_teardown),
1767 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_twice_rec_file,
1768 sshbind_setup, sshbind_teardown),
1769 cmocka_unit_test_setup_teardown(torture_bind_config_loglevel_twice_rec_string,
1770 sshbind_setup, sshbind_teardown),
1771 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_file,
1772 sshbind_setup, sshbind_teardown),
1773 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_string,
1774 sshbind_setup, sshbind_teardown),
1775 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers2_file,
1776 sshbind_setup, sshbind_teardown),
1777 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers2_string,
1778 sshbind_setup, sshbind_teardown),
1779 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_twice_file,
1780 sshbind_setup, sshbind_teardown),
1781 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_twice_string,
1782 sshbind_setup, sshbind_teardown),
1783 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_twice_rec_file,
1784 sshbind_setup, sshbind_teardown),
1785 cmocka_unit_test_setup_teardown(torture_bind_config_ciphers_twice_rec_string,
1786 sshbind_setup, sshbind_teardown),
1787 cmocka_unit_test_setup_teardown(torture_bind_config_macs_file,
1788 sshbind_setup, sshbind_teardown),
1789 cmocka_unit_test_setup_teardown(torture_bind_config_macs_string,
1790 sshbind_setup, sshbind_teardown),
1791 cmocka_unit_test_setup_teardown(torture_bind_config_macs2_file,
1792 sshbind_setup, sshbind_teardown),
1793 cmocka_unit_test_setup_teardown(torture_bind_config_macs2_string,
1794 sshbind_setup, sshbind_teardown),
1795 cmocka_unit_test_setup_teardown(torture_bind_config_macs_twice_file,
1796 sshbind_setup, sshbind_teardown),
1797 cmocka_unit_test_setup_teardown(torture_bind_config_macs_twice_string,
1798 sshbind_setup, sshbind_teardown),
1799 cmocka_unit_test_setup_teardown(torture_bind_config_macs_twice_rec_file,
1800 sshbind_setup, sshbind_teardown),
1801 cmocka_unit_test_setup_teardown(torture_bind_config_macs_twice_rec_string,
1802 sshbind_setup, sshbind_teardown),
1803 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_file,
1804 sshbind_setup, sshbind_teardown),
1805 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_string,
1806 sshbind_setup, sshbind_teardown),
1807 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms2_file,
1808 sshbind_setup, sshbind_teardown),
1809 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms2_string,
1810 sshbind_setup, sshbind_teardown),
1811 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_twice_file,
1812 sshbind_setup, sshbind_teardown),
1813 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_twice_string,
1814 sshbind_setup, sshbind_teardown),
1815 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_twice_rec_file,
1816 sshbind_setup, sshbind_teardown),
1817 cmocka_unit_test_setup_teardown(torture_bind_config_kexalgorithms_twice_rec_string,
1818 sshbind_setup, sshbind_teardown),
1819 cmocka_unit_test_setup_teardown(torture_bind_config_full,
1820 sshbind_setup, sshbind_teardown),
1821 cmocka_unit_test_setup_teardown(torture_bind_config_include,
1822 sshbind_setup, sshbind_teardown),
1823 cmocka_unit_test_setup_teardown(torture_bind_config_include_recursive,
1824 sshbind_setup, sshbind_teardown),
1825 cmocka_unit_test_setup_teardown(torture_bind_config_include_recursive_loop,
1826 sshbind_setup, sshbind_teardown),
1827 cmocka_unit_test_setup_teardown(torture_bind_config_corner_cases,
1828 sshbind_setup, sshbind_teardown),
1829 cmocka_unit_test_setup_teardown(torture_bind_config_match_all,
1830 sshbind_setup, sshbind_teardown),
1831 cmocka_unit_test_setup_teardown(torture_bind_config_match_twice,
1832 sshbind_setup, sshbind_teardown),
1833 cmocka_unit_test_setup_teardown(torture_bind_config_match_unsupported,
1834 sshbind_setup, sshbind_teardown),
1835 cmocka_unit_test_setup_teardown(torture_bind_config_match_not_allowed,
1836 sshbind_setup, sshbind_teardown),
1837 cmocka_unit_test_setup_teardown(torture_bind_config_match_corner_cases,
1838 sshbind_setup, sshbind_teardown),
1839 cmocka_unit_test_setup_teardown(torture_bind_config_match_invalid,
1840 sshbind_setup, sshbind_teardown),
1841 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_file,
1842 sshbind_setup, sshbind_teardown),
1843 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_string,
1844 sshbind_setup, sshbind_teardown),
1845 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_twice_file,
1846 sshbind_setup, sshbind_teardown),
1847 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_twice_string,
1848 sshbind_setup, sshbind_teardown),
1849 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_twice_rec_file,
1850 sshbind_setup, sshbind_teardown),
1851 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_twice_rec_string,
1852 sshbind_setup, sshbind_teardown),
1853 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted2_file,
1854 sshbind_setup, sshbind_teardown),
1855 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted2_string,
1856 sshbind_setup, sshbind_teardown),
1857 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_unknown_file,
1858 sshbind_setup, sshbind_teardown),
1859 cmocka_unit_test_setup_teardown(torture_bind_config_pubkey_accepted_unknown_string,
1860 sshbind_setup, sshbind_teardown),
1861 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_file,
1862 sshbind_setup, sshbind_teardown),
1863 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_string,
1864 sshbind_setup, sshbind_teardown),
1865 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_twice_file,
1866 sshbind_setup, sshbind_teardown),
1867 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_twice_string,
1868 sshbind_setup, sshbind_teardown),
1869 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_twice_rec_file,
1870 sshbind_setup, sshbind_teardown),
1871 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_twice_rec_string,
1872 sshbind_setup, sshbind_teardown),
1873 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms2_file,
1874 sshbind_setup, sshbind_teardown),
1875 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms2_string,
1876 sshbind_setup, sshbind_teardown),
1877 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_unknown_file,
1878 sshbind_setup, sshbind_teardown),
1879 cmocka_unit_test_setup_teardown(torture_bind_config_hostkey_algorithms_unknown_string,
1880 sshbind_setup, sshbind_teardown),
1881 };
1882
1883 ssh_init();
1884 torture_filter_tests(tests);
1885 rc = cmocka_run_group_tests(tests, NULL, NULL);
1886 ssh_finalize();
1887 return rc;
1888 }