summaryrefslogtreecommitdiff
path: root/subversion/tests/libsvn_fs/locks-test.c
blob: 7f743ac16399ba4e299b4a99f722d2dad024a5ff (plain)
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
/* lock-test.c --- tests for the filesystem locking functions
 *
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 */

#include <string.h>
#include <apr_pools.h>
#include <apr_time.h>

#include "../svn_test.h"

#include "svn_error.h"
#include "svn_fs.h"
#include "svn_hash.h"

#include "../svn_test_fs.h"


/*-----------------------------------------------------------------*/

/** Helper functions **/

/* Implementations of the svn_fs_get_locks_callback_t interface and
   baton, for verifying expected output from svn_fs_get_locks(). */

struct get_locks_baton_t
{
  apr_hash_t *locks;
};

static svn_error_t *
get_locks_callback(void *baton,
                   svn_lock_t *lock,
                   apr_pool_t *pool)
{
  struct get_locks_baton_t *b = baton;
  apr_pool_t *hash_pool = apr_hash_pool_get(b->locks);
  svn_string_t *lock_path = svn_string_create(lock->path, hash_pool);

  if (!apr_hash_get(b->locks, lock_path->data, lock_path->len))
    {
      apr_hash_set(b->locks, lock_path->data, lock_path->len,
                   svn_lock_dup(lock, hash_pool));
      return SVN_NO_ERROR;
    }
  else
    {
      return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
                               "Lock for path '%s' is being reported twice.",
                               lock->path);
    }
}

/* A factory function. */

static struct get_locks_baton_t *
make_get_locks_baton(apr_pool_t *pool)
{
  struct get_locks_baton_t *baton = apr_pcalloc(pool, sizeof(*baton));
  baton->locks = apr_hash_make(pool);
  return baton;
}


/* And verification function(s). */

static svn_error_t *
verify_matching_lock_paths(struct get_locks_baton_t *baton,
                           const char *expected_paths[],
                           apr_size_t num_expected_paths,
                           apr_pool_t *pool)
{
  apr_size_t i;
  if (num_expected_paths != apr_hash_count(baton->locks))
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Unexpected number of locks.");
  for (i = 0; i < num_expected_paths; i++)
    {
      const char *path = expected_paths[i];
      if (! apr_hash_get(baton->locks, path, APR_HASH_KEY_STRING))
        return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
                                 "Missing lock for path '%s'", path);
    }
  return SVN_NO_ERROR;
}


/* Create a filesystem in a directory called NAME, and populate it with
 * the standard Greek tree.  Set *FS_P to the new filesystem object and
 * *NEWREV_P to the head revision number.  Unwanted outputs may be NULL. */
static svn_error_t *
create_greek_fs(svn_fs_t **fs_p,
                svn_revnum_t *newrev_p,
                const char *name,
                const svn_test_opts_t *opts,
                apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;

  /* Prepare a filesystem and a new txn. */
  SVN_ERR(svn_test__create_fs(&fs, name, opts, pool));
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, 0, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));

  /* Create the greek tree and commit it. */
  SVN_ERR(svn_test__create_greek_tree(txn_root, pool));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  if (fs_p)
    *fs_p = fs;
  if (newrev_p)
    *newrev_p = newrev;
  return SVN_NO_ERROR;
}


/*-----------------------------------------------------------------*/

/** The actual lock-tests called by `make check` **/



/* Test that we can create a lock--nothing more.  */
static svn_error_t *
lock_only(const svn_test_opts_t *opts,
          apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *mylock;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-lock-only",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock /A/D/G/rho. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  return SVN_NO_ERROR;
}





/* Test that we can create, fetch, and destroy a lock.  It exercises
   each of the five public fs locking functions.  */
static svn_error_t *
lookup_lock_by_path(const svn_test_opts_t *opts,
                    apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *mylock, *somelock;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-lookup-lock-by-path",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock /A/D/G/rho. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Can we look up the lock by path? */
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if ((! somelock) || (strcmp(somelock->token, mylock->token) != 0))
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Couldn't look up a lock by pathname.");

  return SVN_NO_ERROR;
}

/* Test that we can create a lock outside of the fs and attach it to a
   path.  */
