diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2021-09-04 18:55:27 -0400 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2023-05-03 23:11:34 -0400 |
commit | b86d4117be115bf1aa556130a823bdcd233bd726 (patch) | |
tree | 3260446164a6e0771c049e7d3e42112a4055b2a3 /src | |
parent | 96c1b1ddc168e50a52864df7aa09aaf6b6dbe714 (diff) | |
download | lighttpd-git-b86d4117be115bf1aa556130a823bdcd233bd726.tar.gz |
[tests] _WIN32 use TMPDIR (or TEMP) for test files
Diffstat (limited to 'src')
-rw-r--r-- | src/t/test_mod_indexfile.c | 46 | ||||
-rw-r--r-- | src/t/test_mod_ssi.c | 30 | ||||
-rw-r--r-- | src/t/test_mod_staticfile.c | 22 |
3 files changed, 71 insertions, 27 deletions
diff --git a/src/t/test_mod_indexfile.c b/src/t/test_mod_indexfile.c index f316916f..13376181 100644 --- a/src/t/test_mod_indexfile.c +++ b/src/t/test_mod_indexfile.c @@ -10,12 +10,12 @@ #include "fdlog.h" __attribute_noinline__ -static void test_mod_indexfile_reset (request_st * const r) +static void test_mod_indexfile_reset (request_st * const r, const char * const fn, const size_t fnlen) { r->http_status = 0; buffer_copy_string_len(&r->uri.path, CONST_STR_LEN("/")); - buffer_copy_string_len(&r->physical.doc_root, CONST_STR_LEN("/tmp")); - buffer_copy_string_len(&r->physical.path, CONST_STR_LEN("/tmp/")); + buffer_copy_string_len(&r->physical.doc_root, fn, fnlen-1); + buffer_copy_string_len(&r->physical.path, fn, fnlen); } __attribute_noinline__ @@ -40,48 +40,62 @@ run_mod_indexfile_tryfiles (request_st * const r, const array * const indexfiles static void test_mod_indexfile_tryfiles (request_st * const r) { - char fn[] = "/tmp/lighttpd_mod_indexfile.XXXXXX"; + const char *tmpdir = getenv("TMPDIR"); + #ifdef _WIN32 + if (NULL == tmpdir) tmpdir = getenv("TEMP"); + #endif + if (NULL == tmpdir) tmpdir = "/tmp"; + size_t tmpdirlen = strlen(tmpdir); + buffer fnb = { NULL, 0, 0 }; + buffer_copy_path_len2(&fnb, tmpdir, tmpdirlen, + CONST_STR_LEN("lighttpd_mod_indexfile.XXXXXX")); + if (fnb.ptr[tmpdirlen] == '/') ++tmpdirlen; + char * const fn = fnb.ptr; + const size_t fnlen = buffer_clen(&fnb); int fd = fdevent_mkostemp(fn, 0); if (fd < 0) { perror("mkstemp()"); + buffer_free_ptr(&fnb); exit(1); } struct stat st; if (0 != fstat(fd, &st)) { perror("fstat()"); + buffer_free_ptr(&fnb); exit(1); } array * const indexfiles = array_init(3); - test_mod_indexfile_reset(r); + test_mod_indexfile_reset(r, fn, tmpdirlen); run_mod_indexfile_tryfiles(r, indexfiles, __LINE__, 0, "empty indexfiles"); - assert(buffer_eq_slen(&r->physical.path, CONST_STR_LEN("/tmp/"))); - test_mod_indexfile_reset(r); + assert(buffer_eq_slen(&r->physical.path, fn, tmpdirlen)); + test_mod_indexfile_reset(r, fn, tmpdirlen); /*(assumes modified tempfile name does not exist)*/ - array_insert_value(indexfiles, fn+5, sizeof(fn)-6-1); + array_insert_value(indexfiles, fn+tmpdirlen, fnlen-tmpdirlen-1); run_mod_indexfile_tryfiles(r, indexfiles, __LINE__, 0, "non-matching indexfiles"); - assert(buffer_eq_slen(&r->physical.path, CONST_STR_LEN("/tmp/"))); - test_mod_indexfile_reset(r); + assert(buffer_eq_slen(&r->physical.path, fn, tmpdirlen)); + test_mod_indexfile_reset(r, fn, tmpdirlen); - array_insert_value(indexfiles, fn+5, sizeof(fn)-5-1); + array_insert_value(indexfiles, fn+tmpdirlen, fnlen-tmpdirlen); run_mod_indexfile_tryfiles(r, indexfiles, __LINE__, 0, "matching indexfile entry (w/o leading '/')"); - assert(buffer_eq_slen(&r->physical.path, fn, sizeof(fn)-1)); - test_mod_indexfile_reset(r); + assert(buffer_eq_slen(&r->physical.path, fn, fnlen)); + test_mod_indexfile_reset(r, fn, tmpdirlen); array_reset_data_strings(indexfiles); - array_insert_value(indexfiles, fn+4, sizeof(fn)-4-1); + array_insert_value(indexfiles, fn+tmpdirlen-1, fnlen-(tmpdirlen-1)); run_mod_indexfile_tryfiles(r, indexfiles, __LINE__, 0, "matching indexfile entry (w/ leading '/')"); - assert(buffer_eq_slen(&r->physical.path, fn, sizeof(fn)-1)); - test_mod_indexfile_reset(r); + assert(buffer_eq_slen(&r->physical.path, fn, fnlen)); + test_mod_indexfile_reset(r, fn, tmpdirlen); array_free(indexfiles); unlink(fn); + buffer_free_ptr(&fnb); } void test_mod_indexfile (void); diff --git a/src/t/test_mod_ssi.c b/src/t/test_mod_ssi.c index 085d041d..181f39e7 100644 --- a/src/t/test_mod_ssi.c +++ b/src/t/test_mod_ssi.c @@ -37,21 +37,33 @@ static void test_mod_ssi_write_testfile (int fd, const char *buf, size_t len) 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; + const char *tmpdir = getenv("TMPDIR"); + #ifdef _WIN32 + if (NULL == tmpdir) tmpdir = getenv("TEMP"); + #endif + if (NULL == tmpdir) tmpdir = "/tmp"; + size_t tmpdirlen = strlen(tmpdir); + buffer fnb = { NULL, 0, 0 }; + buffer_copy_path_len2(&fnb, tmpdir, tmpdirlen, + CONST_STR_LEN("lighttpd_mod_ssi.XXXXXX")); + if (fnb.ptr[tmpdirlen] == '/') ++tmpdirlen; + char * const fn = fnb.ptr; int fd = fdevent_mkostemp(fn, 0); if (fd < 0) { perror("mkstemp()"); + buffer_free_ptr(&fnb); exit(1); } if (0 != fstat(fd, &st)) { perror("fstat()"); + buffer_free_ptr(&fnb); exit(1); } unlink(fn); + buffer_free_ptr(&fnb); const char ssi_simple[] = "<!--#echo var=\"SCRIPT_NAME\" -->"; @@ -89,7 +101,10 @@ test_mod_ssi_read_fd (request_st * const r, handler_ctx * const hctx) assert(0 == memcmp(buf, "2\n", 2)); hctx->conf.ssi_exec = 0; - char fni[] = "/tmp/lighttpd_mod_ssi_inc_XXXXXX"; + buffer_copy_path_len2(&fnb, tmpdir, tmpdirlen, + CONST_STR_LEN("lighttpd_mod_ssi_inc.XXXXXX")); + char * const fni = fnb.ptr; + const size_t fnilen = buffer_clen(&fnb); int fdi = fdevent_mkostemp(fni, 0); if (fdi < 0) { perror("mkstemp()"); @@ -107,11 +122,11 @@ test_mod_ssi_read_fd (request_st * const r, handler_ctx * const hctx) 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")); + fni+tmpdirlen, fnilen-tmpdirlen, + CONST_STR_LEN("\" -->\n")); /*(step over "/tmp/")*/ buffer_append_str3(b, CONST_STR_LEN("<!--#include file=\""), - fni+5, strlen(fni)-5, /*(step over "/tmp/")*/ - CONST_STR_LEN("\" -->\n")); + fni+tmpdirlen, fnilen-tmpdirlen, + CONST_STR_LEN("\" -->\n")); /*(step over "/tmp/")*/ test_mod_ssi_write_testfile(fd, BUF_PTR_LEN(b)); buffer_free(b); test_mod_ssi_reset(r, hctx); @@ -132,6 +147,7 @@ test_mod_ssi_read_fd (request_st * const r, handler_ctx * const hctx) "ssi-include\n"))); unlink(fni); + buffer_free_ptr(&fnb); test_mod_ssi_reset(r, hctx); close(fd); diff --git a/src/t/test_mod_staticfile.c b/src/t/test_mod_staticfile.c index 1b95179f..854d7a5b 100644 --- a/src/t/test_mod_staticfile.c +++ b/src/t/test_mod_staticfile.c @@ -371,15 +371,28 @@ test_mod_staticfile_process (request_st * const r, plugin_config * const pconf) void test_mod_staticfile (void); void test_mod_staticfile (void) { - char fn[] = "/tmp/lighttpd_mod_staticfile.XXXXXX"; + const char *tmpdir = getenv("TMPDIR"); + #ifdef _WIN32 + if (NULL == tmpdir) tmpdir = getenv("TEMP"); + #endif + if (NULL == tmpdir) tmpdir = "/tmp"; + size_t tmpdirlen = strlen(tmpdir); + buffer fnb = { NULL, 0, 0 }; + buffer_copy_path_len2(&fnb, tmpdir, tmpdirlen, + CONST_STR_LEN("lighttpd_mod_staticfile.XXXXXX")); + if (fnb.ptr[tmpdirlen] == '/') ++tmpdirlen; + char * const fn = fnb.ptr; + const size_t fnlen = buffer_clen(&fnb); int fd = fdevent_mkostemp(fn, 0); if (fd < 0) { perror("mkstemp()"); + buffer_free_ptr(&fnb); exit(1); } struct stat st; if (0 != fstat(fd, &st)) { perror("fstat()"); + buffer_free_ptr(&fnb); exit(1); } @@ -399,18 +412,18 @@ void test_mod_staticfile (void) buffer_copy_string_len(&r.uri.path, CONST_STR_LEN("/")); array * const mimetypes = array_init(1); r.conf.mimetypes = mimetypes; - array_set_key_value(mimetypes, fn+sizeof(fn)-8, 7, + array_set_key_value(mimetypes, fn+fnlen-7, 7, CONST_STR_LEN("text/plain")); strftime_cache_reset(); - buffer_copy_string_len(&r.physical.path, fn, sizeof(fn)-1); + buffer_copy_string_len(&r.physical.path, fn, fnlen); test_http_response_send_file(&r, st.st_mtime); r.rqst_htags = 0; array_reset_data_strings(&r.rqst_headers); - buffer_copy_string_len(&r.physical.path, fn, sizeof(fn)-1); + buffer_copy_string_len(&r.physical.path, fn, fnlen); test_mod_staticfile_process(&r, &p->conf); array_free(mimetypes); @@ -429,4 +442,5 @@ void test_mod_staticfile (void) free(p); stat_cache_free(); unlink(fn); + buffer_free_ptr(&fnb); } |