summaryrefslogtreecommitdiff
path: root/src/t/test_mod_staticfile.c
blob: 1b95179fd6b44b93f7617e7654299b53854d7e41 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "first.h"

#undef NDEBUG
#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "mod_staticfile.c"
#include "fdlog.h"
#include "http_date.h"
#include "http_etag.h"
#include "http_header.h"

__attribute_noinline__
static void test_mod_staticfile_reset (request_st * const r)
{
    r->http_status = 0;
    r->resp_htags = 0;
    array_reset_data_strings(&r->resp_headers);
    http_response_body_clear(r, 0);
    r->conf.etag_flags = ETAG_USE_INODE | ETAG_USE_MTIME | ETAG_USE_SIZE;
}

__attribute_noinline__
static void
run_http_response_send_file (request_st * const r, int line, int status, const char *desc)
{
    http_response_send_file(r, &r->physical.path, NULL);
    if (r->http_status != status) {
        fprintf(stderr,
                "%s.%d: %s() failed: expected '%d', got '%d' for test %s\n",
                __FILE__, line, "http_response_send_file", status,
                r->http_status, desc);
        fflush(stderr);
        abort();
    }
}

static void
test_http_response_send_file (request_st * const r, time_t lmtime)
{
    test_mod_staticfile_reset(r);
    const buffer *vb;

    /*(mismatch test must be first, else stat_cache will have cached mimetype)*/
    array * const mimetypes_empty = array_init(0);
    const array * const mimetypes_orig = r->conf.mimetypes;
    r->conf.mimetypes = mimetypes_empty;
    run_http_response_send_file(r, __LINE__, 200,
      "basic static file (w/o mimetype match)");
    vb = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE,
                                  CONST_STR_LEN("Content-Type"));
    assert(vb && buffer_eq_slen(vb, CONST_STR_LEN("application/octet-stream")));
    test_mod_staticfile_reset(r);
    r->conf.mimetypes = mimetypes_orig;
    array_free(mimetypes_empty);

    run_http_response_send_file(r, __LINE__, 200,
      "basic static file (w/ mimetype match)");
    vb = http_header_response_get(r, HTTP_HEADER_CONTENT_TYPE,
                                  CONST_STR_LEN("Content-Type"));
    assert(vb && buffer_eq_slen(vb, CONST_STR_LEN("text/plain")));
    vb = http_header_response_get(r, HTTP_HEADER_ETAG,
                                  CONST_STR_LEN("ETag"));
    assert(vb && vb->ptr[0] == '"' && vb->ptr[buffer_clen(vb)-1] == '"');
    vb = http_header_response_get(r, HTTP_HEADER_LAST_MODIFIED,
                                  CONST_STR_LEN("Last-Modified"));
    assert(vb);
    test_mod_staticfile_reset(r);

    const uint32_t plen = buffer_clen(&r->physical.path);
    buffer_append_string_len(&r->physical.path, CONST_STR_LEN("-nonexistent"));
    run_http_response_send_file(r, __LINE__, 404,
      "non-existent file");
    test_mod_staticfile_reset(r);
    buffer_truncate(&r->physical.path, plen);

    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            CONST_STR_LEN(""));
    run_http_response_send_file(r, __LINE__, 200,
      "if-modified-since invalid (empty)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            CONST_STR_LEN("foobar"));
    run_http_response_send_file(r, __LINE__, 200,
      "if-modified-since invalid (not time string)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            CONST_STR_LEN("this string is too long to be a valid timestamp"));
    run_http_response_send_file(r, __LINE__, 200,
      "if-modified-since invalid (too long to be valid time string)");
    test_mod_staticfile_reset(r);

    char lmtime_str[HTTP_DATE_SZ];
    uint32_t lmtime_len;

    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),
                                       lmtime ? lmtime-1 : lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 200,
      "if-modified-since older than st_mtime");
    test_mod_staticfile_reset(r);

    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 304,
      "if-modified-since matches st_mtime");
    test_mod_staticfile_reset(r);

    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),lmtime+1);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 304,
      "if-modified-since newer than st_mtime");
    test_mod_staticfile_reset(r);

  #ifdef __COVERITY__ /* Coverity misses that this is set a few lines above */
    force_assert(http_header_request_get(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                 CONST_STR_LEN("If-Modified-Since")));
  #endif
    buffer_append_string_len(
      http_header_request_get(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                              CONST_STR_LEN("If-Modified-Since")),
      CONST_STR_LEN("; foo"));
    run_http_response_send_file(r, __LINE__, 200,
      "if-modified-since newer but overload (invalid)");
    test_mod_staticfile_reset(r);

    http_header_request_unset(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                              CONST_STR_LEN("If-Modified-Since"));

    buffer *etag = buffer_init();
    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_STR_LEN("foo"));
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag mismatch)");
    vb = http_header_response_get(r, HTTP_HEADER_ETAG,
                                  CONST_STR_LEN("ETag"));
    assert(vb);
    buffer_copy_buffer(etag, vb);
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_BUF_LEN(etag));
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag match)");
    test_mod_staticfile_reset(r);

    r->conf.etag_flags = 0;
    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_BUF_LEN(etag));
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag would match, but etags disabled in config)");
    test_mod_staticfile_reset(r);
    r->conf.etag_flags = ETAG_USE_INODE | ETAG_USE_MTIME | ETAG_USE_SIZE;

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_BUF_LEN(etag));
    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),
                                       lmtime ? lmtime-1 : lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag match), "
      "if-modified-since (old) (should be ignored)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_BUF_LEN(etag));
    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag match), "
      "if-modified-since (now) (should be ignored)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_BUF_LEN(etag));
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            CONST_STR_LEN("Sun, 01 Jan 1970 00:00:01 GMT foo"));
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag match), "
      "if-modified-since (overlong; invalid) (should be ignored)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_STR_LEN("foo"));
    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),
                                       lmtime ? lmtime-1 : lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag mismatch), "
      "if-modified-since (old) (should be ignored)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_STR_LEN("foo"));
    lmtime_len = http_date_time_to_str(lmtime_str,sizeof(lmtime_str),lmtime);
    http_header_request_set(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                            CONST_STR_LEN("If-Modified-Since"),
                            lmtime_str, lmtime_len);
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag mismatch), "
      "if-modified-since (now) (should be ignored)");
    test_mod_staticfile_reset(r);

    http_header_request_unset(r, HTTP_HEADER_IF_MODIFIED_SINCE,
                              CONST_STR_LEN("If-Modified-Since"));

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            etag->ptr, buffer_clen(etag)-1);
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag invalid; mismatched quotes)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            etag->ptr+1, buffer_clen(etag)-2);
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag invalid; no quotes)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_STR_LEN("*"));
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag * (unquoted) matches any ETag)");
    test_mod_staticfile_reset(r);

    http_header_request_set(r, HTTP_HEADER_IF_NONE_MATCH,
                            CONST_STR_LEN("If-None-Match"),
                            CONST_STR_LEN("\"*\""));
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (etag \"*\" (quoted) is a regular ETag)");
    test_mod_staticfile_reset(r);

    buffer * const rqst_etag =
      http_header_request_set_ptr(r, HTTP_HEADER_IF_NONE_MATCH,
                                  CONST_STR_LEN("If-None-Match"));

    buffer_copy_string_len(rqst_etag, CONST_STR_LEN("W/"));
    buffer_append_buffer(rqst_etag, etag);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (weak etag) matches like ETag for GET and HEAD)");
    test_mod_staticfile_reset(r);

    /*(200 expected here instead of 206 since Range is handled later)*/
    http_header_request_set(r, HTTP_HEADER_RANGE,
                            CONST_STR_LEN("Range"),
                            CONST_STR_LEN("bytes=0-0"));
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (weak etag) does not match for Range request)");
    test_mod_staticfile_reset(r);
    http_header_request_unset(r, HTTP_HEADER_RANGE, CONST_STR_LEN("Range"));

    buffer_copy_string_len(rqst_etag, CONST_STR_LEN("W/\"12345\""));
    run_http_response_send_file(r, __LINE__, 200,
      "if-none-match (weak etag no match)");
    test_mod_staticfile_reset(r);

    buffer_append_string_len(rqst_etag, CONST_STR_LEN(", "));
    buffer_append_buffer(rqst_etag, etag);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag list, second etag matches)");
    test_mod_staticfile_reset(r);

    buffer_append_string_len(rqst_etag, CONST_STR_LEN(", W/"));
    buffer_append_buffer(rqst_etag, etag);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag list, second etag matches weakly)");
    test_mod_staticfile_reset(r);

    buffer_copy_string_len(rqst_etag, CONST_STR_LEN("\"12345\",, ,,  ,  "));
    buffer_append_buffer(rqst_etag, etag);
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag list non-normalized, ending with etag match)");
    test_mod_staticfile_reset(r);

    buffer_copy_string_len(rqst_etag, CONST_STR_LEN("\"1234\", "));
    buffer_append_buffer(rqst_etag, etag);
    buffer_append_string_len(rqst_etag, CONST_STR_LEN(", \"brokentrailing"));
    run_http_response_send_file(r, __LINE__, 304,
      "if-none-match (etag list with etag match then invalid trailing data)");
    test_mod_staticfile_reset(r);

    http_header_request_unset(r, HTTP_HEADER_IF_NONE_MATCH,
                              CONST_STR_LEN("If-None-Match"));

    buffer_free(etag);
}

