|
1
|
# |
|
2
|
# Copyright (c) 2026 Dan Mestas |
|
3
|
# |
|
4
|
# This program is free software; you can redistribute it and/or |
|
5
|
# modify it under the terms of the Simplified BSD License (also |
|
6
|
# known as the "2-Clause License" or "FreeBSD License".) |
|
7
|
# |
|
8
|
# This program is distributed in the hope that it will be useful, |
|
9
|
# but without any warranty; without even the implied warranty of |
|
10
|
# merchantability or fitness for a particular purpose. |
|
11
|
# |
|
12
|
############################################################################ |
|
13
|
# |
|
14
|
# Random integration tests of sync. Each scenario input in corpus/corpus.pack |
|
15
|
# is a list of operations to perform on three clones of a new repository: |
|
16
|
# commits, branches, merges, syncs and so on. A sync can be made to lose |
|
17
|
# its request, lose its reply, or deliver half a reply. |
|
18
|
# |
|
19
|
# A sync, pull, push or clone that is not made to go wrong must succeed |
|
20
|
# and must deliver: straight afterwards the receiving clone must hold |
|
21
|
# everything the sending clone holds. Any other operation may fail in |
|
22
|
# the ordinary way, because many random operations are not valid: a |
|
23
|
# commit with nothing changed, a branch that already exists. No command |
|
24
|
# may crash or run for more than two minutes. |
|
25
|
# |
|
26
|
# At the end the clones are synced along the chain 0-1-2, so that |
|
27
|
# whatever clones 0 and 2 learn of each other has to pass through clone |
|
28
|
# 1. Each must then pass test-integrity and test-missing, and all three |
|
29
|
# must hold the same artifacts and the same unversioned files. |
|
30
|
# |
|
31
|
# An input is a list of four-byte records: operation, clone, argument, |
|
32
|
# option. See scenario_run below for what each means. |
|
33
|
# |
|
34
|
# To run random inputs as well as the files, set the environment variable |
|
35
|
# FOSSIL_SCENARIO_RANDOM to how many. Each is made from a seed, which is |
|
36
|
# printed with its result. FOSSIL_SCENARIO_SEED sets the first seed, so |
|
37
|
# that a failing run can be repeated. |
|
38
|
# |
|
39
|
# Every operation is a separate run of fossil, so this file takes about |
|
40
|
# a minute. |
|
41
|
# |
|
42
|
|
|
43
|
# One input runs at every level, and the rest need -assurance stress. |
|
44
|
require_no_open_checkout |
|
45
|
|
|
46
|
test_setup "" |
|
47
|
|
|
48
|
set scenarioTop [pwd] |
|
49
|
set scenarioSeconds 120 ;# time allowed for any one command |
|
50
|
set scenarioMaxOps 64 ;# use at most this many records of an input |
|
51
|
|
|
52
|
# Make a sanitizer end the program with abort(), so that its report is |
|
53
|
# told from an ordinary failure, and give the commits a user name. The |
|
54
|
# environment is put back at the end. |
|
55
|
# |
|
56
|
foreach {scenarioVar scenarioOpt} { |
|
57
|
ASAN_OPTIONS abort_on_error=1 |
|
58
|
UBSAN_OPTIONS halt_on_error=1:abort_on_error=1 |
|
59
|
USER scenario |
|
60
|
} { |
|
61
|
if {![info exists ::env($scenarioVar)]} { |
|
62
|
set ::env($scenarioVar) $scenarioOpt |
|
63
|
continue |
|
64
|
} |
|
65
|
set scenarioSaved($scenarioVar) $::env($scenarioVar) |
|
66
|
if {$scenarioVar eq "USER"} { |
|
67
|
set ::env(USER) $scenarioOpt |
|
68
|
} elseif {![string match *abort_on_error* $::env($scenarioVar)]} { |
|
69
|
append ::env($scenarioVar) :$scenarioOpt |
|
70
|
} |
|
71
|
} |
|
72
|
|
|
73
|
# Stop the scenario because it has found a problem. |
|
74
|
# |
|
75
|
proc scenario_fail {msg} { |
|
76
|
return -code error -errorcode SCENARIO $msg |
|
77
|
} |
|
78
|
|
|
79
|
# Run fossil with the arguments given in subdirectory $sub of the |
|
80
|
# scenario. Return 0 if it succeeds and 1 if it fails in the ordinary |
|
81
|
# way. A crash or a command that does not finish is a problem found. |
|
82
|
# The output is left in scenarioOut and added to the log. |
|
83
|
# |
|
84
|
proc scenario_fossil {sub args} { |
|
85
|
global scenarioDir scenarioSeconds scenarioOut scenarioLog CODE RESULT |
|
86
|
cd [file join $scenarioDir $sub] |
|
87
|
fossil_raw -seconds $scenarioSeconds {*}$args |
|
88
|
cd $scenarioDir |
|
89
|
set scenarioOut $RESULT |
|
90
|
lappend scenarioLog "+ $sub: fossil $args" $scenarioOut |
|
91
|
if {$CODE eq "timeout"} { |
|
92
|
scenario_fail "a command did not finish in $scenarioSeconds seconds:\ |
|
93
|
fossil $args" |
|
94
|
} |
|
95
|
if {$CODE eq "crash"} { |
|
96
|
scenario_fail "a command crashed: fossil $args" |
|
97
|
} |
|
98
|
return [expr {$CODE != 0}] |
|
99
|
} |
|
100
|
|
|
101
|
# Run a fossil command that must succeed. |
|
102
|
# |
|
103
|
proc scenario_must {sub args} { |
|
104
|
if {[scenario_fossil $sub {*}$args]} { |
|
105
|
scenario_fail "a command that must succeed has failed: fossil $args" |
|
106
|
} |
|
107
|
} |
|
108
|
|
|
109
|
# The number that one SQL query on clone $i gives. |
|
110
|
# |
|
111
|
proc scenario_count {i sql} { |
|
112
|
global scenarioOut |
|
113
|
scenario_must . sql -R r$i.fossil $sql |
|
114
|
set n [string trim $scenarioOut] |
|
115
|
if {![string is integer -strict $n]} { |
|
116
|
scenario_fail "a count is not a number: $n" |
|
117
|
} |
|
118
|
return $n |
|
119
|
} |
|
120
|
|
|
121
|
# Add a line to a file in checkout $i, or replace the file by the line. |
|
122
|
# |
|
123
|
proc scenario_write {i name line {mode a}} { |
|
124
|
global scenarioDir |
|
125
|
set fd [open [file join $scenarioDir c$i $name] $mode] |
|
126
|
puts $fd $line |
|
127
|
close $fd |
|
128
|
} |
|
129
|
|
|
130
|
# Commit whatever has changed in checkout $i. Every commit has a later |
|
131
|
# date than the one before, so that the result does not depend on the |
|
132
|
# clock. |
|
133
|
# |
|
134
|
proc scenario_commit {i} { |
|
135
|
global scenarioCommits |
|
136
|
set n [incr scenarioCommits] |
|
137
|
set date [format "2026-01-%02d %02d:%02d:00" \ |
|
138
|
[expr {2+$n/1440}] [expr {($n/60)%24}] [expr {$n%60}]] |
|
139
|
return [scenario_fossil c$i commit -m "commit $n" --no-warnings \ |
|
140
|
--allow-fork --allow-conflict --allow-older --date-override $date] |
|
141
|
} |
|
142
|
|
|
143
|
# Open a checkout of clone $i in a new directory. |
|
144
|
# |
|
145
|
proc scenario_open {i} { |
|
146
|
global scenarioDir |
|
147
|
file mkdir [file join $scenarioDir c$i] |
|
148
|
scenario_must c$i open ../r$i.fossil |
|
149
|
scenario_must c$i settings autosync off |
|
150
|
scenario_must c$i uv list |
|
151
|
} |
|
152
|
|
|
153
|
# Require that clone $have holds every public artifact that clone $of |
|
154
|
# holds, and every unversioned file too if $uv is true. $when says what |
|
155
|
# has just been done. |
|
156
|
# |
|
157
|
# An artifact that clone $of holds only as a delta from something it is |
|
158
|
# still waiting for does not count: it cannot be sent until its source |
|
159
|
# arrives. A sync that lost its reply can leave one behind. |
|
160
|
# |
|
161
|
proc scenario_holds {have of uv when} { |
|
162
|
set n [scenario_count $have "ATTACH 'r$of.fossil' AS o; |
|
163
|
WITH RECURSIVE waiting(rid) AS ( |
|
164
|
SELECT rid FROM o.delta |
|
165
|
WHERE srcid IN (SELECT rid FROM o.blob WHERE size<0) |
|
166
|
UNION SELECT d.rid FROM o.delta d, waiting w WHERE d.srcid=w.rid) |
|
167
|
SELECT count(*) FROM o.blob WHERE size>=0 |
|
168
|
AND rid NOT IN (SELECT rid FROM o.private) |
|
169
|
AND rid NOT IN waiting |
|
170
|
AND uuid NOT IN (SELECT uuid FROM blob WHERE size>=0)"] |
|
171
|
if {$n > 0} { |
|
172
|
scenario_fail "$when, clone $have lacks $n artifacts that clone $of holds" |
|
173
|
} |
|
174
|
if {$uv} { |
|
175
|
set n [scenario_count $have "ATTACH 'r$of.fossil' AS o; |
|
176
|
SELECT count(*) FROM o.unversioned WHERE hash IS NOT NULL |
|
177
|
AND name||'/'||hash NOT IN (SELECT name||'/'||hash FROM unversioned |
|
178
|
WHERE hash IS NOT NULL)"] |
|
179
|
if {$n > 0} { |
|
180
|
scenario_fail "$when, clone $have lacks $n unversioned files that\ |
|
181
|
clone $of holds" |
|
182
|
} |
|
183
|
} |
|
184
|
} |
|
185
|
|
|
186
|
# Run the scenario whose records are the bytes of $data. The result is |
|
187
|
# an empty string, or what went wrong. |
|
188
|
# |
|
189
|
proc scenario_run {name data} { |
|
190
|
global scenarioTop scenarioDir scenarioLog scenarioCommits scenarioMaxOps |
|
191
|
set scenarioDir [file join $scenarioTop $name] |
|
192
|
set scenarioLog {} |
|
193
|
set scenarioCommits 0 |
|
194
|
file mkdir $scenarioDir |
|
195
|
cd $scenarioDir |
|
196
|
set rc [catch {scenario_steps $data} msg opts] |
|
197
|
cd $scenarioTop |
|
198
|
if {!$rc} { |
|
199
|
file delete -force $scenarioDir |
|
200
|
return "" |
|
201
|
} |
|
202
|
if {[dict get $opts -errorcode] ne "SCENARIO"} { |
|
203
|
set msg [dict get $opts -errorinfo] |
|
204
|
} |
|
205
|
# The last three commands and the end of what each printed go into the |
|
206
|
# report; the directory does not last. |
|
207
|
foreach {cmd out} [lrange $scenarioLog end-5 end] { |
|
208
|
append msg \n$cmd\n[join [lrange [split [string trim $out] \n] end-5 \ |
|
209
|
end] \n] |
|
210
|
} |
|
211
|
return $msg |
|
212
|
} |
|
213
|
|
|
214
|
proc scenario_steps {data} { |
|
215
|
global scenarioDir scenarioMaxOps scenarioOut |
|
216
|
|
|
217
|
# The script that carries a sync that is to go wrong |
|
218
|
# It is run with tclsh, so it needs no permission to be executed. |
|
219
|
set wire [file join $scenarioDir wire.tcl] |
|
220
|
file copy [file join $::testfiledir standin scenario-wire.tcl] $wire |
|
221
|
set wire "\"[info nameofexecutable]\" \"$wire\"" |
|
222
|
|
|
223
|
# One repository with one check-in, cloned twice |
|
224
|
scenario_must . init r0.fossil -A scenario \ |
|
225
|
--date-override "2026-01-01 00:00:00" |
|
226
|
foreach i {0 1 2} { |
|
227
|
if {$i > 0} {scenario_must . clone r0.fossil r$i.fossil} |
|
228
|
scenario_open $i |
|
229
|
if {$i == 0} { |
|
230
|
foreach x {0 1 2} { |
|
231
|
foreach y {1 2 3 4 5 6 7 8 9} { |
|
232
|
scenario_write 0 f$x.txt "file $x line $y" |
|
233
|
} |
|
234
|
} |
|
235
|
scenario_must c0 add f0.txt f1.txt f2.txt |
|
236
|
if {[scenario_commit 0]} {scenario_fail "the first commit failed"} |
|
237
|
} |
|
238
|
} |
|
239
|
|
|
240
|
# Do the operations |
|
241
|
set nOp [expr {min([string length $data]/4, $scenarioMaxOps)}] |
|
242
|
for {set i 0} {$i < $nOp} {incr i} { |
|
243
|
binary scan $data "@[expr {$i*4}]cu4" rec |
|
244
|
lassign $rec op me a opt |
|
245
|
set me [expr {$me % 3}] |
|
246
|
set ck c$me |
|
247
|
set peer [expr {($me + 1 + $a%2) % 3}] |
|
248
|
switch [expr {$op % 14}] { |
|
249
|
0 - 1 { ;# Change a file and commit |
|
250
|
scenario_write $me f[expr {$a%3}].txt "change $i" |
|
251
|
scenario_commit $me |
|
252
|
} |
|
253
|
2 { ;# Add a file and commit |
|
254
|
scenario_write $me n[expr {$a%8}].txt "new $i" |
|
255
|
scenario_fossil $ck add n[expr {$a%8}].txt |
|
256
|
scenario_commit $me |
|
257
|
} |
|
258
|
3 { ;# Remove a file and commit |
|
259
|
if {![scenario_fossil $ck rm --hard f[expr {$a%3}].txt]} { |
|
260
|
scenario_commit $me |
|
261
|
} |
|
262
|
} |
|
263
|
4 { ;# Start a branch, or move to one that exists |
|
264
|
scenario_fossil $ck branch new b[expr {$a%4}] current |
|
265
|
scenario_fossil $ck update b[expr {$a%4}] |
|
266
|
} |
|
267
|
5 { ;# Merge a branch or trunk and commit |
|
268
|
if {$a & 0x80} { |
|
269
|
scenario_fossil $ck merge trunk |
|
270
|
} else { |
|
271
|
scenario_fossil $ck merge b[expr {$a%4}] |
|
272
|
} |
|
273
|
scenario_commit $me |
|
274
|
} |
|
275
|
6 { ;# Sync, pull or push with another clone |
|
276
|
set verb [lindex {sync pull push} [expr {$opt%3}]] |
|
277
|
set fault [expr {($opt/3) % 5}] |
|
278
|
# Only "sync" takes -u. The option bit is ignored otherwise. |
|
279
|
set uv [expr {($opt & 0x80) && $verb eq "sync"}] |
|
280
|
set args [list $verb] |
|
281
|
if {$fault == 0} { |
|
282
|
lappend args ../r$peer.fossil |
|
283
|
} else { |
|
284
|
lappend args http://scenario.invalid/x --once \ |
|
285
|
--transport-command $wire |
|
286
|
} |
|
287
|
if {$uv} {lappend args -u} |
|
288
|
if {$fault == 0} { |
|
289
|
scenario_must $ck {*}$args |
|
290
|
} else { |
|
291
|
# Fault 4 is the script with no fault, to show that it works |
|
292
|
set ::env(SCN_EXE) $::fossilexe |
|
293
|
set ::env(SCN_REPO) [file join $scenarioDir r$peer.fossil] |
|
294
|
set ::env(SCN_FAULT) $fault |
|
295
|
set rc [catch {scenario_fossil $ck {*}$args} failed opts] |
|
296
|
unset ::env(SCN_EXE) ::env(SCN_REPO) ::env(SCN_FAULT) |
|
297
|
if {$rc} {return -options $opts $failed} |
|
298
|
if {$fault == 4 && $failed} { |
|
299
|
scenario_fail "a sync through the wire script, with no fault,\ |
|
300
|
failed" |
|
301
|
} |
|
302
|
if {($fault == 1 || $fault == 2) && !$failed} { |
|
303
|
scenario_fail "a sync that got no reply reported success" |
|
304
|
} |
|
305
|
} |
|
306
|
if {$fault == 0 || $fault == 4} { |
|
307
|
if {$verb ne "push"} {scenario_holds $me $peer $uv $verb} |
|
308
|
if {$verb ne "pull"} {scenario_holds $peer $me $uv $verb} |
|
309
|
} |
|
310
|
} |
|
311
|
7 { ;# Update the checkout |
|
312
|
if {$a & 1} { |
|
313
|
scenario_fossil $ck update trunk |
|
314
|
} else { |
|
315
|
scenario_fossil $ck update |
|
316
|
} |
|
317
|
} |
|
318
|
8 { ;# Add a tag |
|
319
|
scenario_fossil $ck tag add t[expr {$a%4}] current |
|
320
|
} |
|
321
|
9 { ;# Rebuild |
|
322
|
scenario_fossil . rebuild r$me.fossil |
|
323
|
} |
|
324
|
10 { ;# Change how much this clone will send in one message |
|
325
|
set limits {250000 20000 2000 300} |
|
326
|
scenario_fossil . sql -R r$me.fossil "REPLACE INTO |
|
327
|
config(name,value,mtime) |
|
328
|
VALUES('max-upload',[lindex $limits [expr {$a%4}]],0), |
|
329
|
('max-download',[lindex $limits [expr {($a/4)%4}]],0)" |
|
330
|
} |
|
331
|
11 { ;# Commit many files at once |
|
332
|
file mkdir [file join $scenarioDir $ck d$i] |
|
333
|
set n [expr {40 + 40*($a%4)}] |
|
334
|
for {set k 0} {$k < $n} {incr k} { |
|
335
|
scenario_write $me d$i/b$k.txt "bulk $i file $k" |
|
336
|
} |
|
337
|
scenario_fossil $ck add d$i |
|
338
|
scenario_commit $me |
|
339
|
} |
|
340
|
12 { ;# Add or change an unversioned file |
|
341
|
scenario_write $me u[expr {$a%16}].txt "unversioned $i" w |
|
342
|
scenario_fossil $ck uv add u[expr {$a%16}].txt --mtime \ |
|
343
|
[format "2026-01-01 %02d:%02d:%02d" [expr {$i/60}] \ |
|
344
|
[expr {$i%60}] $me] |
|
345
|
} |
|
346
|
13 { ;# Replace this clone by a new clone of another |
|
347
|
# Skip the operation if the source is waiting for anything. |
|
348
|
# "fossil clone" refuses a source that holds a delta from |
|
349
|
# something missing, and a check-in with a missing file cannot |
|
350
|
# be checked out of the new clone. |
|
351
|
if {[scenario_count $peer "SELECT count(*) FROM phantom"]} continue |
|
352
|
# Also skip it if this clone holds something that neither of the |
|
353
|
# others holds. Throwing away the only copy of an artifact would |
|
354
|
# leave the others waiting for it for ever, and that would be the |
|
355
|
# fault of this test, not of Fossil. |
|
356
|
set o1 [expr {($me+1)%3}] |
|
357
|
set o2 [expr {($me+2)%3}] |
|
358
|
if {[scenario_count $me "ATTACH 'r$o1.fossil' AS o1; |
|
359
|
ATTACH 'r$o2.fossil' AS o2; |
|
360
|
SELECT count(*) FROM blob WHERE size>=0 |
|
361
|
AND uuid NOT IN (SELECT uuid FROM o1.blob WHERE size>=0) |
|
362
|
AND uuid NOT IN (SELECT uuid FROM o2.blob WHERE size>=0)"]} { |
|
363
|
continue |
|
364
|
} |
|
365
|
set uv [expr {($a & 0x80) != 0}] |
|
366
|
scenario_fossil $ck close --force |
|
367
|
file delete -force [file join $scenarioDir $ck] \ |
|
368
|
[file join $scenarioDir r$me.fossil] |
|
369
|
if {$uv} { |
|
370
|
scenario_must . clone r$peer.fossil r$me.fossil -u |
|
371
|
} else { |
|
372
|
scenario_must . clone r$peer.fossil r$me.fossil |
|
373
|
} |
|
374
|
scenario_open $me |
|
375
|
scenario_holds $me $peer $uv clone |
|
376
|
} |
|
377
|
} |
|
378
|
} |
|
379
|
|
|
380
|
# Sync along the chain 0-1-2 until nothing more can move. One pass |
|
381
|
# each way is enough; two more guard the count. |
|
382
|
foreach pass {1 2 3} { |
|
383
|
scenario_must c0 sync -u ../r1.fossil |
|
384
|
scenario_must c1 sync -u ../r2.fossil |
|
385
|
scenario_must c2 sync -u ../r1.fossil |
|
386
|
scenario_must c1 sync -u ../r0.fossil |
|
387
|
} |
|
388
|
|
|
389
|
# Every clone must be sound, and all must hold the same artifacts |
|
390
|
foreach i {0 1 2} { |
|
391
|
scenario_must . test-integrity --parse -R r$i.fossil |
|
392
|
scenario_must . test-missing -R r$i.fossil |
|
393
|
scenario_must . sql -R r$i.fossil "SELECT uuid FROM blob WHERE size>=0 |
|
394
|
AND rid NOT IN private ORDER BY 1; |
|
395
|
SELECT 'uv', name, hash FROM unversioned |
|
396
|
WHERE hash IS NOT NULL ORDER BY 2" |
|
397
|
set held($i) [string trim $scenarioOut] |
|
398
|
if {$held($i) eq ""} {scenario_fail "clone $i lists no artifacts"} |
|
399
|
if {$i > 0 && $held($i) ne $held(0)} { |
|
400
|
scenario_fail "clones 0 and $i hold different artifacts or\ |
|
401
|
unversioned files after syncing" |
|
402
|
} |
|
403
|
} |
|
404
|
} |
|
405
|
|
|
406
|
source [file join $::testfiledir corpus.tcl] |
|
407
|
corpus_unpack [file join $::testfiledir corpus corpus.pack] \ |
|
408
|
[file join [pwd] corpus] |
|
409
|
set scenarioFiles [lsort [glob -nocomplain \ |
|
410
|
[file join [pwd] corpus scenario *]]] |
|
411
|
if {[test_assurance] ne "stress"} { |
|
412
|
test_skip_part "all but one scenario input need -assurance stress" |
|
413
|
set scenarioFiles [lrange $scenarioFiles 0 0] |
|
414
|
} |
|
415
|
foreach scenarioF $scenarioFiles { |
|
416
|
set scenarioFd [open $scenarioF rb] |
|
417
|
set scenarioData [read $scenarioFd] |
|
418
|
close $scenarioFd |
|
419
|
test_eq scenario-[file tail $scenarioF] \ |
|
420
|
[scenario_run [file tail $scenarioF] $scenarioData] "" |
|
421
|
} |
|
422
|
|
|
423
|
# Random inputs, on request. The generator is written out here so that a |
|
424
|
# seed gives the same input with every version of Tcl. |
|
425
|
# |
|
426
|
if {[info exists ::env(FOSSIL_SCENARIO_RANDOM)]} { |
|
427
|
set scenarioSeed [clock seconds] |
|
428
|
if {[info exists ::env(FOSSIL_SCENARIO_SEED)]} { |
|
429
|
set scenarioSeed $::env(FOSSIL_SCENARIO_SEED) |
|
430
|
} |
|
431
|
for {set scenarioN 0} {$scenarioN < $::env(FOSSIL_SCENARIO_RANDOM)} \ |
|
432
|
{incr scenarioN; incr scenarioSeed} { |
|
433
|
set scenarioX $scenarioSeed |
|
434
|
set scenarioData "" |
|
435
|
for {set scenarioK 0} {$scenarioK < 4*$scenarioMaxOps} {incr scenarioK} { |
|
436
|
set scenarioX [expr {($scenarioX*1103515245 + 12345) & 0x7fffffff}] |
|
437
|
append scenarioData [binary format c [expr {($scenarioX>>16) & 255}]] |
|
438
|
} |
|
439
|
test_eq scenario-seed-$scenarioSeed \ |
|
440
|
[scenario_run seed-$scenarioSeed $scenarioData] "" |
|
441
|
} |
|
442
|
} |
|
443
|
|
|
444
|
foreach scenarioVar {ASAN_OPTIONS UBSAN_OPTIONS USER} { |
|
445
|
if {[info exists scenarioSaved($scenarioVar)]} { |
|
446
|
set ::env($scenarioVar) $scenarioSaved($scenarioVar) |
|
447
|
} else { |
|
448
|
unset ::env($scenarioVar) |
|
449
|
} |
|
450
|
} |
|
451
|
|
|
452
|
test_cleanup |
|
453
|
|