Переглянути джерело

[tests] t/test_mod_userdir

create t/test_mod_userdir to replace sparse tests in tests/mod-userdir.t
remove tests/mod-userdir.t
personal/stbuehler/ci-build
Glenn Strauss 8 місяці тому
джерело
коміт
c8a1cba0c1
8 змінених файлів з 239 додано та 59 видалено
  1. +1
    -0
      .gitignore
  2. +12
    -0
      src/CMakeLists.txt
  3. +5
    -0
      src/Makefile.am
  4. +13
    -0
      src/meson.build
  5. +1
    -0
      src/mod_userdir.c
  6. +207
    -0
      src/t/test_mod_userdir.c
  7. +0
    -1
      tests/Makefile.am
  8. +0
    -58
      tests/mod-userdir.t

+ 1
- 0
.gitignore Переглянути файл

@@ -56,5 +56,6 @@ test_configfile
test_mod_access
test_mod_evhost
test_mod_simple_vhost
test_mod_userdir
test_request
versionstamp.h

+ 12
- 0
src/CMakeLists.txt Переглянути файл

@@ -828,6 +828,16 @@ add_executable(test_mod_simple_vhost
)
add_test(NAME test_mod_simple_vhost COMMAND test_mod_simple_vhost)

add_executable(test_mod_userdir
t/test_mod_userdir.c
buffer.c
array.c
data_integer.c
data_string.c
log.c
)
add_test(NAME test_mod_userdir COMMAND test_mod_userdir)

add_executable(test_request
t/test_request.c
request.c
@@ -1066,6 +1076,8 @@ if(WITH_LIBUNWIND)
add_target_properties(test_mod_evhost COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_mod_simple_vhost ${LIBUNWIND_LDFLAGS})
add_target_properties(test_mod_simple_vhost COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_mod_userdir ${LIBUNWIND_LDFLAGS})
add_target_properties(test_mod_userdir COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_request ${LIBUNWIND_LDFLAGS})
add_target_properties(test_request COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
endif()


+ 5
- 0
src/Makefile.am Переглянути файл

@@ -10,6 +10,7 @@ noinst_PROGRAMS=\
t/test_mod_access \
t/test_mod_evhost \
t/test_mod_simple_vhost \
t/test_mod_userdir \
t/test_request

sbin_PROGRAMS=lighttpd lighttpd-angel
@@ -25,6 +26,7 @@ TESTS=\
t/test_mod_access$(EXEEXT) \
t/test_mod_evhost$(EXEEXT) \
t/test_mod_simple_vhost$(EXEEXT) \
t/test_mod_userdir$(EXEEXT) \
t/test_request$(EXEEXT)

lemon$(BUILD_EXEEXT): lemon.c
@@ -582,6 +584,9 @@ t_test_mod_evhost_LDADD = $(LIBUNWIND_LIBS)
t_test_mod_simple_vhost_SOURCES = t/test_mod_simple_vhost.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_simple_vhost_LDADD = $(LIBUNWIND_LIBS)

t_test_mod_userdir_SOURCES = t/test_mod_userdir.c buffer.c array.c data_integer.c data_string.c log.c
t_test_mod_userdir_LDADD = $(LIBUNWIND_LIBS)

t_test_request_SOURCES = t/test_request.c request.c buffer.c array.c data_integer.c data_string.c http_header.c http_kv.c log.c sock_addr.c
t_test_request_LDADD = $(LIBUNWIND_LIBS)



+ 13
- 0
src/meson.build Переглянути файл

@@ -822,6 +822,19 @@ test('test_mod_simple_vhost', executable('test_mod_simple_vhost',
build_by_default: false,
))

test('test_mod_userdir', executable('test_mod_userdir',
sources: [
't/test_mod_userdir.c',
'buffer.c',
'array.c',
'data_integer.c',
'data_string.c',
'log.c',
],
dependencies: common_flags + libunwind,
build_by_default: false,
))

test('test_request', executable('test_request',
sources: [
't/test_request.c',


+ 1
- 0
src/mod_userdir.c Переглянути файл

@@ -240,6 +240,7 @@ URIHANDLER_FUNC(mod_userdir_docroot_handler) {
const char * const uptr = r->uri.path.ptr + 2;
const char * const rel_url = strchr(uptr, '/');
if (NULL == rel_url) {
if (!*uptr) return HANDLER_GO_ON; /* "/~" is not a valid userdir path */
/* / is missing -> redirect to .../ as we are a user - DIRECTORY ! :) */
http_response_redirect_to_directory(r, 301);
return HANDLER_FINISHED;


+ 207
- 0
src/t/test_mod_userdir.c Переглянути файл

@@ -0,0 +1,207 @@
#include "first.h"

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

#include "mod_userdir.c"

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);
}

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

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

request_st r;

memset(&r, 0, sizeof(request_st));
r.tmp_buf = buffer_init();
r.conf.errh = log_error_st_init();
r.conf.errh->errorlog_fd = -1; /* (disable) */

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);

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

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

/*
* stub functions
*/

int http_response_redirect_to_directory(request_st *r, int status) {
r->http_status = status;
return 0;
}

int config_plugin_values_init(server *srv, void *p_d, const config_plugin_keys_t *cpk, const char *mname) {
UNUSED(srv);
UNUSED(p_d);
UNUSED(cpk);
UNUSED(mname);
return 0;
}

int config_check_cond(request_st *r, int context_ndx) {
UNUSED(r);
UNUSED(context_ndx);
return 0;
}

+ 0
- 1
tests/Makefile.am Переглянути файл

@@ -46,7 +46,6 @@ CONFS=\
mod-secdownload.t \
mod-setenv.t \
mod-ssi.t \
mod-userdir.t \
proxy.conf \
request.t \
scgi-responder.conf \


+ 0
- 58
tests/mod-userdir.t Переглянути файл

@@ -1,58 +0,0 @@
#!/usr/bin/env perl
BEGIN {
# add current source dir to the include-path
# we need this for make distcheck
(my $srcdir = $0) =~ s,/[^/]+$,/,;
unshift @INC, $srcdir;
}

use strict;
use IO::Socket;
use Test::More tests => 7;
use LightyTest;

my $tf = LightyTest->new();
my $t;

ok($tf->start_proc == 0, "Starting lighttpd") or die();

# get current user

$t->{REQUEST} = ( <<EOF
GET /~foobar/ HTTP/1.0
EOF
);
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
ok($tf->handle_http($t) == 0, 'valid user');

$t->{REQUEST} = ( <<EOF
GET /%7Efoobar/ HTTP/1.0
EOF
);
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
ok($tf->handle_http($t) == 0, 'valid user with url-encoded ~ as %7E');

$t->{REQUEST} = ( <<EOF
GET /~jan HTTP/1.0
EOF
);
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://'.$tf->{HOSTNAME}.':'.$tf->{PORT}.'/~jan/' } ];
ok($tf->handle_http($t) == 0, 'valid user + redirect');

$t->{REQUEST} = ( <<EOF
GET /%7Ejan HTTP/1.0
EOF
);
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://'.$tf->{HOSTNAME}.':'.$tf->{PORT}.'/~jan/' } ];
ok($tf->handle_http($t) == 0, 'valid user with url encoded ~ as %7E + redirect');

$t->{REQUEST} = ( <<EOF
GET /~jan HTTP/1.0
Host: www.example.org
EOF
);
$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://www.example.org/~jan/' } ];
ok($tf->handle_http($t) == 0, 'valid user + redirect');

ok($tf->stop_proc == 0, "Stopping lighttpd");


Завантаження…
Відмінити
Зберегти