~ubuntu-branches/ubuntu/trusty/serf/trusty-security

« back to all changes in this revision

Viewing changes to test/test_buckets.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Samuelson
  • Date: 2011-06-27 18:09:28 UTC
  • mfrom: (1.2.5 upstream)
  • mto: (3.3.1 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20110627180928-ybwzd3hmx82nu3ir
Tags: 1.0.0~0+svn1514-1
* New upstream snapshot.
  - patches/abi-0.x: Remove as obsolete.
  - patches/kqueue: Forward-port.
  - Bump ABI: libserf0.7{,-dbg} -> libserf1{,-dbg}
  - patches/ip6-localhost: New patch: temporary (I hope) workaround for
    IPv4 / IPv6 confusion in testsuite.
* Implement Multi-Arch: same.
* libserf-dev Conflicts: libserf-0-0-dev, not Breaks.  Thanks, lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
static void test_simple_bucket_readline(CuTest *tc)
26
26
{
27
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
28
 
                                                              NULL);
29
27
    apr_status_t status;
30
28
    serf_bucket_t *bkt;
31
29
    const char *data;
32
30
    int found;
33
31
    apr_size_t len;
34
32
 
 
33
    apr_pool_t *test_pool = test_setup();
 
34
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
35
                                                              NULL);
 
36
 
35
37
    bkt = SERF_BUCKET_SIMPLE_STRING(
36
38
        "line1" CRLF
37
39
        "line2",
56
58
    CuAssertIntEquals(tc, SERF_NEWLINE_NONE, found);
57
59
    CuAssertIntEquals(tc, 5, len);
58
60
    CuAssert(tc, data, strncmp("line2", data, len) == 0);
 
61
    test_teardown(test_pool);
59
62
}
60
63
 
61
64
/* Reads bucket until EOF found and compares read data with zero terminated
85
88
 
86
89
static void test_response_bucket_read(CuTest *tc)
87
90
{
88
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
89
 
                                                              NULL);
90
91
    serf_bucket_t *bkt, *tmp;
91
92
 
 
93
    apr_pool_t *test_pool = test_setup();
 
94
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
95
                                                              NULL);
 
96
 
92
97
    tmp = SERF_BUCKET_SIMPLE_STRING(
93
98
        "HTTP/1.1 200 OK" CRLF
94
99
        "Content-Length: 7" CRLF
100
105
 
101
106
    /* Read all bucket and check it content. */
102
107
    read_and_check_bucket(tc, bkt, "abc1234");
 
108
    test_teardown(test_pool);
103
109
}
104
110
 
105
111
static void test_response_bucket_headers(CuTest *tc)
106
112
{
107
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
108
 
                                                              NULL);
109
113
    serf_bucket_t *bkt, *tmp, *hdr;
110
114
 
 
115
    apr_pool_t *test_pool = test_setup();
 
116
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
117
                                                              NULL);
 
118
 
111
119
    tmp = SERF_BUCKET_SIMPLE_STRING(
112
120
        "HTTP/1.1 405 Method Not Allowed" CRLF
113
121
        "Date: Sat, 12 Jun 2010 14:17:10 GMT"  CRLF
135
143
    CuAssertStrEquals(tc,
136
144
        "",
137
145
        serf_bucket_headers_get(hdr, "NoSpace"));
 
146
    test_teardown(test_pool);
138
147
}
139
148
 
140
149
static void test_response_bucket_chunked_read(CuTest *tc)
141
150
{
142
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
143
 
                                                              NULL);
144
151
    serf_bucket_t *bkt, *tmp, *hdrs;
145
152
 
 
153
    apr_pool_t *test_pool = test_setup();
 
154
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
155
                                                              NULL);
 
156
 
146
157
    tmp = SERF_BUCKET_SIMPLE_STRING(
147
158
        "HTTP/1.1 200 OK" CRLF
148
159
        "Transfer-Encoding: chunked" CRLF
166
177
 
167
178
    /* Check that trailing headers parsed correctly. */
168
179
    CuAssertStrEquals(tc, "value", serf_bucket_headers_get(hdrs, "Footer"));
 
180
    test_teardown(test_pool);
169
181
}
170
182
 
