diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2018-12-09 14:31:59 -0500 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2018-12-10 22:36:23 -0500 |
commit | f03e5e239d024965d4c1476b5e02baa06f876269 (patch) | |
tree | fd485ce250b615a92e136238af1f221f20ca129d | |
parent | dd11144bc81884fa62d90a038ceb5f00c0f88d5e (diff) | |
download | lighttpd-git-f03e5e239d024965d4c1476b5e02baa06f876269.tar.gz |
[tests] t/test_keyvalue
create t/test_keyvalue to replace sparse tests in
tests/mod-redirect.t and tests/mod-rewrite.t
remove tests/mod-redirect.t and tests/mod-rewrite.t
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/CMakeLists.txt | 16 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/meson.build | 15 | ||||
-rw-r--r-- | src/t/test_keyvalue.c | 124 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/SConscript | 2 | ||||
-rw-r--r-- | tests/lighttpd.conf | 28 | ||||
-rw-r--r-- | tests/meson.build | 2 | ||||
-rwxr-xr-x | tests/mod-redirect.t | 59 | ||||
-rwxr-xr-x | tests/mod-rewrite.t | 76 |
12 files changed, 161 insertions, 171 deletions
@@ -51,6 +51,7 @@ test_array test_base64 test_buffer test_burl +test_keyvalue test_configfile test_mod_access test_mod_evhost diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8056cd7b..db5f8a5a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -760,6 +760,18 @@ add_executable(test_configfile ) add_test(NAME test_configfile COMMAND test_configfile) +add_executable(test_keyvalue + t/test_keyvalue.c + burl.c + buffer.c + base64.c + array.c + data_integer.c + data_string.c + log.c +) +add_test(NAME test_keyvalue COMMAND test_keyvalue) + add_executable(test_mod_access t/test_mod_access.c configfile-glue.c @@ -833,6 +845,8 @@ if(HAVE_PCRE_H) add_target_properties(mod_redirect COMPILE_FLAGS ${PCRE_CFLAGS}) target_link_libraries(test_configfile ${PCRE_LDFLAGS}) add_target_properties(test_configfile COMPILE_FLAGS ${PCRE_CFLAGS}) + target_link_libraries(test_keyvalue ${PCRE_LDFLAGS}) + add_target_properties(test_keyvalue COMPILE_FLAGS ${PCRE_CFLAGS}) target_link_libraries(test_mod_access ${PCRE_LDFLAGS}) add_target_properties(test_mod_access COMPILE_FLAGS ${PCRE_CFLAGS}) target_link_libraries(test_mod_evhost ${PCRE_LDFLAGS}) @@ -1036,6 +1050,8 @@ if(WITH_LIBUNWIND) add_target_properties(test_base64 COMPILE_FLAGS ${LIBUNWIND_CFLAGS}) target_link_libraries(test_configfile ${PCRE_LDFLAGS} ${LIBUNWIND_LDFLAGS}) add_target_properties(test_configfile COMPILE_FLAGS ${PCRE_CFLAGS} ${LIBUNWIND_CFLAGS}) + target_link_libraries(test_keyvalue ${PCRE_LDFLAGS} ${LIBUNWIND_LDFLAGS}) + add_target_properties(test_keyvalue COMPILE_FLAGS ${PCRE_CFLAGS} ${LIBUNWIND_CFLAGS}) target_link_libraries(test_mod_access ${PCRE_LDFLAGS} ${LIBUNWIND_LDFLAGS}) add_target_properties(test_mod_access COMPILE_FLAGS ${PCRE_CFLAGS} ${LIBUNWIND_CFLAGS}) target_link_libraries(test_mod_evhost ${PCRE_LDFLAGS} ${LIBUNWIND_LDFLAGS}) diff --git a/src/Makefile.am b/src/Makefile.am index 6bdf40cf..25622a75 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ noinst_PROGRAMS=\ t/test_burl \ t/test_base64 \ t/test_configfile \ + t/test_keyvalue \ t/test_mod_access \ t/test_mod_evhost \ t/test_mod_simple_vhost \ @@ -20,6 +21,7 @@ TESTS=\ t/test_burl$(EXEEXT) \ t/test_base64$(EXEEXT) \ t/test_configfile$(EXEEXT) \ + t/test_keyvalue$(EXEEXT) \ t/test_mod_access$(EXEEXT) \ t/test_mod_evhost$(EXEEXT) \ t/test_mod_simple_vhost$(EXEEXT) \ @@ -557,6 +559,9 @@ t_test_burl_LDADD = $(LIBUNWIND_LIBS) t_test_configfile_SOURCES = t/test_configfile.c buffer.c array.c data_config.c data_integer.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c t_test_configfile_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS) +t_test_keyvalue_SOURCES = t/test_keyvalue.c burl.c buffer.c base64.c array.c data_integer.c data_string.c log.c +t_test_keyvalue_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS) + t_test_mod_access_SOURCES = t/test_mod_access.c configfile-glue.c buffer.c array.c data_config.c data_integer.c data_string.c http_header.c http_kv.c vector.c log.c sock_addr.c t_test_mod_access_LDADD = $(PCRE_LIB) $(LIBUNWIND_LIBS) diff --git a/src/meson.build b/src/meson.build index e59a20ec..842afaa4 100644 --- a/src/meson.build +++ b/src/meson.build @@ -760,6 +760,21 @@ test('test_configfile', executable('test_configfile', build_by_default: false, )) +test('test_keyvalue', executable('test_keyvalue', + sources: [ + 't/test_keyvalue.c', + 'burl.c', + 'buffer.c', + 'base64.c', + 'array.c', + 'data_integer.c', + 'data_string.c', + 'log.c', + ], + dependencies: common_flags + libpcre + libunwind, + build_by_default: false, +)) + test('test_mod_access', executable('test_mod_access', sources: [ 't/test_mod_access.c', diff --git a/src/t/test_keyvalue.c b/src/t/test_keyvalue.c new file mode 100644 index 00000000..83dac923 --- /dev/null +++ b/src/t/test_keyvalue.c @@ -0,0 +1,124 @@ +#include "first.h" + +#undef NDEBUG +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> /* STDERR_FILENO */ + +#include "keyvalue.c" + +#ifdef HAVE_PCRE_H +static pcre_keyvalue_buffer * test_keyvalue_test_kvb_init (void) { + pcre_keyvalue_buffer *kvb = pcre_keyvalue_buffer_init(); + buffer *k = buffer_init(); + buffer *v = buffer_init(); + server srv; + + memset(&srv, 0, sizeof(srv)); + srv.errorlog_fd = STDERR_FILENO; + srv.errorlog_mode = ERRORLOG_FD; + srv.errorlog_buf = buffer_init(); + + buffer_copy_string_len(k, CONST_STR_LEN("^/foo($|\\?.+)")); + buffer_copy_string_len(v, CONST_STR_LEN("/foo/$1")); + assert(0 == pcre_keyvalue_buffer_append(&srv, kvb, k, v)); + buffer_copy_string_len(k, CONST_STR_LEN("^/bar(?:$|\\?(.+))")); + buffer_copy_string_len(v, CONST_STR_LEN("/?bar&$1")); + assert(0 == pcre_keyvalue_buffer_append(&srv, kvb, k, v)); + buffer_copy_string_len(k, CONST_STR_LEN("^/redirect(?:\\?(.*))?$")); + buffer_copy_string_len(v, CONST_STR_LEN("/?seg=%1&$1")); + assert(0 == pcre_keyvalue_buffer_append(&srv, kvb, k, v)); + buffer_copy_string_len(k, CONST_STR_LEN("^(/[^?]*)(?:\\?(.*))?$")); + buffer_copy_string_len(v, CONST_STR_LEN("/?file=$1&$2")); + assert(0 == pcre_keyvalue_buffer_append(&srv, kvb, k, v)); + + buffer_free(k); + buffer_free(v); + buffer_free(srv.errorlog_buf); + + return kvb; +} + +static void test_keyvalue_pcre_keyvalue_buffer_process (void) { + pcre_keyvalue_buffer *kvb = test_keyvalue_test_kvb_init(); + buffer *url = buffer_init(); + buffer *result = buffer_init(); + struct burl_parts_t burl; + cond_cache_t cache; + pcre_keyvalue_ctx ctx; + handler_t rc; + + ctx.burl = &burl; + burl.scheme = buffer_init(); + burl.authority = buffer_init(); + burl.port = 80; + burl.path = buffer_init(); + burl.query = buffer_init(); + buffer_copy_string_len(burl.scheme, CONST_STR_LEN("http")); + buffer_copy_string_len(burl.authority, CONST_STR_LEN("www.example.com")); + /* model outer conditional match of $HTTP["host"] =~ "^(www).example.com$" */ + ctx.cache = &cache; + memset(&cache, 0, sizeof(cache)); + cache.patterncount = 2; + cache.comp_value = burl.authority; + cache.matches[0] = 0; + cache.matches[1] = 15; + cache.matches[2] = 0; + cache.matches[3] = 3; + + /* converted from prior sparse tests/mod-redirect.t and tests/mod-rewrite.t + * (real-world use should prefer ${url.path} and ${qsa} in substitutions) + */ + + buffer_copy_string_len(url, CONST_STR_LEN("/foo")); + buffer_copy_string_len(burl.path, CONST_STR_LEN("/foo")); + buffer_clear(burl.query); + rc = pcre_keyvalue_buffer_process(kvb, &ctx, url, result); + assert(HANDLER_FINISHED == rc); + assert(buffer_is_equal_string(result, CONST_STR_LEN("/foo/"))); + + buffer_copy_string_len(url, CONST_STR_LEN("/foo?a=b")); + buffer_copy_string_len(burl.path, CONST_STR_LEN("/foo")); + buffer_copy_string_len(burl.query, CONST_STR_LEN("a=b")); + rc = pcre_keyvalue_buffer_process(kvb, &ctx, url, result); + assert(HANDLER_FINISHED == rc); + assert(buffer_is_equal_string(result, CONST_STR_LEN("/foo/?a=b"))); + + buffer_copy_string_len(url, CONST_STR_LEN("/bar?a=b")); + buffer_copy_string_len(burl.path, CONST_STR_LEN("/bar")); + buffer_copy_string_len(burl.query, CONST_STR_LEN("a=b")); + rc = pcre_keyvalue_buffer_process(kvb, &ctx, url, result); + assert(HANDLER_FINISHED == rc); + assert(buffer_is_equal_string(result, CONST_STR_LEN("/?bar&a=b"))); + + buffer_copy_string_len(url, CONST_STR_LEN("/nofile?a=b")); + buffer_copy_string_len(burl.path, CONST_STR_LEN("/nofile")); + buffer_copy_string_len(burl.query, CONST_STR_LEN("a=b")); + rc = pcre_keyvalue_buffer_process(kvb, &ctx, url, result); + assert(HANDLER_FINISHED == rc); + assert(buffer_is_equal_string(result, CONST_STR_LEN("/?file=/nofile&a=b"))); + + buffer_copy_string_len(url, CONST_STR_LEN("/redirect?a=b")); + buffer_copy_string_len(burl.path, CONST_STR_LEN("/redirect")); + buffer_copy_string_len(burl.query, CONST_STR_LEN("a=b")); + rc = pcre_keyvalue_buffer_process(kvb, &ctx, url, result); + assert(HANDLER_FINISHED == rc); + assert(buffer_is_equal_string(result, CONST_STR_LEN("/?seg=www&a=b"))); + + buffer_free(url); + buffer_free(result); + buffer_free(burl.scheme); + buffer_free(burl.authority); + buffer_free(burl.path); + buffer_free(burl.query); + pcre_keyvalue_buffer_free(kvb); +} +#endif + +int main (void) { + #ifdef HAVE_PCRE_H + test_keyvalue_pcre_keyvalue_buffer_process(); + #endif + return 0; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6bd93b30..55bbaf60 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,8 +27,6 @@ set(T_FILES mod-extforward.t mod-fastcgi.t mod-proxy.t - mod-redirect.t - mod-rewrite.t mod-secdownload.t mod-setenv.t mod-ssi.t diff --git a/tests/Makefile.am b/tests/Makefile.am index da67cd21..8716b5d1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -45,8 +45,6 @@ CONFS=\ mod-extforward.t \ mod-fastcgi.t \ mod-proxy.t \ - mod-redirect.t \ - mod-rewrite.t \ mod-secdownload.t \ mod-setenv.t \ mod-ssi.t \ diff --git a/tests/SConscript b/tests/SConscript index bbb92df2..c476f33c 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -23,9 +23,7 @@ extra_dist = Split('fastcgi-10.conf \ mod-compress.t \ mod-compress.conf \ mod-fastcgi.t \ - mod-redirect.t \ mod-userdir.t \ - mod-rewrite.t \ request.t \ mod-ssi.t \ LightyTest.pm \ diff --git a/tests/lighttpd.conf b/tests/lighttpd.conf index 6566d1c8..f4454f95 100644 --- a/tests/lighttpd.conf +++ b/tests/lighttpd.conf @@ -245,34 +245,6 @@ $HTTP["host"] == "auth.example.org" { ) } -$HTTP["host"] =~ "(vvv).example.org" { - url.redirect = ( - "^/redirect/$" => "http://localhost:2048/", - ) -} - -$HTTP["host"] =~ "(zzz).example.org" { - url.redirect = ( - "^/redirect/$" => "http://localhost:2048/%1", - ) -} - -$HTTP["host"] =~ "(remoteip)\.example\.org" { - $HTTP["remoteip"] =~ "(127\.0\.0\.1)" { - url.redirect = ( - "^/redirect/$" => "http://localhost:2048/%1", - ) - } -} - -$HTTP["remoteip"] =~ "(127\.0\.0\.1)" { - $HTTP["host"] =~ "(remoteip2)\.example\.org" { - url.redirect = ( - "^/redirect/$" => "http://localhost:2048/%1", - ) - } -} - $HTTP["host"] =~ "bug255\.example\.org$" { $HTTP["remoteip"] == "127.0.0.1" { url.access-deny = ( diff --git a/tests/meson.build b/tests/meson.build index 006268d0..388b954a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -36,8 +36,6 @@ tests = [ 'mod-extforward.t', 'mod-fastcgi.t', 'mod-proxy.t', - 'mod-redirect.t', - 'mod-rewrite.t', 'mod-secdownload.t', 'mod-setenv.t', 'mod-ssi.t', diff --git a/tests/mod-redirect.t b/tests/mod-redirect.t deleted file mode 100755 index 14669d97..00000000 --- a/tests/mod-redirect.t +++ /dev/null @@ -1,59 +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(); - -$t->{REQUEST} = ( <<EOF -GET /redirect/ HTTP/1.0 -Host: vvv.example.org -EOF - ); -$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:'.$tf->{PORT}.'/' } ]; -ok($tf->handle_http($t) == 0, 'external redirect'); - -$t->{REQUEST} = ( <<EOF -GET /redirect/ HTTP/1.0 -Host: vvv.example.org -EOF - ); -$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:'.$tf->{PORT}.'/', 'Content-Length' => '0' } ]; -ok($tf->handle_http($t) == 0, 'external redirect should have a Content-Length: 0'); - -$t->{REQUEST} = ( <<EOF -GET /redirect/ HTTP/1.0 -Host: zzz.example.org -EOF - ); -$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:'.$tf->{PORT}.'/zzz' } ]; -ok($tf->handle_http($t) == 0, 'external redirect with cond regsub'); - -$t->{REQUEST} = ( <<EOF -GET /redirect/ HTTP/1.0 -Host: remoteip.example.org -EOF - ); -$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:'.$tf->{PORT}.'/127.0.0.1' } ]; -ok($tf->handle_http($t) == 0, 'external redirect with cond regsub on remoteip'); - -$t->{REQUEST} = ( <<EOF -GET /redirect/ HTTP/1.0 -Host: remoteip2.example.org -EOF - ); -$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => 'http://localhost:'.$tf->{PORT}.'/remoteip2' } ]; -ok($tf->handle_http($t) == 0, 'external redirect with cond regsub on remoteip2'); - -ok($tf->stop_proc == 0, "Stopping lighttpd"); diff --git a/tests/mod-rewrite.t b/tests/mod-rewrite.t deleted file mode 100755 index 615ac7be..00000000 --- a/tests/mod-rewrite.t +++ /dev/null @@ -1,76 +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 => 8; -use LightyTest; - -my $tf = LightyTest->new(); -my $t; -my $php_child = -1; - -SKIP: { - skip "PHP already running on port 1026", 1 if $tf->listening_on(1026); - skip "no php binary found", 1 unless $LightyTest::HAVE_PHP; - ok(-1 != ($php_child = $tf->spawnfcgi($ENV{'PHP'}, 1026)), "Spawning php"); -} - -SKIP: { - skip "no PHP running on port 1026", 6 unless $tf->listening_on(1026); - - ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup; - - $t->{REQUEST} = ( <<EOF -GET /rewrite/foo HTTP/1.0 -Host: www.example.org -EOF - ); - $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ]; - ok($tf->handle_http($t) == 0, 'valid request'); - - $t->{REQUEST} = ( <<EOF -GET /rewrite/foo?a=b HTTP/1.0 -Host: www.example.org -EOF - ); - $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'a=b' } ]; - ok($tf->handle_http($t) == 0, 'valid request'); - - $t->{REQUEST} = ( <<EOF -GET /rewrite/bar?a=b HTTP/1.0 -Host: www.example.org -EOF - ); - $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'bar&a=b' } ]; - ok($tf->handle_http($t) == 0, 'valid request'); - - $t->{REQUEST} = ( <<EOF -GET /rewrite/nofile?a=b HTTP/1.0 -Host: www.example.org -EOF - ); - $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'file=/rewrite/nofile&a=b' } ]; - ok($tf->handle_http($t) == 0, 'not existing file rewrite'); - - ok($tf->stop_proc == 0, "Stopping lighttpd"); -} - -SKIP: { - skip "PHP not started, cannot stop it", 1 unless $php_child != -1; - ok(0 == $tf->endspawnfcgi($php_child), "Stopping php"); -} - - -exit 0; - -cleanup: ; - -$tf->endspawnfcgi($php_child) if $php_child != -1; - -die(); |