__attribute_noinline__
static void
run_mod_staticfile_process (request_st * const r, plugin_config * const pconf, int line, int status, const char *desc)
{
    handler_t rc = mod_staticfile_process(r, pconf);
    if (r->http_status != status
        || rc != (status ? HANDLER_FINISHED : HANDLER_GO_ON)) {
        fprintf(stderr,
                "%s.%d: %s() failed: expected '%d', got '%d' for test %s\n",
                __FILE__, line, "mod_staticfile_process", status,
                r->http_status, desc);
        fflush(stderr);
        abort();
    }
}

static void
test_mod_staticfile_process (request_st * const r, plugin_config * const pconf)
{
    test_mod_staticfile_reset(r);

    pconf->disable_pathinfo = 0;
    buffer_copy_string_len(&r->pathinfo, CONST_STR_LEN("/pathinfo"));
    run_mod_staticfile_process(r, pconf, __LINE__, 200,
      "pathinfo allowed and present");
    test_mod_staticfile_reset(r);
    pconf->disable_pathinfo = 1;
    run_mod_staticfile_process(r, pconf, __LINE__, 0,
      "pathinfo denied and present");
    test_mod_staticfile_reset(r);
    buffer_clear(&r->pathinfo);
    run_mod_staticfile_process(r, pconf, __LINE__, 200,
      "pathinfo denied and not present");
    test_mod_staticfile_reset(r);
    pconf->disable_pathinfo = 0;

    array * const a = array_init(1);
    array_insert_value(a, CONST_STR_LEN(".exe"));
    pconf->exclude_ext = a;
    run_mod_staticfile_process(r, pconf, __LINE__, 200,
      "extension disallowed (no match)");
    test_mod_staticfile_reset(r);
    buffer_append_string_len(&r->physical.path, CONST_STR_LEN(".exe"));
    run_mod_staticfile_process(r, pconf, __LINE__, 0,
      "extension disallowed (match)");
    test_mod_staticfile_reset(r);
    pconf->exclude_ext = NULL;
    array_free(a);
}

