| 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 |
#define DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN(TEST_NAME) \ |
| 15 |
{ \ |
| 16 |
#TEST_NAME, \ |
| 17 |
TEST_NAME, \ |
| 18 |
session_setup, \ |
| 19 |
session_teardown, \ |
| 20 |
NULL \ |
| 21 |
}, \ |
| 22 |
{ \ |
| 23 |
#TEST_NAME"_proxyjump", \ |
| 24 |
TEST_NAME, \ |
| 25 |
session_proxyjump_setup, \ |
| 26 |
session_teardown, \ |
| 27 |
NULL \ |
| 28 |
} |
| 29 |
|
| 30 |
static int sshd_setup(void **state) |
| 31 |
{ |
| 32 |
torture_setup_sshd_server(state, false); |
| 33 |
torture_setup_sshd_servers(state, false); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static int sshd_teardown(void **state) |
| 38 |
{ |
| 39 |
/* this will take care of the server1 teardown too */ |
| 40 |
torture_teardown_sshd_server(state); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int session_setup_helper(void **state, bool with_proxyjump) |
| 45 |
{ |
| 46 |
struct torture_state *s = *state; |
| 47 |
struct passwd *pwd = NULL; |
| 48 |
int rc; |
| 49 |
|
| 50 |
pwd = getpwnam("bob"); |
| 51 |
assert_non_null(pwd); |
| 52 |
|
| 53 |
rc = setuid(pwd->pw_uid); |
| 54 |
assert_return_code(rc, errno); |
| 55 |
|
| 56 |
if (with_proxyjump) { |
| 57 |
s->ssh.session = torture_ssh_session_proxyjump(); |
| 58 |
} else { |
| 59 |
s->ssh.session = torture_ssh_session(s, |
| 60 |
TORTURE_SSH_SERVER, |
| 61 |
NULL, |
| 62 |
TORTURE_SSH_USER_ALICE, |
| 63 |
NULL); |
| 64 |
} |
| 65 |
assert_non_null(s->ssh.session); |
| 66 |
|
| 67 |
s->ssh.tsftp = torture_sftp_session(s->ssh.session); |
| 68 |
assert_non_null(s->ssh.tsftp); |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static int session_setup(void **state) |
| 74 |
{ |
| 75 |
return session_setup_helper(state, false); |
| 76 |
} |
| 77 |
|
| 78 |
static int session_proxyjump_setup(void **state) |
| 79 |
{ |
| 80 |
return session_setup_helper(state, true); |
| 81 |
} |
| 82 |
|
| 83 |
static int session_teardown(void **state) |
| 84 |
{ |
| 85 |
struct torture_state *s = *state; |
| 86 |
|
| 87 |
torture_rmdirs(s->ssh.tsftp->testdir); |
| 88 |
torture_sftp_close(s->ssh.tsftp); |
| 89 |
ssh_disconnect(s->ssh.session); |
| 90 |
ssh_free(s->ssh.session); |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static void torture_sftp_aio_read_file(void **state) |
| 96 |
{ |
| 97 |
struct torture_state *s = *state; |
| 98 |
struct torture_sftp *t = s->ssh.tsftp; |
| 99 |
|
| 100 |
struct { |
| 101 |
char *buf; |
| 102 |
ssize_t bytes_read; |
| 103 |
} a = {0}, b = {0}; |
| 104 |
|
| 105 |
sftp_file file = NULL; |
| 106 |
sftp_attributes file_attr = NULL; |
| 107 |
int fd; |
| 108 |
|
| 109 |
size_t chunk_size; |
| 110 |
int in_flight_requests = 20; |
| 111 |
|
| 112 |
sftp_aio aio = NULL; |
| 113 |
struct ssh_list *aio_queue = NULL; |
| 114 |
sftp_limits_t li = NULL; |
| 115 |
|
| 116 |
size_t file_size; |
| 117 |
size_t total_bytes_requested; |
| 118 |
size_t to_read, total_bytes_read; |
| 119 |
ssize_t bytes_requested; |
| 120 |
|
| 121 |
int i, rc; |
| 122 |
|
| 123 |
/* Get the max limit for reading, use it as the chunk size */ |
| 124 |
li = sftp_limits(t->sftp); |
| 125 |
assert_non_null(li); |
| 126 |
chunk_size = li->max_read_length; |
| 127 |
|
| 128 |
a.buf = calloc(chunk_size, 1); |
| 129 |
assert_non_null(a.buf); |
| 130 |
|
| 131 |
b.buf = calloc(chunk_size, 1); |
| 132 |
assert_non_null(b.buf); |
| 133 |
|
| 134 |
aio_queue = ssh_list_new(); |
| 135 |
assert_non_null(aio_queue); |
| 136 |
|
| 137 |
file = sftp_open(t->sftp, SSH_EXECUTABLE, O_RDONLY, 0); |
| 138 |
assert_non_null(file); |
| 139 |
|
| 140 |
fd = open(SSH_EXECUTABLE, O_RDONLY, 0); |
| 141 |
assert_int_not_equal(fd, -1); |
| 142 |
|
| 143 |
/* Get the file size */ |
| 144 |
file_attr = sftp_stat(t->sftp, SSH_EXECUTABLE); |
| 145 |
assert_non_null(file_attr); |
| 146 |
file_size = file_attr->size; |
| 147 |
|
| 148 |
total_bytes_requested = 0; |
| 149 |
for (i = 0; |
| 150 |
i < in_flight_requests && total_bytes_requested < file_size; |
| 151 |
++i) { |
| 152 |
to_read = file_size - total_bytes_requested; |
| 153 |
if (to_read > chunk_size) { |
| 154 |
to_read = chunk_size; |
| 155 |
} |
| 156 |
|
| 157 |
bytes_requested = sftp_aio_begin_read(file, to_read, &aio); |
| 158 |
assert_int_equal(bytes_requested, to_read); |
| 159 |
total_bytes_requested += bytes_requested; |
| 160 |
|
| 161 |
/* enqueue */ |
| 162 |
rc = ssh_list_append(aio_queue, aio); |
| 163 |
assert_int_equal(rc, SSH_OK); |
| 164 |
} |
| 165 |
|
| 166 |
total_bytes_read = 0; |
| 167 |
while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) { |
| 168 |
a.bytes_read = sftp_aio_wait_read(&aio, a.buf, chunk_size); |
| 169 |
assert_int_not_equal(a.bytes_read, SSH_ERROR); |
| 170 |
|
| 171 |
total_bytes_read += (size_t)a.bytes_read; |
| 172 |
if (total_bytes_read != file_size) { |
| 173 |
assert_int_equal((size_t)a.bytes_read, chunk_size); |
| 174 |
/* |
| 175 |
* Failure of this assertion means that a short |
| 176 |
* read is encountered but we have not reached |
| 177 |
* the end of file yet. A short read before reaching |
| 178 |
* the end of file should not occur for our test where |
| 179 |
* the chunk size respects the max limit for reading. |
| 180 |
*/ |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Check whether the bytes read above are bytes |
| 185 |
* present in the file or some garbage was stored |
| 186 |
* in the buffer supplied to sftp_aio_wait_read(). |
| 187 |
*/ |
| 188 |
b.bytes_read = read(fd, b.buf, a.bytes_read); |
| 189 |
assert_int_equal(a.bytes_read, b.bytes_read); |
| 190 |
|
| 191 |
rc = memcmp(a.buf, b.buf, (size_t)a.bytes_read); |
| 192 |
assert_int_equal(rc, 0); |
| 193 |
|
| 194 |
/* Issue more read requests if needed */ |
| 195 |
if (total_bytes_requested == file_size) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
/* else issue more requests */ |
| 200 |
to_read = file_size - total_bytes_requested; |
| 201 |
if (to_read > chunk_size) { |
| 202 |
to_read = chunk_size; |
| 203 |
} |
| 204 |
|
| 205 |
bytes_requested = sftp_aio_begin_read(file, to_read, &aio); |
| 206 |
assert_int_equal(bytes_requested, to_read); |
| 207 |
total_bytes_requested += bytes_requested; |
| 208 |
|
| 209 |
/* enqueue */ |
| 210 |
rc = ssh_list_append(aio_queue, aio); |
| 211 |
assert_int_equal(rc, SSH_OK); |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
* Check whether sftp server responds with an |
| 216 |
* eof for more requests. |
| 217 |
*/ |
| 218 |
bytes_requested = sftp_aio_begin_read(file, chunk_size, &aio); |
| 219 |
assert_int_equal(bytes_requested, chunk_size); |
| 220 |
|
| 221 |
a.bytes_read = sftp_aio_wait_read(&aio, a.buf, chunk_size); |
| 222 |
assert_int_equal(a.bytes_read, 0); |
| 223 |
|
| 224 |
/* Clean up */ |
| 225 |
sftp_attributes_free(file_attr); |
| 226 |
close(fd); |
| 227 |
sftp_close(file); |
| 228 |
ssh_list_free(aio_queue); |
| 229 |
free(b.buf); |
| 230 |
free(a.buf); |
| 231 |
sftp_limits_free(li); |
| 232 |
} |
| 233 |
|
| 234 |
static void torture_sftp_aio_read_more_than_cap(void **state) |
| 235 |
{ |
| 236 |
struct torture_state *s = *state; |
| 237 |
struct torture_sftp *t = s->ssh.tsftp; |
| 238 |
|
| 239 |
sftp_limits_t li = NULL; |
| 240 |
sftp_file file = NULL; |
| 241 |
sftp_aio aio = NULL; |
| 242 |
|
| 243 |
char *buf = NULL; |
| 244 |
ssize_t bytes; |
| 245 |
|
| 246 |
/* Get the max limit for reading */ |
| 247 |
li = sftp_limits(t->sftp); |
| 248 |
assert_non_null(li); |
| 249 |
|
| 250 |
file = sftp_open(t->sftp, SSH_EXECUTABLE, O_RDONLY, 0); |
| 251 |
assert_non_null(file); |
| 252 |
|
| 253 |
/* Try reading more than the max limit */ |
| 254 |
bytes = sftp_aio_begin_read(file, |
| 255 |
li->max_read_length * 2, |
| 256 |
&aio); |
| 257 |
assert_int_equal(bytes, li->max_read_length); |
| 258 |
|
| 259 |
buf = calloc(li->max_read_length, 1); |
| 260 |
assert_non_null(buf); |
| 261 |
|
| 262 |
bytes = sftp_aio_wait_read(&aio, buf, li->max_read_length); |
| 263 |
assert_int_not_equal(bytes, SSH_ERROR); |
| 264 |
|
| 265 |
free(buf); |
| 266 |
sftp_close(file); |
| 267 |
sftp_limits_free(li); |
| 268 |
} |
| 269 |
|
| 270 |
static void torture_sftp_aio_write_file(void **state) |
| 271 |
{ |
| 272 |
struct torture_state *s = *state; |
| 273 |
struct torture_sftp *t = s->ssh.tsftp; |
| 274 |
|
| 275 |
char file_path[128] = {0}; |
| 276 |
sftp_file file = NULL; |
| 277 |
int fd; |
| 278 |
|
| 279 |
struct { |
| 280 |
char *buf; |
| 281 |
ssize_t bytes; |
| 282 |
} wr = {0}, rd = {0}; |
| 283 |
|
| 284 |
size_t chunk_size; |
| 285 |
ssize_t bytes_requested; |
| 286 |
int in_flight_requests = 2; |
| 287 |
|
| 288 |
sftp_limits_t li = NULL; |
| 289 |
sftp_aio *aio_queue = NULL; |
| 290 |
int rc, i; |
| 291 |
|
| 292 |
/* Get the max limit for writing, use it as the chunk size */ |
| 293 |
li = sftp_limits(t->sftp); |
| 294 |
assert_non_null(li); |
| 295 |
chunk_size = li->max_write_length; |
| 296 |
|
| 297 |
rd.buf = calloc(chunk_size, 1); |
| 298 |
assert_non_null(rd.buf); |
| 299 |
|
| 300 |
wr.buf = calloc(chunk_size, 1); |
| 301 |
assert_non_null(wr.buf); |
| 302 |
|
| 303 |
aio_queue = malloc(sizeof(sftp_aio) * in_flight_requests); |
| 304 |
assert_non_null(aio_queue); |
| 305 |
|
| 306 |
snprintf(file_path, sizeof(file_path), |
| 307 |
"%s/libssh_sftp_aio_write_test", t->testdir); |
| 308 |
file = sftp_open(t->sftp, file_path, O_CREAT | O_WRONLY, 0777); |
| 309 |
assert_non_null(file); |
| 310 |
|
| 311 |
fd = open(file_path, O_RDONLY, 0); |
| 312 |
assert_int_not_equal(fd, -1); |
| 313 |
|
| 314 |
for (i = 0; i < in_flight_requests; ++i) { |
| 315 |
bytes_requested = sftp_aio_begin_write(file, |
| 316 |
wr.buf, |
| 317 |
chunk_size, |
| 318 |
&aio_queue[i]); |
| 319 |
assert_int_equal(bytes_requested, chunk_size); |
| 320 |
} |
| 321 |
|
| 322 |
for (i = 0; i < in_flight_requests; ++i) { |
| 323 |
wr.bytes = sftp_aio_wait_write(&aio_queue[i]); |
| 324 |
assert_int_equal(wr.bytes, chunk_size); |
| 325 |
|
| 326 |
/* |
| 327 |
* Check whether the bytes written to the file |
| 328 |
* by SFTP AIO write api were the bytes present |
| 329 |
* in the buffer to write or some garbage was |
| 330 |
* written to the file. |
| 331 |
*/ |
| 332 |
rd.bytes = read(fd, rd.buf, wr.bytes); |
| 333 |
assert_int_equal(rd.bytes, wr.bytes); |
| 334 |
|
| 335 |
rc = memcmp(rd.buf, wr.buf, wr.bytes); |
| 336 |
assert_int_equal(rc, 0); |
| 337 |
} |
| 338 |
|
| 339 |
/* Clean up */ |
| 340 |
close(fd); |
| 341 |
sftp_close(file); |
| 342 |
free(aio_queue); |
| 343 |
|
| 344 |
rc = unlink(file_path); |
| 345 |
assert_int_equal(rc, 0); |
| 346 |
|
| 347 |
free(wr.buf); |
| 348 |
free(rd.buf); |
| 349 |
sftp_limits_free(li); |
| 350 |
} |
| 351 |
|
| 352 |
static void torture_sftp_aio_write_more_than_cap(void **state) |
| 353 |
{ |
| 354 |
struct torture_state *s = *state; |
| 355 |
struct torture_sftp *t = s->ssh.tsftp; |
| 356 |
|
| 357 |
sftp_limits_t li = NULL; |
| 358 |
char *buf = NULL; |
| 359 |
size_t buf_size; |
| 360 |
|
| 361 |
char file_path[128] = {0}; |
| 362 |
sftp_file file = NULL; |
| 363 |
|
| 364 |
sftp_aio aio = NULL; |
| 365 |
ssize_t bytes; |
| 366 |
int rc; |
| 367 |
|
| 368 |
li = sftp_limits(t->sftp); |
| 369 |
assert_non_null(li); |
| 370 |
|
| 371 |
buf_size = li->max_write_length * 2; |
| 372 |
buf = calloc(buf_size, 1); |
| 373 |
assert_non_null(buf); |
| 374 |
|
| 375 |
snprintf(file_path, sizeof(file_path), |
| 376 |
"%s/libssh_sftp_aio_write_test_cap", t->testdir); |
| 377 |
file = sftp_open(t->sftp, file_path, O_CREAT | O_WRONLY, 0777); |
| 378 |
assert_non_null(file); |
| 379 |
|
| 380 |
/* Try writing more than the max limit for writing */ |
| 381 |
bytes = sftp_aio_begin_write(file, buf, buf_size, &aio); |
| 382 |
assert_int_equal(bytes, li->max_write_length); |
| 383 |
|
| 384 |
bytes = sftp_aio_wait_write(&aio); |
| 385 |
assert_int_equal(bytes, li->max_write_length); |
| 386 |
|
| 387 |
/* Clean up */ |
| 388 |
sftp_close(file); |
| 389 |
|
| 390 |
rc = unlink(file_path); |
| 391 |
assert_int_equal(rc, 0); |
| 392 |
|
| 393 |
free(buf); |
| 394 |
sftp_limits_free(li); |
| 395 |
} |
| 396 |
|
| 397 |
static void torture_sftp_aio_read_negative(void **state) |
| 398 |
{ |
| 399 |
struct torture_state *s = *state; |
| 400 |
struct torture_sftp *t = s->ssh.tsftp; |
| 401 |
|
| 402 |
char *buf = NULL; |
| 403 |
sftp_file file = NULL; |
| 404 |
sftp_aio aio = NULL; |
| 405 |
sftp_limits_t li = NULL; |
| 406 |
|
| 407 |
size_t chunk_size; |
| 408 |
ssize_t bytes; |
| 409 |
int rc; |
| 410 |
|
| 411 |
li = sftp_limits(t->sftp); |
| 412 |
assert_non_null(li); |
| 413 |
chunk_size = li->max_read_length; |
| 414 |
|
| 415 |
buf = calloc(chunk_size, 1); |
| 416 |
assert_non_null(buf); |
| 417 |
|
| 418 |
/* Open a file for reading */ |
| 419 |
file = sftp_open(t->sftp, SSH_EXECUTABLE, O_RDONLY, 0); |
| 420 |
assert_non_null(file); |
| 421 |
|
| 422 |
/* Passing NULL as the sftp file handle */ |
| 423 |
bytes = sftp_aio_begin_read(NULL, chunk_size, &aio); |
| 424 |
assert_int_equal(bytes, SSH_ERROR); |
| 425 |
|
| 426 |
/* Passing 0 as the number of bytes to read */ |
| 427 |
bytes = sftp_aio_begin_read(file, 0, &aio); |
| 428 |
assert_int_equal(bytes, SSH_ERROR); |
| 429 |
|
| 430 |
/* |
| 431 |
* Passing NULL instead of a pointer to a location to |
| 432 |
* store an aio handle. |
| 433 |
*/ |
| 434 |
bytes = sftp_aio_begin_read(file, chunk_size, NULL); |
| 435 |
assert_int_equal(bytes, SSH_ERROR); |
| 436 |
|
| 437 |
/* Passing NULL instead of a pointer to an aio handle */ |
| 438 |
bytes = sftp_aio_wait_read(NULL, buf, sizeof(buf)); |
| 439 |
assert_int_equal(bytes, SSH_ERROR); |
| 440 |
|
| 441 |
/* Passing NULL as the buffer's address */ |
| 442 |
bytes = sftp_aio_begin_read(file, chunk_size, &aio); |
| 443 |
assert_int_equal(bytes, chunk_size); |
| 444 |
|
| 445 |
bytes = sftp_aio_wait_read(&aio, NULL, sizeof(buf)); |
| 446 |
assert_int_equal(bytes, SSH_ERROR); |
| 447 |
|
| 448 |
/* Passing 0 as the buffer size */ |
| 449 |
bytes = sftp_aio_begin_read(file, chunk_size, &aio); |
| 450 |
assert_int_equal(bytes, chunk_size); |
| 451 |
|
| 452 |
bytes = sftp_aio_wait_read(&aio, buf, 0); |
| 453 |
assert_int_equal(bytes, SSH_ERROR); |
| 454 |
|
| 455 |
/* |
| 456 |
* Test for the scenario when the number |
| 457 |
* of bytes read exceed the buffer size. |
| 458 |
*/ |
| 459 |
rc = sftp_seek(file, 0); /* Seek to the start of file */ |
| 460 |
assert_int_equal(rc, 0); |
| 461 |
|
| 462 |
bytes = sftp_aio_begin_read(file, 2, &aio); |
| 463 |
assert_int_equal(bytes, 2); |
| 464 |
|
| 465 |
bytes = sftp_aio_wait_read(&aio, buf, 1); |
| 466 |
assert_int_equal(bytes, SSH_ERROR); |
| 467 |
|
| 468 |
sftp_close(file); |
| 469 |
free(buf); |
| 470 |
sftp_limits_free(li); |
| 471 |
} |
| 472 |
|
| 473 |
static void torture_sftp_aio_write_negative(void **state) |
| 474 |
{ |
| 475 |
struct torture_state *s = *state; |
| 476 |
struct torture_sftp *t = s->ssh.tsftp; |
| 477 |
|
| 478 |
char *buf = NULL; |
| 479 |
|
| 480 |
char file_path[128] = {0}; |
| 481 |
sftp_file file = NULL; |
| 482 |
sftp_aio aio = NULL; |
| 483 |
sftp_limits_t li = NULL; |
| 484 |
|
| 485 |
size_t chunk_size; |
| 486 |
ssize_t bytes; |
| 487 |
int rc; |
| 488 |
|
| 489 |
li = sftp_limits(t->sftp); |
| 490 |
assert_non_null(li); |
| 491 |
chunk_size = li->max_write_length; |
| 492 |
|
| 493 |
buf = calloc(chunk_size, 1); |
| 494 |
assert_non_null(buf); |
| 495 |
|
| 496 |
/* Open a file for writing */ |
| 497 |
snprintf(file_path, sizeof(file_path), |
| 498 |
"%s/libssh_sftp_aio_write_test_negative", t->testdir); |
| 499 |
file = sftp_open(t->sftp, file_path, O_CREAT | O_WRONLY, 0777); |
| 500 |
assert_non_null(file); |
| 501 |
|
| 502 |
/* Passing NULL as the sftp file handle */ |
| 503 |
bytes = sftp_aio_begin_write(NULL, buf, chunk_size, &aio); |
| 504 |
assert_int_equal(bytes, SSH_ERROR); |
| 505 |
|
| 506 |
/* Passing NULL as the buffer's address */ |
| 507 |
bytes = sftp_aio_begin_write(file, NULL, chunk_size, &aio); |
| 508 |
assert_int_equal(bytes, SSH_ERROR); |
| 509 |
|
| 510 |
/* Passing 0 as the size of buffer */ |
| 511 |
bytes = sftp_aio_begin_write(file, buf, 0, &aio); |
| 512 |
assert_int_equal(bytes, SSH_ERROR); |
| 513 |
|
| 514 |
/* Passing NULL instead of a pointer to a location to store an aio handle */ |
| 515 |
bytes = sftp_aio_begin_write(file, buf, chunk_size, NULL); |
| 516 |
assert_int_equal(bytes, SSH_ERROR); |
| 517 |
|
| 518 |
/* Passing NULL instead of a pointer to an aio handle */ |
| 519 |
bytes = sftp_aio_wait_write(NULL); |
| 520 |
assert_int_equal(bytes, SSH_ERROR); |
| 521 |
|
| 522 |
sftp_close(file); |
| 523 |
rc = unlink(file_path); |
| 524 |
assert_int_equal(rc, 0); |
| 525 |
|
| 526 |
free(buf); |
| 527 |
sftp_limits_free(li); |
| 528 |
} |
| 529 |
|
| 530 |
/* |
| 531 |
* Test that waiting for read responses in an order different from the |
| 532 |
* sending order of corresponding read requests works properly. |
| 533 |
* |
| 534 |
* (For example, if Requests Rq1 and Rq2 have responses Rs1 and Rs2 |
| 535 |
* respectively, and Rq1 is sent first followed by Rq2. Then waiting for |
| 536 |
* response Rs2 first and then Rs1 should work properly) |
| 537 |
*/ |
| 538 |
static void torture_sftp_aio_read_unordered_wait(void **state) |
| 539 |
{ |
| 540 |
struct torture_state *s = *state; |
| 541 |
struct torture_sftp *t = s->ssh.tsftp; |
| 542 |
|
| 543 |
sftp_file file = NULL; |
| 544 |
sftp_aio aio_1 = NULL, aio_2 = NULL; |
| 545 |
ssize_t bytes_requested, bytes_read; |
| 546 |
|
| 547 |
struct { |
| 548 |
/* buffer to store read data */ |
| 549 |
char *buf; |
| 550 |
|
| 551 |
/* buffer to store data expected to be read */ |
| 552 |
char *expected; |
| 553 |
|
| 554 |
/* |
| 555 |
* length of the data to read. Keep this length small enough so that we |
| 556 |
* don't get short reads due to the sftp limits. |
| 557 |
*/ |
| 558 |
size_t len; |
| 559 |
} r1 = {0}, r2 = {0}; |
| 560 |
|
| 561 |
int fd, rc; |
| 562 |
|
| 563 |
/* Initialize r1 */ |
| 564 |
r1.len = 10; |
| 565 |
|
| 566 |
r1.buf = calloc(r1.len, 1); |
| 567 |
assert_non_null(r1.buf); |
| 568 |
|
| 569 |
r1.expected = calloc(r1.len, 1); |
| 570 |
assert_non_null(r1.expected); |
| 571 |
|
| 572 |
/* Initialize r2 */ |
| 573 |
r2.len = 20; |
| 574 |
|
| 575 |
r2.buf = calloc(r2.len, 1); |
| 576 |
assert_non_null(r2.buf); |
| 577 |
|
| 578 |
r2.expected = calloc(r2.len, 1); |
| 579 |
assert_non_null(r2.expected); |
| 580 |
|
| 581 |
/* Get data that is expected to be read from the file */ |
| 582 |
fd = open(SSH_EXECUTABLE, O_RDONLY, 0); |
| 583 |
assert_int_not_equal(fd, -1); |
| 584 |
|
| 585 |
bytes_read = read(fd, r1.expected, r1.len); |
| 586 |
assert_int_equal(bytes_read, r1.len); |
| 587 |
|
| 588 |
bytes_read = read(fd, r2.expected, r2.len); |
| 589 |
assert_int_equal(bytes_read, r2.len); |
| 590 |
|
| 591 |
/* Open an sftp file for reading */ |
| 592 |
file = sftp_open(t->sftp, SSH_EXECUTABLE, O_RDONLY, 0); |
| 593 |
assert_non_null(file); |
| 594 |
|
| 595 |
/* |
| 596 |
* Issue 2 consecutive read requests (send the second request immediately |
| 597 |
* after sending the first without waiting for the first's response) |
| 598 |
*/ |
| 599 |
bytes_requested = sftp_aio_begin_read(file, r1.len, &aio_1); |
| 600 |
assert_int_equal(bytes_requested, r1.len); |
| 601 |
|
| 602 |
bytes_requested = sftp_aio_begin_read(file, r2.len, &aio_2); |
| 603 |
assert_int_equal(bytes_requested, r2.len); |
| 604 |
|
| 605 |
/* |
| 606 |
* Wait for the responses in opposite order (Instead of waiting for response |
| 607 |
* 1 first and then response 2, wait for response 2 first and then wait for |
| 608 |
* response 1) |
| 609 |
*/ |
| 610 |
bytes_read = sftp_aio_wait_read(&aio_2, r2.buf, r2.len); |
| 611 |
assert_int_equal(bytes_read, r2.len); |
| 612 |
assert_memory_equal(r2.buf, r2.expected, r2.len); |
| 613 |
|
| 614 |
bytes_read = sftp_aio_wait_read(&aio_1, r1.buf, r1.len); |
| 615 |
assert_int_equal(bytes_read, r1.len); |
| 616 |
assert_memory_equal(r1.buf, r1.expected, r1.len); |
| 617 |
|
| 618 |
/* Clean up */ |
| 619 |
sftp_close(file); |
| 620 |
|
| 621 |
rc = close(fd); |
| 622 |
assert_int_equal(rc, 0); |
| 623 |
|
| 624 |
free(r2.expected); |
| 625 |
free(r2.buf); |
| 626 |
|
| 627 |
free(r1.expected); |
| 628 |
free(r1.buf); |
| 629 |
} |
| 630 |
|
| 631 |
/* |
| 632 |
* Test that waiting for write responses in an order different from the |
| 633 |
* sending order of corresponding write requests works properly. |
| 634 |
* |
| 635 |
* (For example, if Requests Rq1 and Rq2 have responses Rs1 and Rs2 |
| 636 |
* respectively, and Rq1 is sent first followed by Rq2. Then waiting for |
| 637 |
* response Rs2 first and then Rs1 should work properly) |
| 638 |
*/ |
| 639 |
static void torture_sftp_aio_write_unordered_wait(void **state) |
| 640 |
{ |
| 641 |
struct torture_state *s = *state; |
| 642 |
struct torture_sftp *t = s->ssh.tsftp; |
| 643 |
|
| 644 |
char file_path[128] = {0}; |
| 645 |
sftp_file file = NULL; |
| 646 |
sftp_aio aio_1 = NULL, aio_2 = NULL; |
| 647 |
ssize_t bytes_requested, bytes_written, bytes_read; |
| 648 |
size_t i; |
| 649 |
int rc, fd; |
| 650 |
|
| 651 |
struct { |
| 652 |
/* |
| 653 |
* length of the data to write. Keep this length small enough so that we |
| 654 |
* don't get short writes due to the sftp limits |
| 655 |
*/ |
| 656 |
size_t len; |
| 657 |
|
| 658 |
/* data to write */ |
| 659 |
char *data; |
| 660 |
|
| 661 |
/* buffer used to validate the written data */ |
| 662 |
char *buf; |
| 663 |
} r1 = {0}, r2 = {0}; |
| 664 |
|
| 665 |
/* Initialize r1 */ |
| 666 |
r1.len = 10; |
| 667 |
|
| 668 |
r1.data = calloc(r1.len, 1); |
| 669 |
assert_non_null(r1.data); |
| 670 |
|
| 671 |
for (i = 0; i < r1.len; ++i) { |
| 672 |
r1.data[i] = (char)rand(); |
| 673 |
} |
| 674 |
|
| 675 |
r1.buf = calloc(r1.len, 1); |
| 676 |
assert_non_null(r1.buf); |
| 677 |
|
| 678 |
/* Initialize r2 */ |
| 679 |
r2.len = 20; |
| 680 |
|
| 681 |
r2.data = calloc(r2.len, 1); |
| 682 |
assert_non_null(r2.data); |
| 683 |
|
| 684 |
for (i = 0; i < r2.len; ++i) { |
| 685 |
r2.data[i] = (char)rand(); |
| 686 |
} |
| 687 |
|
| 688 |
r2.buf = calloc(r2.len, 1); |
| 689 |
assert_non_null(r2.buf); |
| 690 |
|
| 691 |
/* Open an sftp file for writing */ |
| 692 |
snprintf(file_path, |
| 693 |
sizeof(file_path), |
| 694 |
"%s/libssh_sftp_aio_write_unordered_wait", |
| 695 |
t->testdir); |
| 696 |
file = sftp_open(t->sftp, file_path, O_CREAT | O_WRONLY, 0777); |
| 697 |
assert_non_null(file); |
| 698 |
|
| 699 |
/* |
| 700 |
* Issue two consecutive write requests (send the second request immediately |
| 701 |
* after sending the first without waiting for the first's response) |
| 702 |
*/ |
| 703 |
bytes_requested = sftp_aio_begin_write(file, r1.data, r1.len, &aio_1); |
| 704 |
assert_int_equal(bytes_requested, r1.len); |
| 705 |
|
| 706 |
bytes_requested = sftp_aio_begin_write(file, r2.data, r2.len, &aio_2); |
| 707 |
assert_int_equal(bytes_requested, r2.len); |
| 708 |
|
| 709 |
/* |
| 710 |
* Wait for the responses in opposite order (Instead of waiting for response |
| 711 |
* 1 first and then response 2, wait for response 2 first and then wait for |
| 712 |
* response 1) |
| 713 |
*/ |
| 714 |
bytes_written = sftp_aio_wait_write(&aio_2); |
| 715 |
assert_int_equal(bytes_written, r2.len); |
| 716 |
|
| 717 |
bytes_written = sftp_aio_wait_write(&aio_1); |
| 718 |
assert_int_equal(bytes_written, r1.len); |
| 719 |
|
| 720 |
/* |
| 721 |
* Validate that the data has been written to the file correctly by reading |
| 722 |
* from the file. |
| 723 |
*/ |
| 724 |
fd = open(file_path, O_RDONLY, 0); |
| 725 |
assert_int_not_equal(fd, -1); |
| 726 |
|
| 727 |
/* Validate that write request 1's data has been written to file */ |
| 728 |
bytes_read = read(fd, r1.buf, r1.len); |
| 729 |
assert_int_equal(bytes_read, r1.len); |
| 730 |
assert_memory_equal(r1.data, r1.buf, r1.len); |
| 731 |
|
| 732 |
/* Validate that write request 2's data has been written to file */ |
| 733 |
bytes_read = read(fd, r2.buf, r2.len); |
| 734 |
assert_int_equal(bytes_read, r2.len); |
| 735 |
assert_memory_equal(r2.data, r2.buf, r2.len); |
| 736 |
|
| 737 |
/* Clean up */ |
| 738 |
rc = close(fd); |
| 739 |
assert_int_equal(rc, 0); |
| 740 |
|
| 741 |
sftp_close(file); |
| 742 |
|
| 743 |
rc = unlink(file_path); |
| 744 |
assert_int_equal(rc, 0); |
| 745 |
|
| 746 |
free(r2.buf); |
| 747 |
free(r2.data); |
| 748 |
|
| 749 |
free(r1.buf); |
| 750 |
free(r1.data); |
| 751 |
} |
| 752 |
|
| 753 |
int torture_run_tests(void) |
| 754 |
{ |
| 755 |
int rc; |
| 756 |
struct CMUnitTest tests[] = { |
| 757 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN(torture_sftp_aio_read_file), |
| 758 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN( |
| 759 |
torture_sftp_aio_read_more_than_cap), |
| 760 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN(torture_sftp_aio_write_file), |
| 761 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN( |
| 762 |
torture_sftp_aio_write_more_than_cap), |
| 763 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN(torture_sftp_aio_read_negative), |
| 764 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN(torture_sftp_aio_write_negative), |
| 765 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN( |
| 766 |
torture_sftp_aio_read_unordered_wait), |
| 767 |
DIRECT_AND_PROXYJUMP_SETUP_TEARDOWN( |
| 768 |
torture_sftp_aio_write_unordered_wait), |
| 769 |
}; |
| 770 |
|
| 771 |
ssh_init(); |
| 772 |
|
| 773 |
torture_filter_tests(tests); |
| 774 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 775 |
ssh_finalize(); |
| 776 |
|
| 777 |
return rc; |
| 778 |
} |