1 /*
2  * torture.c - torture library for testing libssh
3  *
4  * This file is part of the SSH Library
5  *
6  * Copyright (c) 2008-2009 by Andreas Schneider <asn@cryptomilk.org>
7  *
8  * The SSH Library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or (at your
11  * option) any later version.
12  *
13  * The SSH Library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the SSH Library; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA 02111-1307, USA.
22  */
23 
24 #ifndef _TORTURE_H
25 #define _TORTURE_H
26 
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stddef.h>
35 #include <setjmp.h>
36 
37 #include "libssh/priv.h"
38 #include "libssh/server.h"
39 #include "libssh/sftp.h"
40 
41 #include <cmocka.h>
42 
43 #include "torture_cmocka.h"
44 #include "tests_config.h"
45 
46 #ifndef assert_return_code
47 /* hack for older versions of cmocka */
48 #define assert_return_code(code, errno) \
49     assert_true(code >= 0)
50 #endif /* assert_return_code */
51 
52 #define TORTURE_SSH_SERVER "127.0.0.10"
53 #define TORTURE_SSH_USER_BOB "bob"
54 #define TORTURE_SSH_USER_BOB_PASSWORD "secret"
55 
56 #define TORTURE_SSH_USER_ALICE "alice"
57 
58 /* Used by main to communicate with parse_opt. */
59 struct argument_s {
60   const char *pattern;
61   int verbose;
62 };
63 
64 struct torture_sftp {
65     ssh_session ssh;
66     sftp_session sftp;
67     char *testdir;
68 };
69 
70 struct torture_state {
71     char *socket_dir;
72     char *pcap_file;
73     char *srv_pidfile;
74     char *srv_config;
75     bool srv_pam;
76     char *srv_additional_config;
77     struct {
78         ssh_session session;
79         struct torture_sftp *tsftp;
80     } ssh;
81 #ifdef WITH_PCAP
82     ssh_pcap_file plain_pcap;
83 #endif
84 };
85 
86 #ifndef ZERO_STRUCT
87 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
88 #endif
89 
90 void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments);
91 
92 int torture_rmdirs(const char *path);
93 int torture_isdir(const char *path);
94 
95 int torture_terminate_process(const char *pidfile);
96 
97 /*
98  * Returns the verbosity level asked by user
99  */
100 int torture_libssh_verbosity(void);
101 
102 ssh_session torture_ssh_session(struct torture_state *s,
103                                 const char *host,
104                                 const unsigned int *port,
105                                 const char *user,
106                                 const char *password);
107 
108 ssh_bind torture_ssh_bind(const char *addr,
109                           const unsigned int port,
110                           enum ssh_keytypes_e key_type,
111                           const char *private_key_file);
112 
113 struct torture_sftp *torture_sftp_session(ssh_session session);
114 struct torture_sftp *torture_sftp_session_channel(ssh_session session, ssh_channel channel);
115 void torture_sftp_close(struct torture_sftp *t);
116 
117 void torture_write_file(const char *filename, const char *data);
118 
119 #define torture_filter_tests(tests) _torture_filter_tests(tests, sizeof(tests) / sizeof(tests)[0])
120 void _torture_filter_tests(struct CMUnitTest *tests, size_t ntests);
121 
122 const char *torture_server_address(int domain);
123 int torture_server_port(void);
124 
125 void torture_setup_socket_dir(void **state);
126 void torture_setup_sshd_server(void **state, bool pam);
127 
128 void torture_teardown_socket_dir(void **state);
129 void torture_teardown_sshd_server(void **state);
130 
131 int torture_update_sshd_config(void **state, const char *config);
132 
133 void torture_reset_config(ssh_session session);
134 
135 /*
136  * This function must be defined in every unit test file.
137  */
138 int torture_run_tests(void);
139 
140 char *torture_make_temp_dir(const char *template);
141 char *torture_create_temp_file(const char *template);
142 
143 char *torture_get_current_working_dir(void);
144 int torture_change_dir(char *path);
145 
146 #endif /* _TORTURE_H */
147