~ubuntu-branches/ubuntu/trusty/subversion/trusty-proposed

« back to all changes in this revision

Viewing changes to subversion/tests/libsvn_subr/stream-test.c

  • Committer: Package Import Robot
  • Author(s): Andy Whitcroft
  • Date: 2012-06-21 15:36:36 UTC
  • mfrom: (0.4.13 sid)
  • Revision ID: package-import@ubuntu.com-20120621153636-amqqmuidgwgxz1ly
Tags: 1.7.5-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Create pot file on build.
  - Build a python-subversion-dbg package.
  - Build-depend on python-dbg.
  - Build-depend on default-jre-headless/-jdk.
  - Do not apply java-build patch.
  - debian/rules: Manually create the doxygen output directory, otherwise
    we get weird build failures when running parallel builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * stream-test.c -- test the stream functions
3
3
 *
4
4
 * ====================================================================
5
 
 * Copyright (c) 2000-2004 CollabNet.  All rights reserved.
6
 
 *
7
 
 * This software is licensed as described in the file COPYING, which
8
 
 * you should have received as part of this distribution.  The terms
9
 
 * are also available at http://subversion.tigris.org/license-1.html.
10
 
 * If newer versions of this license are posted there, you may use a
11
 
 * newer version instead, at your option.
12
 
 *
13
 
 * This software consists of voluntary contributions made by many
14
 
 * individuals.  For exact contribution history, see the revision
15
 
 * history and logs, available at http://subversion.tigris.org/.
 
5
 *    Licensed to the Apache Software Foundation (ASF) under one
 
6
 *    or more contributor license agreements.  See the NOTICE file
 
7
 *    distributed with this work for additional information
 
8
 *    regarding copyright ownership.  The ASF licenses this file
 
9
 *    to you under the Apache License, Version 2.0 (the
 
10
 *    "License"); you may not use this file except in compliance
 
11
 *    with the License.  You may obtain a copy of the License at
 
12
 *
 
13
 *      http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 *    Unless required by applicable law or agreed to in writing,
 
16
 *    software distributed under the License is distributed on an
 
17
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
18
 *    KIND, either express or implied.  See the License for the
 
19
 *    specific language governing permissions and limitations
 
20
 *    under the License.
16
21
 * ====================================================================
17
22
 */
18
23
 
19
24
#include <stdio.h>
20
25
#include "svn_pools.h"
21
26
#include "svn_io.h"
 
27
#include "svn_subst.h"
 
28
#include "svn_base64.h"
22
29
#include <apr_general.h>
23
30
 
 
31
#include "private/svn_io_private.h"
 
32
 
24
33
#include "../svn_test.h"
25
34
 
26
35
 
27
36
static svn_error_t *
28
 
test_stream_from_string(const char **msg,
29
 
                        svn_boolean_t msg_only,
30
 
                        svn_test_opts_t *opts,
31
 
                        apr_pool_t *pool)
 
37
test_stream_from_string(apr_pool_t *pool)
32
38
{
33
39
  int i;
34
40
  apr_pool_t *subpool = svn_pool_create(pool);
52
58
    "peers.  And that demands some amount of respect, wouldn't you say?"
53
59
  };
54
60
 
55
 
  *msg = "test svn_stream_from_string";
56
 
 
57
 
  if (msg_only)
58
 
    return SVN_NO_ERROR;
59
 
 
60
61
  /* Test svn_stream_from_stringbuf() as a readable stream. */
61
62
  for (i = 0; i < NUM_TEST_STRINGS; i++)
