Skip to content

Commit 446c6fa

Browse files
Ramsay Allan JonesJunio C Hamano
authored andcommitted
New tests and en-passant modifications to mktag.
These changes were originally part of the next patch, but have been split out since they were peripheral to the main purpose of that patch. - update comment describing the signature format to reflect the current code. - remove trailing \n in calls to error(), since a \n is already provided by error(). - remove redundant call to get_sha1_hex(). - call sha1_to_hex(sha1) to convert to ascii, rather than attempting to print the raw sha1. The new tests provide a regression suite to support the modifications to git-mktag in this and the next patch. Signed-off-by: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 7ffe709 commit 446c6fa

File tree

2 files changed

+245
-17
lines changed

2 files changed

+245
-17
lines changed

mktag.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#include "tag.h"
33

44
/*
5-
* A signature file has a very simple fixed format: three lines
6-
* of "object <sha1>" + "type <typename>" + "tag <tagname>",
7-
* followed by some free-form signature that git itself doesn't
8-
* care about, but that can be verified with gpg or similar.
5+
* A signature file has a very simple fixed format: four lines
6+
* of "object <sha1>" + "type <typename>" + "tag <tagname>" +
7+
* "tagger <committer>", followed by a blank line, a free-form tag
8+
* message and a signature block that git itself doesn't care about,
9+
* but that can be verified with gpg or similar.
910
*
1011
* The first three lines are guaranteed to be at least 63 bytes:
1112
* "object <sha1>\n" is 48 bytes, "type tag\n" at 9 bytes is the
@@ -46,45 +47,42 @@ static int verify_tag(char *buffer, unsigned long size)
4647
const char *object, *type_line, *tag_line, *tagger_line;
4748

4849
if (size < 64)
49-
return error("wanna fool me ? you obviously got the size wrong !\n");
50+
return error("wanna fool me ? you obviously got the size wrong !");
5051

5152
buffer[size] = 0;
5253

5354
/* Verify object line */
5455
object = buffer;
5556
if (memcmp(object, "object ", 7))
56-
return error("char%d: does not start with \"object \"\n", 0);
57+
return error("char%d: does not start with \"object \"", 0);
5758

5859
if (get_sha1_hex(object + 7, sha1))
59-
return error("char%d: could not get SHA1 hash\n", 7);
60+
return error("char%d: could not get SHA1 hash", 7);
6061

6162
/* Verify type line */
6263
type_line = object + 48;
6364
if (memcmp(type_line - 1, "\ntype ", 6))
64-
return error("char%d: could not find \"\\ntype \"\n", 47);
65+
return error("char%d: could not find \"\\ntype \"", 47);
6566

6667
/* Verify tag-line */
6768
tag_line = strchr(type_line, '\n');
6869
if (!tag_line)
69-
return error("char%td: could not find next \"\\n\"\n", type_line - buffer);
70+
return error("char%td: could not find next \"\\n\"", type_line - buffer);
7071
tag_line++;
7172
if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
72-
return error("char%td: no \"tag \" found\n", tag_line - buffer);
73+
return error("char%td: no \"tag \" found", tag_line - buffer);
7374

7475
/* Get the actual type */
7576
typelen = tag_line - type_line - strlen("type \n");
7677
if (typelen >= sizeof(type))
77-
return error("char%td: type too long\n", type_line+5 - buffer);
78+
return error("char%td: type too long", type_line+5 - buffer);
7879

7980
memcpy(type, type_line+5, typelen);
8081
type[typelen] = 0;
8182

8283
/* Verify that the object matches */
83-
if (get_sha1_hex(object + 7, sha1))
84-
return error("char%d: could not get SHA1 hash but this is really odd since i got it before !\n", 7);
85-
8684
if (verify_object(sha1, type))
87-
return error("char%d: could not verify object %s\n", 7, sha1);
85+
return error("char%d: could not verify object %s", 7, sha1_to_hex(sha1));
8886

8987
/* Verify the tag-name: we don't allow control characters or spaces in it */
9088
tag_line += 4;
@@ -94,14 +92,17 @@ static int verify_tag(char *buffer, unsigned long size)
9492
break;
9593
if (c > ' ')
9694
continue;
97-
return error("char%td: could not verify tag name\n", tag_line - buffer);
95+
return error("char%td: could not verify tag name", tag_line - buffer);
9896
}
9997

10098
/* Verify the tagger line */
10199
tagger_line = tag_line;
102100

103101
if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
104-
return error("char%td: could not find \"tagger\"\n", tagger_line - buffer);
102+
return error("char%td: could not find \"tagger\"", tagger_line - buffer);
103+
104+
/* TODO: check for committer info + blank line? */
105+
/* Also, the minimum length is probably + "tagger .", or 63+8=71 */
105106

106107
/* The actual stuff afterwards we don't care about.. */
107108
return 0;

