aboutsummaryrefslogtreecommitdiff
path: root/tests/client/torture_sftp_read.c
blob: dac5ef7bc85b90b153f85db5be88255603f348de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#define LIBSSH_STATIC

#include "torture.h"
#include "sftp.c"

#define MAX_XFER_BUF_SIZE 16384

static void setup(void **state) {
    ssh_session session;
    struct torture_sftp *t;
    const char *host;
    const char *user;
    const char *password;

    host = getenv("TORTURE_HOST");
    if (host == NULL) {
        host = "localhost";
    }

    user = getenv("TORTURE_USER");
    password = getenv("TORTURE_PASSWORD");

    session = torture_ssh_session(host, NULL, user, password);
    assert_false(session == NULL);
    t = torture_sftp_session(session);
    assert_false(t == NULL);

    *state = t;
}

static void teardown(void **state) {
    struct torture_sftp *t = (struct torture_sftp*) *state;

    assert_false(t == NULL);

    torture_rmdirs(t->testdir);
    torture_sftp_close(t);
}

static void torture_sftp_read_blocking(void **state) {
    struct torture_sftp *t = (struct torture_sftp*) *state;
    char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX";
    char buf[MAX_XFER_BUF_SIZE];
    ssize_t bytesread;
    ssize_t byteswritten;
    int fd;
    sftp_file file;


    file = sftp_open(t->sftp, "/usr/bin/ssh", O_RDONLY, 0);
    assert_non_null(file);

    fd = mkstemp(libssh_tmp_file);
    unlink(libssh_tmp_file);

    for (;;) {
        bytesread = sftp_read(file, buf, MAX_XFER_BUF_SIZE);
        if (bytesread == 0) {
                break; /* EOF */
        }
        assert_false(bytesread < 0);

        byteswritten = write(fd, buf, bytesread);
        assert_int_equal(byteswritten, bytesread);
    }

    close(fd);
    sftp_close(file);
}

int torture_run_tests(void) {
    int rc;
    const UnitTest tests[] = {
        unit_test_setup_teardown(torture_sftp_read_blocking, setup, teardown)
    };

    ssh_init();

    rc = run_tests(tests);
    ssh_finalize();

    return rc;
}