diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2020-01-19 22:10:46 -0500 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2020-07-08 19:54:29 -0400 |
commit | c8a1cba0c1f337d9a7ae9108226952abb34424ef (patch) | |
tree | c7f228883b9df19715d325bd54616c2cc5ad6011 | |
parent | 8b7e110973cb411a29c580d16d3c65cd2d8a4ab8 (diff) | |
download | lighttpd-git-c8a1cba0c1f337d9a7ae9108226952abb34424ef.tar.gz |
[tests] t/test_mod_userdir
create t/test_mod_userdir to replace sparse tests in tests/mod-userdir.t
remove tests/mod-userdir.t
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/meson.build | 13 | ||||
-rw-r--r-- | src/mod_userdir.c | 1 | ||||
-rw-r--r-- | src/t/test_mod_userdir.c | 207 | ||||
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rwxr-xr-x | tests/mod-userdir.t | 58 |
8 files changed, 239 insertions, 59 deletions
@@ -56,5 +56,6 @@ test_configfile test_mod_access test_mod_evhost test_mod_simple_vhost +test_mod_userdir test_request versionstamp.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c1939660..a32a18b5 100644 --- a/src/CMakeLists.txt +++ b/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() diff --git a/src/Makefile.am b/src/Makefile.am index 14dba23b..94bc84e1 100644 --- a/src/Makefile.am +++ b/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) diff --git a/src/meson.build b/src/meson.build index 5bbedd23..8ef15011 100644 --- a/src/meson.build +++ b/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', diff --git a/src/mod_userdir.c b/src/mod_userdir.c index c2c3e07d..9359fd53 100644 --- a/src/mod_userdir.c +++ b/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; diff --git a/src/t/test_mod_userdir.c b/src/t/test_mod_userdir.c new file mode 100644 index 00000000..d9db7c1f --- /dev/null +++ b/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; +} diff --git a/tests/Makefile.am b/tests/Makefile.am index 1582be86..af901c9e 100644 --- a/tests/Makefile.am +++ b/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 \ diff --git a/tests/mod-userdir.t b/tests/mod-userdir.t deleted file mode 100755 index 52f9c32f..00000000 --- a/tests/mod-userdir.t +++ /dev/null @@ -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"); - |