171
183
static void test_bucket_header_set(CuTest *tc)
172
184
{
 
185
    apr_pool_t *test_pool = test_setup();
173
186
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
174
187
                                                              NULL);
175
188
    serf_bucket_t *hdrs = serf_bucket_headers_create(alloc);
190
203
 
191
204
    // headers are case insensitive.
192
205
    CuAssertStrEquals(tc, "bar,baz,test", serf_bucket_headers_get(hdrs, "fOo"));
193
 
}
194
 
 
195
 
static void test_simple_read_restore_snapshot_read(CuTest *tc)
196
 
{
197
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
198
 
                                                              NULL);
199
 
    apr_status_t status;
200
 
    serf_bucket_t *bkt;
201
 
    const char *data;
202
 
    int found;
203
 
    apr_size_t len;
204
 
 
205
 
    bkt = SERF_BUCKET_SIMPLE_STRING(
206
 
        "line1" CRLF
207
 
        "line2",
208
 
        alloc);
209
 
 
210
 
    /* Take snapshot. */
211
 
    status = serf_bucket_snapshot(bkt);
212
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
213
 
 
214
 
    /* Read the first line of text from the bucket. */
215
 
    status = serf_bucket_readline(bkt, SERF_NEWLINE_CRLF, &found, &data, &len);
216
 
 
217
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
218
 
    CuAssertIntEquals(tc, SERF_NEWLINE_CRLF, found);
219
 
    CuAssertIntEquals(tc, 7, len);
220
 
    CuAssert(tc, data, strncmp("line1" CRLF, data, len) == 0);
221
 
 
222
 
    /* Restore the buckets original content. */
223
 
    CuAssertTrue(tc, serf_bucket_is_snapshot_set(bkt) != 0);
224
 
    status = serf_bucket_restore_snapshot(bkt);
225
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
226
 
    CuAssertTrue(tc, serf_bucket_is_snapshot_set(bkt) == 0);
227
 
 
228
 
    /* Now read both lines from the bucket. */
229
 
    status = serf_bucket_readline(bkt, SERF_NEWLINE_CRLF, &found, &data, &len);
230
 
 
231
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
232
 
    CuAssertIntEquals(tc, SERF_NEWLINE_CRLF, found);
233
 
    CuAssertIntEquals(tc, 7, len);
234
 
    CuAssert(tc, data, strncmp("line1" CRLF, data, len) == 0);
235
 
 
236
 
    status = serf_bucket_readline(bkt, SERF_NEWLINE_CRLF, &found, &data, &len);
237
 
 
238
 
    CuAssertIntEquals(tc, APR_EOF, status);
239
 
    CuAssertIntEquals(tc, SERF_NEWLINE_NONE, found);
240
 
    CuAssertIntEquals(tc, 5, len);
241
 
    CuAssert(tc, data, strncmp("line2", data, len) == 0);
 
206
    test_teardown(test_pool);
242
207
}
243
208
 
244
209
static apr_status_t read_requested_bytes(serf_bucket_t *bkt,
266
231
    return status;
267
232
}
268
233
 
269
 
static void test_aggregate_read_restore_snapshot_read(CuTest *tc)
 
234
 
 
235
static void test_iovec_buckets(CuTest *tc)
270
236
{
271
 
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
272
 
                                                              NULL);
273
237
    apr_status_t status;
274
 
    serf_bucket_t *bkt;
275
 
    serf_bucket_t *tmp;
 
238
    serf_bucket_t *bkt, *iobkt;
276
239
    const char *data;
277
240
    apr_size_t len;
278
 
 
279
 
    bkt = serf_bucket_aggregate_create(alloc);
280
 
 
281
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN("<", 1, alloc);
282
 
    serf_bucket_aggregate_append(bkt, tmp);
283
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN("tagname", 7, alloc);
284
 
    serf_bucket_aggregate_append(bkt, tmp);
285
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN(">", 1, alloc);
286
 
    serf_bucket_aggregate_append(bkt, tmp);