t/t3800-mktag.sh

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#!/bin/sh
2+
#
3+
#
4+
5+
test_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+
14+
check_verify_failure () {
15+
test_expect_success \
16+
"$1" \
17+
'git-mktag <tag.sig 2>message ||
18+
egrep -q -f expect.pat message'
19+
}
20+
21+
###########################################################
22+
# first create a commit, so we have a valid object/type
23+
# for the tag.
24+
echo Hello >A
25+
git-update-index --add A
26+
git-commit -m "Initial commit"
27+
head=$(git-rev-parse --verify HEAD)
28+
29+
############################################################
30+
# 1. length check
31+
32+
cat >tag.sig <<EOF
33+
too short for a tag
34+
EOF
35+
36+
cat >expect.pat <<EOF
37+
^error: .*size wrong.*$
38+
EOF
39+
40+
check_verify_failure 'Tag object length check'
41+
42+
############################################################
43+
# 2. object line label check
44+
45+
cat >tag.sig <<EOF
46+
xxxxxx 139e9b33986b1c2670fff52c5067603117b3e895
47+
type tag
48+
tag mytag
49+
EOF
50+
51+
cat >expect.pat <<EOF
52+
^error: char0: .*"object "$
53+
EOF
54+
55+
check_verify_failure '"object" line label check'
56+
57+
############################################################
58+
# 3. object line SHA1 check
59+
60+
cat >tag.sig <<EOF
61+
object zz9e9b33986b1c2670fff52c5067603117b3e895
62+
type tag
63+
tag mytag
64+
EOF
65+
66+
cat >expect.pat <<EOF
67+
^error: char7: .*SHA1 hash$
68+
EOF
69+
70+
check_verify_failure '"object" line SHA1 check'
71+
72+
############################################################
73+
# 4. type line label check
74+
75+
cat >tag.sig <<EOF
76+
object 779e9b33986b1c2670fff52c5067603117b3e895
77+
xxxx tag
78+
tag mytag
79+
EOF
80+
81+
cat >expect.pat <<EOF
82+
^error: char47: .*"[\]ntype "$
83+
EOF
84+
85+
check_verify_failure '"type" line label check'
86+
87+
############################################################
88+
# 5. type line eol check
89+
90+
echo "object 779e9b33986b1c2670fff52c5067603117b3e895" >tag.sig
91+
echo -n "type tagsssssssssssssssssssssssssssssss" >>tag.sig
92+
93+
cat >expect.pat <<EOF
94+
^error: char48: .*"[\]n"$
95+
EOF
96+
97+
check_verify_failure '"type" line eol check'
98+
99+
############################################################
100+
# 6. tag line label check #1
101+
102+
cat >tag.sig <<EOF
103+
object 779e9b33986b1c2670fff52c5067603117b3e895
104+
type tag
105+
xxx mytag
106+
EOF
107+
108+
cat >expect.pat <<EOF
109+
^error: char57: no "tag " found$
110+
EOF
111+
112+
check_verify_failure '"tag" line label check #1'
113+
114+
############################################################
115+
# 7. tag line label check #2
116+
117+
cat >tag.sig <<EOF
118+
object 779e9b33986b1c2670fff52c5067603117b3e895
119+
type taggggggggggggggggggggggggggggggg
120+
tag
121+
EOF
122+
123+
cat >expect.pat <<EOF
124+
^error: char87: no "tag " found$
125+
EOF
126+
127+
check_verify_failure '"tag" line label check #2'
128+
129+
############################################################
130+
# 8. type line type-name length check
131+
132+
cat >tag.sig <<EOF
133+
object 779e9b33986b1c2670fff52c5067603117b3e895
134+
type taggggggggggggggggggggggggggggggg
135+
tag mytag
136+
EOF
137+
138+
cat >expect.pat <<EOF
139+
^error: char53: type too long$
140+
EOF
141+
142+
check_verify_failure '"type" line type-name length check'
143+
144+
############################################################
145+
# 9. verify object (SHA1/type) check
146+
147+
cat >tag.sig <<EOF
148+
object 779e9b33986b1c2670fff52c5067603117b3e895
149+
type tagggg
150+
tag mytag
151+
EOF
152+
153+
cat >expect.pat <<EOF
154+
^error: char7: could not verify object.*$
155+
EOF
156+
157+
check_verify_failure 'verify object (SHA1/type) check'
158+
159+
############################################################
160+
# 10. verify tag-name check
161+
162+
cat >tag.sig <<EOF
163+
object $head
164+
type commit
165+
tag my tag
166+
EOF
167+
168+
cat >expect.pat <<EOF
169+
^error: char67: could not verify tag name$
170+
EOF
171+
172+
check_verify_failure 'verify tag-name check'
173+
174+
############################################################
175+
# 11. tagger line lable check #1
176+
177+
cat >tag.sig <<EOF
178+
object $head
179+
type commit
180+
tag mytag
181+
EOF
182+
183+
cat >expect.pat <<EOF
184+
^error: char70: could not find "tagger"$
185+
EOF
186+
187+
check_verify_failure '"tagger" line label check #1'
188+
189+
############################################################
190+
# 12. tagger line lable check #2
191+
192+
cat >tag.sig <<EOF
193+
object $head
194+
type commit
195+
tag mytag
196+
tagger
197+
EOF
198+
199+
cat >expect.pat <<EOF
200+
^error: char70: could not find "tagger"$
201+
EOF
202+
203+
check_verify_failure '"tagger" line label check #2'
204+
205+
############################################################
206+
# 13. create valid tag
207+
208+
cat >tag.sig <<EOF
209+
object $head
210+
type commit
211+
tag mytag
212+
tagger another@example.com
213+
EOF
214+
215+
test_expect_success \
216+
'create valid tag' \
217+
'git-mktag <tag.sig >.git/refs/tags/mytag 2>message'
218+
219+
############################################################
220+
# 14. check mytag
221+
222+
test_expect_success \
223+
'check mytag' \
224+
'git-tag -l | grep mytag'
225+
226+
227+
test_done

0 commit comments

Comments
 (0)