| 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 <pwd.h> |
| 10 |
#include <errno.h> |
| 11 |
|
| 12 |
#define MAX_XFER_BUF_SIZE 16384 |
| 13 |
|
| 14 |
static int sshd_setup(void **state) |
| 15 |
{ |
| 16 |
torture_setup_sshd_server(state, false); |
| 17 |
|
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
static int sshd_teardown(void **state) { |
| 22 |
torture_teardown_sshd_server(state); |
| 23 |
|
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
static int session_setup(void **state) |
| 28 |
{ |
| 29 |
struct torture_state *s = *state; |
| 30 |
struct passwd *pwd; |
| 31 |
int rc; |
| 32 |
|
| 33 |
pwd = getpwnam("bob"); |
| 34 |
assert_non_null(pwd); |
| 35 |
|
| 36 |
rc = setuid(pwd->pw_uid); |
| 37 |
assert_return_code(rc, errno); |
| 38 |
|
| 39 |
s->ssh.session = torture_ssh_session(s, |
| 40 |
TORTURE_SSH_SERVER, |
| 41 |
NULL, |
| 42 |
TORTURE_SSH_USER_ALICE, |
| 43 |
NULL); |
| 44 |
assert_non_null(s->ssh.session); |
| 45 |
|
| 46 |
s->ssh.tsftp = torture_sftp_session(s->ssh.session); |
| 47 |
assert_non_null(s->ssh.tsftp); |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
static int session_teardown(void **state) |
| 53 |
{ |
| 54 |
struct torture_state *s = *state; |
| 55 |
|
| 56 |
torture_rmdirs(s->ssh.tsftp->testdir); |
| 57 |
torture_sftp_close(s->ssh.tsftp); |
| 58 |
ssh_disconnect(s->ssh.session); |
| 59 |
ssh_free(s->ssh.session); |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
static void torture_sftp_fsync(void **state) { |
| 65 |
struct torture_state *s = *state; |
| 66 |
struct torture_sftp *t = s->ssh.tsftp; |
| 67 |
|
| 68 |
char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX"; |
| 69 |
char buf[MAX_XFER_BUF_SIZE] = {0}; |
| 70 |
char buf_verify[MAX_XFER_BUF_SIZE] = {0}; |
| 71 |
size_t count; |
| 72 |
size_t bytesread; |
| 73 |
ssize_t byteswritten; |
| 74 |
int fd; |
| 75 |
sftp_file file; |
| 76 |
mode_t mask; |
| 77 |
int rc; |
| 78 |
FILE *fp; |
| 79 |
struct stat sb; |
| 80 |
|
| 81 |
mask = umask(S_IRWXO | S_IRWXG); |
| 82 |
fd = mkstemp(libssh_tmp_file); |
| 83 |
umask(mask); |
| 84 |
assert_return_code(fd, errno); |
| 85 |
close(fd); |
| 86 |
unlink(libssh_tmp_file); |
| 87 |
|
| 88 |
file = sftp_open(t->sftp, libssh_tmp_file, O_WRONLY | O_CREAT, 0600); |
| 89 |
assert_non_null(file); |
| 90 |
|
| 91 |
rc = lstat(libssh_tmp_file, &sb); |
| 92 |
assert_return_code(rc, errno); |
| 93 |
|
| 94 |
snprintf(buf, sizeof(buf), "libssh fsync test\n"); |
| 95 |
count = strlen(buf) + 1; |
| 96 |
|
| 97 |
byteswritten = sftp_write(file, buf, count); |
| 98 |
assert_int_equal(byteswritten, count); |
| 99 |
|
| 100 |
rc = sftp_fsync(file); |
| 101 |
assert_return_code(rc, errno); |
| 102 |
|
| 103 |
fp = fopen(libssh_tmp_file, "r"); |
| 104 |
assert_non_null(fp); |
| 105 |
|
| 106 |
rc = fstat(fileno(fp), &sb); |
| 107 |
assert_return_code(rc, errno); |
| 108 |
|
| 109 |
bytesread = fread(buf_verify, sizeof(buf_verify), 1, fp); |
| 110 |
if (bytesread == 0) { |
| 111 |
if (!feof(fp)) { |
| 112 |
assert_int_equal(bytesread, count); |
| 113 |
} |
| 114 |
} |
| 115 |
assert_string_equal(buf, buf_verify); |
| 116 |
|
| 117 |
sftp_close(file); |
| 118 |
fclose(fp); |
| 119 |
unlink(libssh_tmp_file); |
| 120 |
} |
| 121 |
|
| 122 |
int torture_run_tests(void) { |
| 123 |
int rc; |
| 124 |
struct CMUnitTest tests[] = { |
| 125 |
cmocka_unit_test_setup_teardown(torture_sftp_fsync, |
| 126 |
session_setup, |
| 127 |
session_teardown) |
| 128 |
}; |
| 129 |
|
| 130 |
ssh_init(); |
| 131 |
|
| 132 |
torture_filter_tests(tests); |
| 133 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 134 |
ssh_finalize(); |
| 135 |
|
| 136 |
return rc; |
| 137 |
} |