File: basic.c

package info (click to toggle)
neon27 0.36.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,836 kB
  • sloc: ansic: 27,754; xml: 4,634; makefile: 629; sh: 328
file content (481 lines) | stat: -rw-r--r-- 13,327 bytes parent folder | download
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* 
   Tests for high-level HTTP interface (ne_basic.h)
   Copyright (C) 2002-2008, 2012, Joe Orton <joe@manyfish.co.uk>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "config.h"

#include <sys/types.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <fcntl.h>

#include "ne_basic.h"

#include "tests.h"
#include "child.h"
#include "utils.h"

static int content_type(void)
{
    int n;
    static const struct {
	const char *value, *type, *subtype, *charset;
    } ctypes[] = {
	{ "foo/bar", "foo", "bar", NULL },
	{ "foo/bar  ", "foo", "bar", NULL },
	{ "application/xml", "application", "xml", NULL },
        /* these rules previously applied RFC 3023, now updated for 7303. */
        { "text/lemon", "text", "lemon", NULL },
        { "text/xml", "text", "xml", NULL },
#undef TXU
#define TXU "text", "xml", "utf-8"
	/* 2616 doesn't *say* that charset can be quoted, but bets are
	 * that some servers do it anyway. */
	{ "text/xml; charset=utf-8", TXU },
	{ "text/xml; charset=utf-8; foo=bar", TXU },
	{ "text/xml;charset=utf-8", TXU },
	{ "text/xml ;charset=utf-8", TXU },
	{ "text/xml;charset=utf-8;foo=bar", TXU },
	{ "text/xml; foo=bar; charset=utf-8", TXU },
	{ "text/xml; foo=bar; charset=utf-8; bar=foo", TXU },
	{ "text/xml; charset=\"utf-8\"", TXU },
	{ "text/xml; charset='utf-8'", TXU },
	{ "text/xml; foo=bar; charset=\"utf-8\"; bar=foo", TXU },
#undef TXU
	/* badly quoted charset should come out as NULL */
	{ "foo/lemon; charset=\"utf-8", "foo", "lemon", NULL },
	{ NULL }
    };

    for (n = 0; ctypes[n].value != NULL; n++) {
        ne_content_type ct;
        ne_session *sess;
        ne_request *req;
        char resp[200];
        int rv;

        ct.type = ct.subtype = ct.charset = ct.value = "unset";

        ne_snprintf(resp, sizeof resp,
                    "HTTP/1.0 200 OK\r\n" "Content-Length: 0\r\n"
                    "Content-Type: %s\r\n" "\r\n", ctypes[n].value);
        
        CALL(make_session(&sess, single_serve_string, resp));

        req = ne_request_create(sess, "GET", "/anyfoo");
        ONREQ(ne_request_dispatch(req));
        rv = ne_get_content_type(req, &ct);
        
        ONV(rv == 0 && !ctypes[n].type,
            ("expected c-t parse failure for %s", ctypes[n].value));

        ONV(rv != 0 && ctypes[n].type,
            ("c-t parse failure %d for %s", rv, ctypes[n].value));

        ne_request_destroy(req);
        ne_session_destroy(sess);
        CALL(await_server());

        if (rv) continue;	

	ONV(strcmp(ct.type, ctypes[n].type),
	    ("for `%s': type was `%s'", ctypes[n].value, ct.type));

	ONV(strcmp(ct.subtype, ctypes[n].subtype),
	    ("for `%s': subtype was `%s'", ctypes[n].value, ct.subtype));

	ONV(ctypes[n].charset && ct.charset == NULL,
	    ("for `%s': charset unset", ctypes[n].value));
	
	ONV(ctypes[n].charset == NULL && ct.charset != NULL,
	    ("for `%s': unexpected charset `%s'", ctypes[n].value, 
	     ct.charset));

	ONV(ctypes[n].charset && ct.charset &&
	    strcmp(ctypes[n].charset, ct.charset),
	    ("for `%s': charset was `%s'", ctypes[n].value, ct.charset));

	ne_free(ct.value);
    }

    return OK;
}

