summaryrefslogtreecommitdiff
path: root/src/t/test_mod_ssi.c
blob: a3f2dc7929e7583e047c65da1a3e50bac369d135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "first.h"

#undef NDEBUG
#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "sys-unistd.h" /* unlink() */

#include "mod_ssi.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)
{
    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;
  #ifdef _WIN32
    else if (fnb.ptr[tmpdirlen] == '\\') ++tmpdirlen;
  #endif
    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);
    }

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

  #ifndef _WIN32 /* TODO: command for cmd.exe */
    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;
  #endif

    buffer fnib = { NULL, 0, 0 };
    buffer_copy_path_len2(&fnib, tmpdir, strlen(tmpdir),
                          CONST_STR_LEN("lighttpd_mod_ssi_inc.XXXXXX"));
    char * const fni = fnib.ptr;
    const size_t fnilen = buffer_clen(&fnib);
    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+tmpdirlen, fnilen-tmpdirlen,
                          CONST_STR_LEN("\" -->\n")); /*(step over "/tmp/")*/
    buffer_append_str3(b, CONST_STR_LEN("<!--#include file=\""),
                          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);
    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, tmpdir, strlen(tmpdir));
    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_path_len2(&r->physical.path, tmpdir, strlen(tmpdir),
                          CONST_STR_LEN("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);
    buffer_free_ptr(&fnib);

    test_mod_ssi_reset(r, hctx);
    close(fd);
    unlink(fn);
    buffer_free_ptr(&fnb);
}

void test_mod_ssi (void);
void test_mod_ssi (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) */
    r.conf.follow_symlink    = 1;

    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.path.ptr);
    free(r.physical.rel_path.ptr);
    free(r.physical.doc_root.ptr);

    mod_ssi_free(p);
    free(p);
    stat_cache_free();
}