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
|
test_description='Verify snapshot'
. ./setup.sh
check_snapshot_headers() {
sed "/^$/q" tmp >headers &&
grep "^Status: 200 OK$" headers &&
grep "^Content-Type: $2$" headers &&
grep "^Content-Disposition: inline; filename=\"master.$1\"$" headers
}
check_snapshot_content() {
ls master/ >output &&
test_line_count = 5 output &&
grep "^5$" master/file-5 &&
test_line_count = 1 master/file-5
}
snapshot_members() {
strip_headers <tmp >snapshot.tar &&
tar -tf snapshot.tar >members
}
test_expect_success 'set up the archives git writes' '
git -C repos/foo archive --format=tar --prefix=master/ master >expect.tar &&
git -C repos/foo archive --format=zip --prefix=master/ master >expect.zip
'
test_expect_success 'a tar snapshot is the archive git writes' '
cgit_ok_url "foo/snapshot/master.tar" >tmp &&
check_snapshot_headers tar application/x-tar &&
strip_headers <tmp >master.tar &&
test_cmp_bin expect.tar master.tar &&
rm -rf master &&
tar -xf master.tar &&
check_snapshot_content
'
test_tar_snapshot() {
ext=$1
type=$2
tool=$3
prereq=$4
test_lazy_prereq $prereq "command -v $tool"
test_expect_success $prereq "a $ext snapshot unpacks to the tar git writes" "
cgit_ok_url 'foo/snapshot/master.$ext' >tmp &&
check_snapshot_headers $ext $type &&
strip_headers <tmp >master.$ext &&
'$tool' -t 'master.$ext' &&
'$tool' -dc 'master.$ext' >master.tar &&
test_cmp_bin expect.tar master.tar
"
}
test_tar_snapshot tar.gz application/gzip gzip GZIP
test_tar_snapshot tar.bz2 application/x-bzip2 bzip2 BZIP2
test_tar_snapshot tar.lz application/x-lzip lzip LZIP
test_tar_snapshot tar.xz application/x-xz xz XZ
test_tar_snapshot tar.zst application/zstd zstd ZSTD
test_expect_success 'a zip snapshot is the archive git writes' '
cgit_ok_url "foo/snapshot/master.zip" >tmp &&
check_snapshot_headers zip application/zip &&
strip_headers <tmp >master.zip &&
test_cmp_bin expect.zip master.zip
'
test_expect_success UNZIP 'a zip snapshot unzips' '
cgit_ok_url "foo/snapshot/master.zip" >tmp &&
strip_headers <tmp >master.zip &&
rm -rf master &&
"$GIT_UNZIP" master.zip &&
check_snapshot_content
'
test_expect_success 'set up a repo with tags of every shape' '
mkrepo repos/vtags 2 &&
git -C repos/vtags update-ref refs/tags/V2.0 HEAD~1 &&
git -C repos/vtags update-ref refs/tags/vtags-3.0 HEAD~1 &&
git -C repos/vtags update-ref refs/tags/release/2.0 HEAD~1 &&
git -C repos/vtags update-ref refs/heads/topic/x HEAD~1 &&
git -C repos/vtags pack-refs --all &&
git -C repos/vtags update-ref refs/tags/v1.0 HEAD~1 &&
{
! test_have_prereq REFFILES ||
test_path_is_file repos/vtags/.git/refs/tags/v1.0
} &&
cgitrc_repo vtagsrc vtags snapshots=tar
'
test_expect_success 'a loose v tag sheds its v and the name leads back to it' '
cgit_ok vtagsrc "url=vtags/refs/" >tmp &&
grep "<a href=./vtags/snapshot/vtags-1.0.tar.>vtags-1.0.tar</a>" tmp &&
! grep "vtags-v1.0.tar" tmp &&
cgit_ok vtagsrc "url=vtags/snapshot/vtags-1.0.tar" >tmp &&
snapshot_members &&
grep "^vtags-1.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'an underscore after the prefix leads back to the tag too' '
cgit_ok vtagsrc "url=vtags/snapshot/vtags_1.0.tar" >tmp &&
snapshot_members &&
grep "^vtags_1.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'a V tag sheds its V and the name leads back to it' '
cgit_ok vtagsrc "url=vtags/refs/" >tmp &&
grep "<a href=./vtags/snapshot/vtags-2.0.tar.>vtags-2.0.tar</a>" tmp &&
cgit_ok vtagsrc "url=vtags/snapshot/vtags-2.0.tar" >tmp &&
snapshot_members &&
grep "^vtags-2.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'a tag that starts with the prefix is not prefixed again' '
cgit_ok vtagsrc "url=vtags/refs/" >tmp &&
grep "<a href=./vtags/snapshot/vtags-3.0.tar.>vtags-3.0.tar</a>" tmp &&
! grep "vtags-vtags-3.0" tmp &&
cgit_ok vtagsrc "url=vtags/snapshot/vtags-3.0.tar" >tmp &&
snapshot_members &&
grep "^vtags-3.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'a snapshot-prefix names the snapshots in its place' '
cgitrc_repo prefixrc vtags snapshots=tar repo.snapshot-prefix=proj &&
cgit_ok prefixrc "url=vtags/refs/" >tmp &&
grep "<a href=./vtags/snapshot/proj-1.0.tar.>proj-1.0.tar</a>" tmp &&
cgit_ok prefixrc "url=vtags/snapshot/proj-1.0.tar" >tmp &&
snapshot_members &&
grep "^proj-1.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'snapshots=all offers every format' '
cgitrc_repo allrc vtags snapshots=all &&
cgit_ok allrc "url=vtags/refs/" >tmp &&
for suffix in tar tar.gz tar.bz2 tar.lz tar.xz tar.zst zip
do
grep ">vtags-1.0.$suffix</a>" tmp || return 1
done
'
test_expect_success 'a ref with a slash is linked under a dashed name' '
cgit_ok vtagsrc "url=vtags/refs/" >tmp &&
grep "<a href=./vtags/snapshot/vtags-release-2.0.tar?id=release/2.0.>vtags-release-2.0.tar</a>" tmp &&
cgit_ok vtagsrc "url=vtags/commit/&h=topic/x" >tmp &&
grep "<a href=./vtags/snapshot/vtags-topic-x.tar?id=topic/x.>vtags-topic-x.tar</a>" tmp
'
test_expect_success 'the dashed name serves the ref under the repository prefix' '
cgit_ok vtagsrc "url=vtags/snapshot/vtags-release-2.0.tar&id=release/2.0" >tmp &&
snapshot_members &&
commit=$(git -C repos/vtags get-tar-commit-id <snapshot.tar) &&
want=$(git -C repos/vtags rev-parse "release/2.0^{commit}") &&
test -n "$want" &&
test "$commit" = "$want" &&
grep "^vtags/file-1$" members &&
! grep "^vtags-release" members
'
test_expect_success 'a format the repository does not offer is refused' '
cgit_rc vtagsrc "url=vtags/snapshot/vtags-1.0.zip" >tmp &&
grep "^Status: 400" tmp &&
grep "<div class=.error.>Unsupported snapshot format: vtags-1.0.zip</div>" tmp &&
cgit_rc vtagsrc "url=vtags/snapshot/vtags-1.0.tar.xz" >tmp &&
grep "^Status: 400" tmp &&
grep "<div class=.error.>Unsupported snapshot format: vtags-1.0.tar.xz</div>" tmp
'
test_expect_success 'a request naming no snapshot is refused' '
cgit_rc vtagsrc "url=vtags/snapshot/" >tmp &&
grep "^Status: 400" tmp &&
grep "<div class=.error.>No snapshot name specified</div>" tmp
'
test_expect_success 'an id naming no commit is refused' '
cgit_rc vtagsrc "url=vtags/snapshot/vtags.tar&id=nosuchref" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Bad object id: nosuchref</div>" tmp
'
test_expect_success 'set up a repo with a tag holding a slash' '
mkrepo repos/names 2 &&
git -C repos/names tag rel/1 &&
cgitrc_repo namesrc names snapshots=tar
'
test_expect_success 'a snapshot name holding a slash is refused' '
for name in rel/1 :/../../..
do
cgit_rc namesrc "url=names/snapshot/$name.tar" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Not found</div>" tmp || return 1
done &&
cgit_ok namesrc "url=names/snapshot/names-master.tar" >tmp
'
test_expect_success 'a snapshot name in revision syntax is refused' '
cgit_rc namesrc "url=names/snapshot/master~1.tar" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Not found</div>" tmp &&
cgit_ok namesrc "url=names/snapshot/master.tar" >tmp
'
test_expect_success 'set up a repo whose tags differ only in case' '
mkrepo repos/vcase 2 &&
git -C repos/vcase update-ref refs/tags/v1.0 HEAD &&
git -C repos/vcase pack-refs --all &&
git -C repos/vcase update-ref refs/tags/V1.0 HEAD~1 &&
git -C repos/vcase pack-refs --all &&
cgitrc_repo vcaserc vcase snapshots=tar
'
test_expect_success 'a real case collision keeps the v on both tags' '
cgit_ok vcaserc "url=vcase/refs/" >tmp &&
grep ">vcase-v1.0.tar</a>" tmp &&
grep ">vcase-V1.0.tar</a>" tmp &&
! grep ">vcase-1.0.tar</a>" tmp
'
test_expect_success 'each of those names leads back to its own tag' '
cgit_ok vcaserc "url=vcase/snapshot/vcase-v1.0.tar" >tmp &&
snapshot_members &&
grep "^vcase-v1.0/file-2$" members &&
cgit_ok vcaserc "url=vcase/snapshot/vcase-V1.0.tar" >tmp &&
snapshot_members &&
grep "^vcase-V1.0/file-1$" members &&
! grep "file-2" members
'
test_expect_success 'set up a repo with a branch named v' '
mkrepo repos/stem 1 &&
git -C repos/stem branch v HEAD &&
cgitrc_repo stemrc stem snapshots=tar
'
test_expect_success 'a snapshot name with nothing before its suffix is refused' '
cgit_rc stemrc "url=stem/snapshot/.tar" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Not found</div>" tmp &&
cgit_ok stemrc "url=stem/snapshot/stem-v.tar" >tmp &&
snapshot_members &&
grep "^stem-v/file-1$" members
'
test_expect_success 'an empty snapshot-prefix puts no slash ahead of the members' '
cgitrc_repo noprefixrc stem snapshots=tar repo.snapshot-prefix= &&
cgit_ok noprefixrc "url=stem/snapshot/stem.tar&id=master" >tmp &&
snapshot_members &&
grep "^file-1$" members &&
! grep "^/" members
'
test_expect_success 'set up repositories whose urls end in .git' '
mkrepo repos/dotgit 1 &&
git -C repos/dotgit tag v1.0 &&
{
echo "virtual-root=/" &&
echo "cache-size=0" &&
echo "snapshots=tar" &&
echo "repo.url=.git" &&
echo "repo.path=$PWD/repos/dotgit/.git" &&
echo "repo.url=sub/.git" &&
echo "repo.path=$PWD/repos/dotgit/.git"
} >dotgitrc
'
test_expect_success 'a url of .git names a snapshot after the ref alone' '
cgit_ok dotgitrc "url=.git/refs/" >tmp &&
grep ">v1.0.tar<" tmp &&
cgit_ok dotgitrc "url=.git/snapshot/v1.0.tar" >tmp &&
snapshot_members &&
grep "^v1.0/file-1$" members
'
test_expect_success 'a url ending in /.git names a snapshot after its directory' '
cgit_ok dotgitrc "url=sub/.git/refs/" >tmp &&
grep ">sub-1.0.tar<" tmp &&
cgit_ok dotgitrc "url=sub/.git/snapshot/sub-1.0.tar" >tmp &&
snapshot_members &&
grep "^sub-1.0/file-1$" members
'
test -n "$CGIT_VALGRIND" || test_set_prereq NO_VALGRIND
test_expect_success GZIP,NO_VALGRIND 'a compressed snapshot is made without a PATH' '
cgitrc_copy nopathrc &&
cgit_bin=$(command -v cgit) &&
(
unset PATH &&
CGIT_CONFIG="$PWD/nopathrc" QUERY_STRING="url=foo/snapshot/master.tar.gz" "$cgit_bin" >tmp 2>err
) &&
test_must_be_empty err &&
grep "^Status: 200" tmp &&
strip_headers <tmp >nopath.tar.gz &&
gzip -dc nopath.tar.gz >nopath.tar &&
test_cmp_bin expect.tar nopath.tar
'
test_expect_success 'a snapshot name leads to the tag ahead of a branch' '
mkrepo repos/shadow 1 &&
git -C repos/shadow tag v1.0 &&
shadow=$(git -C repos/shadow commit-tree -m shadow "HEAD^{tree}") &&
git -C repos/shadow update-ref refs/heads/1.0 "$shadow" &&
cgitrc_repo shadowrc shadow snapshots=tar &&
cgit_ok shadowrc "url=shadow/snapshot/shadow-1.0.tar" >tmp &&
strip_headers <tmp >tag.tar &&
commit=$(git -C repos/shadow get-tar-commit-id <tag.tar) &&
want=$(git -C repos/shadow rev-parse "v1.0^{commit}") &&
test -n "$want" &&
test "$commit" = "$want"
'
test_expect_success 'set up a repo with signed snapshots' '
test_create_repo repos/signed &&
(
cd repos/signed &&
for tag in one two three big
do
git commit --allow-empty -m $tag &&
git tag $tag || return 1
done &&
echo signature >sig &&
sig=$(git hash-object -w sig) &&
git notes --ref signatures/tar.gz add -C $sig one &&
echo plain >plain &&
plain=$(git hash-object -w plain) &&
git notes --ref signatures/tar add -C $plain three &&
big=$(test_seq 1 2000 | git hash-object -w --stdin) &&
git notes --ref signatures/tar.gz add -C $big big
) &&
cgitrc_repo signedrc signed snapshots=tar.gz
'
test_expect_success 'a snapshot and its signature are linked as one unit' '
cgit_ok signedrc "url=signed/refs/" >tmp &&
grep "<span class=.snapshot.><a href=./signed/snapshot/signed-one.tar.gz.>signed-one.tar.gz</a> (<a href=./signed/snapshot/signed-one.tar.gz.asc.>sig</a>)</span>" tmp
'
test_expect_success 'a signature is served as the note holds it' '
cgit_ok signedrc "url=signed/snapshot/signed-one.tar.gz.asc" >tmp &&
sed "/^$/q" tmp >headers &&
grep "^Content-Type: application/pgp-signature$" headers &&
grep "^Content-Disposition: inline; filename=\"signed-one.tar.gz.asc\"$" headers &&
grep "^X-Content-Type-Options: nosniff$" headers &&
grep "^Content-Security-Policy: default-src .none.$" headers &&
strip_headers <tmp >actual &&
echo signature >expect &&
test_cmp expect actual
'
test_expect_success 'a ref with no signature has none to offer' '
cgit_ok signedrc "url=signed/refs/" >tmp &&
grep "<span class=.snapshot.><a href=./signed/snapshot/signed-two.tar.gz.>signed-two.tar.gz</a></span>" tmp &&
cgit_rc signedrc "url=signed/snapshot/signed-two.tar.gz.asc" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>No signature for signed-two.tar.gz</div>" tmp
'
test_expect_success 'a compressed tar offers the plain tar signature' '
cgit_ok signedrc "url=signed/refs/" >tmp &&
grep "<span class=.snapshot.><a href=./signed/snapshot/signed-three.tar.gz.>signed-three.tar.gz</a> (<a href=./signed/snapshot/signed-three.tar.asc.>sig</a>)</span>" tmp &&
cgit_ok signedrc "url=signed/snapshot/signed-three.tar.asc" >tmp &&
strip_headers <tmp >actual &&
echo plain >expect &&
test_cmp expect actual
'
test_expect_success 'a signature past max-blob-size is refused' '
cgitrc_repo sigrc signed snapshots=tar.gz max-blob-size=1 &&
cgit_rc sigrc "url=signed/snapshot/signed-big.tar.gz.asc" >tmp &&
grep "^Status: 413" tmp &&
cgit_ok sigrc "url=signed/snapshot/signed-one.tar.gz.asc" >tmp
'
test_expect_success 'a snapshot name in UTF-8 is also sent as filename*' '
mkrepo repos/accent 1 &&
tag=$(printf "caf\303\251") &&
git -C repos/accent tag "$tag" &&
cgitrc_repo accentrc accent snapshots=tar &&
cgit_ok accentrc "url=accent/refs/tags" >tmp &&
test_grep "<a href=./accent/snapshot/accent-caf%C3%A9.tar.>accent-$tag.tar</a>" tmp &&
cgit_ok accentrc "url=accent/snapshot/accent-caf%C3%A9.tar" >tmp &&
sed "/^$/q" tmp >headers &&
test_grep "^Content-Disposition: inline; filename=.accent-$tag.tar.; filename\*=UTF-8..accent-caf%C3%A9.tar$" headers
'
test_done
|