aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_misc.c
blob: 5996bd9753b6a3db33544df421336af14e528b19 (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
#include <sys/types.h>
#include <pwd.h>

#define LIBSSH_STATIC
#include <libssh/priv.h>

#include "torture.h"
#include "misc.c"
#define DIR "/usr/local/bin/truc/much/.."

ssh_session session;

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

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

START_TEST (torture_get_user_home_dir)
{
    struct passwd *pwd;
    char *user;

    pwd = getpwuid(getuid());

    user = ssh_get_user_home_dir();
    ck_assert_str_eq(user, pwd->pw_dir);

    SAFE_FREE(user);
}
END_TEST

START_TEST (torture_basename)
{
    char *path;
    path=ssh_basename(DIR "/test");
    ck_assert(path != NULL);
    ck_assert_str_eq(path, "test");
    SAFE_FREE(path);
    path=ssh_basename(DIR "/test/");
    ck_assert(path != NULL);
    ck_assert_str_eq(path, "test");
    SAFE_FREE(path);
}
END_TEST

START_TEST (torture_dirname)
{
    char *path;
    path=ssh_dirname(DIR "/test");
    ck_assert(path != NULL);
    ck_assert_str_eq(path, DIR );
    SAFE_FREE(path);
    path=ssh_dirname(DIR "/test/");
    ck_assert(path != NULL);
    ck_assert_str_eq(path, DIR);
    SAFE_FREE(path);
}
END_TEST

START_TEST (torture_ntohll)
{
    uint32_t sample = 1;
    unsigned char *ptr=(unsigned char *) &sample;
    uint64_t value = 0x0123456789abcdef;
    uint64_t check;
    if(ptr[0]==1){
      /* we're in little endian */
      check = 0xefcdab8967452301;
    } else {
      /* big endian */
      check = value;
    }
    value=ntohll(value);
    ck_assert(value == check);
}
END_TEST

START_TEST (torture_path_expand_tilde)
{
    char h[256];
    char *d;

    snprintf(h, 256 - 1, "%s/.ssh", getenv("HOME"));

    d = ssh_path_expand_tilde("~/.ssh");
    ck_assert_str_eq(d, h);
    free(d);

    d = ssh_path_expand_tilde("/guru/meditation");
    ck_assert_str_eq(d, "/guru/meditation");
    free(d);

    snprintf(h, 256 - 1, "~%s/.ssh", getenv("USER"));
    d = ssh_path_expand_tilde(h);

    snprintf(h, 256 - 1, "%s/.ssh", getenv("HOME"));
    ck_assert_str_eq(d, h);
    free(d);
}
END_TEST

START_TEST (torture_path_expand_escape)
{
    const char *s = "%d/%h/by/%r";
    char *e;

    ssh_options_set(session, SSH_OPTIONS_SSH_DIR, "guru");
    ssh_options_set(session, SSH_OPTIONS_HOST, "meditation");
    ssh_options_set(session, SSH_OPTIONS_USER, "root");

    e = ssh_path_expand_escape(session, s);
    ck_assert_str_eq(e, "guru/meditation/by/root");
    free(e);
}
END_TEST

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

  torture_create_case(s, "torture_get_user_home_dir", torture_get_user_home_dir);
  torture_create_case(s, "torture_basename", torture_basename);
  torture_create_case(s, "torture_dirname", torture_dirname);
  torture_create_case(s, "torture_ntohll", torture_ntohll);
  torture_create_case(s, "torture_path_expand_tilde", torture_path_expand_tilde);
  torture_create_case_fixture(s, "torture_path_expand_escape",
          torture_path_expand_escape, setup, teardown);

  return s;
}