Fossil SCM

fossil-scm / test / content.test
Blame History Raw 700 lines
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
# The content store: src/content.c. Reading an artifact through a long
15
# chain of deltas, turning stored content into a delta and back, content
16
# that arrives after it was asked for, and the commands that look for
17
# damage: test-integrity, test-missing, test-orphans.
18
#
19
# integrity.test damages a repository and requires the damage to be
20
# found. This file covers what is left.
21
#
22
23
require_no_open_checkout
24
25
test_setup
26
27
# The record number of the artifact with the given hash.
28
#
29
proc content_rid {hash} {
30
return [fossil_sql "SELECT rid FROM blob WHERE uuid='$hash'"]
31
}
32
33
# Write a file that holds the cards $body and the Z card for them.
34
#
35
proc content_artifact {filename body} {
36
global RESULT
37
set fd [open $filename wb]
38
puts -nonewline $fd $body
39
close $fd
40
fossil md5sum $filename
41
set sum [lindex $RESULT 0]
42
set fd [open $filename ab]
43
puts -nonewline $fd "Z $sum\n"
44
close $fd
45
}
46
47
###############################################################################
48
# Thirty versions of one file. Each version but the last is stored as a
49
# delta from the one after it, so the first is read through 29 deltas.
50
51
set contentText ""
52
for {set contentI 0} {$contentI < 40} {incr contentI} {
53
append contentText "line $contentI of a file that is long enough to delta\n"
54
}
55
set contentVersions {}
56
for {set contentI 1} {$contentI <= 30} {incr contentI} {
57
append contentText "added by version $contentI\n"
58
write_file long.txt $contentText
59
if {$contentI == 1} {fossil add long.txt}
60
fossil commit -m "content-v$contentI" --no-warnings
61
lappend contentVersions $contentText
62
}
63
set contentFirst [fossil_sql "SELECT uuid FROM blob, mlink, event
64
WHERE blob.rid=mlink.fid AND mlink.mid=event.objid
65
AND event.comment='content-v1'"]
66
set contentLast [fossil_sql "SELECT uuid FROM blob, mlink, event
67
WHERE blob.rid=mlink.fid AND mlink.mid=event.objid
68
AND event.comment='content-v30'"]
69
test content-setup {$contentFirst ne "" && $contentLast ne ""}
70
71
proc content_chain {rid} {
72
return [fossil_sql "WITH RECURSIVE c(rid,n) AS (SELECT $rid, 0
73
UNION ALL SELECT srcid, n+1 FROM delta, c WHERE delta.rid=c.rid)
74
SELECT max(n) FROM c"]
75
}
76
test_eq content-chain-length [content_chain [content_rid $contentFirst]] 29
77
78
fossil artifact $contentFirst first.out
79
test content-artifact-through-chain {$CODE == 0}
80
test_eq content-artifact-through-chain-text [read_file first.out] \
81
[lindex $contentVersions 0]
82
fossil artifact $contentLast
83
test_eq content-artifact-to-stdout [string trim $RESULT] \
84
[string trim [lindex $contentVersions end]]
85
86
fossil artifact -expectError
87
test content-artifact-usage {$CODE != 0}
88
fossil artifact no-such-artifact -expectError
89
test content-artifact-unknown {$CODE != 0}
90
91
###############################################################################
92
# The stored form of an artifact, and turning a delta back into content.
93
94
set contentRid [content_rid $contentFirst]
95
fossil test-content-rawget $contentFirst raw.out
96
test content-rawget {$CODE == 0}
97
test content-rawget-is-a-delta \
98
{[file size raw.out] < [string length [lindex $contentVersions 0]]/2}
99
fossil test-content-rawget $contentFirst
100
test content-rawget-stdout {$CODE == 0 && $RESULT ne ""}
101
fossil test-content-rawget -expectError
102
test content-rawget-usage {$CODE != 0}
103
104
fossil test-content-undelta $contentRid
105
test content-undelta {$CODE == 0}
106
test_eq content-undelta-no-delta-row [fossil_sql \
107
"SELECT count(*) FROM delta WHERE rid=$contentRid"] 0
108
fossil test-content-rawget $contentFirst raw.out
109
test_eq content-undelta-stored-whole [read_file raw.out] \
110
[lindex $contentVersions 0]
111
fossil test-content-undelta $contentRid
112
test content-undelta-again {$CODE == 0}
113
fossil test-content-undelta -expectError
114
test content-undelta-usage {$CODE != 0}
115
116
###############################################################################
117
# Turning content into a delta.
118
119
set contentSecond [fossil_sql "SELECT blob.rid FROM blob, mlink, event
120
WHERE blob.rid=mlink.fid AND mlink.mid=event.objid
121
AND event.comment='content-v2'"]
122
set contentThird [fossil_sql "SELECT blob.rid FROM blob, mlink, event
123
WHERE blob.rid=mlink.fid AND mlink.mid=event.objid
124
AND event.comment='content-v3'"]
125
set contentLastRid [content_rid $contentLast]
126
127
# The artifact itself and an artifact that is a delta from it are not
128
# used as sources. Of the two that remain, one is taken.
129
fossil test-content-deltify $contentRid $contentRid $contentLastRid \
130
$contentSecond
131
test content-deltify {$CODE == 0}
132
test content-deltify-source {[fossil_sql "SELECT srcid FROM delta
133
WHERE rid=$contentRid"] in [list $contentLastRid $contentSecond]}
134
fossil artifact $contentFirst first.out
135
test_eq content-deltify-same-content [read_file first.out] \
136
[lindex $contentVersions 0]
137
138
# Already a delta: left alone without --force, done again with it.
139
set contentSrc [fossil_sql "SELECT srcid FROM delta WHERE rid=$contentRid"]
140
fossil test-content-deltify $contentRid $contentThird
141
test_eq content-deltify-not-forced [fossil_sql "SELECT srcid FROM delta
142
WHERE rid=$contentRid"] $contentSrc
143
fossil test-content-deltify --force $contentRid $contentThird
144
test_eq content-deltify-forced [fossil_sql "SELECT srcid FROM delta
145
WHERE rid=$contentRid"] $contentThird
146
fossil artifact $contentFirst first.out
147
test_eq content-deltify-forced-same-content [read_file first.out] \
148
[lindex $contentVersions 0]
149
150
# A source that is itself a delta from the artifact, directly or not, is
151
# stored whole first, so that no loop is made.
152
fossil test-content-deltify --force $contentThird $contentLastRid
153
fossil test-content-deltify --force $contentThird $contentRid
154
test content-deltify-no-loop {$CODE == 0}
155
fossil artifact $contentFirst first.out
156
test_eq content-deltify-no-loop-content [read_file first.out] \
157
[lindex $contentVersions 0]
158
159
# Content of less than 50 bytes is never made a delta, nor used as the
160
# source of one.
161
write_file small.txt "small\n"
162
write_file small2.txt "small too\n"
163
fossil test-content-put small.txt small2.txt
164
set contentSmall [content_rid [sha3_file small.txt]]
165
set contentSmall2 [content_rid [sha3_file small2.txt]]
166
fossil test-content-deltify $contentSmall $contentSmall2
167
test_eq content-deltify-small [fossil_sql "SELECT count(*) FROM delta
168
WHERE rid=$contentSmall"] 0
169
fossil test-content-undelta $contentRid
170
fossil test-content-deltify $contentRid $contentSmall
171
test_eq content-deltify-small-source [fossil_sql "SELECT count(*) FROM delta
172
WHERE rid=$contentRid"] 0
173
174
# Unrelated content is not worth a delta.
175
write_file other.txt [string repeat "nothing in common with the file\n" 40]
176
fossil test-content-put other.txt
177
fossil test-content-deltify $contentRid [content_rid [sha3_file other.txt]]
178
test_eq content-deltify-unrelated [fossil_sql "SELECT count(*) FROM delta
179
WHERE rid=$contentRid"] 0
180
181
# Public content is not made a delta from private content.
182
write_file private.txt "${contentText}and one private line\n"
183
fossil test-content-put private.txt
184
set contentPrivate [content_rid [sha3_file private.txt]]
185
fossil sql "INSERT INTO private(rid) VALUES($contentPrivate)"
186
fossil test-content-deltify $contentRid $contentPrivate
187
test_eq content-deltify-private-source [fossil_sql "SELECT count(*) FROM delta
188
WHERE rid=$contentRid"] 0
189
fossil sql "DELETE FROM private WHERE rid=$contentPrivate"
190
fossil test-content-deltify $contentRid $contentPrivate
191
test_eq content-deltify-once-public [fossil_sql "SELECT srcid FROM delta
192
WHERE rid=$contentRid"] $contentPrivate
193
fossil test-content-undelta $contentRid
194
195
# Of two sources that both make a worthwhile delta, the one that makes
196
# the smaller is taken, whichever comes first.
197
set contentBase ""
198
for {set contentI 0} {$contentI < 60} {incr contentI} {
199
append contentBase "line $contentI of content with two possible sources\n"
200
}
201
write_file two-target.txt $contentBase
202
write_file two-near.txt "${contentBase}one more line\n"
203
write_file two-far.txt "[string range $contentBase 0 1500][string repeat \
204
"a line that the target does not have\n" 30]"
205
fossil test-content-put two-target.txt two-near.txt two-far.txt
206
set contentTarget [content_rid [sha3_file two-target.txt]]
207
set contentNear [content_rid [sha3_file two-near.txt]]
208
set contentFar [content_rid [sha3_file two-far.txt]]
209
fossil test-content-deltify $contentTarget $contentFar $contentNear
210
test_eq content-deltify-better-source-last [fossil_sql "SELECT srcid
211
FROM delta WHERE rid=$contentTarget"] $contentNear
212
fossil test-content-undelta $contentTarget
213
fossil test-content-deltify $contentTarget $contentNear $contentFar
214
test_eq content-deltify-better-source-first [fossil_sql "SELECT srcid
215
FROM delta WHERE rid=$contentTarget"] $contentNear
216
fossil test-content-undelta $contentTarget
217
fossil test-content-deltify $contentTarget $contentFar
218
test_eq content-deltify-far-source-alone [fossil_sql "SELECT srcid
219
FROM delta WHERE rid=$contentTarget"] $contentFar
220
fossil test-content-undelta $contentTarget
221
222
# Private content may be a delta from private content.
223
fossil sql "INSERT OR IGNORE INTO private(rid) VALUES($contentPrivate)"
224
fossil sql "INSERT OR IGNORE INTO private(rid) VALUES($contentRid)"
225
fossil test-content-deltify $contentRid $contentPrivate
226
test_eq content-deltify-private-from-private [fossil_sql "SELECT srcid
227
FROM delta WHERE rid=$contentRid"] $contentPrivate
228
fossil test-content-undelta $contentRid
229
fossil sql "DELETE FROM private WHERE rid IN ($contentPrivate,$contentRid)"
230
231
fossil test-content-deltify -expectError
232
test content-deltify-usage {$CODE != 0}
233
234
fossil test-integrity
235
test content-integrity-after-deltify {$CODE == 0}
236
237
###############################################################################
238
# test-content-put with --moderate.
239
240
write_file moderated.txt "content awaiting moderation\n"
241
fossil test-content-put --moderate moderated.txt
242
test content-put-moderate {$CODE == 0}
243
test_eq content-put-moderate-row [fossil_sql "SELECT count(*) FROM modreq
244
WHERE objid=[content_rid [sha3_file moderated.txt]]"] 1
245
246
###############################################################################
247
# Content that arrives after it was asked for. A check-in names a file
248
# that is not in the repository; when the file is stored, the check-in
249
# has it. A delta manifest is stored before its baseline; when the
250
# baseline arrives, the delta manifest becomes a check-in.
251
252
write_file late.txt "this file arrives late\n"
253
set contentLate [sha3_file late.txt]
254
content_artifact ci-late.txt "C content-late
255
D 2026-03-01T10:00:00.000
256
F late.txt $contentLate
257
U alice
258
"
259
fossil test-content-put --crosslink ci-late.txt
260
test_eq content-phantom-made [fossil_sql "SELECT size FROM blob
261
WHERE uuid='$contentLate'"] -1
262
fossil artifact $contentLate
263
test content-phantom-has-no-content {$CODE == 0 && $RESULT eq ""}
264
fossil test-content-put late.txt
265
test_eq content-phantom-filled [fossil_sql "SELECT size FROM blob
266
WHERE uuid='$contentLate'"] [file size late.txt]
267
fossil artifact $contentLate late.out
268
test content-phantom-filled-content {[same_file late.txt late.out]}
269
270
content_artifact ci-base.txt "C content-base
271
D 2026-03-02T10:00:00.000
272
F a.txt $contentLate
273
F b.txt $contentLate
274
U alice
275
"
276
set contentBase [sha3_file ci-base.txt]
277
content_artifact ci-delta.txt "B $contentBase
278
C content-delta
279
D 2026-03-02T11:00:00.000
280
F b.txt
281
F c.txt $contentLate
282
P $contentBase
283
U alice
284
"
285
fossil test-content-put --crosslink ci-delta.txt
286
test_eq content-orphan-not-a-checkin [fossil_sql "SELECT count(*) FROM event
287
WHERE comment='content-delta'"] 0
288
test_eq content-orphan-recorded [fossil_sql "SELECT count(*) FROM orphan"] 1
289
fossil test-content-put --crosslink ci-base.txt
290
test content-orphan-baseline-arrives {$CODE == 0}
291
test_eq content-orphan-now-a-checkin [fossil_sql "SELECT count(*) FROM event
292
WHERE comment IN ('content-delta','content-base')"] 2
293
test_eq content-orphan-cleared [fossil_sql "SELECT count(*) FROM orphan"] 0
294
fossil ls -r [sha3_file ci-delta.txt]
295
test_eq content-orphan-files [normalize_result] "a.txt\nc.txt"
296
297
###############################################################################
298
# test-integrity: the options that check the database only, and --parse
299
# on content that looks like an artifact and is not one.
300
301
fossil test-integrity --db-only
302
test_eq content-integrity-db-only [string trim $RESULT] ok
303
fossil test-integrity --quick
304
test_eq content-integrity-quick [string trim $RESULT] ok
305
306
write_file looks1.txt "C this looks like a card\nbut the rest does not\n"
307
write_file looks2.txt "-----BEGIN PGP SIGNED MESSAGE-----\nnot signed at all\n"
308
write_file looks3.txt "-----BEGIN SSH SIGNED MESSAGE-----\nnot signed either\n"
309
write_file looks4.txt "I am not an artifact, although I begin with a card\n"
310
write_file looks5.txt "D 2026-03-03T10:00:00.000\nno newline at the end"
311
# Content that does not begin with a card at all, content whose first line
312
# holds a zero byte, and content whose first line is very long.
313
write_file looks6.txt "1 begins with a digit, which no card does\n"
314
write_file looks7.txt "AB has no space after its first letter\n"
315
write_file looks8.txt \
316
"C a first line\x00with a zero byte in it\nand a second line\n"
317
write_file looks9.txt "C [string repeat x 500]\nand a second line\n"
318
# Content that is the first line of a signed message and nothing more,
319
# with no end of line.
320
write_file looks10.txt "-----BEGIN PGP SIGNED MESSAGE-----"
321
fossil test-content-put looks1.txt looks2.txt looks3.txt looks4.txt looks5.txt \
322
looks6.txt looks7.txt looks8.txt looks9.txt looks10.txt
323
set contentLooks10 [sha3_file looks10.txt]
324
fossil test-integrity --parse
325
test content-integrity-parse {$CODE == 0}
326
test content-integrity-parse-counts \
327
{[regexp {(\d+) control artifacts failed to parse} $RESULT all n] && $n ==
328
6}
329
test content-integrity-parse-one-line-signed \
330
{[string first "manifest_parse failed for $contentLooks10:" $RESULT] >= 0}
331
test content-integrity-parse-first-line \
332
{[string first "\"C this looks like a card\"" $RESULT] >= 0}
333
334
# The same content is an error once the repository has it down as a
335
# wiki page.
336
fossil sql "INSERT INTO event(type,mtime,objid,user,comment)
337
VALUES('w',2461000.5,[content_rid [sha3_file looks1.txt]],'alice','+x')"
338
fossil test-integrity --parse -expectError
339
test content-integrity-parse-listed-artifact {$CODE != 0}
340
fossil sql "DELETE FROM event
341
WHERE objid=[content_rid [sha3_file looks1.txt]]"
342
343
###############################################################################
344
# test-orphans lists content that nothing refers to.
345
346
fossil test-orphans
347
test content-orphans {$CODE == 0}
348
test content-orphans-count \
349
{[regexp {(\d+) orphans} $RESULT all n] && $n >= 9}
350
test content-orphans-lists-one \
351
{[string first [sha3_file other.txt] $RESULT] >= 0}
352
353
###############################################################################
354
# test-missing reports each kind of reference to content that is not
355
# there.
356
357
fossil test-missing
358
test content-missing-none {$CODE == 0}
359
test_match content-missing-none-text $RESULT \
360
{^0 missing or shunned references in \d+ control artifacts}
361
fossil test-missing --quiet
362
test_eq content-missing-none-quiet [string trim $RESULT] ""
363
364
set contentGone [string repeat 0123456789 4]
365
set contentGone2 [string repeat 9876543210 4]
366
set contentCi [fossil_sql "SELECT uuid FROM blob, event
367
WHERE rid=objid AND comment='content-v30'"]
368
foreach {contentName contentCount contentBody contentWant} [list \
369
file 1 \
370
[join [list \
371
"C content-missing-file\nD 2026-03-04T10:00:00.000\n" \
372
"F gone.txt $contentGone\nU alice\n"] ""] \
373
"MISSING: $contentGone\n*file of check-in *\n*gone.txt" \
374
parent 2 \
375
[join [list \
376
"D 2026-03-04T11:00:00.000\nL Missing\\sparent\n" \
377
"P $contentGone\nU alice\nW 1\nx\n"] ""] \
378
"MISSING: $contentGone\n*parent of wiki *" \
379
cherrypick 4 \
380
[join [list \
381
"C content-missing-pick\nD 2026-03-04T12:00:00.000\n" \
382
"P $contentCi\nQ +$contentGone $contentGone2\nU alice\n"] ""] \
383
[join [list \
384
"MISSING: $contentGone\n" \
385
"*cherry-pick target of check-in *MISSING: $contentGone2\n" \
386
"*cherry-pick baseline of check-in *"] ""] \
387
technote 5 \
388
[join [list \
389
"D 2026-03-04T12:30:00.000\n" \
390
"E 2026-03-04T09:00:00 [string repeat ab 20]\nP $contentGone\n" \
391
"U alice\nW 1\nx\n"] ""] \
392
"MISSING: $contentGone\n*parent of event *" \
393
tag 6 "D 2026-03-04T13:00:00.000\nT +sym-missing $contentGone\nU alice\n" \
394
"MISSING: $contentGone\n*target of tag *" \
395
attachment 7 \
396
[join [list \
397
"A f.txt Missing\\sparent $contentGone\n" \
398
"D 2026-03-04T14:00:00.000\nU alice\n"] ""] \
399
"MISSING: $contentGone\n*file of attachment *" \
400
cluster 9 "M $contentGone\nM $contentGone2\n" \
401
"MISSING: $contentGone\n*in cluster *" \
402
] {
403
content_artifact missing-$contentName.txt $contentBody
404
fossil test-content-put --crosslink missing-$contentName.txt
405
fossil test-missing -expectError
406
test content-missing-$contentName {$CODE != 0 && [string match \
407
"*\n$contentCount missing or shunned references *" $RESULT]}
408
test content-missing-$contentName-text \
409
{[string match "*$contentWant*" $RESULT]}
410
}
411
# An attachment that takes a file away names none, and is not counted.
412
content_artifact missing-detach.txt \
413
"A f.txt Missing\\sparent\nD 2026-03-04T15:00:00.000\nU alice\n"
414
fossil test-content-put --crosslink missing-detach.txt
415
test content-missing-detach-stored {$CODE == 0}
416
417
fossil test-missing --quiet -expectError
418
test content-missing-quiet-still-reports \
419
{$CODE != 0 && [string first MISSING $RESULT] >= 0}
420
421
# A missing artifact that is shunned is reported as shunned, or not at
422
# all with --notshunned.
423
fossil sql "INSERT INTO shun(uuid,mtime) VALUES('$contentGone',0),
424
('$contentGone2',0)"
425
fossil test-missing -expectError
426
test content-missing-shunned {$CODE != 0 &&
427
[holds_strings $RESULT [list "SHUNNED: $contentGone" !MISSING]]}
428
fossil test-missing --notshunned
429
test content-missing-notshunned {$CODE == 0}
430
test_match content-missing-notshunned-text $RESULT \
431
{^0 missing or shunned references}
432
fossil sql "DELETE FROM shun"
433
434
###############################################################################
435
# test-content-erase asks first. Erasing content that others are deltas
436
# from stores those whole.
437
438
fossil test-content-undelta $contentRid
439
fossil test-content-deltify --force $contentThird $contentRid
440
test_eq content-erase-setup [fossil_sql "SELECT count(*) FROM delta
441
WHERE srcid=$contentRid"] 1
442
fossil_raw -input "n\n" test-content-erase $contentRid
443
test_eq content-erase-declined [fossil_sql "SELECT count(*) FROM blob
444
WHERE rid=$contentRid"] 1
445
fossil_raw -input "Y\n" test-content-erase $contentRid
446
test content-erase {$CODE == 0}
447
test_eq content-erase-gone [fossil_sql "SELECT count(*) FROM blob
448
WHERE rid=$contentRid"] 0
449
test_eq content-erase-no-delta-left [fossil_sql "SELECT count(*) FROM delta
450
WHERE srcid=$contentRid OR rid=$contentRid"] 0
451
fossil artifact [fossil_sql "SELECT uuid FROM blob WHERE rid=$contentThird"] \
452
third.out
453
test_eq content-erase-dependent-still-whole [read_file third.out] \
454
[lindex $contentVersions 2]
455
456
# The answer may be in either case.
457
fossil_raw -input "y\n" test-content-erase $contentSmall
458
test_eq content-erase-lower-case-answer [fossil_sql "SELECT count(*) FROM blob
459
WHERE rid=$contentSmall"] 0
460
461
###############################################################################
462
# A "parent" tag on a private check-in is itself private, and so is the
463
# placeholder for a parent that the repository does not have.
464
465
content_artifact ci-private.txt "C content-private
466
D 2026-03-05T10:00:00.000
467
F late.txt $contentLate
468
U alice
469
"
470
fossil test-content-put --crosslink ci-private.txt
471
set contentPriv [sha3_file ci-private.txt]
472
fossil sql "INSERT INTO private(rid) VALUES([content_rid $contentPriv])"
473
set contentAbsent [string repeat 4d 20]
474
fossil tag add --raw parent $contentPriv $contentAbsent
475
test content-private-placeholder-tag {$CODE == 0}
476
test_eq content-private-placeholder [fossil_sql "SELECT count(*)
477
FROM private, blob WHERE private.rid=blob.rid AND uuid='$contentAbsent'
478
AND size<0"] 1
479
test_eq content-private-placeholder-not-unclustered [fossil_sql "SELECT count(*)
480
FROM unclustered, blob WHERE unclustered.rid=blob.rid
481
AND uuid='$contentAbsent'"] 0
482
test_eq content-private-placeholder-is-parent [fossil_sql "SELECT count(*)
483
FROM plink WHERE cid=[content_rid $contentPriv]
484
AND pid=[content_rid $contentAbsent]"] 1
485
486
###############################################################################
487
# Content that arrives in a sync message, sent to the server side through
488
# "fossil test-xfer".
489
490
fossil init x.fossil
491
fossil sql -R x.fossil "UPDATE user SET cap='oi' WHERE login='nobody'"
492
fossil sql -R x.fossil "SELECT value FROM config WHERE name='project-code'"
493
set contentPcode [string trim $RESULT]
494
495
# Send the cards BODY as a push. With LOGIN, the message has a login
496
# card, which test-xfer takes as a grant of every permission.
497
#
498
proc content_push {body {login ""}} {
499
global contentPcode CLIENT_VERSION_CARD
500
write_file msg.txt "$login${CLIENT_VERSION_CARD}push x $contentPcode\n$body"
501
fossil_raw test-xfer -R x.fossil msg.txt
502
}
503
proc content_x {sql} {
504
fossil sql -R x.fossil $sql
505
return [string trim $::RESULT]
506
}
507
proc content_sha3 {text} {
508
write_file sha3-me.txt $text
509
fossil sha3sum sha3-me.txt
510
return [lindex $::RESULT 0]
511
}
512
513
# A repository that holds only SHA1 names and whose hash policy is "auto"
514
# keeps the policy while only SHA1 names arrive, and moves to SHA3 with
515
# the first SHA3 name.
516
fossil init --sha1 y.fossil
517
fossil sql -R y.fossil "UPDATE user SET cap='oi' WHERE login='nobody'"
518
fossil hash-policy auto -R y.fossil
519
fossil sql -R y.fossil "SELECT value FROM config WHERE name='project-code'"
520
set contentYcode [string trim $RESULT]
521
proc content_push_y {body} {
522
global contentYcode CLIENT_VERSION_CARD
523
write_file msg.txt "${CLIENT_VERSION_CARD}push x $contentYcode\n$body"
524
fossil_raw test-xfer -R y.fossil msg.txt
525
}
526
set contentA "an artifact with a SHA1 name\n"
527
content_push_y "file [sha1_text $contentA] [string length $contentA]\n$contentA"
528
test content-policy-sha1-name-accepted {$CODE == 0}
529
fossil hash-policy -R y.fossil
530
test_eq content-policy-still-auto [string trim $RESULT] auto
531
set contentB "an artifact with a SHA3 name\n"
532
content_push_y \
533
"file [content_sha3 $contentB] [string length $contentB]\n$contentB"
534
fossil hash-policy -R y.fossil
535
test_eq content-policy-now-sha3 [string trim $RESULT] sha3
536
537
# An artifact that the server has only heard of is a phantom. It is
538
# filled when its content arrives as a compressed delta from content the
539
# server holds.
540
set contentSrc ""
541
for {set contentI 0} {$contentI < 30} {incr contentI} {
542
append contentSrc "line $contentI of the source of a delta\n"
543
}
544
set contentTgt "${contentSrc}and the line that the target adds\n"
545
set contentSrcHash [content_sha3 $contentSrc]
546
set contentTgtHash [content_sha3 $contentTgt]
547
write_file delta-src.txt $contentSrc
548
write_file delta-tgt.txt $contentTgt
549
fossil test-delta-create delta-src.txt delta-tgt.txt delta.out
550
set contentDelta [read_file delta.out]
551
set contentZ [binary format I [string length $contentDelta]][zlib compress \
552
$contentDelta]
553
content_push "file $contentSrcHash [string length $contentSrc]\n$contentSrc"
554
content_push "igot $contentTgtHash\n"
555
test_eq content-sync-phantom [content_x "SELECT size FROM blob
556
WHERE uuid='$contentTgtHash'"] -1
557
content_push "cfile $contentTgtHash $contentSrcHash [string length $contentTgt]\
558
[string length $contentZ]\n$contentZ"
559
test content-sync-phantom-filled-by-delta {$CODE == 0}
560
fossil artifact $contentTgtHash -R x.fossil tgt.out
561
test_eq content-sync-phantom-filled-content [read_file tgt.out] $contentTgt
562
563
# A phantom that is filled by a private card becomes private.
564
set contentP "content that arrives marked private\n"
565
set contentPHash [content_sha3 $contentP]
566
content_push "igot $contentPHash\n"
567
content_push \
568
"private\nfile $contentPHash [string length $contentP]\n$contentP" \
569
"login tester 0 0\n"
570
test content-sync-private-fill {$CODE == 0}
571
test_eq content-sync-private-fill-marked [content_x "SELECT count(*)
572
FROM private, blob WHERE private.rid=blob.rid AND uuid='$contentPHash'
573
AND size>=0"] 1
574
575
# The server's common script asks for the record that the next artifact
576
# will be given, before any artifact has arrived. An artifact that then
577
# arrives whole is stored and can be read at once: a delta from it, sent
578
# in the same message, is applied and stored whole. An artifact that
579
# arrives as a compressed delta from content the server holds is stored
580
# and can be read.
581
fossil sql -R x.fossil "REPLACE INTO config(name,value,mtime)
582
VALUES('xfer-common-script',
583
'query {SELECT artifact_to_json((SELECT max(rid)+1 FROM blob)) AS j} {}',
584
now())"
585
set contentQ1 "${contentSrc}the first of two that arrive together\n"
586
set contentQ2 "${contentQ1}and the second, sent as a delta from the first\n"
587
set contentQ1Hash [content_sha3 $contentQ1]
588
set contentQ2Hash [content_sha3 $contentQ2]
589
write_file q1.txt $contentQ1
590
write_file q2.txt $contentQ2
591
fossil test-delta-create q1.txt q2.txt q.delta
592
set contentDelta [read_file q.delta]
593
content_push "file $contentQ1Hash [string length $contentQ1]\n${contentQ1}file\
594
$contentQ2Hash $contentQ1Hash [string length $contentDelta]\n$contentDelta"
595
test content-sync-asked-early {$CODE == 0
596
&& [string first "error " $RESULT] < 0}
597
fossil artifact $contentQ1Hash -R x.fossil q1.out
598
test_eq content-sync-asked-early-content [read_file q1.out] $contentQ1
599
fossil artifact $contentQ2Hash -R x.fossil q2.out
600
test_eq content-sync-asked-early-delta-content [read_file q2.out] $contentQ2
601
test_eq content-sync-asked-early-delta-stored-whole [content_x "SELECT count(*)
602
FROM delta, blob WHERE delta.rid=blob.rid AND uuid='$contentQ2Hash'"] 0
603
604
set contentQ3 "${contentSrc}and a line that only the third has\n"
605
set contentQ3Hash [content_sha3 $contentQ3]
606
write_file q3.txt $contentQ3
607
fossil test-delta-create delta-src.txt q3.txt q3.delta
608
set contentDelta [read_file q3.delta]
609
set contentZ [binary format I [string length $contentDelta]][zlib compress \
610
$contentDelta]
611
content_push "cfile $contentQ3Hash $contentSrcHash [string length $contentQ3]\
612
[string length $contentZ]\n$contentZ"
613
test content-sync-asked-early-cfile {$CODE == 0
614
&& [string first "error " $RESULT] < 0}
615
fossil artifact $contentQ3Hash -R x.fossil q3.out
616
test_eq content-sync-asked-early-cfile-content [read_file q3.out] $contentQ3
617
test_eq content-sync-asked-early-cfile-is-delta [content_x "SELECT count(*)
618
FROM delta, blob WHERE delta.rid=blob.rid AND uuid='$contentQ3Hash'"] 1
619
fossil sql -R x.fossil "DELETE FROM config WHERE name='xfer-common-script'"
620
621
fossil test-integrity -R x.fossil
622
test content-sync-integrity {$CODE == 0}
623
624
###############################################################################
625
# The cache of reconstructed artifacts keeps every eighth artifact along a
626
# delta chain. It drops its oldest entry once it holds more than 2,000
627
# entries, and its oldest entries, as many as it takes, while it holds
628
# more than 256 MB. Both limits are constants in content.c, so a test
629
# has to store enough to exceed them. If Fossil gets a setting for the
630
# size of this cache, set it far lower here, at least for -assurance
631
# smoke, and run these tests at every level.
632
633
if {[test_assurance] eq "smoke"} {
634
test_skip_part "the content cache limits need -assurance ci or stress"
635
} else {
636
# A chain of 18,000 deltas is stored and read back in one process, so
637
# the cache holds more than 2,000 entries and drops the oldest while
638
# the chain is read; each version read afterwards, through dropped and
639
# kept entries, must still be right.
640
fossil new cache.fossil
641
fossil sql -R cache.fossil {
642
CREATE TEMP TABLE v(k INTEGER PRIMARY KEY, t BLOB);
643
WITH RECURSIVE c(k) AS (SELECT 0 UNION ALL SELECT k+1 FROM c WHERE k<17999)
644
INSERT INTO v SELECT k, CAST(printf('common text%.100c version %d', '.', k)
645
AS BLOB) FROM c;
646
INSERT INTO blob(rid,rcvid,size,uuid,content)
647
SELECT 100000+k, 0, length(t), printf('%064d', k),
648
compress(CASE WHEN k=0 THEN t
649
ELSE delta_create((SELECT t FROM v p WHERE p.k=v.k-1), t)
650
END)
651
FROM v;
652
INSERT INTO delta(rid,srcid) SELECT 100000+k, 100000+k-1 FROM v WHERE k>0;
653
SELECT content('rid:117999')=(SELECT t FROM v WHERE k=17999);
654
SELECT count(*) FROM v WHERE k%997=0 AND content('rid:'||(100000+k))=t;
655
}
656
test content-cache-expire {$CODE == 0}
657
test_eq content-cache-expire-reads [string trim $RESULT] "1\n19"
658
659
# A chain of 104 versions takes the cache over 256 MB. Only every
660
# eighth version is cached, so those are 30 MB each and the versions
661
# between them are a few bytes, which makes the chain quick to store
662
# and to read. The two oldest cached versions are small (version 7)
663
# and empty (version 15). When the cache first exceeds the limit,
664
# dropping version 7 is not enough, and dropping version 15 frees
665
# nothing, which leaves the cache over the limit. The next entry
666
# drops two 30 MB versions. Each version read afterwards must still
667
# be right.
668
fossil new cache-size.fossil
669
fossil sql -R cache-size.fossil {
670
CREATE TEMP TABLE c(kind INTEGER PRIMARY KEY, t BLOB);
671
INSERT INTO c VALUES(0, CAST('a small version' AS BLOB)),
672
(1, zeroblob(30000000)), (2, x'');
673
CREATE TEMP TABLE d(src, dst, z BLOB, PRIMARY KEY(src, dst));
674
INSERT INTO d SELECT a.kind, b.kind, compress(delta_create(a.t, b.t))
675
FROM c a, c b WHERE a.kind=0 OR b.kind=0;
676
CREATE TEMP TABLE n(k INTEGER PRIMARY KEY, kind);
677
WITH RECURSIVE s(k) AS (SELECT 0 UNION ALL SELECT k+1 FROM s WHERE k<103)
678
INSERT INTO n SELECT k, CASE WHEN k=15 THEN 2 WHEN k%8=7 AND k>15 THEN 1
679
ELSE 0 END FROM s;
680
INSERT INTO blob(rid,rcvid,size,uuid,content)
681
SELECT 200000+k, 0, (SELECT length(t) FROM c WHERE c.kind=n.kind),
682
printf('%064d', k),
683
CASE WHEN k=0 THEN (SELECT compress(t) FROM c WHERE c.kind=n.kind)
684
ELSE (SELECT z FROM d WHERE dst=n.kind
685
AND src=(SELECT kind FROM n p WHERE p.k=n.k-1)) END
686
FROM n;
687
INSERT INTO delta(rid,srcid) SELECT 200000+k, 200000+k-1 FROM n WHERE k>0;
688
SELECT content('rid:200103')=(SELECT t FROM c WHERE kind=1);
689
SELECT length(content('rid:200015'));
690
SELECT count(*) FROM n WHERE k IN (0,7,14,16,23,31,39,95,102,103)
691
AND content('rid:'||(200000+k))=(SELECT t FROM c WHERE c.kind=n.kind);
692
}
693
test content-cache-size {$CODE == 0}
694
test_eq content-cache-size-reads [string trim $RESULT] "1\n0\n10"
695
}
696
697
###############################################################################
698
699
test_cleanup
700

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button