/* Do ranged GET for range 'start' to 'end'; with 'resp' as response.
 * If 'fail' is non-NULL, expect ne_get_range to fail, and fail the
 * test with given message if it doesn't. */
static int do_range(off_t start, off_t end, const char *fail,
		    char *resp)
{
    ne_session *sess;
    ne_content_range range = {0};
    int fd, ret;

    CALL(make_session(&sess, single_serve_string, resp));
    
    range.start = start;
    range.end = end;
    
    fd = open("/dev/null", O_WRONLY);
    
    ret = ne_get_range(sess, "/foo", &range, fd);

    close(fd);
    ne_close_connection(sess);
    CALL(await_server());
    
    if (fail) {
#if 0
	t_warning("error was %s", ne_get_error(sess));
#endif
	ONV(ret == NE_OK, ("%s", fail));
    } else {
	ONREQ(ret);
    }
    
    ne_session_destroy(sess);
    return OK;
}

static int get_range(void)
{
    return do_range(1, 10, NULL,
		    "HTTP/1.1 206 Widgets\r\n" "Connection: close\r\n"
		    "Content-Range: bytes 1-10/10\r\n"
		    "Content-Length: 10\r\n\r\nabcdefghij");
}

static int get_eof_range(void)
{
    return do_range(1, -1, NULL,
		    "HTTP/1.1 206 Widgets\r\n" "Connection: close\r\n"
		    "Content-Range: bytes 1-10/10\r\n"
		    "Content-Length: 10\r\n\r\nabcdefghij");
}

static int fail_range_length(void)
{
    return do_range(1, 10, "range response length mismatch should fail",
		    "HTTP/1.1 206 Widgets\r\n" "Connection: close\r\n"
		    "Content-Range: bytes 1-2/2\r\n"
		    "Content-Length: 2\r\n\r\nab");
}

static int fail_range_units(void)
{
    return do_range(1, 2, "range response units check should fail",
		    "HTTP/1.1 206 Widgets\r\n" "Connection: close\r\n"
		    "Content-Range: fish 1-2/2\r\n"
		    "Content-Length: 2\r\n\r\nab");
}

static int fail_range_notrange(void)
{
    return do_range(1, 2, "non-ranged response should fail",
		    "HTTP/1.1 200 Widgets\r\n" "Connection: close\r\n"
		    "Content-Range: bytes 1-2/2\r\n"
		    "Content-Length: 2\r\n\r\nab");
}

static int fail_range_unsatify(void)
{
    return do_range(1, 2, "unsatisfiable range should fail",
		    "HTTP/1.1 416 No Go\r\n" "Connection: close\r\n"
		    "Content-Length: 2\r\n\r\nab");
}

static int dav_capabilities(void)
{
    static const struct {
	const char *hdrs;
	unsigned int class1, class2, exec;
    } caps[] = {
	{ "DAV: 1,2\r\n", 1, 1, 0 },
	{ "DAV: 1 2\r\n", 0, 0, 0 },
	/* these aren't strictly legal DAV: headers: */
	{ "DAV: 2,1\r\n", 1, 1, 0 },
	{ "DAV:  1, 2  \r\n", 1, 1, 0 },
	{ "DAV: 1\r\nDAV:2\r\n", 1, 1, 0 },
	{ NULL, 0, 0, 0 }
    };
    char resp[BUFSIZ];
    int n;

    for (n = 0; caps[n].hdrs != NULL; n++) {
	ne_server_capabilities c = {0};
	ne_session *sess;

	ne_snprintf(resp, BUFSIZ, "HTTP/1.0 200 OK\r\n"
		    "Connection: close\r\n"
		    "%s" "\r\n", caps[n].hdrs);

	CALL(make_session(&sess, single_serve_string, resp));

	ONREQ(ne_options(sess, "/foo", &c));

	ONV(c.dav_class1 != caps[n].class1,
	    ("class1 was %d not %d", c.dav_class1, caps[n].class1));
	ONV(c.dav_class2 != caps[n].class2,
	    ("class2 was %d not %d", c.dav_class2, caps[n].class2));
	ONV(c.dav_executable != caps[n].exec,
	    ("class2 was %d not %d", c.dav_executable, caps[n].exec));

	CALL(destroy_and_wait(sess));
    }

    return OK;	
}