62
63
    {
130
131
 
131
132
  for (total = 0, repeat = repeat_iter = 1, c = 0; total < num_bytes; total++)
132
133
    {
133
 
      svn_stringbuf_appendbytes(buffer, &c, 1);
 
134
      svn_stringbuf_appendbyte(buffer, c);
134
135
 
135
136
      repeat_iter--;
136
137
      if (repeat_iter == 0)
147
148
 
148
149
 
149
150
static svn_error_t *
150
 
test_stream_compressed(const char **msg,
151
 
                       svn_boolean_t msg_only,
152
 
                       svn_test_opts_t *opts,
153
 
                       apr_pool_t *pool)
 
151
test_stream_compressed(apr_pool_t *pool)
154
152
{
155
153
#define NUM_TEST_STRINGS 5
156
154
#define TEST_BUF_SIZE 10
177
175
  };
178
176
 
179
177
 
180
 
  *msg = "test compressed streams";
181
 
 
182
 
  if (msg_only)
183
 
    return SVN_NO_ERROR;
184
 
 
185
178
  for (i = 0; i < (NUM_TEST_STRINGS - 1); i++)
186
179
    bufs[i] = svn_stringbuf_create(strings[i], pool);
187
180
 
229
222
 
230
223
#undef NUM_TEST_STRINGS
231
224
#undef TEST_BUF_SIZE
232
 
#undef GENEREATED_SIZE
 
225
#undef GENERATED_SIZE
233
226
 
234
227
  svn_pool_destroy(subpool);
235
228
  return SVN_NO_ERROR;
236
229
}
237
230
 
238
 
 
 
231
static svn_error_t *
 
232
test_stream_tee(apr_pool_t *pool)
 
233
{
 
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;
 
241
 
 
242
  tee_stream = svn_stream_tee(output_stream1, output_stream2, pool);
 
243
  SVN_ERR(svn_stream_copy3(source_stream, tee_stream, NULL, NULL, pool));
 
244
 
 
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.");
 
248
 
 
249
  return SVN_NO_ERROR;
 
250
}
 
251
 
 
252
static svn_error_t *
 
253
test_stream_seek_file(apr_pool_t *pool)
 
254
{
 
255
  static const char *file_data[2] = {"One", "Two"};
 
256
  svn_stream_t *stream;
 
257
  svn_stringbuf_t *line;
 
258
  svn_boolean_t eof;
 
259
  apr_file_t *f;
 
260
  static const char *fname = "test_stream_seek.txt";
 
261
  int j;
 
262
  apr_status_t status;
 
263
  static const char *NL = APR_EOL_STR;
 
264
  svn_stream_mark_t *mark;
 
265
 
 
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'",
 
270
                             fname);
 
271
 
 
272
  /* Create the file. */
 
273
  for (j = 0; j < 2; j++)
 
274
    {
 
275
      apr_size_t len;
 
276
 
 
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);
 
282
      len = strlen(NL);
 
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);
 
287
    }
 
288
 
 
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);
 
306
 
 
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);
 
317
 
 
318
  SVN_ERR(svn_stream_close(stream));
 
319
 
 
320
  return SVN_NO_ERROR;
 
321
}
 
322
 
 
323
static svn_error_t *
 
324
test_stream_seek_stringbuf(apr_pool_t *pool)
 
325
{
 
326
  svn_stream_t *stream;
 
327
  svn_stringbuf_t *stringbuf;
 
328
  char buf[4];
 
329
  apr_size_t len;
 
330
  svn_stream_mark_t *mark;
 
331
 
 
332
  stringbuf = svn_stringbuf_create("OneTwo", pool);
 
333
  stream = svn_stream_from_stringbuf(stringbuf, pool);
 
334
  len = 3;
 
335
  SVN_ERR(svn_stream_read(stream, buf, &len));
 
336
  buf[3] = '\0';
 
337
  SVN_TEST_STRING_ASSERT(buf, "One");
 
338
  SVN_ERR(svn_stream_mark(stream, &mark, pool));
 
339
  len = 3;
 
340
  SVN_ERR(svn_stream_read(stream, buf, &len));
 
341
  buf[3] = '\0';
 
342
  SVN_TEST_STRING_ASSERT(buf, "Two");
 
343
  SVN_ERR(svn_stream_seek(stream, mark));
 
344
  len = 3;
 
345
  SVN_ERR(svn_stream_read(stream, buf, &len));
 
346
  buf[3] = '\0';
 
347
  SVN_TEST_STRING_ASSERT(buf, "Two");
 
348
 
 
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 */
 
353
  len = 3;
 
354
  SVN_ERR(svn_stream_read(stream, buf, &len));
 
355
  buf[len] = '\0';
 
356
  SVN_TEST_ASSERT(len == 1);
 
357
  SVN_TEST_STRING_ASSERT(buf, "o");
 
358
 
 
359
  SVN_ERR(svn_stream_close(stream));
 
360
 
 
361
  return SVN_NO_ERROR;
 
362
}
 
