230
223
#undef NUM_TEST_STRINGS
231
224
#undef TEST_BUF_SIZE
232
#undef GENEREATED_SIZE
225
#undef GENERATED_SIZE
234
227
svn_pool_destroy(subpool);
235
228
return SVN_NO_ERROR;
232
test_stream_tee(apr_pool_t *pool)
234
svn_stringbuf_t *test_bytes = generate_test_bytes(100, pool);
235
svn_stringbuf_t *output_buf1 = svn_stringbuf_create("", pool);
236
svn_stringbuf_t *output_buf2 = svn_stringbuf_create("", pool);
237
svn_stream_t *source_stream = svn_stream_from_stringbuf(test_bytes, pool);
238
svn_stream_t *output_stream1 = svn_stream_from_stringbuf(output_buf1, pool);
239
svn_stream_t *output_stream2 = svn_stream_from_stringbuf(output_buf2, pool);
240
svn_stream_t *tee_stream;
242
tee_stream = svn_stream_tee(output_stream1, output_stream2, pool);
243
SVN_ERR(svn_stream_copy3(source_stream, tee_stream, NULL, NULL, pool));
245
if (!svn_stringbuf_compare(output_buf1, output_buf2))
246
return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
247
"Duplicated streams did not match.");
253
test_stream_seek_file(apr_pool_t *pool)
255
static const char *file_data[2] = {"One", "Two"};
256
svn_stream_t *stream;
257
svn_stringbuf_t *line;
260
static const char *fname = "test_stream_seek.txt";
263
static const char *NL = APR_EOL_STR;
264
svn_stream_mark_t *mark;
266
status = apr_file_open(&f, fname, (APR_READ | APR_WRITE | APR_CREATE |
267
APR_TRUNCATE | APR_DELONCLOSE), APR_OS_DEFAULT, pool);
268
if (status != APR_SUCCESS)
269
return svn_error_createf(SVN_ERR_TEST_FAILED, NULL, "Cannot open '%s'",
272
/* Create the file. */
273
for (j = 0; j < 2; j++)
277
len = strlen(file_data[j]);
278
status = apr_file_write(f, file_data[j], &len);
279
if (status || len != strlen(file_data[j]))
280
return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
281
"Cannot write to '%s'", fname);
283
status = apr_file_write(f, NL, &len);
284
if (status || len != strlen(NL))
285
return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
286
"Cannot write to '%s'", fname);
289
/* Create a stream to read from the file. */
290
stream = svn_stream_from_aprfile2(f, FALSE, pool);
291
SVN_ERR(svn_stream_reset(stream));
292
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
293
SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[0]) == 0);
294
/* Set a mark at the beginning of the second line of the file. */
295
SVN_ERR(svn_stream_mark(stream, &mark, pool));
296
/* Read the second line and then seek back to the mark. */
297
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
298
SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[1]) == 0);
299
SVN_ERR(svn_stream_seek(stream, mark));
300
/* The next read should return the second line again. */
301
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
302
SVN_TEST_ASSERT(! eof && strcmp(line->data, file_data[1]) == 0);
303
/* The next read should return EOF. */
304
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
305
SVN_TEST_ASSERT(eof);
307
/* Go back to the beginning of the last line and try to skip it
308
* NOT including the EOL. */
309
SVN_ERR(svn_stream_seek(stream, mark));
310
SVN_ERR(svn_stream_skip(stream, strlen(file_data[1])));
311
/* The remaining line should be empty */
312
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
313
SVN_TEST_ASSERT(! eof && strcmp(line->data, "") == 0);
314
/* The next read should return EOF. */
315
SVN_ERR(svn_stream_readline(stream, &line, NL, &eof, pool));
316
SVN_TEST_ASSERT(eof);
318
SVN_ERR(svn_stream_close(stream));
324
test_stream_seek_stringbuf(apr_pool_t *pool)
326
svn_stream_t *stream;
327
svn_stringbuf_t *stringbuf;
330
svn_stream_mark_t *mark;
332
stringbuf = svn_stringbuf_create("OneTwo", pool);
333
stream = svn_stream_from_stringbuf(stringbuf, pool);
335
SVN_ERR(svn_stream_read(stream, buf, &len));
337
SVN_TEST_STRING_ASSERT(buf, "One");
338
SVN_ERR(svn_stream_mark(stream, &mark, pool));
340
SVN_ERR(svn_stream_read(stream, buf, &len));
342
SVN_TEST_STRING_ASSERT(buf, "Two");
343
SVN_ERR(svn_stream_seek(stream, mark));
345
SVN_ERR(svn_stream_read(stream, buf, &len));
347
SVN_TEST_STRING_ASSERT(buf, "Two");
349
/* Go back to the begin of last word and try to skip some of it */
350
SVN_ERR(svn_stream_seek(stream, mark));
351
SVN_ERR(svn_stream_skip(stream, 2));
352
/* The remaining line should be empty */
354
SVN_ERR(svn_stream_read(stream, buf, &len));
356
SVN_TEST_ASSERT(len == 1);
357
SVN_TEST_STRING_ASSERT(buf, "o");
359
SVN_ERR(svn_stream_close(stream));
365
test_stream_seek_translated(apr_pool_t *pool)
367
svn_stream_t *stream, *translated_stream;
368
svn_stringbuf_t *stringbuf;
369
char buf[44]; /* strlen("One$MyKeyword: my keyword was expanded $Two") + \0 */
371
svn_stream_mark_t *mark;
372
apr_hash_t *keywords;
373
svn_string_t *keyword_val;
375
keywords = apr_hash_make(pool);
376
keyword_val = svn_string_create("my keyword was expanded", pool);
377
apr_hash_set(keywords, "MyKeyword", APR_HASH_KEY_STRING, keyword_val);
378
stringbuf = svn_stringbuf_create("One$MyKeyword$Two", pool);
379
stream = svn_stream_from_stringbuf(stringbuf, pool);
380
translated_stream = svn_subst_stream_translated(stream, APR_EOL_STR,
381
FALSE, keywords, TRUE, pool);
382
/* Seek from outside of keyword to inside of keyword. */
384
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
385
SVN_TEST_ASSERT(len == 25);
387
SVN_TEST_STRING_ASSERT(buf, "One$MyKeyword: my keyword");
388
SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
389
SVN_ERR(svn_stream_reset(translated_stream));
390
SVN_ERR(svn_stream_seek(translated_stream, mark));
392
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
393
SVN_TEST_ASSERT(len == 4);
395
SVN_TEST_STRING_ASSERT(buf, " was");
397
SVN_ERR(svn_stream_seek(translated_stream, mark));
398
SVN_ERR(svn_stream_skip(translated_stream, 2));
400
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
401
SVN_TEST_ASSERT(len == 2);
403
SVN_TEST_STRING_ASSERT(buf, "as");
405
/* Seek from inside of keyword to inside of keyword. */
406
SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
408
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
409
SVN_TEST_ASSERT(len == 9);
411
SVN_TEST_STRING_ASSERT(buf, " expanded");
412
SVN_ERR(svn_stream_seek(translated_stream, mark));
414
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
415
SVN_TEST_ASSERT(len == 9);
417
SVN_TEST_STRING_ASSERT(buf, " expanded");
419
SVN_ERR(svn_stream_seek(translated_stream, mark));
420
SVN_ERR(svn_stream_skip(translated_stream, 6));
422
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
423
SVN_TEST_ASSERT(len == 3);
425
SVN_TEST_STRING_ASSERT(buf, "ded");
427
/* Seek from inside of keyword to outside of keyword. */
428
SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
430
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
431
SVN_TEST_ASSERT(len == 4);
433
SVN_TEST_STRING_ASSERT(buf, " $Tw");
434
SVN_ERR(svn_stream_seek(translated_stream, mark));
436
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
437
SVN_TEST_ASSERT(len == 4);
439
SVN_TEST_STRING_ASSERT(buf, " $Tw");
441
SVN_ERR(svn_stream_seek(translated_stream, mark));
442
SVN_ERR(svn_stream_skip(translated_stream, 2));
444
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
445
SVN_TEST_ASSERT(len == 2);
447
SVN_TEST_STRING_ASSERT(buf, "Tw");
449
/* Seek from outside of keyword to outside of keyword. */
450
SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
452
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
453
SVN_TEST_ASSERT(len == 1);
455
SVN_TEST_STRING_ASSERT(buf, "o");
456
SVN_ERR(svn_stream_seek(translated_stream, mark));
458
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
459
SVN_TEST_ASSERT(len == 1);
461
SVN_TEST_STRING_ASSERT(buf, "o");
463
SVN_ERR(svn_stream_seek(translated_stream, mark));
464
SVN_ERR(svn_stream_skip(translated_stream, 2));
466
SVN_ERR(svn_stream_read(translated_stream, buf, &len));
467
SVN_TEST_ASSERT(len == 0);
469
SVN_TEST_STRING_ASSERT(buf, "");
471
SVN_ERR(svn_stream_close(stream));
477
test_readonly(apr_pool_t *pool)
481
svn_boolean_t read_only;
482
apr_int32_t wanted = APR_FINFO_SIZE | APR_FINFO_MTIME | APR_FINFO_TYPE
483
| APR_FINFO_LINK | APR_FINFO_PROT;
486
SVN_ERR(svn_io_open_unique_file3(NULL, &path, NULL,
487
svn_io_file_del_on_pool_cleanup,
490
/* File should be writable */
491
SVN_ERR(svn_io_stat(&finfo, path, wanted, pool));
492
SVN_ERR(svn_io__is_finfo_read_only(&read_only, &finfo, pool));
493
SVN_TEST_ASSERT(read_only == FALSE);
496
SVN_ERR(svn_io_set_file_read_only(path, FALSE, pool));
498
/* File should be read only */
499
SVN_ERR(svn_io_stat(&finfo, path, wanted, pool));
500
SVN_ERR(svn_io__is_finfo_read_only(&read_only, &finfo, pool));
501
SVN_TEST_ASSERT(read_only);
504
SVN_ERR(svn_io_set_file_read_write(path, FALSE, pool));
506
/* File should be writable */
507
SVN_ERR(svn_io_stat(&finfo, path, wanted, pool));
508
SVN_ERR(svn_io__is_finfo_read_only(&read_only, &finfo, pool));
509
SVN_TEST_ASSERT(read_only == FALSE);
515
test_stream_compressed_empty_file(apr_pool_t *pool)
517
svn_stream_t *stream, *empty_file_stream;
521
/* Reading an empty file with a compressed stream should not error. */
522
SVN_ERR(svn_stream_open_unique(&empty_file_stream, NULL, NULL,
523
svn_io_file_del_on_pool_cleanup,
525
stream = svn_stream_compressed(empty_file_stream, pool);
527
SVN_ERR(svn_stream_read(stream, buf, &len));
529
return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
530
"Got unexpected result.");
532
SVN_ERR(svn_stream_close(stream));
538
test_stream_base64(apr_pool_t *pool)
540
svn_stream_t *stream;
541
svn_stringbuf_t *actual = svn_stringbuf_create("", pool);
542
svn_stringbuf_t *expected = svn_stringbuf_create("", pool);
544
static const char *strings[] = {
545
"fairly boring test data... blah blah",
552
stream = svn_stream_from_stringbuf(actual, pool);
553
stream = svn_base64_decode(stream, pool);
554
stream = svn_base64_encode(stream, pool);
556
for (i = 0; strings[i]; i++)
558
apr_size_t len = strlen(strings[i]);
560
svn_stringbuf_appendbytes(expected, strings[i], len);
561
SVN_ERR(svn_stream_write(stream, strings[i], &len));
564
SVN_ERR(svn_stream_close(stream));
566
SVN_TEST_STRING_ASSERT(actual->data, expected->data);
239
571
/* The test table. */
241
573
struct svn_test_descriptor_t test_funcs[] =
244
SVN_TEST_PASS(test_stream_from_string),
245
SVN_TEST_PASS(test_stream_compressed),
576
SVN_TEST_PASS2(test_stream_from_string,
577
"test svn_stream_from_string"),
578
SVN_TEST_PASS2(test_stream_compressed,
579
"test compressed streams"),
580
SVN_TEST_PASS2(test_stream_tee,
581
"test 'tee' streams"),
582
SVN_TEST_PASS2(test_stream_seek_file,
583
"test stream seeking for files"),
584
SVN_TEST_PASS2(test_stream_seek_stringbuf,
585
"test stream seeking for stringbufs"),
586
SVN_TEST_PASS2(test_stream_seek_translated,
587
"test stream seeking for translated streams"),
588
SVN_TEST_PASS2(test_readonly,
589
"test setting a file readonly"),
590
SVN_TEST_PASS2(test_stream_compressed_empty_file,
591
"test compressed streams with empty files"),
592
SVN_TEST_PASS2(test_stream_base64,
593
"test base64 encoding/decoding streams"),