static int get(void)
{
    ne_session *sess;
    int fd;
    
    CALL(make_session(&sess, single_serve_string, 
                      "HTTP/1.0 200 OK\r\n"
                      "Content-Length: 5\r\n"
                      "\r\n"
                      "abcde"));
    
    fd = open("/dev/null", O_WRONLY);
    ONREQ(ne_get(sess, "/getit", fd));
    close(fd);

    return destroy_and_wait(sess);
}


#define CHUNK(len, data) #len "\r\n" data "\r\n"

#define ABCDE_CHUNKS CHUNK(1, "a") CHUNK(1, "b") \
 CHUNK(1, "c") CHUNK(1, "d") \
 CHUNK(1, "e") CHUNK(0, "")

static int getbuf(void)
{
    ne_session *sess;
    char buffer[16];
    size_t buflen = sizeof buffer;

    CALL(make_session(&sess, single_serve_string,
                      "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
                      "Transfer-Encoding: chunked\r\n"
                      "\r\n"
                      ABCDE_CHUNKS));

    ONREQ(ne_getbuf(sess, "/", buffer, &buflen));

    ONV(buflen != 5, ("unexpected buffer length %" NE_FMT_SIZE_T, buflen));

    ONV(memcmp("abcde", buffer, 5) != 0,
        ("mismatched chunked response: [%5s]", buffer));

    return destroy_and_wait(sess);
}

static int getbuf_fail(void)
{
    ne_session *sess;
    char buffer[16];
    size_t buflen = sizeof buffer;
    int ret;

    CALL(make_session(&sess, single_serve_string,
                      "HTTP/1.1 302 OK\r\n" "Server: neon-test-server\r\n"
                      "Transfer-Encoding: chunked\r\n"
                      "\r\n"
                      ABCDE_CHUNKS));

    ret = ne_getbuf(sess, "/", buffer, &buflen);
    ONN("success for non-2xx response", ret != NE_ERROR);

    ONV(buflen != sizeof buffer,
        ("buffer length adjusted to %" NE_FMT_SIZE_T, buflen));

    return destroy_and_wait(sess);
}

static int getbuf_fill(void)
{
    ne_session *sess;
    char buffer[5];
    size_t buflen = sizeof buffer;

    CALL(make_session(&sess, single_serve_string,
                      "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
                      "Transfer-Encoding: chunked\r\n"
                      "\r\n"
                      ABCDE_CHUNKS));

    ONREQ(ne_getbuf(sess, "/", buffer, &buflen));

    ONV(buflen != 5, ("unexpected buffer length %" NE_FMT_SIZE_T, buflen));

    ONV(memcmp("abcde", buffer, 5) != 0,
        ("mismatched chunked response: [%5s]", buffer));

    return destroy_and_wait(sess);
}

static int getbuf2(void)
{
    ne_session *sess;
    char buf;
    size_t buflen = sizeof buf;
    int ret;

    CALL(make_session(&sess, single_serve_string,
                      "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
                      "Content-Length: 5\r\n"
                      "\r\n"
                      "abcde"
                      "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
                      "Content-Length: 5\r\n"
                      "\r\n"
                      "abcde"));

    ret = ne_getbuf(sess, "/", &buf, &buflen);
    ONV(ret != NE_FAILED, ("overflow case gave %d not FAILED", ret));
    ONV(buflen != 1, ("buffer length returned as %" NE_FMT_SIZE_T, buflen));

    ret = any_request(sess, "/closeme");
    ONN("failed to close connection", ret != NE_CONNECT);

    return destroy_and_wait(sess);
}

