1#!/bin/sh
2#
3#
4
5test_description='git mktag: tag object verify test'
6
7. ./test-lib.sh
8
9###########################################################
10# check the tag.sig file, expecting verify_tag() to fail,
11# and checking that the error message matches the pattern
12# given in the expect.pat file.
13
14check_verify_failure () {
15	subject=$1 &&
16	message=$2 &&
17	shift 2 &&
18
19	no_strict= &&
20	fsck_obj_ok= &&
21	no_strict= &&
22	while test $# != 0
23	do
24		case "$1" in
25		--no-strict)
26			no_strict=yes
27			;;
28		--fsck-obj-ok)
29			fsck_obj_ok=yes
30			;;
31		esac &&
32		shift
33	done &&
34
35	test_expect_success "fail with [--[no-]strict]: $subject" '
36		test_must_fail git mktag <tag.sig 2>err &&
37		if test -z "$no_strict"
38		then
39			test_must_fail git mktag <tag.sig 2>err2 &&
40			test_cmp err err2
41		else
42			git mktag --no-strict <tag.sig
43		fi
44	'
45
46	test_expect_success "setup: $subject" '
47		tag_ref=refs/tags/bad_tag &&
48
49		# Reset any leftover state from the last $subject
50		rm -rf bad-tag &&
51
52		git init --bare bad-tag &&
53		bad_tag=$(git -C bad-tag hash-object -t tag -w --stdin --literally <tag.sig)
54	'
55
56	test_expect_success "hash-object & fsck unreachable: $subject" '
57		if test -n "$fsck_obj_ok"
58		then
59			git -C bad-tag fsck
60		else
61			test_must_fail git -C bad-tag fsck
62		fi
63	'
64
65	test_expect_success "update-ref & fsck reachable: $subject" '
66		# Make sure the earlier test created it for us
67		git rev-parse "$bad_tag" &&
68
69		# The update-ref of the bad content will fail, do it
70		# anyway to see if it segfaults
71		test_might_fail git -C bad-tag update-ref "$tag_ref" "$bad_tag" &&
72
73		# Manually create the broken, we cannot do it with
74		# update-ref
75		echo "$bad_tag" >"bad-tag/$tag_ref" &&
76
77		# Unlike fsck-ing unreachable content above, this
78		# will always fail.
79		test_must_fail git -C bad-tag fsck
80	'
81
82	test_expect_success "for-each-ref: $subject" '
83		# Make sure the earlier test created it for us
84		git rev-parse "$bad_tag" &&
85
86		echo "$bad_tag" >"bad-tag/$tag_ref" &&
87
88		printf "%s tag\t%s\n" "$bad_tag" "$tag_ref" >expected &&
89		git -C bad-tag for-each-ref "$tag_ref" >actual &&
90		test_cmp expected actual &&
91
92		test_must_fail git -C bad-tag for-each-ref --format="%(*objectname)"
93	'
94
95	test_expect_success "fast-export & fast-import: $subject" '
96		# Make sure the earlier test created it for us
97		git rev-parse "$bad_tag" &&
98
99		test_must_fail git -C bad-tag fast-export --all &&
100		test_must_fail git -C bad-tag fast-export "$bad_tag"
101	'
102}
103
104test_expect_mktag_success() {
105	test_expect_success "$1" '
106		git hash-object -t tag -w --stdin <tag.sig >expected &&
107		git fsck --strict &&
108
109		git mktag <tag.sig >hash &&
110		test_cmp expected hash &&
111		test_when_finished "git update-ref -d refs/tags/mytag $(cat hash)" &&
112		git update-ref refs/tags/mytag $(cat hash) $(test_oid zero) &&
113		git fsck --strict
114	'
115}
116
117###########################################################
118# first create a commit, so we have a valid object/type
119# for the tag.
120test_expect_success 'setup' '
121	test_commit A &&
122	test_commit B &&
123	head=$(git rev-parse --verify HEAD) &&
124	head_parent=$(git rev-parse --verify HEAD~) &&
125	tree=$(git rev-parse HEAD^{tree}) &&
126	blob=$(git rev-parse --verify HEAD:B.t)
127'
128
129test_expect_success 'basic usage' '
130	cat >tag.sig <<-EOF &&
131	object $head
132	type commit
133	tag mytag
134	tagger T A Gger <tagger@example.com> 1206478233 -0500
135	EOF
136	git mktag <tag.sig &&
137	git mktag --end-of-options <tag.sig &&
138	test_expect_code 129 git mktag --unknown-option
139'
140
141############################################################
142#  1. length check
143
144cat >tag.sig <<EOF
145too short for a tag
146EOF
147
148check_verify_failure 'Tag object length check' \
149	'^error:.* missingObject:' 'strict'
150
151############################################################
152#  2. object line label check
153
154cat >tag.sig <<EOF
155xxxxxx $head
156type tag
157tag mytag
158tagger . <> 0 +0000
159
160EOF
161
162check_verify_failure '"object" line label check' '^error:.* missingObject:'
163
164############################################################
165#  3. object line hash check
166
167cat >tag.sig <<EOF
168object $(echo $head | tr 0-9a-f z)
169type tag
170tag mytag
171tagger . <> 0 +0000
172
173EOF
174
175check_verify_failure '"object" line check' '^error:.* badObjectSha1:'
176
177############################################################
178#  4. type line label check
179
180cat >tag.sig <<EOF
181object $head
182xxxx tag
183tag mytag
184tagger . <> 0 +0000
185
186EOF
187
188check_verify_failure '"type" line label check' '^error:.* missingTypeEntry:'
189
190############################################################
191#  5. type line eol check
192
193echo "object $head" >tag.sig
194printf "type tagsssssssssssssssssssssssssssssss" >>tag.sig
195
196check_verify_failure '"type" line eol check' '^error:.* unterminatedHeader:'
197
198############################################################
199#  6. tag line label check #1
200
201cat >tag.sig <<EOF
202object $head
203type tag
204xxx mytag
205tagger . <> 0 +0000
206
207EOF
208
209check_verify_failure '"tag" line label check #1' \
210	'^error:.* missingTagEntry:'
211
212############################################################
213#  7. tag line label check #2
214
215cat >tag.sig <<EOF
216object $head
217type taggggggggggggggggggggggggggggggg
218tag
219EOF
220
221check_verify_failure '"tag" line label check #2' \
222	'^error:.* badType:'
223
224############################################################
225#  8. type line type-name length check
226
227cat >tag.sig <<EOF
228object $head
229type taggggggggggggggggggggggggggggggg
230tag mytag
231EOF
232
233check_verify_failure '"type" line type-name length check' \
234	'^error:.* badType:'
235
236############################################################
237#  9. verify object (hash/type) check
238
239cat >tag.sig <<EOF
240object $(test_oid deadbeef)
241type tag
242tag mytag
243tagger . <> 0 +0000
244
245EOF
246
247check_verify_failure 'verify object (hash/type) check -- correct type, nonexisting object' \
248	'^fatal: could not read tagged object' \
249	--fsck-obj-ok
250
251cat >tag.sig <<EOF
252object $head
253type tagggg
254tag mytag
255tagger . <> 0 +0000
256
257EOF
258
259check_verify_failure 'verify object (hash/type) check -- made-up type, valid object' \
260	'^error:.* badType:'
261
262cat >tag.sig <<EOF
263object $(test_oid deadbeef)
264type tagggg
265tag mytag
266tagger . <> 0 +0000
267
268EOF
269
270check_verify_failure 'verify object (hash/type) check -- made-up type, nonexisting object' \
271	'^error:.* badType:'
272
273cat >tag.sig <<EOF
274object $head
275type tree
276tag mytag
277tagger . <> 0 +0000
278
279EOF
280
281check_verify_failure 'verify object (hash/type) check -- mismatched type, valid object' \
282	'^fatal: object.*tagged as.*tree.*but is.*commit' \
283	--fsck-obj-ok
284
285############################################################
286#  9.5. verify object (hash/type) check -- replacement
287
288test_expect_success 'setup replacement of commit -> commit and tree -> blob' '
289	git replace $head_parent $head &&
290	git replace -f $tree $blob
291'
292
293cat >tag.sig <<EOF
294object $head_parent
295type commit
296tag mytag
297tagger . <> 0 +0000
298
299EOF
300
301test_expect_mktag_success 'tag to a commit replaced by another commit'
302
303cat >tag.sig <<EOF
304object $tree
305type tree
306tag mytag
307tagger . <> 0 +0000
308
309EOF
310
311check_verify_failure 'verify object (hash/type) check -- mismatched type, valid object' \
312	'^fatal: object.*tagged as.*tree.*but is.*blob' \
313	--fsck-obj-ok
314
315############################################################
316# 10. verify tag-name check
317
318cat >tag.sig <<EOF
319object $head
320type commit
321tag my	tag
322tagger . <> 0 +0000
323
324EOF
325
326check_verify_failure 'verify tag-name check' \
327	'^error:.* badTagName:' \
328	--no-strict \
329	--fsck-obj-ok
330
331############################################################
332# 11. tagger line label check #1
333
334cat >tag.sig <<EOF
335object $head
336type commit
337tag mytag
338
339This is filler
340EOF
341
342check_verify_failure '"tagger" line label check #1' \
343	'^error:.* missingTaggerEntry:' \
344	--no-strict \
345	--fsck-obj-ok
346
347############################################################
348# 12. tagger line label check #2
349
350cat >tag.sig <<EOF
351object $head
352type commit
353tag mytag
354tagger
355
356This is filler
357EOF
358
359check_verify_failure '"tagger" line label check #2' \
360	'^error:.* missingTaggerEntry:' \
361	--no-strict \
362	--fsck-obj-ok
363
364############################################################
365# 13. allow missing tag author name like fsck
366
367cat >tag.sig <<EOF
368object $head
369type commit
370tag mytag
371tagger  <> 0 +0000
372
373This is filler
374EOF
375
376test_expect_mktag_success 'allow missing tag author name'
377
378############################################################
379# 14. disallow missing tag author name
380
381cat >tag.sig <<EOF
382object $head
383type commit
384tag mytag
385tagger T A Gger <
386 > 0 +0000
387
388EOF
389
390check_verify_failure 'disallow malformed tagger' \
391	'^error:.* badEmail:' \
392	--no-strict \
393	--fsck-obj-ok
394
395############################################################
396# 15. allow empty tag email
397
398cat >tag.sig <<EOF
399object $head
400type commit
401tag mytag
402tagger T A Gger <> 0 +0000
403
404EOF
405
406test_expect_mktag_success 'allow empty tag email'
407
408############################################################
409# 16. allow spaces in tag email like fsck
410
411cat >tag.sig <<EOF
412object $head
413type commit
414tag mytag
415tagger T A Gger <tag ger@example.com> 0 +0000
416
417EOF
418
419test_expect_mktag_success 'allow spaces in tag email like fsck'
420
421############################################################
422# 17. disallow missing tag timestamp
423
424tr '_' ' ' >tag.sig <<EOF
425object $head
426type commit
427tag mytag
428tagger T A Gger <tagger@example.com>__
429
430EOF
431
432check_verify_failure 'disallow missing tag timestamp' \
433	'^error:.* badDate:'
434
435############################################################
436# 18. detect invalid tag timestamp1
437
438cat >tag.sig <<EOF
439object $head
440type commit
441tag mytag
442tagger T A Gger <tagger@example.com> Tue Mar 25 15:47:44 2008
443
444EOF
445
446check_verify_failure 'detect invalid tag timestamp1' \
447	'^error:.* badDate:'
448
449############################################################
450# 19. detect invalid tag timestamp2
451
452cat >tag.sig <<EOF
453object $head
454type commit
455tag mytag
456tagger T A Gger <tagger@example.com> 2008-03-31T12:20:15-0500
457
458EOF
459
460check_verify_failure 'detect invalid tag timestamp2' \
461	'^error:.* badDate:'
462
463############################################################
464# 20. detect invalid tag timezone1
465
466cat >tag.sig <<EOF
467object $head
468type commit
469tag mytag
470tagger T A Gger <tagger@example.com> 1206478233 GMT
471
472EOF
473
474check_verify_failure 'detect invalid tag timezone1' \
475	'^error:.* badTimezone:'
476
477############################################################
478# 21. detect invalid tag timezone2
479
480cat >tag.sig <<EOF
481object $head
482type commit
483tag mytag
484tagger T A Gger <tagger@example.com> 1206478233 +  30
485
486EOF
487
488check_verify_failure 'detect invalid tag timezone2' \
489	'^error:.* badTimezone:'
490
491############################################################
492# 22. allow invalid tag timezone3 (the maximum is -1200/+1400)
493
494cat >tag.sig <<EOF
495object $head
496type commit
497tag mytag
498tagger T A Gger <tagger@example.com> 1206478233 -1430
499
500EOF
501
502test_expect_mktag_success 'allow invalid tag timezone'
503
504############################################################
505# 23. detect invalid header entry
506
507cat >tag.sig <<EOF
508object $head
509type commit
510tag mytag
511tagger T A Gger <tagger@example.com> 1206478233 -0500
512this line should not be here
513
514EOF
515
516check_verify_failure 'detect invalid header entry' \
517	'^error:.* extraHeaderEntry:' \
518	--no-strict \
519	--fsck-obj-ok
520
521test_expect_success 'invalid header entry config & fsck' '
522	test_must_fail git mktag <tag.sig &&
523	git mktag --no-strict <tag.sig &&
524
525	test_must_fail git -c fsck.extraHeaderEntry=error mktag <tag.sig &&
526	test_must_fail git -c fsck.extraHeaderEntry=error mktag --no-strict <tag.sig &&
527
528	test_must_fail git -c fsck.extraHeaderEntry=warn mktag <tag.sig &&
529	git -c fsck.extraHeaderEntry=warn mktag --no-strict <tag.sig &&
530
531	git -c fsck.extraHeaderEntry=ignore mktag <tag.sig &&
532	git -c fsck.extraHeaderEntry=ignore mktag --no-strict <tag.sig &&
533
534	git fsck &&
535	git -c fsck.extraHeaderEntry=warn fsck 2>err &&
536	grep "warning .*extraHeaderEntry:" err &&
537	test_must_fail git -c fsck.extraHeaderEntry=error 2>err fsck &&
538	grep "error .* extraHeaderEntry:" err
539'
540
541cat >tag.sig <<EOF
542object $head
543type commit
544tag mytag
545tagger T A Gger <tagger@example.com> 1206478233 -0500
546
547
548this line comes after an extra newline
549EOF
550
551test_expect_mktag_success 'allow extra newlines at start of body'
552
553cat >tag.sig <<EOF
554object $head
555type commit
556tag mytag
557tagger T A Gger <tagger@example.com> 1206478233 -0500
558
559EOF
560
561test_expect_mktag_success 'allow a blank line before an empty body (1)'
562
563cat >tag.sig <<EOF
564object $head
565type commit
566tag mytag
567tagger T A Gger <tagger@example.com> 1206478233 -0500
568EOF
569
570test_expect_mktag_success 'allow no blank line before an empty body (2)'
571
572############################################################
573# 24. create valid tag
574
575cat >tag.sig <<EOF
576object $head
577type commit
578tag mytag
579tagger T A Gger <tagger@example.com> 1206478233 -0500
580EOF
581
582test_expect_mktag_success 'create valid tag object'
583
584test_done
585