aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_options.c
blob: bba5500ac9758fa7b7a9bbd068a06db91712bd0e (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define LIBSSH_STATIC

#include "torture.h"
#include "options.c"

ssh_session session;
#ifndef _WIN32
struct passwd pwd;
#endif /* _WIN32 */

static void setup(void) {
    session = ssh_new();
}

static void setup_passwd(void) {
    int rc;
#ifndef _WIN32
#ifndef NSS_BUFLEN_PASSWD
#define NSS_BUFLEN_PASSWD 4096
#endif /* NSS_BUFLEN_PASSWD */
    struct passwd *pwdbuf;
    char buf[NSS_BUFLEN_PASSWD];

    /* get local username */
    rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
    ck_assert(rc == 0);
#endif /* _WIN32 */
    session = ssh_new();
}

static void teardown(void) {
    ZERO_STRUCT(pwd);
    ssh_free(session);
}

START_TEST (torture_options_set_host)
{
    int rc;

    rc = ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
    ck_assert(rc == 0);
    ck_assert_str_eq(session->host, "localhost");

    rc = ssh_options_set(session, SSH_OPTIONS_HOST, "guru@meditation");
    ck_assert(rc == 0);
    ck_assert_str_eq(session->host, "meditation");
    ck_assert_str_eq(session->username, "guru");
}
END_TEST

START_TEST (torture_options_set_port)
{
    int rc;
    unsigned int port = 42;

    rc = ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    ck_assert(rc == 0);
    ck_assert(session->port == port);

    rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "23");
    ck_assert(rc == 0);
    ck_assert(session->port == 23);

    rc = ssh_options_set(session, SSH_OPTIONS_PORT_STR, "five");
    ck_assert(rc == 0);
    ck_assert(session->port == 0);

    rc = ssh_options_set(session, SSH_OPTIONS_PORT, NULL);
    ck_assert(rc == 0);
    ck_assert(session->port == 22);
}
END_TEST

START_TEST (torture_options_set_fd)
{
    socket_t fd = 42;
    int rc;

    rc = ssh_options_set(session, SSH_OPTIONS_FD, &fd);
    ck_assert(rc == 0);
    ck_assert(session->fd == fd);

    rc = ssh_options_set(session, SSH_OPTIONS_FD, NULL);
    ck_assert(rc == 0);
    ck_assert(session->fd == SSH_INVALID_SOCKET);
}
END_TEST

START_TEST (torture_options_set_user)
{
    int rc;

    rc = ssh_options_set(session, SSH_OPTIONS_USER, "guru");
    ck_assert(rc == 0);
    ck_assert_str_eq(session->username, "guru");


    rc = ssh_options_set(session, SSH_OPTIONS_USER, NULL);
    ck_assert(rc == 0);

#ifndef _WIN32
    ck_assert_str_eq(session->username, pwd.pw_name);
#endif
}
END_TEST

/* TODO */
#if 0
START_TEST (torture_options_set_sshdir)
{
}
END_TEST
#endif

START_TEST (torture_options_set_identity)
{
    int rc;

    rc = ssh_options_set(session, SSH_OPTIONS_ADD_IDENTITY, "identity1");
    ck_assert(rc == 0);
    ck_assert_str_eq(session->identity->root->data, "identity1");

    rc = ssh_options_set(session, SSH_OPTIONS_IDENTITY, "identity2");
    ck_assert(rc == 0);
    ck_assert_str_eq(session->identity->root->data, "identity2");
    ck_assert_str_eq(session->identity->root->next->data, "identity1");
}
END_TEST

Suite *torture_make_suite(void) {
  Suite *s = suite_create("libssh_options");

  torture_create_case_fixture(s, "torture_options_set_host",
          torture_options_set_host, setup, teardown);
  torture_create_case_fixture(s, "torture_options_set_port",
          torture_options_set_port, setup, teardown);
  torture_create_case_fixture(s, "torture_options_set_fd",
          torture_options_set_fd, setup, teardown);
  torture_create_case_fixture(s, "torture_options_set_user",
          torture_options_set_user, setup_passwd, teardown);
  torture_create_case_fixture(s, "torture_options_set_identity",
          torture_options_set_identity, setup, teardown);

  return s;
}