287
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN("value", 5, alloc);
288
 
    serf_bucket_aggregate_append(bkt, tmp);
289
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN("</", 2, alloc);
290
 
    serf_bucket_aggregate_append(bkt, tmp);
291
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN("tagname", 7, alloc);
292
 
    serf_bucket_aggregate_append(bkt, tmp);
293
 
    tmp = SERF_BUCKET_SIMPLE_STRING_LEN(">", 1, alloc);
294
 
    serf_bucket_aggregate_append(bkt, tmp);
295
 
 
296
 
    /* Take snapshot. */
297
 
    status = serf_bucket_snapshot(bkt);
298
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
299
 
 
300
 
    status = read_requested_bytes(bkt, 9, &data, &len, test_pool);
301
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
302
 
    CuAssertIntEquals(tc, 9, len);
303
 
    CuAssert(tc, data, strncmp("<tagname>", data, len) == 0);
304
 
 
305
 
    CuAssertTrue(tc, serf_bucket_is_snapshot_set(bkt) != 0);
306
 
    status = serf_bucket_restore_snapshot(bkt);
307
 
    CuAssertIntEquals(tc, APR_SUCCESS, status);
308
 
    CuAssertTrue(tc, serf_bucket_is_snapshot_set(bkt) == 0);
309
 
 
310
 
    /* Ask more bytes than expected, just to make sure that we get 
311
 
       everything. */
312
 
    status = read_requested_bytes(bkt, 50, &data, &len, test_pool);
313
 
    CuAssertIntEquals(tc, APR_EOF, status);
314
 
    CuAssertIntEquals(tc, 24, len);
315
 
    CuAssert(tc, data, strncmp("<tagname>value</tagname>", data, len) == 0);
 
241
    struct iovec vecs[32];
 
242
    struct iovec tgt_vecs[32];
 
243
    int i;
 
244
    int vecs_used;
 
245
 
 
246
    apr_pool_t *test_pool = test_setup();
 
247
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
248
                                                              NULL);
 
249
 
 
250
    /* Test 1: Read a single string in an iovec, store it in a iovec_bucket
 
251
       and then read it back. */
 
252
    bkt = SERF_BUCKET_SIMPLE_STRING(
 
253
        "line1" CRLF
 
254
        "line2",
 
255
        alloc);
 
256
 
 
257
    status = serf_bucket_read_iovec(bkt, SERF_READ_ALL_AVAIL, 32, vecs,
 
258
                                    &vecs_used);
 
259
 
 
260
    iobkt = serf_bucket_iovec_create(vecs, vecs_used, alloc);
 
261
 
 
262
    /* Check available data */
 
263
    status = serf_bucket_peek(iobkt, &data, &len);
 
264
    CuAssertIntEquals(tc, APR_EOF, status);
 
265
    CuAssertIntEquals(tc, strlen("line1" CRLF "line2"), len);
 
266
 
 
267
    /* Try to read only a few bytes (less than what's in the first buffer). */
 
268
    status = serf_bucket_read_iovec(iobkt, 3, 32, tgt_vecs, &vecs_used);
 
269
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
270
    CuAssertIntEquals(tc, 1, vecs_used);
 
271
    CuAssertIntEquals(tc, 3, tgt_vecs[0].iov_len);
 
272
    CuAssert(tc, tgt_vecs[0].iov_base,
 
273
             strncmp("lin", tgt_vecs[0].iov_base, tgt_vecs[0].iov_len) == 0);
 
274
 
 
275
    /* Read the rest of the data. */
 
276
    status = serf_bucket_read_iovec(iobkt, SERF_READ_ALL_AVAIL, 32, tgt_vecs,
 
277
                                    &vecs_used);
 
278
    CuAssertIntEquals(tc, APR_EOF, status);
 
279
    CuAssertIntEquals(tc, 1, vecs_used);
 
280
    CuAssertIntEquals(tc, strlen("e1" CRLF "line2"), tgt_vecs[0].iov_len);
 
