| 1 |
#define LIBSSH_STATIC |
| 2 |
|
| 3 |
#include "config.h" |
| 4 |
|
| 5 |
#include "torture.h" |
| 6 |
#include "sftp.c" |
| 7 |
|
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/resource.h> |
| 10 |
#include <pwd.h> |
| 11 |
#include <errno.h> |
| 12 |
|
| 13 |
#if HAVE_VALGRIND_VALGRIND_H |
| 14 |
#include <valgrind/valgrind.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
static int sshd_setup(void **state) |
| 18 |
{ |
| 19 |
torture_setup_sshd_server(state, false); |
| 20 |
return 0; |
| 21 |
} |
| 22 |
|
| 23 |
static int sshd_teardown(void **state) |
| 24 |
{ |
| 25 |
torture_teardown_sshd_server(state); |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
static int session_setup(void **state) |
| 30 |
{ |
| 31 |
struct torture_state *s = *state; |
| 32 |
struct passwd *pwd = NULL; |
| 33 |
int rc; |
| 34 |
|
| 35 |
pwd = getpwnam("bob"); |
| 36 |
assert_non_null(pwd); |
| 37 |
|
| 38 |
rc = setuid(pwd->pw_uid); |
| 39 |
assert_return_code(rc, errno); |
| 40 |
|
| 41 |
s->ssh.session = torture_ssh_session(s, |
| 42 |
TORTURE_SSH_SERVER, |
| 43 |
NULL, |
| 44 |
TORTURE_SSH_USER_ALICE, |
| 45 |
NULL); |
| 46 |
assert_non_null(s->ssh.session); |
| 47 |
|
| 48 |
s->ssh.tsftp = torture_sftp_session(s->ssh.session); |
| 49 |
assert_non_null(s->ssh.tsftp); |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static int session_teardown(void **state) |
| 55 |
{ |
| 56 |
struct torture_state *s = *state; |
| 57 |
|
| 58 |
torture_rmdirs(s->ssh.tsftp->testdir); |
| 59 |
torture_sftp_close(s->ssh.tsftp); |
| 60 |
ssh_disconnect(s->ssh.session); |
| 61 |
ssh_free(s->ssh.session); |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static void torture_sftp_limits(void **state) |
| 67 |
{ |
| 68 |
struct torture_state *s = *state; |
| 69 |
struct torture_sftp *t = s->ssh.tsftp; |
| 70 |
sftp_limits_t li = NULL; |
| 71 |
int rc; |
| 72 |
|
| 73 |
li = sftp_limits(t->sftp); |
| 74 |
assert_non_null(li); |
| 75 |
|
| 76 |
rc = sftp_extension_supported(t->sftp, "[email protected]", "1"); |
| 77 |
if (rc == 1) { |
| 78 |
/* |
| 79 |
* Tests are run against the OpenSSH server, hence we check for the |
| 80 |
* specific limits used by OpenSSH. |
| 81 |
*/ |
| 82 |
uint64_t openssh_max_packet_length = 256 * 1024; |
| 83 |
uint64_t openssh_max_read_length = openssh_max_packet_length - 1024; |
| 84 |
uint64_t openssh_max_write_length = openssh_max_packet_length - 1024; |
| 85 |
size_t vg = 0; |
| 86 |
|
| 87 |
assert_int_equal(li->max_packet_length, openssh_max_packet_length); |
| 88 |
assert_int_equal(li->max_read_length, openssh_max_read_length); |
| 89 |
assert_int_equal(li->max_write_length, openssh_max_write_length); |
| 90 |
|
| 91 |
/* |
| 92 |
* fds - File descriptors, w.r.to - With respect to |
| 93 |
* |
| 94 |
* Valgrind reserves some fds for itself and changes the rlimits |
| 95 |
* w.r.to fds for the process its inspecting. Due to this reservation |
| 96 |
* the rlimits w.r.to fds for our test may not be the same as the |
| 97 |
* rlimits w.r.to fds seen by OpenSSH server (which Valgrind isn't |
| 98 |
* inspecting). |
| 99 |
* |
| 100 |
* Valgrind changes the limits in such a way that after seeing the |
| 101 |
* changed limits, the test cannot predict the original unchanged |
| 102 |
* limits (which OpenSSH would be using). Hence, the test cannot |
| 103 |
* determine the correct value of "max_open_handles" that the OpenSSH |
| 104 |
* server should've sent. |
| 105 |
* |
| 106 |
* So if Valgrind is running our test, we don't provide any kind of |
| 107 |
* check for max_open_handles. Check for >= 0 is also not provided in |
| 108 |
* this case since that's always true for an uint64_t (an unsigned type) |
| 109 |
*/ |
| 110 |
#if HAVE_VALGRIND_VALGRIND_H |
| 111 |
vg = RUNNING_ON_VALGRIND; |
| 112 |
#endif |
| 113 |
|
| 114 |
if (vg == 0) { |
| 115 |
struct rlimit rlim = {0}; |
| 116 |
uint64_t openssh_max_open_handles = 0; |
| 117 |
|
| 118 |
/* |
| 119 |
* Get the resource limit for max file descriptors that a process |
| 120 |
* can open. Since the client and the server run on the same machine |
| 121 |
* in case of tests, this limit should be same for both (except the |
| 122 |
* case when Valgrind runs the test) |
| 123 |
*/ |
| 124 |
rc = getrlimit(RLIMIT_NOFILE, &rlim); |
| 125 |
assert_int_equal(rc, 0); |
| 126 |
if (rlim.rlim_cur > 5) { |
| 127 |
/* |
| 128 |
* Leaving file handles for stdout, stdin, stderr, syslog and |
| 129 |
* a spare file handle, OpenSSH server allows the client to open |
| 130 |
* at max (rlim.rlim_cur - 5) handles. |
| 131 |
*/ |
| 132 |
openssh_max_open_handles = rlim.rlim_cur - 5; |
| 133 |
} |
| 134 |
|
| 135 |
assert_int_equal(li->max_open_handles, openssh_max_open_handles); |
| 136 |
} |
| 137 |
} else { |
| 138 |
/* Check for the default limits */ |
| 139 |
assert_int_equal(li->max_packet_length, 34000); |
| 140 |
assert_int_equal(li->max_read_length, 32768); |
| 141 |
assert_int_equal(li->max_write_length, 32768); |
| 142 |
assert_int_equal(li->max_open_handles, 0); |
| 143 |
} |
| 144 |
|
| 145 |
sftp_limits_free(li); |
| 146 |
} |
| 147 |
|
| 148 |
static void torture_sftp_limits_negative(void **state) |
| 149 |
{ |
| 150 |
sftp_limits_t li = NULL; |
| 151 |
|
| 152 |
(void)state; |
| 153 |
li = sftp_limits(NULL); |
| 154 |
assert_null(li); |
| 155 |
} |
| 156 |
|
| 157 |
int torture_run_tests(void) |
| 158 |
{ |
| 159 |
int rc; |
| 160 |
struct CMUnitTest tests[] = { |
| 161 |
cmocka_unit_test_setup_teardown(torture_sftp_limits, |
| 162 |
session_setup, |
| 163 |
session_teardown), |
| 164 |
|
| 165 |
cmocka_unit_test_setup_teardown(torture_sftp_limits_negative, |
| 166 |
session_setup, |
| 167 |
session_teardown) |
| 168 |
}; |
| 169 |
|
| 170 |
ssh_init(); |
| 171 |
|
| 172 |
torture_filter_tests(tests); |
| 173 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 174 |
ssh_finalize(); |
| 175 |
|
| 176 |
return rc; |
| 177 |
} |