static int post_getbuf_retry(ne_request *req, void *userdata, 
                           const ne_status *status)
{
    return status->code == 401 ? NE_RETRY : NE_OK;    
}

static int getbuf_retry(void)
{
    ne_session *sess;
    char buf[6] = "00000";
    size_t buflen = sizeof buf;
    int ret;

    CALL(make_session(&sess, single_serve_string,
                      "HTTP/1.1 401 Retry Please\r\n" "Server: neon-test-server\r\n"
                      "Content-Length: 8\r\n"
                      "\r\n"
                      "zzzzzzzz"
                      "HTTP/1.1 200 OK\r\n" "Server: neon-test-server\r\n"
                      "Content-Length: 1\r\n"
                      "\r\n"
                      "a"));

    ne_hook_post_send(sess, post_getbuf_retry, NULL);
    
    ONREQ(ne_getbuf(sess, "/", buf, &buflen));
    ONV(ret != NE_OK, ("overflow case gave %d not FAILED", ret));
    ONV(buflen != 1, ("buffer length returned as %" NE_FMT_SIZE_T, buflen));

    ret = any_request(sess, "/closeme");
    ONN("failed to close connection", ret != NE_CONNECT);

    return destroy_and_wait(sess);
}

#define CLASS_12 (NE_CAP_DAV_CLASS1 | NE_CAP_DAV_CLASS2)

static int options2(void)
{
    static const struct {
        const char *hdrs;
        unsigned int caps;
    } ts[] = {
        { "1,2\r\n", CLASS_12 },
        { "1 2\r\n", 0 },
        /* these aren't strictly legal headers: */
        { "2,1\r\n", CLASS_12 },
        { " 1, 2  \r\n", CLASS_12 },
        { "1\r\nDAV:2\r\n", CLASS_12 },
        /* extended types */
        { "1, 2, extended-mkcol", CLASS_12 | NE_CAP_EXT_MKCOL },
        { NULL, 0 }
    };
    char resp[BUFSIZ];
    int n;

    for (n = 0; ts[n].hdrs != NULL; n++) {
        ne_session *sess;
        unsigned int caps;

        ne_snprintf(resp, BUFSIZ, "HTTP/1.0 200 OK\r\n"
                    "Connection: close\r\n"
                    "Content-Length: 0\r\n"
                    "DAV: %s" "\r\n\r\n", ts[n].hdrs);

        CALL(make_session(&sess, single_serve_string, resp));

        ONREQ(ne_options2(sess, "/foo", &caps));

        ONV(caps != ts[n].caps,
            ("capabilities for 'DAV: %s' were 0x%x, expected 0x%x", 
             ts[n].hdrs, caps, ts[n].caps));

        CALL(destroy_and_wait(sess));
    }

    return OK;  
}

static int put(void)
{
    ne_session *sess;
    
    CALL(make_session(&sess, single_serve_string, "HTTP/1.1 204 OK\r\n"
                      "Content-Length: 200\r\n"
                      "\r\n"));
    
    ONREQ(ne_putbuf(sess, "/foo", "foobar", 6));

    return destroy_and_wait(sess);
}

ne_test tests[] = {
    T(lookup_localhost),
    T(content_type),
    T(get_range),
    T(get_eof_range),
    T(fail_range_length),
    T(fail_range_units),
    T(fail_range_notrange),
    T(fail_range_unsatify),
    T(dav_capabilities),
    T(get),
    T(getbuf),
    T(getbuf_fail),
    T(getbuf_fill),
    T(getbuf2),
    T(getbuf_retry),
    T(options2),
    T(put),
    T(NULL) 
};