3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 local testroot=`test_init tag_create`
21 local commit_id=`git_show_head $testroot/repo`
25 # Create a tag based on repository's HEAD reference
26 got tag -m 'test' -r $testroot/repo -c HEAD $tag > $testroot/stdout
28 if [ $ret -ne 0 ]; then
29 echo "got ref command failed unexpectedly"
30 test_done "$testroot" "$ret"
34 tag_id=`got ref -r $testroot/repo -l \
35 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
36 echo "Created tag $tag_id" > $testroot/stdout.expected
37 cmp -s $testroot/stdout $testroot/stdout.expected
39 if [ $ret -ne 0 ]; then
40 diff -u $testroot/stdout.expected $testroot/stdout
41 test_done "$testroot" "$ret"
45 # Ensure that Git recognizes the tag Got has created
46 (cd $testroot/repo && git checkout -q $tag)
48 if [ $ret -ne 0 ]; then
49 echo "git checkout command failed unexpectedly"
50 test_done "$testroot" "$ret"
54 # Ensure Got recognizes the new tag
55 got checkout -c $tag $testroot/repo $testroot/wt >/dev/null
57 if [ $ret -ne 0 ]; then
58 echo "got checkout command failed unexpectedly"
59 test_done "$testroot" "$ret"
63 # Create a tag based on implied worktree HEAD ref
64 (cd $testroot/wt && got branch foo > /dev/null)
65 echo 'foo' >> $testroot/wt/alpha
66 (cd $testroot/wt && got commit -m foo > /dev/null)
67 local commit_id2=`git_show_branch_head $testroot/repo foo`
68 (cd $testroot/wt && got tag -m 'test' $tag2 > $testroot/stdout)
70 if [ $ret -ne 0 ]; then
71 test_done "$testroot" "$ret"
75 tag_id2=`got ref -r $testroot/repo -l \
76 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
77 echo "Created tag $tag_id2" > $testroot/stdout.expected
78 cmp -s $testroot/stdout $testroot/stdout.expected
80 if [ $ret -ne 0 ]; then
81 diff -u $testroot/stdout.expected $testroot/stdout
82 test_done "$testroot" "$ret"
86 tagged_commit=`got cat -r $testroot/repo $tag2 | grep ^object \
88 if [ "$tagged_commit" != "$commit_id2" ]; then
89 echo "wrong commit was tagged" >&2
90 test_done "$testroot" "1"
94 (cd $testroot/repo && git checkout -q $tag2)
96 if [ $ret -ne 0 ]; then
97 echo "git checkout command failed unexpectedly"
98 test_done "$testroot" "$ret"
102 # Attempt to create a tag pointing at a non-commit
103 local tree_id=`git_show_tree $testroot/repo`
104 (cd $testroot/wt && got tag -m 'test' -c $tree_id foobar \
107 if [ $ret -eq 0 ]; then
108 echo "got tag command succeeded unexpectedly"
109 test_done "$testroot" "1"
113 echo "got: commit $tree_id: object not found" \
114 > $testroot/stderr.expected
115 cmp -s $testroot/stderr $testroot/stderr.expected
117 if [ $ret -ne 0 ]; then
118 diff -u $testroot/stderr.expected $testroot/stderr
119 test_done "$testroot" "$ret"
123 got ref -r $testroot/repo -l > $testroot/stdout
124 echo "HEAD: $commit_id2" > $testroot/stdout.expected
125 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
126 cat $testroot/wt/.got/uuid | tr -d '\n' >> $testroot/stdout.expected
127 echo ": $commit_id2" >> $testroot/stdout.expected
128 echo "refs/heads/foo: $commit_id2" >> $testroot/stdout.expected
129 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
130 echo "refs/tags/$tag: $tag_id" >> $testroot/stdout.expected
131 echo "refs/tags/$tag2: $tag_id2" >> $testroot/stdout.expected
132 cmp -s $testroot/stdout $testroot/stdout.expected
134 if [ $ret -ne 0 ]; then
135 diff -u $testroot/stdout.expected $testroot/stdout
137 test_done "$testroot" "$ret"
141 local testroot=`test_init tag_list`
142 local commit_id=`git_show_head $testroot/repo`
146 # create tag with Git
147 (cd $testroot/repo && git tag -a -m 'test' $tag)
148 # create tag with Got
149 (cd $testroot/repo && got tag -m 'test' $tag2 > /dev/null)
151 tag_id=`got ref -r $testroot/repo -l \
152 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
153 local tagger_time=`git_show_tagger_time $testroot/repo $tag`
154 d1=`date -u -r $tagger_time +"%a %b %e %X %Y UTC"`
155 tag_id2=`got ref -r $testroot/repo -l \
156 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
157 local tagger_time2=`git_show_tagger_time $testroot/repo $tag2`
158 d2=`date -u -r $tagger_time2 +"%a %b %e %X %Y UTC"`
160 got tag -r $testroot/repo -l > $testroot/stdout
162 echo "-----------------------------------------------" \
163 > $testroot/stdout.expected
164 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
165 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
166 echo "date: $d2" >> $testroot/stdout.expected
167 echo "object: commit $commit_id" >> $testroot/stdout.expected
168 echo " " >> $testroot/stdout.expected
169 echo " test" >> $testroot/stdout.expected
170 echo " " >> $testroot/stdout.expected
171 echo "-----------------------------------------------" \
172 >> $testroot/stdout.expected
173 echo "tag $tag $tag_id" >> $testroot/stdout.expected
174 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
175 echo "date: $d1" >> $testroot/stdout.expected
176 echo "object: commit $commit_id" >> $testroot/stdout.expected
177 echo " " >> $testroot/stdout.expected
178 echo " test" >> $testroot/stdout.expected
179 echo " " >> $testroot/stdout.expected
180 cmp -s $testroot/stdout $testroot/stdout.expected
182 if [ $ret -ne 0 ]; then
183 diff -u $testroot/stdout.expected $testroot/stdout
184 test_done "$testroot" "$ret"
188 got tag -r $testroot/repo -l $tag > $testroot/stdout
190 echo "-----------------------------------------------" \
191 > $testroot/stdout.expected
192 echo "tag $tag $tag_id" >> $testroot/stdout.expected
193 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
194 echo "date: $d1" >> $testroot/stdout.expected
195 echo "object: commit $commit_id" >> $testroot/stdout.expected
196 echo " " >> $testroot/stdout.expected
197 echo " test" >> $testroot/stdout.expected
198 echo " " >> $testroot/stdout.expected
199 cmp -s $testroot/stdout $testroot/stdout.expected
201 if [ $ret -ne 0 ]; then
202 diff -u $testroot/stdout.expected $testroot/stdout
203 test_done "$testroot" "$ret"
207 got tag -r $testroot/repo -l $tag2 > $testroot/stdout
209 echo "-----------------------------------------------" \
210 > $testroot/stdout.expected
211 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
212 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
213 echo "date: $d2" >> $testroot/stdout.expected
214 echo "object: commit $commit_id" >> $testroot/stdout.expected
215 echo " " >> $testroot/stdout.expected
216 echo " test" >> $testroot/stdout.expected
217 echo " " >> $testroot/stdout.expected
218 cmp -s $testroot/stdout $testroot/stdout.expected
220 if [ $ret -ne 0 ]; then
221 diff -u $testroot/stdout.expected $testroot/stdout
223 test_done "$testroot" "$ret"
226 test_tag_list_lightweight() {
227 local testroot=`test_init tag_list_lightweight`
228 local commit_id=`git_show_head $testroot/repo`
232 # create "lightweight" tag with Git
233 (cd $testroot/repo && git tag $tag)
234 (cd $testroot/repo && git tag $tag2)
236 tag_id=`got ref -r $testroot/repo -l \
237 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
238 local tagger_time=`git_show_author_time $testroot/repo $tag`
239 d1=`date -u -r $tagger_time +"%a %b %e %X %Y UTC"`
240 tag_id2=`got ref -r $testroot/repo -l \
241 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
242 local tagger_time2=`git_show_author_time $testroot/repo $tag2`
243 d2=`date -u -r $tagger_time2 +"%a %b %e %X %Y UTC"`
245 got tag -r $testroot/repo -l > $testroot/stdout
247 echo "-----------------------------------------------" \
248 > $testroot/stdout.expected
249 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
250 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
251 echo "date: $d2" >> $testroot/stdout.expected
252 echo "object: commit $commit_id" >> $testroot/stdout.expected
253 echo " " >> $testroot/stdout.expected
254 echo " adding the test tree" >> $testroot/stdout.expected
255 echo " " >> $testroot/stdout.expected
256 echo "-----------------------------------------------" \
257 >> $testroot/stdout.expected
258 echo "tag $tag $tag_id" >> $testroot/stdout.expected
259 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
260 echo "date: $d1" >> $testroot/stdout.expected
261 echo "object: commit $commit_id" >> $testroot/stdout.expected
262 echo " " >> $testroot/stdout.expected
263 echo " adding the test tree" >> $testroot/stdout.expected
264 echo " " >> $testroot/stdout.expected
265 cmp -s $testroot/stdout $testroot/stdout.expected
267 if [ $ret -ne 0 ]; then
268 diff -u $testroot/stdout.expected $testroot/stdout
270 test_done "$testroot" "$ret"
273 test_tag_create_ssh_signed() {
274 local testroot=`test_init tag_create`
275 local commit_id=`git_show_head $testroot/repo`
280 ssh-keygen -q -N '' -t ed25519 -f $testroot/id_ed25519
282 if [ $ret -ne 0 ]; then
283 echo "ssh-keygen failed unexpectedly"
284 test_done "$testroot" "$ret"
287 touch $testroot/allowed_signers
288 touch $testroot/revoked_signers
289 echo "allowed_signers \"$testroot/allowed_signers\"" >> \
290 $testroot/repo/.git/got.conf
291 echo "revoked_signers \"$testroot/revoked_signers\"" >> \
292 $testroot/repo/.git/got.conf
294 # Create a signed tag based on repository's HEAD reference
295 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo -c HEAD \
296 $tag > $testroot/stdout
298 if [ $ret -ne 0 ]; then
299 echo "got tag command failed unexpectedly"
300 test_done "$testroot" "$ret"
304 tag_id=`got ref -r $testroot/repo -l \
305 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
306 echo "Created tag $tag_id" > $testroot/stdout.expected
307 cmp -s $testroot/stdout $testroot/stdout.expected
309 if [ $ret -ne 0 ]; then
310 diff -u $testroot/stdout.expected $testroot/stdout
311 test_done "$testroot" "$ret"
315 # Ensure validation fails when the key is not allowed
316 echo "signature: Could not verify signature." > \
317 $testroot/stdout.expected
318 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
320 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
321 if [ $ret -eq 0 ]; then
322 diff -u $testroot/stdout.expected $testroot/stdout
323 test_done "$testroot" "1"
327 GOOD_SIG='Good "git" signature for flan_hacker@openbsd.org with ED25519 key '
329 # Validate the signature with the key allowed
330 echo -n 'flan_hacker@openbsd.org ' > $testroot/allowed_signers
331 cat $testroot/id_ed25519.pub >> $testroot/allowed_signers
332 GOT_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
334 if [ $ret -ne 0 ]; then
335 echo "got tag command failed unexpectedly"
336 diff -u $testroot/stdout.expected $testroot/stdout
337 test_done "$testroot" "$ret"
341 if ! echo "$GOT_STDOUT" | grep -q "^signature: $GOOD_SIG"; then
342 echo "got tag command failed to validate signature"
343 test_done "$testroot" "1"
347 # Ensure validation fails after revoking the key
348 ssh-keygen -y -f $testroot/id_ed25519 >> $testroot/revoked_signers
349 echo "signature: Could not verify signature." > \
350 $testroot/stdout.expected
351 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
353 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
354 if [ $ret -eq 0 ]; then
355 diff -u $testroot/stdout.expected $testroot/stdout
356 test_done "$testroot" "1"
360 # Later tests expect validation to work
361 echo -n > $testroot/revoked_signers
363 # Ensure that Git recognizes and verifies the tag Got has created
364 (cd $testroot/repo && git checkout -q $tag)
366 if [ $ret -ne 0 ]; then
367 echo "git checkout command failed unexpectedly"
368 test_done "$testroot" "$ret"
371 (cd $testroot/repo && git config --local gpg.ssh.allowedSignersFile \
372 $testroot/allowed_signers)
373 GIT_STDERR=$(cd $testroot/repo && git tag -v $tag 2>&1 1>/dev/null)
374 if ! echo "$GIT_STDERR" | grep -q "^$GOOD_SIG"; then
375 echo "git tag command failed to validate signature"
376 test_done "$testroot" "1"
380 # Ensure Got recognizes the new tag
381 got checkout -c $tag $testroot/repo $testroot/wt >/dev/null
383 if [ $ret -ne 0 ]; then
384 echo "got checkout command failed unexpectedly"
385 test_done "$testroot" "$ret"
389 # Create another signed tag with a SHA1 commit ID
390 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo \
391 -c $commit_id $tag2 > $testroot/stdout
393 # Create another signed tag with key defined in got.conf(5)
394 echo "signer_id \"$testroot/id_ed25519\"" >> \
395 $testroot/repo/.git/got.conf
396 got tag -m 'test' -r $testroot/repo -c HEAD $tag3 > $testroot/stdout
398 if [ $ret -ne 0 ]; then
399 echo "got tag command failed unexpectedly"
400 test_done "$testroot" "$ret"
404 # got tag -V behaves like got tag -l, but with verification enabled.
405 got tag -l -r $testroot/repo > $testroot/stdout.list
406 got tag -V -r $testroot/repo > $testroot/stdout.verify
407 diff -U0 $testroot/stdout.list $testroot/stdout.verify |
408 sed -e '/^--- /d' -e '/^+++ /d' > $testroot/stdout
409 echo "@@ -5,0 +6 @@" > $testroot/stdout.expected
410 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
411 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
412 >> $testroot/stdout.expected
413 echo "@@ -19,0 +21 @@" >> $testroot/stdout.expected
414 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
415 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
416 >> $testroot/stdout.expected
417 echo "@@ -33,0 +36 @@" >> $testroot/stdout.expected
418 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
419 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
420 >> $testroot/stdout.expected
421 cmp -s $testroot/stdout $testroot/stdout.expected
423 if [ $ret -ne 0 ]; then
424 diff -u $testroot/stdout.expected $testroot/stdout
426 test_done "$testroot" "$ret"
429 test_tag_create_ssh_signed_missing_key() {
430 local testroot=`test_init tag_create`
431 local commit_id=`git_show_head $testroot/repo`
434 # Fail to create a signed tag due to a missing SSH key
435 got tag -s $testroot/bogus -m 'test' -r $testroot/repo \
436 -c HEAD $tag > $testroot/stdout 2> $testroot/stderr
438 if [ $ret -eq 0 ]; then
439 echo "got tag command succeeded unexpectedly"
440 test_done "$testroot" 1
444 got ref -r $testroot/repo -l > $testroot/stdout
445 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
446 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
447 cmp -s $testroot/stdout $testroot/stdout.expected
449 if [ $ret -ne 0 ]; then
450 diff -u $testroot/stdout.expected $testroot/stdout
451 test_done "$testroot" "$ret"
454 printf "Couldn't load public key $testroot/bogus: " \
455 >> $testroot/stderr.expected
456 printf "No such file or directory\r\n" >> $testroot/stderr.expected
457 echo "got: unable to sign tag" >> $testroot/stderr.expected
458 cmp -s $testroot/stderr $testroot/stderr.expected
460 if [ $ret -ne 0 ]; then
461 diff -u $testroot/stderr.expected $testroot/stderr
463 test_done "$testroot" "$ret"
467 run_test test_tag_create
468 run_test test_tag_list
469 run_test test_tag_list_lightweight
470 run_test test_tag_create_ssh_signed
471 run_test test_tag_create_ssh_signed_missing_key