summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2021-11-01 05:46:20 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2021-11-01 05:46:20 -0400
commit84449e1497d034cab4f31ac69432edddb28390a6 (patch)
treefdecc47f111817ca0bae34b63ca75adbb3a761e1
parent98ba0310c4e352b5ff95d051e467f63f59f04a85 (diff)
downloadlighttpd-git-84449e1497d034cab4f31ac69432edddb28390a6.tar.gz
[tests] t/test_mod_ssi
-rw-r--r--.gitignore1
-rw-r--r--src/CMakeLists.txt28
-rw-r--r--src/Makefile.am5
-rw-r--r--src/meson.build33
-rw-r--r--src/t/test_mod_ssi.c202
5 files changed, 269 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 99e7458c..0327b9bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,6 +56,7 @@ test_mod_access
test_mod_evhost
test_mod_indexfile
test_mod_simple_vhost
+test_mod_ssi
test_mod_staticfile
test_mod_userdir
test_request
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index b87760d5..c9510b71 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -961,6 +961,31 @@ add_executable(test_mod_simple_vhost
)
add_test(NAME test_mod_simple_vhost COMMAND test_mod_simple_vhost)
+add_executable(test_mod_ssi
+ t/test_mod_ssi.c
+ request.c
+ base64.c
+ buffer.c
+ burl.c
+ array.c
+ chunk.c
+ fdevent.c
+ http-header-glue.c
+ http_cgi.c
+ http_chunk.c
+ http_date.c
+ http_etag.c
+ http_header.c
+ http_kv.c
+ log.c
+ fdlog.c
+ sock_addr.c
+ stat_cache.c
+ algo_splaytree.c
+ ck.c
+)
+add_test(NAME test_mod_ssi COMMAND test_mod_ssi)
+
add_executable(test_mod_staticfile
t/test_mod_staticfile.c
request.c
@@ -1145,6 +1170,7 @@ endif()
if(HAVE_LIBFAM)
target_link_libraries(lighttpd fam)
+ target_link_libraries(test_mod_ssi fam)
target_link_libraries(test_mod_staticfile fam)
endif()
@@ -1268,6 +1294,8 @@ if(WITH_LIBUNWIND)
add_target_properties(test_mod_indexfile 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_ssi ${LIBUNWIND_LDFLAGS})
+ add_target_properties(test_mod_ssi COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_mod_staticfile ${LIBUNWIND_LDFLAGS})
add_target_properties(test_mod_staticfile COMPILE_FLAGS ${LIBUNWIND_CFLAGS})
target_link_libraries(test_mod_userdir ${LIBUNWIND_LDFLAGS})
diff --git a/src/Makefile.am b/src/Makefile.am
index 29b17d0e..f33c1290 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,6 +11,7 @@ noinst_PROGRAMS=\
t/test_mod_evhost \
t/test_mod_indexfile \
t/test_mod_simple_vhost \
+ t/test_mod_ssi \
t/test_mod_staticfile \
t/test_mod_userdir \
t/test_request
@@ -29,6 +30,7 @@ TESTS=\
t/test_mod_evhost$(EXEEXT) \
t/test_mod_indexfile$(EXEEXT) \
t/test_mod_simple_vhost$(EXEEXT) \
+ t/test_mod_ssi$(EXEEXT) \
t/test_mod_staticfile$(EXEEXT) \
t/test_mod_userdir$(EXEEXT) \
t/test_request$(EXEEXT)
@@ -673,6 +675,9 @@ t_test_mod_indexfile_LDADD = $(LIBUNWIND_LIBS) $(FAM_LIBS)
t_test_mod_simple_vhost_SOURCES = t/test_mod_simple_vhost.c buffer.c array.c log.c fdlog.c ck.c
t_test_mod_simple_vhost_LDADD = $(LIBUNWIND_LIBS)
+t_test_mod_ssi_SOURCES = t/test_mod_ssi.c request.c base64.c buffer.c burl.c array.c chunk.c fdevent.c http-header-glue.c http_cgi.c http_chunk.c http_date.c http_etag.c http_header.c http_kv.c log.c fdlog.c sock_addr.c stat_cache.c algo_splaytree.c ck.c
+t_test_mod_ssi_LDADD = $(LIBUNWIND_LIBS) $(FAM_LIBS)
+
t_test_mod_staticfile_SOURCES = t/test_mod_staticfile.c request.c base64.c buffer.c burl.c array.c chunk.c fdevent.c http-header-glue.c http_cgi.c http_chunk.c http_date.c http_etag.c http_header.c http_kv.c log.c fdlog.c sock_addr.c stat_cache.c algo_splaytree.c ck.c
t_test_mod_staticfile_LDADD = $(LIBUNWIND_LIBS) $(FAM_LIBS)
diff --git a/src/meson.build b/src/meson.build
index 9e2e1160..b0e58ae0 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -998,6 +998,39 @@ test('test_mod_simple_vhost', executable('test_mod_simple_vhost',
build_by_default: false,
))
+test('test_mod_ssi', executable('test_mod_ssi',
+ sources: [
+ 't/test_mod_ssi.c',
+ 'request.c',
+ 'base64.c',
+ 'buffer.c',
+ 'burl.c',
+ 'array.c',
+ 'chunk.c',
+ 'fdevent.c',
+ 'http-header-glue.c',
+ 'http_cgi.c',
+ 'http_chunk.c',
+ 'http_date.c',
+ 'http_etag.c',
+ 'http_header.c',
+ 'http_kv.c',
+ 'log.c',
+ 'fdlog.c',
+ 'sock_addr.c',
+ 'stat_cache.c',
+ 'algo_splaytree.c',
+ 'ck.c'
+ ],
+ dependencies: [ common_flags
+ , libfam
+ , libpcre
+ , libunwind
+ , libws2_32
+ ],
+ build_by_default: false,
+))
+
test('test_mod_staticfile', executable('test_mod_staticfile',
sources: [
't/test_mod_staticfile.c',
diff --git a/src/t/test_mod_ssi.c b/src/t/test_mod_ssi.c
new file mode 100644
index 00000000..92b0b0f8
--- /dev/null
+++ b/src/t/test_mod_ssi.c
@@ -0,0 +1,202 @@
+#include "first.h"
+
+#undef NDEBUG
+#include <sys/types.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "mod_ssi.c"
+#include "mod_ssi_expr.c"
+#include "mod_ssi_exprparser.c"
+#include "fdlog.h"
+
+static void test_mod_ssi_reset (request_st * const r, handler_ctx * const hctx)
+{
+ r->http_status = 0;
+ r->resp_htags = 0;
+ array_reset_data_strings(&r->resp_headers);
+ http_response_body_clear(r, 0);
+
+ buffer_clear(hctx->timefmt);
+ array_reset_data_strings(hctx->ssi_vars);
+ array_reset_data_strings(hctx->ssi_cgi_env);
+}
+
+static void test_mod_ssi_write_testfile (int fd, const char *buf, size_t len)
+{
+ if (0 != lseek(fd, 0, SEEK_SET)
+ || (ssize_t)len != write(fd, buf, len)
+ || 0 != ftruncate(fd, (off_t)len)
+ || 0 != lseek(fd, 0, SEEK_SET)) {
+ perror("lseek(),ftruncate(),write()"); /*(unlikely: partial write)*/
+ exit(1);
+ }
+}
+
+
+static void
+test_mod_ssi_read_fd (request_st * const r, handler_ctx * const hctx)
+{
+ /* TODO: add adjustments for _WIN32 */
+ char fn[] = "/tmp/lighttpd_mod_ssi_XXXXXX";
+ struct stat st;
+ chunkqueue * const cq = &r->write_queue;
+
+ int fd = fdevent_mkostemp(fn, 0);
+ if (fd < 0) {
+ perror("mkstemp()");
+ exit(1);
+ }
+ if (0 != fstat(fd, &st)) {
+ perror("fstat()");
+ exit(1);
+ }
+ unlink(fn);
+
+ const char ssi_simple[] =
+ "<!--#echo var=\"SCRIPT_NAME\" -->";
+ test_mod_ssi_write_testfile(fd, ssi_simple, sizeof(ssi_simple)-1);
+ test_mod_ssi_reset(r, hctx);
+ array_set_key_value(hctx->ssi_cgi_env,
+ CONST_STR_LEN("SCRIPT_NAME"),
+ CONST_STR_LEN("/ssi.shtml"));
+ mod_ssi_read_fd(r, hctx, &st, fd);
+ assert(cq->first);
+ assert(buffer_eq_slen(cq->first->mem,
+ CONST_STR_LEN("/ssi.shtml")));
+
+ hctx->conf.ssi_exec = 0; /* default */
+ const char ssi_exec[] =
+ "<!--#exec cmd=\"expr 1 + 1\"-->";
+ test_mod_ssi_write_testfile(fd, ssi_exec, sizeof(ssi_exec)-1);
+ test_mod_ssi_reset(r, hctx);
+ mod_ssi_read_fd(r, hctx, &st, fd);
+ assert(NULL == cq->first);
+
+ hctx->conf.ssi_exec = 1;
+ test_mod_ssi_write_testfile(fd, ssi_exec, sizeof(ssi_exec)-1);
+ test_mod_ssi_reset(r, hctx);
+ mod_ssi_read_fd(r, hctx, &st, fd);
+ assert(cq->first);
+ assert(cq->first->type == FILE_CHUNK);
+ assert(2 == chunkqueue_length(cq));
+ char buf[80];
+ if (0 != lseek(cq->first->file.fd, 0, SEEK_SET)
+ || 2 != read(cq->first->file.fd, buf, sizeof(buf))) {
+ perror("lseek(),read()");
+ exit(1);
+ }
+ assert(0 == memcmp(buf, "2\n", 2));
+ hctx->conf.ssi_exec = 0;
+
+ char fni[] = "/tmp/lighttpd_mod_ssi_inc_XXXXXX";
+ int fdi = fdevent_mkostemp(fni, 0);
+ if (fdi < 0) {
+ perror("mkstemp()");
+ exit(1);
+ }
+ const char ssi_include[] =
+ "ssi-include";
+ test_mod_ssi_write_testfile(fdi, ssi_include, sizeof(ssi_include)-1);
+ close(fdi);
+
+ const char ssi_include_shtml[] =
+ "<!--#echo var=SCRIPT_NAME-->\n"
+ "<!--#echo var='SCRIPT_NAME'-->\n"
+ "<!--#echo var=\"SCRIPT_NAME\"-->\n";
+ buffer * const b = buffer_init();
+ buffer_copy_string_len(b, CONST_STR_LEN(ssi_include_shtml));
+ buffer_append_str3(b, CONST_STR_LEN("<!--#include virtual=\""),
+ fni+5, strlen(fni)-5, /*(step over "/tmp/")*/
+ CONST_STR_LEN("\" -->\n"));
+ buffer_append_str3(b, CONST_STR_LEN("<!--#include file=\""),
+ fni+5, strlen(fni)-5, /*(step over "/tmp/")*/
+ CONST_STR_LEN("\" -->\n"));
+ test_mod_ssi_write_testfile(fd, BUF_PTR_LEN(b));
+ buffer_free(b);
+ test_mod_ssi_reset(r, hctx);
+ array_set_key_value(hctx->ssi_cgi_env,
+ CONST_STR_LEN("SCRIPT_NAME"),
+ CONST_STR_LEN("/ssi-include.shtml"));
+ buffer_copy_string_len(&r->physical.doc_root, CONST_STR_LEN("/tmp"));
+ buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/ssi-include.shtml"));
+ buffer_copy_string_len(&r->physical.rel_path, CONST_STR_LEN("/ssi-include.shtml"));
+ buffer_copy_string_len(&r->physical.path, CONST_STR_LEN("/tmp/ssi-include.shtml"));
+ mod_ssi_read_fd(r, hctx, &st, fd);
+ chunkqueue_read_squash(cq, r->conf.errh);
+ assert(buffer_eq_slen(cq->first->mem,
+ CONST_STR_LEN("/ssi-include.shtml\n"
+ "/ssi-include.shtml\n"
+ "/ssi-include.shtml\n"
+ "ssi-include\n"
+ "ssi-include\n")));
+
+ unlink(fni);
+
+ test_mod_ssi_reset(r, hctx);
+ close(fd);
+}
+
+int main (void)
+{
+ plugin_data * const p = mod_ssi_init();
+ assert(NULL != p);
+
+ request_st r;
+
+ memset(&r, 0, sizeof(request_st));
+ r.tmp_buf = buffer_init();
+ r.conf.errh = fdlog_init(NULL, -1, FDLOG_FD);
+ r.conf.errh->fd = -1; /* (disable) */
+
+ handler_ctx * const hctx = handler_ctx_init(p, r.conf.errh);
+ assert(NULL != hctx);
+
+ test_mod_ssi_read_fd(&r, hctx);
+
+ handler_ctx_free(hctx);
+
+ fdlog_free(r.conf.errh);
+ buffer_free(r.tmp_buf);
+ chunkqueue_reset(&r.write_queue);
+
+ free(r.uri.path.ptr);
+ free(r.physical.etag.ptr);
+ free(r.physical.path.ptr);
+ free(r.physical.rel_path.ptr);
+ free(r.physical.doc_root.ptr);
+
+ mod_ssi_free(p);
+ free(p);
+ stat_cache_free();
+ return 0;
+}
+
+
+/*
+ * stub functions
+ */
+
+#include "fdevent_impl.h"
+int fdevent_select_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_poll_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_linux_sysepoll_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_solaris_devpoll_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_solaris_port_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_freebsd_kqueue_init(struct fdevents *ev) { return NULL == ev; }
+int fdevent_libev_init(struct fdevents *ev) { return NULL == ev; }
+
+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;
+}