Browse Source

[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
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
f03e5e239d
  1. 1
      .gitignore
  2. 16
      src/CMakeLists.txt
  3. 5
      src/Makefile.am
  4. 15
      src/meson.build
  5. 124
      src/t/test_keyvalue.c
  6. 2
      tests/CMakeLists.txt
  7. 2
      tests/Makefile.am
  8. 2
      tests/SConscript
  9. 28
      tests/lighttpd.conf
  10. 2
      tests/meson.build
  11. 59
      tests/mod-redirect.t
  12. 76
      tests/mod-rewrite.t

1
.gitignore

@ -51,6 +51,7 @@ test_array
test_base64
test_buffer
test_burl
test_keyvalue
test_configfile
test_mod_access
test_mod_evhost

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

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

15
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',

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

2
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

2
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 \

2
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 \

28
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 = (

2
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',

59
tests/mod-redirect.t

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

76
tests/mod-rewrite.t

@ -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();
Loading…
Cancel
Save