Debian Codegraph @

tests/unittests/torture_unit_server.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include <errno.h>
6 #include <pthread.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <signal.h>
11
12 #include <libssh/libssh.h>
13 #include <libssh/bind.h>
14 #include "torture.h"
15 #include "torture_key.h"
16
17 #define TEST_SERVER_PORT 2222
18
19 #if 0
20 struct test_state {
21 const char *hostkey;
22 char *hostkey_path;
23 enum ssh_keytypes_e key_type;
24 int fd;
25 };
26
27 static int setup(void **state)
28 {
29 struct test_state *ts = NULL;
30 mode_t mask;
31 int rc;
32
33 ssh_threads_set_callbacks(ssh_threads_get_pthread());
34 rc = ssh_init();
35 if (rc != SSH_OK) {
36 return -1;
37 }
38
39 ts = malloc(sizeof(struct test_state));
40 assert_non_null(ts);
41
42 ts->hostkey_path = strdup("/tmp/libssh_hostkey_XXXXXX");
43
44 mask = umask(S_IRWXO | S_IRWXG);
45 ts->fd = mkstemp(ts->hostkey_path);
46 umask(mask);
47 assert_return_code(ts->fd, errno);
48 close(ts->fd);
49
50 ts->key_type = SSH_KEYTYPE_ECDSA_P256;
51 ts->hostkey = torture_get_testkey(ts->key_type, 0);
52
53 torture_write_file(ts->hostkey_path, ts->hostkey);
54
55 *state = ts;
56
57 return 0;
58 }
59
60 static int teardown(void **state)
61 {
62 struct test_state *ts = (struct test_state *)*state;
63
64 unlink(ts->hostkey);
65 free(ts->hostkey_path);
66 free(ts);
67
68 ssh_finalize();
69
70 return 0;
71 }
72
73 /* TODO the signals are handled by cmocka so they are not testable her :( */
74 static void *int_thread(void *arg)
75 {
76 usleep(1);
77 kill(getpid(), SIGUSR1);
78 return NULL;
79 }
80
81 static void *client_thread(void *arg)
82 {
83 unsigned int test_port = TEST_SERVER_PORT;
84 int rc;
85 ssh_session session;
86 ssh_channel channel;
87
88 /* unused */
89 (void)arg;
90
91 usleep(200);
92 session = torture_ssh_session(NULL, "localhost",
93 &test_port,
94 "foo", "bar");
95 assert_non_null(session);
96
97 channel = ssh_channel_new(session);
98 assert_non_null(channel);
99
100 rc = ssh_channel_open_session(channel);
101 assert_int_equal(rc, SSH_OK);
102
103 ssh_free(session);
104 return NULL;
105 }
106
107 static void test_ssh_accept_interrupt(void **state)
108 {
109 struct test_state *ts = (struct test_state *)*state;
110 int rc;
111 pthread_t client_pthread, interrupt_pthread;
112 ssh_bind sshbind = NULL;
113 ssh_session server;
114
115 /* Create server */
116 sshbind = torture_ssh_bind("localhost",
117 TEST_SERVER_PORT,
118 ts->key_type,
119 ts->hostkey_path);
120 assert_non_null(sshbind);
121
122 server = ssh_new();
123 assert_non_null(server);
124
125 /* Send interrupt in 1 second */
126 rc = pthread_create(&interrupt_pthread, NULL, int_thread, NULL);
127 assert_return_code(rc, errno);
128
129 rc = pthread_join(interrupt_pthread, NULL);
130 assert_int_equal(rc, 0);
131
132 rc = ssh_bind_accept(sshbind, server);
133 assert_int_equal(rc, SSH_ERROR);
134 assert_int_equal(ssh_get_error_code(sshbind), SSH_EINTR);
135
136 /* Get client to connect now */
137 rc = pthread_create(&client_pthread, NULL, client_thread, NULL);
138 assert_return_code(rc, errno);
139
140 /* Now, try again */
141 rc = ssh_bind_accept(sshbind, server);
142 assert_int_equal(rc, SSH_OK);
143
144 /* Cleanup */
145 ssh_bind_free(sshbind);
146
147 rc = pthread_join(client_pthread, NULL);
148 assert_int_equal(rc, 0);
149 }
150 #endif
151
152
153 static void test_default_hostkey_paths(void **state)
154 {
155 int rc;
156 ssh_bind sshbind = NULL;
157
158 /* state not used */
159 (void)state;
160
161 /* Create server */
162 rc = ssh_init();
163 assert_int_equal(rc, 0);
164
165 sshbind = ssh_bind_new();
166 assert_non_null(sshbind);
167
168 /* This will fail because we don't have permission to import keys unless we run as root
169 * TODO: Implement some filesystem wrapper, that would allow this check to pass by
170 * reading the keys from some accessible test location */
171 ssh_bind_listen(sshbind);
172
173 assert_string_equal(sshbind->rsakey, "/etc/ssh/ssh_host_rsa_key");
174 assert_string_equal(sshbind->ecdsakey, "/etc/ssh/ssh_host_ecdsa_key");
175 assert_string_equal(sshbind->ed25519key, "/etc/ssh/ssh_host_ed25519_key");
176
177 /* Cleanup */
178 ssh_bind_free(sshbind);
179 ssh_finalize();
180 }
181
182 int torture_run_tests(void)
183 {
184 int rc;
185 const struct CMUnitTest tests[] = {
186 cmocka_unit_test(test_default_hostkey_paths),
187 /* Not working correctly the signals are not testable under cmocka
188 cmocka_unit_test_setup_teardown(test_ssh_accept_interrupt,
189 setup,
190 teardown) */
191 };
192
193 rc = cmocka_run_group_tests(tests, NULL, NULL);
194 return rc;
195 }