~ubuntu-branches/debian/sid/subversion/sid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James McCoy
  • Date: 2015-08-07 21:32:47 UTC
  • mfrom: (0.2.15) (4.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20150807213247-ozyewtmgsr6tkewl
Tags: 1.9.0-1
* Upload to unstable
* New upstream release.
  + Security fixes
    - CVE-2015-3184: Mixed anonymous/authenticated path-based authz with
      httpd 2.4
    - CVE-2015-3187: svn_repos_trace_node_locations() reveals paths hidden
      by authz
* Add >= 2.7 requirement for python-all-dev Build-Depends, needed to run
  tests.
* Remove Build-Conflicts against ruby-test-unit.  (Closes: #791844)
* Remove patches/apache_module_dependency in favor of expressing the
  dependencies in authz_svn.load/dav_svn.load.
* Build-Depend on apache2-dev (>= 2.4.16) to ensure ap_some_authn_required()
  is available when building mod_authz_svn and Depend on apache2-bin (>=
  2.4.16) for runtime support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
#include "svn_io.h"
40
40
#include "svn_error.h"
 
41
#include "svn_sorts.h"    /* MIN / MAX */
41
42
#include "svn_string.h"   /* This includes <apr_*.h> */
42
43
#include "private/svn_string_private.h"
43
44
 
58
59
 
59
60
/* Some of our own global variables, for simplicity.  Yes,
60
61
   simplicity. */
61
 
svn_stringbuf_t *a = NULL, *b = NULL, *c = NULL;
62
 
const char *phrase_1 = "hello, ";
63
 
const char *phrase_2 = "a longish phrase of sorts, longer than 16 anyway";
 
62
static const char *phrase_1 = "hello, ";
 
63
static const char *phrase_2 = "a longish phrase of sorts, longer than 16 anyway";
64
64
 
65
65
 
66
66
 
68
68
static svn_error_t *
69
69
test1(apr_pool_t *pool)
70
70
{
71
 
  a = svn_stringbuf_create(phrase_1, pool);
 
71
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
72
72
 
73
73
  /* Test that length, data, and null-termination are correct. */
74
74
  if ((a->len == strlen(phrase_1)) && ((strcmp(a->data, phrase_1)) == 0))
81
81
static svn_error_t *
82
82
test2(apr_pool_t *pool)
83
83
{
84
 
  b = svn_stringbuf_ncreate(phrase_2, 16, pool);
 
84
  svn_stringbuf_t *b = svn_stringbuf_ncreate(phrase_2, 16, pool);
85
85
 
86
86
  /* Test that length, data, and null-termination are correct. */
87
87
  if ((b->len == 16) && ((strncmp(b->data, phrase_2, 16)) == 0))
97
97
  char *tmp;
98
98
  size_t old_len;
99
99
 
100
 
  a = svn_stringbuf_create(phrase_1, pool);
101
 
  b = svn_stringbuf_ncreate(phrase_2, 16, pool);
 
100
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
 
101
  svn_stringbuf_t *b = svn_stringbuf_ncreate(phrase_2, 16, pool);
102
102
 
103
103
  tmp = apr_palloc(pool, (a->len + b->len + 1));
104
104
  strcpy(tmp, a->data);
117
117
static svn_error_t *
118
118
test4(apr_pool_t *pool)
119
119
{
120
 
  a = svn_stringbuf_create(phrase_1, pool);
 
120
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
121
121
  svn_stringbuf_appendcstr(a, "new bytes to append");
122
122
 
123
123
  /* Test that length, data, and null-termination are correct. */
132
132
static svn_error_t *
133
133
test5(apr_pool_t *pool)
134
134
{
135
 
  a = svn_stringbuf_create(phrase_1, pool);
 
135
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
136
136
  svn_stringbuf_appendbytes(a, "new bytes to append", 9);
137
137
 
138
138
  /* Test that length, data, and null-termination are correct. */
147
147
static svn_error_t *
148
148
test6(apr_pool_t *pool)
149
149
{
150
 
  a = svn_stringbuf_create(phrase_1, pool);
151
 
  b = svn_stringbuf_create(phrase_2, pool);
152
 
  c = svn_stringbuf_dup(a, pool);
 
150
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
 
151
  svn_stringbuf_t *b = svn_stringbuf_create(phrase_2, pool);
 
152
  svn_stringbuf_t *c = svn_stringbuf_dup(a, pool);
153
153
 
154
154
  /* Test that length, data, and null-termination are correct. */
155
155
  if ((svn_stringbuf_compare(a, c)) && (! svn_stringbuf_compare(b, c)))
165
165
  char *tmp;
166
166
  size_t tmp_len;
167
167
 
168
 
  c = svn_stringbuf_create(phrase_2, pool);
 
168
  svn_stringbuf_t *c = svn_stringbuf_create(phrase_2, pool);
169
169
 
170
170
  tmp_len = c->len;
171
171
  tmp = apr_palloc(pool, c->len + 1);
185
185
static svn_error_t *
186
186
test8(apr_pool_t *pool)
187
187
{
188
 
  c = svn_stringbuf_create(phrase_2, pool);
 
188
  svn_stringbuf_t *c = svn_stringbuf_create(phrase_2, pool);
189
189
 
190
190
  svn_stringbuf_setempty(c);
191
191
 
199
199
static svn_error_t *
200
200
test9(apr_pool_t *pool)
201
201
{
202
 
  a = svn_stringbuf_create(phrase_1, pool);
 
202
  svn_stringbuf_t *a = svn_stringbuf_create(phrase_1, pool);
203
203
 
204
204
  svn_stringbuf_fillchar(a, '#');
205
205
 
379
379
{
380
380
  apr_size_t i;
381
381
 
382
 
  a = svn_stringbuf_create(data, pool);
 
382
  svn_stringbuf_t *a = svn_stringbuf_create(data, pool);
383
383
  i = svn_stringbuf_find_char_backward(a, ch);
384
384
 
385
385
  if (i == pos)
391
391
static svn_error_t *
392
392
test13(apr_pool_t *pool)
393
393
{
394
 
  a = svn_stringbuf_create("test, test", pool);
 
394
  svn_stringbuf_t *a = svn_stringbuf_create("test, test", pool);
395
395
 
396
396
  return test_find_char_backward(a->data, a->len, ',', 4, pool);
397
397
}
399
399
static svn_error_t *
400
400
test14(apr_pool_t *pool)
401
401
{
402
 
  a = svn_stringbuf_create(",test test", pool);
 
402
  svn_stringbuf_t *a = svn_stringbuf_create(",test test", pool);
403
403
 
404
404
  return test_find_char_backward(a->data, a->len, ',', 0, pool);
405
405
}
407
407
static svn_error_t *
408
408
test15(apr_pool_t *pool)
409
409
{
410
 
  a = svn_stringbuf_create("testing,", pool);
 
410
  svn_stringbuf_t *a = svn_stringbuf_create("testing,", pool);
411
411
 
412
412
  return test_find_char_backward(a->data,
413
413
                                 a->len,
419
419
static svn_error_t *
420
420
test16(apr_pool_t *pool)
421
421
{
422
 
  a = svn_stringbuf_create_empty(pool);
 
422
  svn_stringbuf_t *a = svn_stringbuf_create_empty(pool);
423
423
 
424
424
  return test_find_char_backward(a->data, a->len, ',', 0, pool);
425
425
}
427
427
static svn_error_t *
428
428
test17(apr_pool_t *pool)
429
429
{
430
 
  a = svn_stringbuf_create("test test test", pool);
 
430
  svn_stringbuf_t *a = svn_stringbuf_create("test test test", pool);
431
431
 
432
432
  return test_find_char_backward(a->data,
433
433
                                 a->len,
443
443
{
444
444
  apr_size_t i;
445
445
 
446
 
  a = svn_stringbuf_create(str, pool);
 
446
  svn_stringbuf_t *a = svn_stringbuf_create(str, pool);
447
447
 
448
448
  i = svn_stringbuf_first_non_whitespace(a);
449
449
 
474
474
static svn_error_t *
475
475
test21(apr_pool_t *pool)
476
476
{
477
 
  a = svn_stringbuf_create("    \ttest\t\t  \t  ", pool);
478
 
  b = svn_stringbuf_create("test", pool);
 
477
  svn_stringbuf_t *a = svn_stringbuf_create("    \ttest\t\t  \t  ", pool);
 
478
  svn_stringbuf_t *b = svn_stringbuf_create("test", pool);
479
479
 
480
480
  svn_stringbuf_strip_whitespace(a);
481
481
 
490
490
                       const char* str2,
491
491
                       apr_pool_t *pool)
492
492
{
493
 
  a = svn_stringbuf_create(str1, pool);
494
 
  b = svn_stringbuf_create(str2, pool);
 
493
  svn_stringbuf_t *a = svn_stringbuf_create(str1, pool);
 
494
  svn_stringbuf_t *b = svn_stringbuf_create(str2, pool);
495
495
 
496
496
  if (svn_stringbuf_compare(a, b))
497
497
    return fail(pool, "test failed");
521
521
  SVN_TEST_ASSERT(length == 1);
522
522
  SVN_TEST_STRING_ASSERT(buffer, "0");
523
523
 
524
 
  length = svn__i64toa(buffer, 0x8000000000000000ll);
 
524
  length = svn__i64toa(buffer, APR_INT64_MIN);
525
525
  SVN_TEST_ASSERT(length == 20);
526
526
  SVN_TEST_STRING_ASSERT(buffer, "-9223372036854775808");
527
527
 
528
 
  length = svn__i64toa(buffer, 0x7fffffffffffffffll);
 
528
  length = svn__i64toa(buffer, APR_INT64_MAX);
529
529
  SVN_TEST_ASSERT(length == 19);
530
530
  SVN_TEST_STRING_ASSERT(buffer, "9223372036854775807");
531
531
 
532
 
  length = svn__ui64toa(buffer, 0ull);
 
532
  length = svn__ui64toa(buffer, 0u);
533
533
  SVN_TEST_ASSERT(length == 1);
534
534
  SVN_TEST_STRING_ASSERT(buffer, "0");
535
535
 
536
 
  length = svn__ui64toa(buffer, 0xffffffffffffffffull);
 
536
  length = svn__ui64toa(buffer, APR_UINT64_MAX);
537
537
  SVN_TEST_ASSERT(length == 20);
538
538
  SVN_TEST_STRING_ASSERT(buffer, "18446744073709551615");
539
539
 
540
 
  return test_stringbuf_unequal("abc", "abb", pool);
 
540
  return SVN_NO_ERROR;
 
541
}
 
542
 
 
543
static svn_error_t *
 
544
sub_test_base36(apr_uint64_t value, const char *base36)
 
545
{
 
546
  char buffer[SVN_INT64_BUFFER_SIZE];
 
547
  apr_size_t length;
 
548
  apr_size_t expected_length = strlen(base36);
 
549
  const char *end = buffer;
 
550
  apr_uint64_t result;
 
551
 
 
552
  length = svn__ui64tobase36(buffer, value);
 
553
  SVN_TEST_ASSERT(length == expected_length);
 
554
  SVN_TEST_STRING_ASSERT(buffer, base36);
 
555
 
 
556
  result = svn__base36toui64(&end, buffer);
 
557
  SVN_TEST_ASSERT(end - buffer == length);
 
558
  SVN_TEST_ASSERT(result == value);
 
559
 
 
560
  result = svn__base36toui64(NULL, buffer);
 
561
  SVN_TEST_ASSERT(result == value);
 
562
 
 
563
  return SVN_NO_ERROR;
 
564
}
 
565
 
 
566
static svn_error_t *
 
567
test_base36(apr_pool_t *pool)
 
568
{
 
569
  SVN_ERR(sub_test_base36(0, "0"));
 
570
  SVN_ERR(sub_test_base36(APR_UINT64_C(1234567890), "kf12oi"));
 
571
  SVN_ERR(sub_test_base36(APR_UINT64_C(0x7fffffffffffffff), "1y2p0ij32e8e7"));
 
572
  SVN_ERR(sub_test_base36(APR_UINT64_C(0x8000000000000000), "1y2p0ij32e8e8"));
 
573
  SVN_ERR(sub_test_base36(APR_UINT64_MAX, "3w5e11264sgsf"));
 
574
 
 
575
  return SVN_NO_ERROR;
541
576
}
542
577
 
543
578
static svn_error_t *
554
589
static svn_error_t *
555
590
test_stringbuf_insert(apr_pool_t *pool)
556
591
{
557
 
  a = svn_stringbuf_create("st , ", pool);
 
592
  svn_stringbuf_t *a = svn_stringbuf_create("st , ", pool);
558
593
 
559
594
  svn_stringbuf_insert(a, 0, "teflon", 2);
560
595
  SVN_TEST_STRING_ASSERT(a->data, "test , ");
587
622
static svn_error_t *
588
623
test_stringbuf_remove(apr_pool_t *pool)
589
624
{
590
 
  a = svn_stringbuf_create("test hello, world!", pool);
 
625
  svn_stringbuf_t *a = svn_stringbuf_create("test hello, world!", pool);
591
626
 
592
627
  svn_stringbuf_remove(a, 0, 2);
593
628
  SVN_TEST_STRING_ASSERT(a->data, "st hello, world!");
605
640
static svn_error_t *
606
641
test_stringbuf_replace(apr_pool_t *pool)
607
642
{
608
 
  a = svn_stringbuf_create("odd with some world?", pool);
 
643
  svn_stringbuf_t *a = svn_stringbuf_create("odd with some world?", pool);
609
644
 
610
645
  svn_stringbuf_replace(a, 0, 3, "tester", 4);
611
646
  SVN_TEST_STRING_ASSERT(a->data, "test with some world?");
648
683
    const char *stra;
649
684
    const char *strb;
650
685
    apr_size_t lcs;
651
 
    int score;
 
686
    unsigned int score;
652
687
  } tests[] =
653
688
      {
654
 
#define SCORE(lcs, len) ((2000 * (lcs) + (len)/2) / (len))
 
689
#define SCORE(lcs, len) \
 
690
   ((2 * SVN_STRING__SIM_RANGE_MAX * (lcs) + (len)/2) / (len))
655
691
 
656
692
        /* Equality */
657
 
        {"",       "",          0, 1000},
 
693
        {"",       "",          0, SVN_STRING__SIM_RANGE_MAX},
658
694
        {"quoth",  "quoth",     5, SCORE(5, 5+5)},
659
695
 
660
696
        /* Deletion at start */
708
744
  for (t = tests; t->stra; ++t)
709
745
    {
710
746
      apr_size_t lcs;
711
 
      const unsigned int score =
 
747
      const apr_size_t score =
712
748
        svn_cstring__similarity(t->stra, t->strb, &buffer, &lcs);
713
749
      /*
714
750
      fprintf(stderr,
715
 
              "lcs %s ~ %s score %.3f (%"APR_SIZE_T_FMT
716
 
              ") expected %.3f (%"APR_SIZE_T_FMT"))\n",
717
 
              t->stra, t->strb, score/1000.0, lcs, t->score/1000.0, t->lcs);
 
751
              "lcs %s ~ %s score %.6f (%"APR_SIZE_T_FMT
 
752
              ") expected %.6f (%"APR_SIZE_T_FMT"))\n",
 
753
              t->stra, t->strb, score/1.0/SVN_STRING__SIM_RANGE_MAX,
 
754
              lcs, t->score/1.0/SVN_STRING__SIM_RANGE_MAX, t->lcs);
718
755
      */
719
756
      if (score != t->score)
720
 
        return fail(pool, "%s ~ %s score %.3f <> expected %.3f",
721
 
                    t->stra, t->strb, score/1000.0, t->score/1000.0);
 
757
        return fail(pool, "%s ~ %s score %.6f <> expected %.6f",
 
758
                    t->stra, t->strb,
 
759
                    score/1.0/SVN_STRING__SIM_RANGE_MAX,
 
760
                    t->score/1.0/SVN_STRING__SIM_RANGE_MAX);
722
761
 
723
762
      if (lcs != t->lcs)
724
763
        return fail(pool,
731
770
  {
732
771
    const svn_string_t foo = {"svn:foo", 4};
733
772
    const svn_string_t bar = {"svn:bar", 4};
734
 
    if (1000 != svn_string__similarity(&foo, &bar, &buffer, NULL))
 
773
    if (SVN_STRING__SIM_RANGE_MAX
 
774
        != svn_string__similarity(&foo, &bar, &buffer, NULL))
735
775
      return fail(pool, "'%s'[:4] ~ '%s'[:4] found different",
736
776
                  foo.data, bar.data);
737
777
  }
739
779
  return SVN_NO_ERROR;
740
780
}
741
781
 
 
782
static svn_error_t *
 
783
test_string_matching(apr_pool_t *pool)
 
784
{
 
785
  const struct test_data_t
 
786
    {
 
787
      const char *a;
 
788
      const char *b;
 
789
      apr_size_t match_len;
 
790
      apr_size_t rmatch_len;
 
791
    }
 
792
  tests[] =
 
793
    {
 
794
      /* edge cases */
 
795
      {"", "", 0, 0},
 
796
      {"", "x", 0, 0},
 
797
      {"x", "", 0, 0},
 
798
      {"x", "x", 1, 1},
 
799
      {"", "1234567890abcdef", 0, 0},
 
800
      {"1234567890abcdef", "", 0, 0},
 
801
      {"1234567890abcdef", "1234567890abcdef", 16, 16},
 
802
 
 
803
      /* left-side matches */
 
804
      {"x", "y", 0, 0},
 
805
      {"ax", "ay", 1, 0},
 
806
      {"ax", "a", 1, 0},
 
807
      {"a", "ay", 1, 0},
 
808
      {"1234567890abcdef", "1234567890abcdeg", 15, 0},
 
809
      {"1234567890abcdef_", "1234567890abcdefg", 16, 0},
 
810
      {"12345678_0abcdef", "1234567890abcdeg", 8, 0},
 
811
      {"1234567890abcdef", "12345678", 8, 0},
 
812
      {"12345678", "1234567890abcdef", 8, 0},
 
813
      {"12345678_0ab", "1234567890abcdef", 8, 0},
 
814
 
 
815
      /* right-side matches */
 
816
      {"xa", "ya", 0, 1},
 
817
      {"xa", "a", 0, 1},
 
818
      {"a", "ya", 0, 1},
 
819
      {"_234567890abcdef", "1234567890abcdef", 0, 15},
 
820
      {"_1234567890abcdef", "x1234567890abcdef", 0, 16},
 
821
      {"1234567_90abcdef", "_1234567890abcdef", 0, 8},
 
822
      {"1234567890abcdef", "90abcdef", 0, 8},
 
823
      {"90abcdef", "1234567890abcdef", 0, 8},
 
824
      {"8_0abcdef", "7890abcdef", 0, 7},
 
825
 
 
826
      /* two-side matches */
 
827
      {"bxa", "bya", 1, 1},
 
828
      {"bxa", "ba", 1, 1},
 
829
      {"ba", "bya", 1, 1},
 
830
      {"1234567_90abcdef", "1234567890abcdef", 7, 8},
 
831
      {"12345678_90abcdef", "1234567890abcdef", 8, 8},
 
832
      {"12345678_0abcdef", "1234567890abcdef", 8, 7},
 
833
      {"123456_abcdef", "1234sdffdssdf567890abcdef", 4, 6},
 
834
      {"1234567890abcdef", "12345678ef", 8, 2},
 
835
      {"x_234567890abcdef", "x1234567890abcdef", 1, 15},
 
836
      {"1234567890abcdefx", "1234567890abcdex", 15, 1},
 
837
 
 
838
      /* list terminator */
 
839
      {NULL}
 
840
    };
 
841
 
 
842
  const struct test_data_t *test;
 
843
  for (test = tests; test->a != NULL; ++test)
 
844
    {
 
845
      apr_size_t a_len = strlen(test->a);
 
846
      apr_size_t b_len = strlen(test->b);
 
847
      apr_size_t max_match = MIN(a_len, b_len);
 
848
      apr_size_t match_len
 
849
        = svn_cstring__match_length(test->a, test->b, max_match);
 
850
      apr_size_t rmatch_len
 
851
        = svn_cstring__reverse_match_length(test->a + a_len, test->b + b_len,
 
852
                                            max_match);
 
853
 
 
854
      SVN_TEST_ASSERT(match_len == test->match_len);
 
855
      SVN_TEST_ASSERT(rmatch_len == test->rmatch_len);
 
856
    }
 
857
 
 
858
  return SVN_NO_ERROR;
 
859
}
 
860
 
 
861
static svn_error_t *
 
862
test_string_skip_prefix(apr_pool_t *pool)
 
863
{
 
864
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("12345", "12345"),
 
865
                         "");
 
866
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("12345", "123"),
 
867
                         "45");
 
868
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("12345", ""),
 
869
                         "12345");
 
870
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("12345", "23"),
 
871
                         NULL);
 
872
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("1", "12"),
 
873
                         NULL);
 
874
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("", ""),
 
875
                         "");
 
876
  SVN_TEST_STRING_ASSERT(svn_cstring_skip_prefix("", "12"),
 
877
                         NULL);
 
878
 
 
879
  return SVN_NO_ERROR;
 
880
}
 
881
 
742
882
/*
743
883
   ====================================================================
744
884
   If you add a new test to this file, update this array.
747
887
*/
748
888
 
749
889
/* An array of all test functions */
750
 
struct svn_test_descriptor_t test_funcs[] =
 
890
 
 
891
static int max_threads = 1;
 
892
 
 
893
static struct svn_test_descriptor_t test_funcs[] =
751
894
  {
752
895
    SVN_TEST_NULL,
753
896
    SVN_TEST_PASS2(test1,
783
926
    SVN_TEST_PASS2(test16,
784
927
                   "find_char_backward; len = 0 case"),
785
928
    SVN_TEST_PASS2(test17,
786
 
                   "find_char_backward; no occurence case"),
 
929
                   "find_char_backward; no occurrence case"),
787
930
    SVN_TEST_PASS2(test18,
788
931
                   "check whitespace removal; common case"),
789
932
    SVN_TEST_PASS2(test19,
798
941
                   "compare stringbufs; same length, different content"),
799
942
    SVN_TEST_PASS2(test24,
800
943
                   "verify i64toa"),
 
944
    SVN_TEST_PASS2(test_base36,
 
945
                   "verify base36 conversion"),
801
946
    SVN_TEST_PASS2(test_stringbuf_insert,
802
947
                   "check inserting into svn_stringbuf_t"),
803
948
    SVN_TEST_PASS2(test_stringbuf_remove,
806
951
                   "check replacement in svn_stringbuf_t"),
807
952
    SVN_TEST_PASS2(test_string_similarity,
808
953
                   "test string similarity scores"),
 
954
    SVN_TEST_PASS2(test_string_matching,
 
955
                   "test string matching"),
 
956
    SVN_TEST_PASS2(test_string_skip_prefix,
 
957
                   "test svn_cstring_skip_prefix()"),
809
958
    SVN_TEST_NULL
810
959
  };
 
960
 
 
961
SVN_TEST_MAIN