363
 
 
364
static svn_error_t *
 
365
test_stream_seek_translated(apr_pool_t *pool)
 
366
{
 
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 */
 
370
  apr_size_t len;
 
371
  svn_stream_mark_t *mark;
 
372
  apr_hash_t *keywords;
 
373
  svn_string_t *keyword_val;
 
374
 
 
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. */
 
383
  len = 25;
 
384
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
385
  SVN_TEST_ASSERT(len == 25);
 
386
  buf[25] = '\0';
 
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));
 
391
  len = 4;
 
392
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
393
  SVN_TEST_ASSERT(len == 4);
 
394
  buf[4] = '\0';
 
395
  SVN_TEST_STRING_ASSERT(buf, " was");
 
396
 
 
397
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
398
  SVN_ERR(svn_stream_skip(translated_stream, 2));
 
399
  len = 2;
 
400
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
401
  SVN_TEST_ASSERT(len == 2);
 
402
  buf[len] = '\0';
 
403
  SVN_TEST_STRING_ASSERT(buf, "as");
 
404
 
 
405
  /* Seek from inside of keyword to inside of keyword. */
 
406
  SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
 
407
  len = 9;
 
408
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
409
  SVN_TEST_ASSERT(len == 9);
 
410
  buf[9] = '\0';
 
411
  SVN_TEST_STRING_ASSERT(buf, " expanded");
 
412
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
413
  len = 9;
 
414
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
415
  SVN_TEST_ASSERT(len == 9);
 
416
  buf[9] = '\0';
 
417
  SVN_TEST_STRING_ASSERT(buf, " expanded");
 
418
 
 
419
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
420
  SVN_ERR(svn_stream_skip(translated_stream, 6));
 
421
  len = 3;
 
422
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
423
  SVN_TEST_ASSERT(len == 3);
 
424
  buf[len] = '\0';
 
425
  SVN_TEST_STRING_ASSERT(buf, "ded");
 
426
 
 
427
  /* Seek from inside of keyword to outside of keyword. */
 
428
  SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
 
429
  len = 4;
 
430
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
431
  SVN_TEST_ASSERT(len == 4);
 
432
  buf[4] = '\0';
 
433
  SVN_TEST_STRING_ASSERT(buf, " $Tw");
 
434
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
435
  len = 4;
 
436
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
437
  SVN_TEST_ASSERT(len == 4);
 
438
  buf[4] = '\0';
 
439
  SVN_TEST_STRING_ASSERT(buf, " $Tw");
 
440
 
 
441
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
442
  SVN_ERR(svn_stream_skip(translated_stream, 2));
 
443
  len = 2;
 
444
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
445
  SVN_TEST_ASSERT(len == 2);
 
446
  buf[len] = '\0';
 
447
  SVN_TEST_STRING_ASSERT(buf, "Tw");
 
448
 
 
449
  /* Seek from outside of keyword to outside of keyword. */
 
450
  SVN_ERR(svn_stream_mark(translated_stream, &mark, pool));
 
451
  len = 1;
 
452
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
453
  SVN_TEST_ASSERT(len == 1);
 
454
  buf[1] = '\0';
 
455
  SVN_TEST_STRING_ASSERT(buf, "o");
 
456
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
457
  len = 1;
 
458
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
459
  SVN_TEST_ASSERT(len == 1);
 
460
  buf[1] = '\0';
 