281
    CuAssert(tc, tgt_vecs[0].iov_base,
 
282
             strncmp("e1" CRLF "line2", tgt_vecs[0].iov_base, tgt_vecs[0].iov_len - 3) == 0);
 
283
 
 
284
    /* Bucket should now be empty */
 
285
    status = serf_bucket_peek(iobkt, &data, &len);
 
286
    CuAssertIntEquals(tc, APR_EOF, status);
 
287
    CuAssertIntEquals(tc, 0, len);
 
288
 
 
289
    /* Test 2: Read multiple character bufs in an iovec, then read them back
 
290
       in bursts. */
 
291
    for (i = 0; i < 32 ; i++) {
 
292
        vecs[i].iov_base = apr_psprintf(test_pool, "data %02d 901234567890", i);
 
293
        vecs[i].iov_len = strlen(vecs[i].iov_base);
 
294
    }
 
295
 
 
296
    iobkt = serf_bucket_iovec_create(vecs, 32, alloc);
 
297
 
 
298
    /* Check that some data is in the buffer. Don't verify the actual data, the
 
299
       amount of data returned is not guaranteed to be the full buffer. */
 
300
    status = serf_bucket_peek(iobkt, &data, &len);
 
301
    CuAssertTrue(tc, len > 0);
 
302
    CuAssertIntEquals(tc, APR_SUCCESS, status); /* this assumes not all data is
 
303
                                                   returned at once,
 
304
                                                   not guaranteed! */
 
305
 
 
306
    /* Read 1 buf.   20 = sizeof("data %2d 901234567890") */
 
307
    status = serf_bucket_read_iovec(iobkt, 1 * 20, 32,
 
308
                                    tgt_vecs, &vecs_used);
 
309
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
310
    CuAssertIntEquals(tc, 1, vecs_used);
 
311
    CuAssert(tc, tgt_vecs[0].iov_base,
 
312
             strncmp("data 00 901234567890", tgt_vecs[0].iov_base, tgt_vecs[0].iov_len) == 0);
 
313
 
 
314
    /* Read 2 bufs. */
 
315
    status = serf_bucket_read_iovec(iobkt, 2 * 20, 32,
 
316
                                    tgt_vecs, &vecs_used);
 
317
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
318
    CuAssertIntEquals(tc, 2, vecs_used);
 
319
 
 
320
    /* Read the remaining 29 bufs. */
 
321
    vecs_used = 400;  /* test if iovec code correctly resets vecs_used */
 
322
    status = serf_bucket_read_iovec(iobkt, SERF_READ_ALL_AVAIL, 32,
 
323
                                    tgt_vecs, &vecs_used);
 
324
    CuAssertIntEquals(tc, APR_EOF, status);
 
325
    CuAssertIntEquals(tc, 29, vecs_used);
 
326
 
 
327
    /* Test 3: use serf_bucket_read */
 
328
    for (i = 0; i < 32 ; i++) {
 
329
        vecs[i].iov_base = apr_psprintf(test_pool, "DATA %02d 901234567890", i);
 
330
        vecs[i].iov_len = strlen(vecs[i].iov_base);
 
331
    }
 
332
 
 
333
    iobkt = serf_bucket_iovec_create(vecs, 32, alloc);
 
334
 
 
335
    status = serf_bucket_read(iobkt, 10, &data, &len);
 
336
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
337
    CuAssertIntEquals(tc, 10, len);
 
338
    CuAssert(tc, data,
 
339
             strncmp("DATA 00 90", data, len) == 0);
 
340
 
 
341
    status = serf_bucket_read(iobkt, 10, &data, &len);
 
342
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
343
    CuAssertIntEquals(tc, 10, len);
 
344
    CuAssert(tc, tgt_vecs[0].iov_base,
 
345
             strncmp("1234567890", data, len) == 0);
 
346
 
 
347
    for (i = 1; i < 31 ; i++) {
 
348
        const char *exp = apr_psprintf(test_pool, "DATA %02d 901234567890", i);
 
349
        status = serf_bucket_read(iobkt, SERF_READ_ALL_AVAIL, &data, &len);
 
350
        CuAssertIntEquals(tc, APR_SUCCESS, status);
 
351
        CuAssertIntEquals(tc, 20, len);
 
352
        CuAssert(tc, data,
 
353
                 strncmp(exp, data, len) == 0);
 
354
 
 
355
    }
 