static svn_error_t *
attach_lock(const svn_test_opts_t *opts,
            apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *somelock;
  svn_lock_t *mylock;
  const char *token;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-attach-lock",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  SVN_ERR(svn_fs_generate_lock_token(&token, fs, pool));
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", token,
                      "This is a comment.  Yay comment!", 0,
                      apr_time_now() + apr_time_from_sec(3),
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Can we look up the lock by path? */
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if ((! somelock) || (strcmp(somelock->token, mylock->token) != 0))
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Couldn't look up a lock by pathname.");

  /* Unlock /A/D/G/rho, and verify that it's gone. */
  SVN_ERR(svn_fs_unlock(fs, mylock->path, mylock->token, 0, pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (somelock)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Removed a lock, but it's still there.");

  return SVN_NO_ERROR;
}


/* Test that we can get all locks under a directory. */
static svn_error_t *
get_locks(const svn_test_opts_t *opts,
          apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *mylock;
  struct get_locks_baton_t *get_locks_baton;
  apr_size_t i, num_expected_paths;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-get-locks",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock our paths; verify from "/". */
  {
    static const char *expected_paths[] = {
      "/A/D/G/pi",
      "/A/D/G/rho",
      "/A/D/G/tau",
      "/A/D/H/psi",
      "/A/D/H/chi",
      "/A/D/H/omega",
      "/A/B/E/alpha",
      "/A/B/E/beta",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    for (i = 0; i < num_expected_paths; i++)
      {
        SVN_ERR(svn_fs_lock(&mylock, fs, expected_paths[i], NULL, "", 0, 0,
                            SVN_INVALID_REVNUM, FALSE, pool));
      }
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Verify from "/A/B". */
  {
    static const char *expected_paths[] = {
      "/A/B/E/alpha",
      "/A/B/E/beta",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "A/B", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Verify from "/A/D". */
  {
    static const char *expected_paths[] = {
      "/A/D/G/pi",
      "/A/D/G/rho",
      "/A/D/G/tau",
      "/A/D/H/psi",
      "/A/D/H/chi",
      "/A/D/H/omega",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "A/D", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Verify from "/A/D/G". */
  {
    static const char *expected_paths[] = {
      "/A/D/G/pi",
      "/A/D/G/rho",
      "/A/D/G/tau",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "A/D/G", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Verify from "/A/D/H/omega". */
  {
    static const char *expected_paths[] = {
      "/A/D/H/omega",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "A/D/H/omega", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Verify from "/iota" (which wasn't locked... tricky...). */
  {
    static const char *expected_paths[] = { 0 };
    num_expected_paths = 0;
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "iota", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* A path that is longer and alphabetically earlier than some locked
     paths, this exercises the r1205848 BDB lock code. */
  {
    static const char *expected_paths[] = { 0 };
    num_expected_paths = 0;
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "A/D/H/ABCDEFGHIJKLMNOPQR", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  return SVN_NO_ERROR;
}


/* Test that we can create, fetch, and destroy a lock.  It exercises
   each of the five public fs locking functions.  */
static svn_error_t *
basic_lock(const svn_test_opts_t *opts,
           apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *mylock, *somelock;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-basic-lock",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock /A/D/G/rho. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Can we look up the lock by path? */
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if ((! somelock) || (strcmp(somelock->token, mylock->token) != 0))
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Couldn't look up a lock by pathname.");

  /* Unlock /A/D/G/rho, and verify that it's gone. */
  SVN_ERR(svn_fs_unlock(fs, mylock->path, mylock->token, 0, pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (somelock)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Removed a lock, but it's still there.");

  return SVN_NO_ERROR;
}



/* Test that locks are enforced -- specifically that both a username
   and token are required to make use of the lock.  */
static svn_error_t *
lock_credentials(const svn_test_opts_t *opts,
                 apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_lock_t *mylock;
  svn_error_t *err;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-repo-lock-credentials",
                          opts, pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock /A/D/G/rho. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Push the proper lock-token into the fs access context. */
  SVN_ERR(svn_fs_access_add_lock_token(access, mylock->token));

  /* Make a new transaction and change rho. */
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_test__set_file_contents(txn_root, "/A/D/G/rho",
                                      "new contents", pool));

  /* We are no longer 'bubba'.  We're nobody. */
  SVN_ERR(svn_fs_set_access(fs, NULL));

  /* Try to commit the file change.  Should fail, because we're nobody. */
  err = svn_fs_commit_txn(&conflict, &newrev, txn, pool);
  SVN_TEST_ASSERT(! SVN_IS_VALID_REVNUM(newrev));
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to commit locked file without any fs username.");
  svn_error_clear(err);

  /* We are now 'hortense'. */
  SVN_ERR(svn_fs_create_access(&access, "hortense", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Try to commit the file change.  Should fail, because we're 'hortense'. */
  err = svn_fs_commit_txn(&conflict, &newrev, txn, pool);
  SVN_TEST_ASSERT(! SVN_IS_VALID_REVNUM(newrev));
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to commit locked file as non-owner.");
  svn_error_clear(err);

  /* Be 'bubba' again. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Try to commit the file change.  Should fail, because there's no token. */
  err = svn_fs_commit_txn(&conflict, &newrev, txn, pool);
  SVN_TEST_ASSERT(! SVN_IS_VALID_REVNUM(newrev));
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to commit locked file with no lock token.");
  svn_error_clear(err);

  /* Push the proper lock-token into the fs access context. */
  SVN_ERR(svn_fs_access_add_lock_token(access, mylock->token));

  /* Commit should now succeed. */
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  return SVN_NO_ERROR;
}



/* Test that locks are enforced at commit time.  Somebody might lock
   something behind your back, right before you run
   svn_fs_commit_txn().  Also, this test verifies that recursive
   lock-checks on directories is working properly. */
static svn_error_t *
final_lock_check(const svn_test_opts_t *opts,
                 apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_lock_t *mylock;
  svn_error_t *err;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-repo-final-lock-check",
                          opts, pool));

  /* Make a new transaction and delete "/A" */
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_fs_delete(txn_root, "/A", pool));

  /* Become 'bubba' and lock "/A/D/G/rho". */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* We are no longer 'bubba'.  We're nobody. */
  SVN_ERR(svn_fs_set_access(fs, NULL));

  /* Try to commit the transaction.  Should fail, because a child of
     the deleted directory is locked by someone else. */
  err = svn_fs_commit_txn(&conflict, &newrev, txn, pool);
  SVN_TEST_ASSERT(! SVN_IS_VALID_REVNUM(newrev));
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to commit dir deletion when a child is locked.");
  svn_error_clear(err);

  /* Supply correct username and token;  commit should work. */
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_access_add_lock_token(access, mylock->token));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  return SVN_NO_ERROR;
}



/* If a directory's child is locked by someone else, we should still
   be able to commit a propchange on the directory. */
static svn_error_t *
lock_dir_propchange(const svn_test_opts_t *opts,
                    apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_lock_t *mylock;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-repo-lock-dir-propchange",
                          opts, pool));

  /* Become 'bubba' and lock "/A/D/G/rho". */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* We are no longer 'bubba'.  We're nobody. */
  SVN_ERR(svn_fs_set_access(fs, NULL));

  /* Make a new transaction and make a propchange on "/A" */
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_fs_change_node_prop(txn_root, "/A",
                                  "foo", svn_string_create("bar", pool),
                                  pool));

  /* Commit should succeed;  this means we're doing a non-recursive
     lock-check on directory, rather than a recursive one.  */
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  return SVN_NO_ERROR;
}

/* Test that locks auto-expire correctly. */
static svn_error_t *
lock_expiration(const svn_test_opts_t *opts,
                apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_lock_t *mylock;
  svn_error_t *err;
  struct get_locks_baton_t *get_locks_baton;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-repo-lock-expiration",
                          opts, pool));

  /* Make a new transaction and change rho. */
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_test__set_file_contents(txn_root, "/A/D/G/rho",
                                      "new contents", pool));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Lock /A/D/G/rho, with an expiration 2 seconds from now. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0,
                      apr_time_now() + apr_time_from_sec(2),
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Become nobody. */
  SVN_ERR(svn_fs_set_access(fs, NULL));

  /* Try to commit.  Should fail because we're 'nobody', and the lock
     hasn't expired yet. */
  err = svn_fs_commit_txn(&conflict, &newrev, txn, pool);
  SVN_TEST_ASSERT(! SVN_IS_VALID_REVNUM(newrev));
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to commit a file that has a non-expired lock.");
  svn_error_clear(err);

  /* Check that the lock is there, by getting it via the paths parent. */
  {
    static const char *expected_paths [] = {
      "/A/D/G/rho"
    };
    apr_size_t num_expected_paths = (sizeof(expected_paths)
                                     / sizeof(expected_paths[0]));
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "/A/D/G", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  /* Sleep 2 seconds, so the lock auto-expires.  Anonymous commit
     should then succeed. */
  apr_sleep(apr_time_from_sec(3));

  /* Verify that the lock auto-expired even in the recursive case. */
  {
    static const char *expected_paths [] = { 0 };
    apr_size_t num_expected_paths = 0;
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "/A/D/G", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  return SVN_NO_ERROR;
}

/* Test that a lock can be broken, stolen, or refreshed */
static svn_error_t *
lock_break_steal_refresh(const svn_test_opts_t *opts,
                         apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_lock_t *mylock, *somelock;

  SVN_ERR(create_greek_fs(&fs, NULL, "test-repo-steal-refresh",
                          opts, pool));

  /* Become 'bubba' and lock "/A/D/G/rho". */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));

  /* Become 'hortense' and break bubba's lock, then verify it's gone. */
  SVN_ERR(svn_fs_create_access(&access, "hortense", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_unlock(fs, mylock->path, mylock->token,
                        1 /* FORCE BREAK */, pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (somelock)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Tried to break a lock, but it's still there.");

  /* As hortense, create a new lock, and verify that we own it. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0,
                      SVN_INVALID_REVNUM, FALSE, pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (strcmp(somelock->owner, mylock->owner) != 0)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Made a lock, but we don't seem to own it.");

  /* As bubba, steal hortense's lock, creating a new one that expires. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0,
                      apr_time_now() + apr_time_from_sec(300), /* 5 min. */
                      SVN_INVALID_REVNUM,
                      TRUE /* FORCE STEAL */,
                      pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (strcmp(somelock->owner, mylock->owner) != 0)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Made a lock, but we don't seem to own it.");
  if (! somelock->expiration_date)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Made expiring lock, but seems not to expire.");

  /* Refresh the lock, so that it never expires. */
  SVN_ERR(svn_fs_lock(&somelock, fs, somelock->path, somelock->token,
                      somelock->comment, 0, 0,
                      SVN_INVALID_REVNUM,
                      TRUE /* FORCE STEAL */,
                      pool));
  SVN_ERR(svn_fs_get_lock(&somelock, fs, "/A/D/G/rho", pool));
  if (somelock->expiration_date)
    return svn_error_create(SVN_ERR_TEST_FAILED, NULL,
                            "Made non-expirirng lock, but it expires.");

  return SVN_NO_ERROR;
}


/* Test that svn_fs_lock() and svn_fs_attach_lock() can do
   out-of-dateness checks..  */
static svn_error_t *
lock_out_of_date(const svn_test_opts_t *opts,
                 apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_lock_t *mylock;
  svn_error_t *err;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-repo-lock-out-of-date",
                          opts, pool));

  /* Commit a small change to /A/D/G/rho, creating revision 2. */
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_test__set_file_contents(txn_root, "/A/D/G/rho",
                                      "new contents", pool));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));
  SVN_TEST_ASSERT(SVN_IS_VALID_REVNUM(newrev));

  /* We are now 'bubba'. */
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Try to lock /A/D/G/rho, but claim that we still have r1 of the file. */
  err = svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0, 0, 1, FALSE, pool);
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to lock an out-of-date file.");
  svn_error_clear(err);

  /* Attempt lock again, this time claiming to have r2. */
  SVN_ERR(svn_fs_lock(&mylock, fs, "/A/D/G/rho", NULL, "", 0,
                      0, 2, FALSE, pool));

  /* 'Refresh' the lock, claiming to have r1... should fail. */
  err = svn_fs_lock(&mylock, fs, mylock->path,
                    mylock->token, mylock->comment, 0,
                    apr_time_now() + apr_time_from_sec(50),
                    1,
                    TRUE /* FORCE STEAL */,
                    pool);
  if (! err)
    return svn_error_create
      (SVN_ERR_TEST_FAILED, NULL,
       "Uhoh, able to refresh a lock on an out-of-date file.");
  svn_error_clear(err);

  return SVN_NO_ERROR;
}

struct lock_result_t {
  const svn_lock_t *lock;
  svn_error_t *fs_err;
};

static svn_error_t *
expect_lock(const char *path,
            apr_hash_t *results,
            svn_fs_t *fs,
            apr_pool_t *scratch_pool)
{
  svn_lock_t *lock;
  struct lock_result_t *result = svn_hash_gets(results, path);

  SVN_TEST_ASSERT(result && result->lock && !result->fs_err);
  SVN_ERR(svn_fs_get_lock(&lock, fs, path, scratch_pool));
  SVN_TEST_ASSERT(lock);
  return SVN_NO_ERROR;
}

static svn_error_t *
expect_error(const char *path,
             apr_hash_t *results,
             svn_fs_t *fs,
             apr_pool_t *scratch_pool)
{
  svn_lock_t *lock;
  struct lock_result_t *result = svn_hash_gets(results, path);

  SVN_TEST_ASSERT(result && !result->lock && result->fs_err);
  svn_error_clear(result->fs_err);
  SVN_ERR(svn_fs_get_lock(&lock, fs, path, scratch_pool));
  SVN_TEST_ASSERT(!lock);
  return SVN_NO_ERROR;
}

static svn_error_t *
expect_unlock(const char *path,
              apr_hash_t *results,
              svn_fs_t *fs,
              apr_pool_t *scratch_pool)
{
  svn_lock_t *lock;
  struct lock_result_t *result = svn_hash_gets(results, path);

  SVN_TEST_ASSERT(result && !result->fs_err);
  SVN_ERR(svn_fs_get_lock(&lock, fs, path, scratch_pool));
  SVN_TEST_ASSERT(!lock);
  return SVN_NO_ERROR;
}

static svn_error_t *
expect_unlock_error(const char *path,
                    apr_hash_t *results,
                    svn_fs_t *fs,
                    apr_pool_t *scratch_pool)
{
  svn_lock_t *lock;
  struct lock_result_t *result = svn_hash_gets(results, path);

  SVN_TEST_ASSERT(result && result->fs_err);
  svn_error_clear(result->fs_err);
  SVN_ERR(svn_fs_get_lock(&lock, fs, path, scratch_pool));
  SVN_TEST_ASSERT(lock);
  return SVN_NO_ERROR;
}

struct lock_many_baton_t {
  apr_hash_t *results;
  apr_pool_t *pool;
  int count;
};

/* Implements svn_fs_lock_callback_t. */
static svn_error_t *
lock_many_cb(void *lock_baton,
             const char *path,
             const svn_lock_t *lock,
             svn_error_t *fs_err,
             apr_pool_t *pool)
{
  struct lock_many_baton_t *b = lock_baton;
  struct lock_result_t *result = apr_palloc(b->pool,
                                            sizeof(struct lock_result_t));

  result->lock = lock;
  result->fs_err = svn_error_dup(fs_err);
  svn_hash_sets(b->results, apr_pstrdup(b->pool, path), result);

  if (b->count)
    if (!--(b->count))
      return svn_error_create(SVN_ERR_FS_GENERAL, NULL, "lock_many_cb");

  return SVN_NO_ERROR;
}

static svn_error_t *
lock_multiple_paths(const svn_test_opts_t *opts,
                    apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_txn_t *txn;
  svn_fs_root_t *root, *txn_root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_fs_lock_target_t *target;
  struct lock_many_baton_t baton;
  apr_hash_t *lock_paths, *unlock_paths;
  apr_hash_index_t *hi;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-lock-multiple-paths",
                          opts, pool));
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));
  SVN_ERR(svn_fs_revision_root(&root, fs, newrev, pool));
  SVN_ERR(svn_fs_begin_txn2(&txn, fs, newrev, SVN_FS_TXN_CHECK_LOCKS, pool));
  SVN_ERR(svn_fs_txn_root(&txn_root, txn, pool));
  SVN_ERR(svn_fs_make_dir(txn_root, "/A/BB", pool));
  SVN_ERR(svn_fs_make_dir(txn_root, "/A/BBB", pool));
  SVN_ERR(svn_fs_copy(root, "/A/mu", txn_root, "/A/BB/mu", pool));
  SVN_ERR(svn_fs_copy(root, "/A/mu", txn_root, "/A/BBB/mu", pool));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));

  baton.results = apr_hash_make(pool);
  baton.pool = pool;
  baton.count = 0;
  lock_paths = apr_hash_make(pool);
  unlock_paths = apr_hash_make(pool);
  target = svn_fs_lock_target_create(NULL, newrev, pool);

  svn_hash_sets(lock_paths, "/A/B/E/alpha", target);
  svn_hash_sets(lock_paths, "/A/B/E/beta", target);
  svn_hash_sets(lock_paths, "/A/B/E/zulu", target);
  svn_hash_sets(lock_paths, "/A/BB/mu", target);
  svn_hash_sets(lock_paths, "/A/BBB/mu", target);
  svn_hash_sets(lock_paths, "/A/D/G/pi", target);
  svn_hash_sets(lock_paths, "/A/D/G/rho", target);
  svn_hash_sets(lock_paths, "/A/mu", target);
  svn_hash_sets(lock_paths, "/X/zulu", target);

  /* Lock some paths. */
  apr_hash_clear(baton.results);
  SVN_ERR(svn_fs_lock_many(fs, lock_paths, "comment", 0, 0, 0,
                           lock_many_cb, &baton,
                           pool, pool));

  SVN_ERR(expect_lock("/A/B/E/alpha", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/B/E/beta", baton.results, fs, pool));
  SVN_ERR(expect_error("/A/B/E/zulu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/BB/mu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/BBB/mu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/D/G/pi", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/D/G/rho", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/mu", baton.results, fs, pool));
  SVN_ERR(expect_error("/X/zulu", baton.results, fs, pool));

  /* Unlock without force and wrong tokens. */
  for (hi = apr_hash_first(pool, lock_paths); hi; hi = apr_hash_next(hi))
    svn_hash_sets(unlock_paths, apr_hash_this_key(hi), "wrong-token");
  apr_hash_clear(baton.results);
  SVN_ERR(svn_fs_unlock_many(fs, unlock_paths, FALSE, lock_many_cb, &baton,
                             pool, pool));

  SVN_ERR(expect_unlock_error("/A/B/E/alpha", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/B/E/beta", baton.results, fs, pool));
  SVN_ERR(expect_error("/A/B/E/zulu", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/BB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/BBB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/D/G/pi", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/D/G/rho", baton.results, fs, pool));
  SVN_ERR(expect_unlock_error("/A/mu", baton.results, fs, pool));
  SVN_ERR(expect_error("/X/zulu", baton.results, fs, pool));

  /* Force unlock. */
  for (hi = apr_hash_first(pool, lock_paths); hi; hi = apr_hash_next(hi))
    svn_hash_sets(unlock_paths, apr_hash_this_key(hi), "");
  apr_hash_clear(baton.results);
  SVN_ERR(svn_fs_unlock_many(fs, unlock_paths, TRUE, lock_many_cb, &baton,
                             pool, pool));

  SVN_ERR(expect_unlock("/A/B/E/alpha", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/B/E/beta", baton.results, fs, pool));
  SVN_ERR(expect_error("/A/B/E/zulu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/BB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/BBB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/D/G/pi", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/D/G/rho", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/mu", baton.results, fs, pool));
  SVN_ERR(expect_error("/X/zulu", baton.results, fs, pool));

  /* Lock again. */
  apr_hash_clear(baton.results);
  SVN_ERR(svn_fs_lock_many(fs, lock_paths, "comment", 0, 0, 0,
                           lock_many_cb, &baton,
                           pool, pool));

  SVN_ERR(expect_lock("/A/B/E/alpha", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/B/E/beta", baton.results, fs, pool));
  SVN_ERR(expect_error("/A/B/E/zulu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/BB/mu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/BBB/mu", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/D/G/pi", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/D/G/rho", baton.results, fs, pool));
  SVN_ERR(expect_lock("/A/mu", baton.results, fs, pool));
  SVN_ERR(expect_error("/X/zulu", baton.results, fs, pool));

  /* Unlock without force. */
  for (hi = apr_hash_first(pool, baton.results); hi; hi = apr_hash_next(hi))
    {
      struct lock_result_t *result = apr_hash_this_val(hi);
      svn_hash_sets(unlock_paths, apr_hash_this_key(hi),
                    result->lock ? result->lock->token : "non-existent-token");
    }
  apr_hash_clear(baton.results);
  SVN_ERR(svn_fs_unlock_many(fs, unlock_paths, FALSE, lock_many_cb, &baton,
                             pool, pool));

  SVN_ERR(expect_unlock("/A/B/E/alpha", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/B/E/beta", baton.results, fs, pool));
  SVN_ERR(expect_error("/A/B/E/zulu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/BB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/BBB/mu", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/D/G/pi", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/D/G/rho", baton.results, fs, pool));
  SVN_ERR(expect_unlock("/A/mu", baton.results, fs, pool));
  SVN_ERR(expect_error("/X/zulu", baton.results, fs, pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
lock_cb_error(const svn_test_opts_t *opts,
              apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_fs_lock_target_t *target;
  struct lock_many_baton_t baton;
  apr_hash_t *lock_paths, *unlock_paths;
  svn_lock_t *lock;

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-lock-cb-error", opts, pool));
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  baton.results = apr_hash_make(pool);
  baton.pool = pool;
  baton.count = 1;
  lock_paths = apr_hash_make(pool);
  unlock_paths = apr_hash_make(pool);
  target = svn_fs_lock_target_create(NULL, newrev, pool);

  svn_hash_sets(lock_paths, "/A/B/E/alpha", target);
  svn_hash_sets(lock_paths, "/A/B/E/beta", target);

  apr_hash_clear(baton.results);
  SVN_TEST_ASSERT_ERROR(svn_fs_lock_many(fs, lock_paths, "comment", 0, 0, 0,
                                         lock_many_cb, &baton,
                                         pool, pool),
                        SVN_ERR_FS_GENERAL);

  SVN_TEST_ASSERT(apr_hash_count(baton.results) == 1);
  SVN_TEST_ASSERT(svn_hash_gets(baton.results, "/A/B/E/alpha")
                  || svn_hash_gets(baton.results, "/A/B/E/beta"));
  SVN_ERR(svn_fs_get_lock(&lock, fs, "/A/B/E/alpha", pool));
  SVN_TEST_ASSERT(lock);
  svn_hash_sets(unlock_paths, "/A/B/E/alpha", lock->token);
  SVN_ERR(svn_fs_get_lock(&lock, fs, "/A/B/E/beta", pool));
  SVN_TEST_ASSERT(lock);
  svn_hash_sets(unlock_paths, "/A/B/E/beta", lock->token);

  baton.count = 1;
  apr_hash_clear(baton.results);
  SVN_TEST_ASSERT_ERROR(svn_fs_unlock_many(fs, unlock_paths, FALSE,
                                           lock_many_cb, &baton,
                                           pool, pool),
                        SVN_ERR_FS_GENERAL);

  SVN_TEST_ASSERT(apr_hash_count(baton.results) == 1);
  SVN_TEST_ASSERT(svn_hash_gets(baton.results, "/A/B/E/alpha")
                  || svn_hash_gets(baton.results, "/A/B/E/beta"));

  SVN_ERR(svn_fs_get_lock(&lock, fs, "/A/B/E/alpha", pool));
  SVN_TEST_ASSERT(!lock);
  SVN_ERR(svn_fs_get_lock(&lock, fs, "/A/B/E/beta", pool));
  SVN_TEST_ASSERT(!lock);

  return SVN_NO_ERROR;
}

/* XXX NOTE:
   This test will fail on most Unix-like systems when run as the
   root user, because flock() will ignore file permissions. */
static svn_error_t *
obtain_write_lock_failure(const svn_test_opts_t *opts,
                          apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_revnum_t newrev;
  svn_fs_access_t *access;
  svn_fs_lock_target_t *target;
  struct lock_many_baton_t baton;
  apr_hash_t *lock_paths, *unlock_paths;

  /* The test makes sense only for FSFS. */
  if (strcmp(opts->fs_type, SVN_FS_TYPE_FSFS) != 0
      && strcmp(opts->fs_type, SVN_FS_TYPE_FSX) != 0)
    return svn_error_create(SVN_ERR_TEST_SKIPPED, NULL,
                            "this will test FSFS/FSX repositories only");

  SVN_ERR(create_greek_fs(&fs, &newrev, "test-obtain-write-lock-failure",
                          opts, pool));
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Make a read only 'write-lock' file.  This prevents any write operations
     from being executed. */
  SVN_ERR(svn_io_set_file_read_only("test-obtain-write-lock-failure/write-lock",
                                    FALSE, pool));

  baton.results = apr_hash_make(pool);
  baton.pool = pool;
  baton.count = 0;

  /* Trying to lock some paths.  We don't really care about error; the test
     shouldn't crash. */
  target = svn_fs_lock_target_create(NULL, newrev, pool);
  lock_paths = apr_hash_make(pool);
  svn_hash_sets(lock_paths, "/iota", target);
  svn_hash_sets(lock_paths, "/A/mu", target);

  apr_hash_clear(baton.results);
  SVN_TEST_ASSERT_ANY_ERROR(svn_fs_lock_many(fs, lock_paths, "comment", 0, 0, 0,
                                             lock_many_cb, &baton, pool, pool));

  /* Trying to unlock some paths.  We don't really care about error; the test
     shouldn't crash. */
  unlock_paths = apr_hash_make(pool);
  svn_hash_sets(unlock_paths, "/iota", "");
  svn_hash_sets(unlock_paths, "/A/mu", "");

  apr_hash_clear(baton.results);
  SVN_TEST_ASSERT_ANY_ERROR(svn_fs_unlock_many(fs, unlock_paths, TRUE,
                                               lock_many_cb, &baton, pool,
                                               pool));

  return SVN_NO_ERROR;
}

static svn_error_t *
parent_and_child_lock(const svn_test_opts_t *opts,
                      apr_pool_t *pool)
{
  svn_fs_t *fs;
  svn_fs_access_t *access;
  svn_fs_txn_t *txn;
  svn_fs_root_t *root;
  const char *conflict;
  svn_revnum_t newrev;
  svn_lock_t *lock;
  struct get_locks_baton_t *get_locks_baton;
  apr_size_t num_expected_paths;

  SVN_ERR(svn_test__create_fs(&fs, "test-parent-and-child-lock", opts, pool));
  SVN_ERR(svn_fs_create_access(&access, "bubba", pool));
  SVN_ERR(svn_fs_set_access(fs, access));

  /* Make a file '/A'. */
  SVN_ERR(svn_fs_begin_txn(&txn, fs, 0, pool));
  SVN_ERR(svn_fs_txn_root(&root, txn, pool));
  SVN_ERR(svn_fs_make_file(root, "/A", pool));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));

  /* Obtain a lock on '/A'. */
  SVN_ERR(svn_fs_lock(&lock, fs, "/A", NULL, NULL, FALSE, 0, newrev, FALSE,
                      pool));

  /* Add a lock token to FS access context. */
  SVN_ERR(svn_fs_access_add_lock_token(access, lock->token));

  /* Make some weird change: replace file '/A' by a directory with a
     child.  Issue 2507 means that the result is that the directory /A
     remains locked. */
  SVN_ERR(svn_fs_begin_txn(&txn, fs, newrev, pool));
  SVN_ERR(svn_fs_txn_root(&root, txn, pool));
  SVN_ERR(svn_fs_delete(root, "/A", pool));
  SVN_ERR(svn_fs_make_dir(root, "/A", pool));
  SVN_ERR(svn_fs_make_file(root, "/A/b", pool));
  SVN_ERR(svn_fs_commit_txn(&conflict, &newrev, txn, pool));

  /* Obtain a lock on '/A/b'.  Issue 2507 means that the lock index
     for / refers to both /A and /A/b, and that the lock index for /A
     refers to /A/b. */
  SVN_ERR(svn_fs_lock(&lock, fs, "/A/b", NULL, NULL, FALSE, 0, newrev, FALSE,
                      pool));

  /* Verify the locked paths. The lock for /A/b should not be reported
     twice even though issue 2507 means we access the index for / and
     the index for /A both of which refer to /A/b. */
  {
    static const char *expected_paths[] = {
      "/A",
      "/A/b",
    };
    num_expected_paths = sizeof(expected_paths) / sizeof(const char *);
    get_locks_baton = make_get_locks_baton(pool);
    SVN_ERR(svn_fs_get_locks(fs, "/", get_locks_callback,
                             get_locks_baton, pool));
    SVN_ERR(verify_matching_lock_paths(get_locks_baton, expected_paths,
                                       num_expected_paths, pool));
  }

  return SVN_NO_ERROR;
}

/* ------------------------------------------------------------------------ */

/* The test table.  */

static int max_threads = 2;

static struct svn_test_descriptor_t test_funcs[] =
  {
    SVN_TEST_NULL,
    SVN_TEST_OPTS_PASS(lock_expiration,
                       "test that locks can expire"),
    SVN_TEST_OPTS_PASS(lock_only,
                       "lock only"),
    SVN_TEST_OPTS_PASS(lookup_lock_by_path,
                       "lookup lock by path"),
    SVN_TEST_OPTS_PASS(attach_lock,
                       "attach lock"),
    SVN_TEST_OPTS_PASS(get_locks,
                       "get locks"),
    SVN_TEST_OPTS_PASS(basic_lock,
                       "basic locking"),
    SVN_TEST_OPTS_PASS(lock_credentials,
                       "test that locking requires proper credentials"),
    SVN_TEST_OPTS_PASS(final_lock_check,
                       "test that locking is enforced in final commit step"),
    SVN_TEST_OPTS_PASS(lock_dir_propchange,
                       "dir propchange can be committed with locked child"),
    SVN_TEST_OPTS_PASS(lock_break_steal_refresh,
                       "breaking, stealing, refreshing a lock"),
    SVN_TEST_OPTS_PASS(lock_out_of_date,
                       "check out-of-dateness before locking"),
    SVN_TEST_OPTS_PASS(lock_multiple_paths,
                       "lock multiple paths"),
    SVN_TEST_OPTS_PASS(lock_cb_error,
                       "lock callback error"),
    SVN_TEST_OPTS_PASS(obtain_write_lock_failure,
                       "lock/unlock when 'write-lock' couldn't be obtained"),
    SVN_TEST_OPTS_PASS(parent_and_child_lock,
                       "lock parent and it's child"),
    SVN_TEST_NULL
  };

SVN_TEST_MAIN