461
  SVN_TEST_STRING_ASSERT(buf, "o");
 
462
 
 
463
  SVN_ERR(svn_stream_seek(translated_stream, mark));
 
464
  SVN_ERR(svn_stream_skip(translated_stream, 2));
 
465
  len = 1;
 
466
  SVN_ERR(svn_stream_read(translated_stream, buf, &len));
 
467
  SVN_TEST_ASSERT(len == 0);
 
468
  buf[len] = '\0';
 
469
  SVN_TEST_STRING_ASSERT(buf, "");
 
470
 
 
471
  SVN_ERR(svn_stream_close(stream));
 
472
 
 
473
  return SVN_NO_ERROR;
 
474
}
 
475
 
 
476
static svn_error_t *
 
477
test_readonly(apr_pool_t *pool)
 
478
{
 
479
  const char *path;
 
480
  apr_finfo_t finfo;
 
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;
 
484
 
 
485
 
 
486
  SVN_ERR(svn_io_open_unique_file3(NULL, &path, NULL,
 
487
                                   svn_io_file_del_on_pool_cleanup,
 
488
                                   pool, pool));
 
489
 
 
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);
 
494
 
 
495
  /* Set read only */
 
496
  SVN_ERR(svn_io_set_file_read_only(path, FALSE, pool));
 
497
 
 
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);
 
502
 
 
503
  /* Set writable */
 
504
  SVN_ERR(svn_io_set_file_read_write(path, FALSE, pool));
 
505
 
 
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);
 
510
 
 
511
  return SVN_NO_ERROR;
 
512
}
 
513
 
 
514
static svn_error_t *
 
515
test_stream_compressed_empty_file(apr_pool_t *pool)
 
516
{
 
517
  svn_stream_t *stream, *empty_file_stream;
 
518
  char buf[1];
 
519
  apr_size_t len;
 
520
 
 
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,
 
524
                                 pool, pool));
 
525
  stream = svn_stream_compressed(empty_file_stream, pool);
 
526
  len = sizeof(buf);
 
527
  SVN_ERR(svn_stream_read(stream, buf, &len));
 
528
  if (len > 0)
 
529
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
 
530
                            "Got unexpected result.");
 
531
 
 
532
  SVN_ERR(svn_stream_close(stream));
 
533
 
 
534
  return SVN_NO_ERROR;
 
535
}
 
536
 
 
537
static svn_error_t *
 
538
test_stream_base64(apr_pool_t *pool)
 
539
{
 
540
  svn_stream_t *stream;
 
541
  svn_stringbuf_t *actual = svn_stringbuf_create("", pool);
 
542
  svn_stringbuf_t *expected = svn_stringbuf_create("", pool);
 
543
  int i;
 
544
  static const char *strings[] = {
 
545
    "fairly boring test data... blah blah",
 
546
    "A",
 
547
    "abc",
 
548
    "012345679",
 
549
    NULL
 
550
  };
 
551
 
 
552
  stream = svn_stream_from_stringbuf(actual, pool);
 
553
  stream = svn_base64_decode(stream, pool);
 
554
  stream = svn_base64_encode(stream, pool);
 
555
 
 
556
  for (i = 0; strings[i]; i++)
 
557
    {
 
558
      apr_size_t len = strlen(strings[i]);
 
559
 
 
560
      svn_stringbuf_appendbytes(expected, strings[i], len);
 
561
      SVN_ERR(svn_stream_write(stream, strings[i], &len));
 
562
    }
 
563
 
 
564
  SVN_ERR(svn_stream_close(stream));
 
565
 
 
566
  SVN_TEST_STRING_ASSERT(actual->data, expected->data);
 
567
 
 
568
  return SVN_NO_ERROR;
 
569
}
 
570
 
239
571
/* The test table.  */
240
572
 
241
573
struct svn_test_descriptor_t test_funcs[] =
242
574
  {
243
575
    SVN_TEST_NULL,
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"),
246
594
    SVN_TEST_NULL
247
595
  };