356
 
 
357
    status = serf_bucket_read(iobkt, 20, &data, &len);
 
358
    CuAssertIntEquals(tc, APR_EOF, status);
 
359
    CuAssertIntEquals(tc, 20, len);
 
360
    CuAssert(tc, data,
 
361
             strncmp("DATA 31 901234567890", data, len) == 0);
 
362
 
 
363
    /* Test 3: read an empty iovec */
 
364
    iobkt = serf_bucket_iovec_create(vecs, 0, alloc);
 
365
    status = serf_bucket_read_iovec(iobkt, SERF_READ_ALL_AVAIL, 32,
 
366
                                    tgt_vecs, &vecs_used);
 
367
    CuAssertIntEquals(tc, APR_EOF, status);
 
368
    CuAssertIntEquals(tc, 0, vecs_used);
 
369
 
 
370
    status = serf_bucket_read(iobkt, SERF_READ_ALL_AVAIL, &data, &len);
 
371
    CuAssertIntEquals(tc, APR_EOF, status);
 
372
    CuAssertIntEquals(tc, 0, len);
 
373
 
 
374
    /* Test 4: read 0 bytes from an iovec */
 
375
    bkt = SERF_BUCKET_SIMPLE_STRING("line1" CRLF, alloc);
 
376
    status = serf_bucket_read_iovec(bkt, SERF_READ_ALL_AVAIL, 32, vecs,
 
377
                                    &vecs_used);
 
378
    iobkt = serf_bucket_iovec_create(vecs, vecs_used, alloc);
 
379
    status = serf_bucket_read_iovec(iobkt, 0, 32,
 
380
                                    tgt_vecs, &vecs_used);
 
381
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
382
    CuAssertIntEquals(tc, 0, vecs_used);
 
383
 
 
384
    test_teardown(test_pool);
 
385
}
 
386
 
 
387
static void test_aggregate_buckets(CuTest *tc)
 
388
{
 
389
    apr_status_t status;
 
390
    serf_bucket_t *bkt, *aggbkt;
 
391
    struct iovec tgt_vecs[32];
 
392
    int vecs_used;
 
393
 
 
394
    apr_pool_t *test_pool = test_setup();
 
395
    serf_bucket_alloc_t *alloc = serf_bucket_allocator_create(test_pool, NULL,
 
396
                                                              NULL);
 
397
 
 
398
    /* Test 1: read 0 bytes from an aggregate */
 
399
    aggbkt = serf_bucket_aggregate_create(alloc);
 
400
 
 
401
    bkt = SERF_BUCKET_SIMPLE_STRING("line1" CRLF, alloc);
 
402
    serf_bucket_aggregate_append(aggbkt, bkt);
 
403
 
 
404
    status = serf_bucket_read_iovec(aggbkt, 0, 32,
 
405
                                    tgt_vecs, &vecs_used);
 
406
    CuAssertIntEquals(tc, APR_SUCCESS, status);
 
407
    CuAssertIntEquals(tc, 0, vecs_used);
 
408
 
 
409
    test_teardown(test_pool);
316
410
}
317
411
 
318
412
CuSuite *test_buckets(void)
324
418
    SUITE_ADD_TEST(suite, test_response_bucket_headers);
325
419
    SUITE_ADD_TEST(suite, test_response_bucket_chunked_read);
326
420
    SUITE_ADD_TEST(suite, test_bucket_header_set);
327
 
    SUITE_ADD_TEST(suite, test_simple_read_restore_snapshot_read);
328
 
    SUITE_ADD_TEST(suite, test_aggregate_read_restore_snapshot_read);
 
421
    SUITE_ADD_TEST(suite, test_iovec_buckets);
 
422
    SUITE_ADD_TEST(suite, test_aggregate_buckets);
329
423
 
330
424
    return suite;
331
425
}