#include "sys-unistd.h" /* unlink() */
#include "fdevent.h"

void test_mod_staticfile (void);
void test_mod_staticfile (void)
{
    char fn[] = "/tmp/lighttpd_mod_staticfile.XXXXXX";
    int fd = fdevent_mkostemp(fn, 0);
    if (fd < 0) {
        perror("mkstemp()");
        exit(1);
    }
    struct stat st;
    if (0 != fstat(fd, &st)) {
        perror("fstat()");
        exit(1);
    }

    plugin_data * const p = mod_staticfile_init();
    assert(NULL != p);
    p->conf.etags_used = 1;

    request_st r;

    memset(&r, 0, sizeof(request_st));
    r.http_method            = HTTP_METHOD_GET;
    r.http_version           = HTTP_VERSION_1_1;
    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;
    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,
                                   CONST_STR_LEN("text/plain"));

    strftime_cache_reset();

    buffer_copy_string_len(&r.physical.path, fn, sizeof(fn)-1);
    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);
    test_mod_staticfile_process(&r, &p->conf);

    array_free(mimetypes);
    fdlog_free(r.conf.errh);
    buffer_free(r.tmp_buf);
    chunkqueue_reset(&r.write_queue);

    free(r.uri.path.ptr);
    free(r.pathinfo.ptr);
    free(r.physical.path.ptr);
    free(r.physical.rel_path.ptr);
    free(r.physical.doc_root.ptr);
    array_free_data(&r.rqst_headers);
    array_free_data(&r.resp_headers);

    free(p);
    stat_cache_free();
    unlink(fn);
}