summaryrefslogtreecommitdiff
path: root/src/t/test_mod_userdir.c
blob: 6cadc81582402e0c4a905fdf28aac6694339e3f5 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "first.h"

#undef NDEBUG
#include <assert.h>
#include <stdlib.h>

#include "mod_userdir.c"
#include "fdlog.h"

static void test_mod_userdir_reset(request_st * const r)
{
    r->http_status = 0;
    buffer_clear(&r->physical.basedir);
    buffer_clear(&r->physical.path);
}

static void
test_mod_userdir_docroot_handler(request_st * const r, plugin_data * const p)
{
    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN(""));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    p->defaults.active = 1;

    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/other"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~no-trailing-slash"));
    test_mod_userdir_reset(r);
    assert(HANDLER_FINISHED == mod_userdir_docroot_handler(r, p));
    assert(301 == r->http_status);
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_eq_slen(&r->physical.basedir,
                          CONST_STR_LEN("/web/u/jan/public_html")));
    assert(buffer_eq_slen(&r->physical.path,
                          CONST_STR_LEN("/web/u/jan/public_html/")));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/more"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/more"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_eq_slen(&r->physical.basedir,
                          CONST_STR_LEN("/web/u/jan/public_html")));
    assert(buffer_eq_slen(&r->physical.path,
                          CONST_STR_LEN("/web/u/jan/public_html/more")));

    p->defaults.letterhomes = 1;

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~.jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_eq_slen(&r->physical.basedir,
                          CONST_STR_LEN("/web/u/j/jan/public_html")));
    assert(buffer_eq_slen(&r->physical.path,
                          CONST_STR_LEN("/web/u/j/jan/public_html/")));

    p->defaults.letterhomes = 0;

    array *include_user = array_init(2);
    array *exclude_user = array_init(2);

    array_insert_value(include_user, CONST_STR_LEN("notjan"));

    p->defaults.include_user = include_user;

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    array_insert_value(include_user, CONST_STR_LEN("jan"));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_eq_slen(&r->physical.basedir,
                          CONST_STR_LEN("/web/u/jan/public_html")));
    assert(buffer_eq_slen(&r->physical.path,
                          CONST_STR_LEN("/web/u/jan/public_html/")));

    p->defaults.exclude_user = exclude_user;

    array_insert_value(exclude_user, CONST_STR_LEN("notjan"));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_eq_slen(&r->physical.basedir,
                          CONST_STR_LEN("/web/u/jan/public_html")));
    assert(buffer_eq_slen(&r->physical.path,
                          CONST_STR_LEN("/web/u/jan/public_html/")));

    array_insert_value(exclude_user, CONST_STR_LEN("jan"));

    buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/~jan/"));
    buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/~jan/"));
    test_mod_userdir_reset(r);
    assert(HANDLER_GO_ON == mod_userdir_docroot_handler(r, p));
    assert(buffer_is_empty(&r->physical.basedir));
    assert(buffer_is_empty(&r->physical.path));

    p->defaults.include_user = NULL;
    p->defaults.exclude_user = NULL;
    array_free(include_user);
    array_free(exclude_user);
}

#include "base.h"

void test_mod_userdir (void);
void test_mod_userdir (void)
{
    plugin_data * const p = mod_userdir_init();
    assert(NULL != p);

    buffer *basepath = buffer_init();
    buffer *path     = buffer_init();
    buffer_copy_string(basepath, "/web/u/"); /*(skip getpwnam())*/
    buffer_copy_string(path, "public_html");
    p->defaults.basepath = basepath;
    p->defaults.path = path;

    request_st r;
    connection con;
    server srv;

    memset(&r, 0, sizeof(request_st));
    memset(&con, 0, sizeof(connection));
    memset(&srv, 0, sizeof(server));
    r.tmp_buf                = buffer_init();
    r.conf.errh              = fdlog_init(NULL, -1, FDLOG_FD);
    r.conf.errh->fd          = -1; /* (disable) */
    /* r->con->srv->srvconf.absolute_dir_redirect
     * in http_response_redirect_to_directory() */
    r.con = &con;
    con.srv = &srv;

    test_mod_userdir_docroot_handler(&r, p);

    free(r.uri.path.ptr);
    free(r.physical.basedir.ptr);
    free(r.physical.path.ptr);
    free(r.physical.rel_path.ptr);
    array_free_data(&r.resp_headers);

    fdlog_free(r.conf.errh);
    buffer_free(r.tmp_buf);

    buffer_free(basepath);
    buffer_free(path);
    free(p);
}