Fossil SCM

fossil-scm / test / integrity.test
Blame History Raw 232 lines
1
#
2
# Copyright (c) 2026 Dan Shearer
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
# Integrity of a repository. Two kinds of test:
15
#
16
# * A "fossil rebuild", and a "fossil deconstruct" and "fossil
17
# reconstruct" round trip, keep every artifact; "fossil
18
# test-integrity" reports an artifact whose content is damaged.
19
#
20
# * "fossil test-integrity" and "fossil test-missing" pass on a sound
21
# repository and fail, with a non-zero exit status, on each of nine
22
# kinds of damage made with SQL; and "fossil rebuild" leaves the
23
# tables derived from the artifacts as they were, or repairs them.
24
#
25
26
require_no_open_checkout
27
28
test_setup ""
29
set ::env(FOSSIL_USER) tester
30
31
# Returns the hashes of the artifacts in repository REPO that are not
32
# phantoms, in order.
33
#
34
proc integrity_artifacts {repo} {
35
fossil sql -R $repo "SELECT uuid FROM blob WHERE size>=0 ORDER BY uuid"
36
return [normalize_result]
37
}
38
39
# Returns the check-ins in repository REPO with their parents and
40
# comments, in order of hash.
41
#
42
proc integrity_checkins {repo} {
43
fossil sql -R $repo "SELECT b.uuid, coalesce(p.uuid,''), e.comment\
44
FROM event e JOIN blob b ON b.rid=e.objid\
45
LEFT JOIN plink ON cid=e.objid LEFT JOIN blob p ON p.rid=pid\
46
WHERE e.type='ci' ORDER BY b.uuid"
47
return [normalize_result]
48
}
49
50
# Test NAME passes when "fossil test-integrity --parse" finds no
51
# problems in repository REPO.
52
#
53
proc integrity_ok {name repo} {
54
fossil test-integrity --parse -R $repo
55
test_match $name [normalize_result] {checked: 0 errors}
56
}
57
58
# The tables of a repository that are derived from its artifacts, with
59
# each record number replaced by the hash or name it stands for and the
60
# rows in a fixed order. Two repositories that hold the same artifacts
61
# give the same text, and so does one repository before and after a
62
# rebuild.
63
#
64
set integrityDerivedSql {
65
SELECT 'event', (SELECT uuid FROM blob WHERE rid=objid), type, mtime,
66
user, euser, comment, ecomment, bgcolor, brief
67
FROM event ORDER BY 2;
68
SELECT 'plink', (SELECT uuid FROM blob WHERE rid=pid),
69
(SELECT uuid FROM blob WHERE rid=cid), isprim, mtime
70
FROM plink ORDER BY 2, 3;
71
SELECT 'mlink', (SELECT uuid FROM blob WHERE rid=mid),
72
(SELECT uuid FROM blob WHERE rid=fid),
73
(SELECT uuid FROM blob WHERE rid=pid),
74
(SELECT name FROM filename WHERE fnid=mlink.fnid),
75
(SELECT name FROM filename WHERE fnid=mlink.pfnid), mperm, isaux
76
FROM mlink ORDER BY 2, 5, 3, 4;
77
SELECT 'tagxref', (SELECT tagname FROM tag WHERE tagid=tagxref.tagid),
78
tagtype, (SELECT uuid FROM blob WHERE rid=srcid),
79
(SELECT uuid FROM blob WHERE rid=origid), value, mtime,
80
(SELECT uuid FROM blob WHERE rid=tagxref.rid)
81
FROM tagxref ORDER BY 2, 8, 3;
82
SELECT 'leaf', (SELECT uuid FROM blob WHERE rid=leaf.rid)
83
FROM leaf ORDER BY 2;
84
SELECT 'filename', name FROM filename ORDER BY 2;
85
SELECT 'tag', tagname FROM tag
86
WHERE tagid IN (SELECT tagid FROM tagxref) ORDER BY 2;
87
SELECT 'backlink', target, srctype,
88
(SELECT uuid FROM blob WHERE rid=srcid), mtime
89
FROM backlink ORDER BY 2, 4;
90
SELECT 'attachment', isLatest, mtime, src, target, filename, comment, user
91
FROM attachment ORDER BY 3, 4;
92
}
93
94
# Returns the derived tables of repository REPO as such text.
95
#
96
proc integrity_derived {repo} {
97
fossil sql -R $repo $::integrityDerivedSql
98
return $::RESULT
99
}
100
101
# An origin repository with three check-ins of one file (so the store
102
# holds deltas), a tag and a wiki page.
103
fossil new r.fossil
104
file mkdir ck
105
cd ck
106
fossil open ../r.fossil
107
foreach {integrityFile integrityComment} \
108
{a.txt one b.txt two a.txt three c.txt four} {
109
write_file $integrityFile \
110
"$integrityComment\n[string repeat $integrityComment 200]\n"
111
fossil add $integrityFile
112
fossil commit -m $integrityComment
113
}
114
fossil tag add release current
115
write_file ../wiki.txt "A page"
116
fossil wiki create Page ../wiki.txt
117
fossil close
118
cd ..
119
set artifacts [integrity_artifacts r.fossil]
120
set checkins [integrity_checkins r.fossil]
121
integrity_ok integrity-new r.fossil
122
123
###############################################################################
124
# Round trips keep every artifact.
125
126
fossil rebuild r.fossil
127
test integrity-rebuild {$CODE == 0}
128
test_eq integrity-rebuild-artifacts [integrity_artifacts r.fossil] $artifacts
129
test_eq integrity-rebuild-checkins [integrity_checkins r.fossil] $checkins
130
integrity_ok integrity-rebuild-check r.fossil
131
132
file mkdir artifacts
133
fossil deconstruct -R r.fossil artifacts
134
test integrity-deconstruct {$CODE == 0}
135
fossil reconstruct r2.fossil artifacts
136
test integrity-reconstruct {$CODE == 0}
137
test_eq integrity-reconstruct-artifacts [integrity_artifacts r2.fossil] \
138
$artifacts
139
test_eq integrity-reconstruct-checkins [integrity_checkins r2.fossil] \
140
$checkins
141
integrity_ok integrity-reconstruct-check r2.fossil
142
143
# A file artifact whose stored content does not decompress.
144
fossil sql -R r2.fossil "UPDATE blob SET content=x'00000010deadbeef'\
145
WHERE rid=(SELECT fid FROM mlink JOIN filename USING(fnid)\
146
WHERE name='b.txt' LIMIT 1)"
147
fossil test-integrity -R r2.fossil -expectError
148
test_match integrity-damaged-content [normalize_result] \
149
{wrong hash on artifact \d+}
150
# An undamaged artifact is still readable.
151
fossil sql -R r2.fossil "SELECT uuid FROM blob JOIN mlink ON fid=rid\
152
JOIN filename USING(fnid) WHERE name='c.txt'"
153
fossil artifact [normalize_result] -R r2.fossil
154
test_eq integrity-artifact-readable [normalize_result] \
155
"four\n[string repeat four 200]"
156
157
###############################################################################
158
# test-integrity and test-missing: exit 0 on a sound repository, non-zero
159
# on each kind of damage. Each kind is made to a copy.
160
161
fossil sql -R r.fossil "SELECT count(*) FROM delta"
162
test integrity-has-deltas {$RESULT > 0}
163
fossil test-integrity --parse -R r.fossil
164
test integrity-sound {$CODE == 0}
165
fossil test-missing -R r.fossil
166
test integrity-nothing-missing {$CODE == 0}
167
168
# A file of a check-in that is neither a delta nor the source of one.
169
set integrityOneFile "(SELECT fid FROM mlink WHERE fid>0\
170
AND fid NOT IN (SELECT srcid FROM delta)\
171
AND fid NOT IN (SELECT rid FROM delta) LIMIT 1)"
172
173
foreach {integrityName integritySql integrityCmd} [list \
174
wrong-size \
175
"UPDATE blob SET size=size+1 WHERE rid=$integrityOneFile" \
176
test-integrity \
177
content-does-not-match-hash \
178
"UPDATE blob SET content=x'0102030405060708' WHERE rid=$integrityOneFile" \
179
test-integrity \
180
hash-does-not-match-content \
181
"UPDATE blob SET uuid=substr(uuid,1,length(uuid)-1) ||\
182
CASE substr(uuid,-1) WHEN '0' THEN '1' ELSE '0' END\
183
WHERE rid=$integrityOneFile" \
184
test-integrity \
185
delta-is-its-own-source \
186
"UPDATE delta SET srcid=rid WHERE rid=(SELECT rid FROM delta LIMIT 1)" \
187
test-integrity \
188
delta-source-gone \
189
"DELETE FROM blob WHERE rid=(SELECT srcid FROM delta LIMIT 1)" \
190
test-integrity \
191
public-delta-from-private \
192
"INSERT INTO private SELECT srcid FROM delta LIMIT 1" \
193
test-integrity \
194
checkin-is-not-a-manifest \
195
"UPDATE blob\
196
SET content=(SELECT content FROM blob WHERE rid=$integrityOneFile)\
197
WHERE rid=(SELECT max(objid) FROM event WHERE type='ci')" \
198
{test-integrity --parse} \
199
file-gone \
200
"DELETE FROM blob WHERE rid=$integrityOneFile" \
201
test-missing \
202
file-is-a-phantom \
203
"UPDATE blob SET size=-1, content=NULL WHERE rid=$integrityOneFile" \
204
test-missing \
205
] {
206
set integrityDamaged [file join [pwd] damaged.fossil]
207
file copy -force r.fossil $integrityDamaged
208
fossil sql -R $integrityDamaged $integritySql
209
fossil {*}$integrityCmd -R $integrityDamaged -expectError
210
test integrity-damage-$integrityName {$CODE != 0}
211
}
212
213
###############################################################################
214
# Rebuild leaves the derived tables alone, or repairs a wrong one.
215
216
set integrityBefore [integrity_derived r.fossil]
217
test integrity-derived-not-empty {[string length $integrityBefore] > 0}
218
fossil rebuild r.fossil
219
test_eq integrity-rebuild-same [integrity_derived r.fossil] $integrityBefore
220
221
file copy -force r.fossil $integrityDamaged
222
fossil sql -R $integrityDamaged \
223
"DELETE FROM plink WHERE rowid=(SELECT max(rowid) FROM plink)"
224
test integrity-missing-parent-link-shows \
225
{[integrity_derived $integrityDamaged] ne $integrityBefore}
226
fossil rebuild $integrityDamaged
227
test_eq integrity-rebuild-repairs [integrity_derived $integrityDamaged] \
228
$integrityBefore
229
230
test_cleanup
231
unset ::env(FOSSIL_USER)
232

Keyboard Shortcuts

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