Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
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.
8 #
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.
17 . ./common.sh
19 test_tag_create() {
20 local testroot=`test_init tag_create`
21 local commit_id=`git_show_head $testroot/repo`
22 local tag=1.0.0
23 local tag2=2.0.0
25 # Create a tag based on repository's HEAD reference
26 got tag -m 'test' -r $testroot/repo -c HEAD $tag > $testroot/stdout
27 ret=$?
28 if [ $ret -ne 0 ]; then
29 echo "got ref command failed unexpectedly"
30 test_done "$testroot" "$ret"
31 return 1
32 fi
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
38 ret=$?
39 if [ $ret -ne 0 ]; then
40 diff -u $testroot/stdout.expected $testroot/stdout
41 test_done "$testroot" "$ret"
42 return 1
43 fi
45 # Ensure that Git recognizes the tag Got has created
46 (cd $testroot/repo && git checkout -q $tag)
47 ret=$?
48 if [ $ret -ne 0 ]; then
49 echo "git checkout command failed unexpectedly"
50 test_done "$testroot" "$ret"
51 return 1
52 fi
54 # Ensure Got recognizes the new tag
55 got checkout -c $tag $testroot/repo $testroot/wt >/dev/null
56 ret=$?
57 if [ $ret -ne 0 ]; then
58 echo "got checkout command failed unexpectedly"
59 test_done "$testroot" "$ret"
60 return 1
61 fi
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)
69 ret=$?
70 if [ $ret -ne 0 ]; then
71 test_done "$testroot" "$ret"
72 return 1
73 fi
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
79 ret=$?
80 if [ $ret -ne 0 ]; then
81 diff -u $testroot/stdout.expected $testroot/stdout
82 test_done "$testroot" "$ret"
83 return 1
84 fi
86 tagged_commit=`got cat -r $testroot/repo $tag2 | grep ^object \
87 | cut -d' ' -f2`
88 if [ "$tagged_commit" != "$commit_id2" ]; then
89 echo "wrong commit was tagged" >&2
90 test_done "$testroot" "1"
91 return 1
92 fi
94 (cd $testroot/repo && git checkout -q $tag2)
95 ret=$?
96 if [ $ret -ne 0 ]; then
97 echo "git checkout command failed unexpectedly"
98 test_done "$testroot" "$ret"
99 return 1
100 fi
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 \
105 2> $testroot/stderr)
106 ret=$?
107 if [ $ret -eq 0 ]; then
108 echo "got tag command succeeded unexpectedly"
109 test_done "$testroot" "1"
110 return 1
111 fi
113 echo "got: commit $tree_id: object not found" \
114 > $testroot/stderr.expected
115 cmp -s $testroot/stderr $testroot/stderr.expected
116 ret=$?
117 if [ $ret -ne 0 ]; then
118 diff -u $testroot/stderr.expected $testroot/stderr
119 test_done "$testroot" "$ret"
120 return 1
121 fi
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
133 ret=$?
134 if [ $ret -ne 0 ]; then
135 diff -u $testroot/stdout.expected $testroot/stdout
136 fi
137 test_done "$testroot" "$ret"
140 test_tag_list() {
141 local testroot=`test_init tag_list`
142 local commit_id=`git_show_head $testroot/repo`
143 local tag=1.0.0
144 local tag2=2.0.0
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
181 ret=$?
182 if [ $ret -ne 0 ]; then
183 diff -u $testroot/stdout.expected $testroot/stdout
184 test_done "$testroot" "$ret"
185 return 1
186 fi
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
200 ret=$?
201 if [ $ret -ne 0 ]; then
202 diff -u $testroot/stdout.expected $testroot/stdout
203 test_done "$testroot" "$ret"
204 return 1
205 fi
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
219 ret=$?
220 if [ $ret -ne 0 ]; then
221 diff -u $testroot/stdout.expected $testroot/stdout
222 fi
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`
229 local tag=1.0.0
230 local tag2=2.0.0
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 # test signature validation ignoring lightweight tags
248 got tag -r $testroot/repo -V > $testroot/stdout
250 echo "-----------------------------------------------" \
251 > $testroot/stdout.expected
252 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
253 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
254 echo "date: $d2" >> $testroot/stdout.expected
255 echo "object: commit $commit_id" >> $testroot/stdout.expected
256 echo " " >> $testroot/stdout.expected
257 echo " adding the test tree" >> $testroot/stdout.expected
258 echo " " >> $testroot/stdout.expected
259 echo "-----------------------------------------------" \
260 >> $testroot/stdout.expected
261 echo "tag $tag $tag_id" >> $testroot/stdout.expected
262 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
263 echo "date: $d1" >> $testroot/stdout.expected
264 echo "object: commit $commit_id" >> $testroot/stdout.expected
265 echo " " >> $testroot/stdout.expected
266 echo " adding the test tree" >> $testroot/stdout.expected
267 echo " " >> $testroot/stdout.expected
268 cmp -s $testroot/stdout $testroot/stdout.expected
269 ret=$?
270 if [ $ret -ne 0 ]; then
271 diff -u $testroot/stdout.expected $testroot/stdout
272 fi
273 test_done "$testroot" "$ret"
276 test_tag_create_ssh_signed() {
277 local testroot=`test_init tag_create`
278 local commit_id=`git_show_head $testroot/repo`
279 local tag=1.0.0
280 local tag2=2.0.0
281 local tag3=3.0.0
283 ssh-keygen -q -N '' -t ed25519 -f $testroot/id_ed25519
284 ret=$?
285 if [ $ret -ne 0 ]; then
286 echo "ssh-keygen failed unexpectedly"
287 test_done "$testroot" "$ret"
288 return 1
289 fi
290 touch $testroot/allowed_signers
291 touch $testroot/revoked_signers
292 echo "allowed_signers \"$testroot/allowed_signers\"" >> \
293 $testroot/repo/.git/got.conf
294 echo "revoked_signers \"$testroot/revoked_signers\"" >> \
295 $testroot/repo/.git/got.conf
297 # Create a signed tag based on repository's HEAD reference
298 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo -c HEAD \
299 $tag > $testroot/stdout
300 ret=$?
301 if [ $ret -ne 0 ]; then
302 echo "got tag command failed unexpectedly"
303 test_done "$testroot" "$ret"
304 return 1
305 fi
307 tag_id=`got ref -r $testroot/repo -l \
308 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
309 echo "Created tag $tag_id" > $testroot/stdout.expected
310 cmp -s $testroot/stdout $testroot/stdout.expected
311 ret=$?
312 if [ $ret -ne 0 ]; then
313 diff -u $testroot/stdout.expected $testroot/stdout
314 test_done "$testroot" "$ret"
315 return 1
316 fi
318 # Ensure validation fails when the key is not allowed
319 echo "signature: Could not verify signature." > \
320 $testroot/stdout.expected
321 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
322 ret=$?
323 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
324 if [ $ret -eq 0 ]; then
325 diff -u $testroot/stdout.expected $testroot/stdout
326 test_done "$testroot" "1"
327 return 1
328 fi
330 GOOD_SIG='Good "git" signature for flan_hacker@openbsd.org with ED25519 key '
332 # Validate the signature with the key allowed
333 echo -n 'flan_hacker@openbsd.org ' > $testroot/allowed_signers
334 cat $testroot/id_ed25519.pub >> $testroot/allowed_signers
335 GOT_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
336 ret=$?
337 if [ $ret -ne 0 ]; then
338 echo "got tag command failed unexpectedly"
339 diff -u $testroot/stdout.expected $testroot/stdout
340 test_done "$testroot" "$ret"
341 return 1
342 fi
344 if ! echo "$GOT_STDOUT" | grep -q "^signature: $GOOD_SIG"; then
345 echo "got tag command failed to validate signature"
346 test_done "$testroot" "1"
347 return 1
348 fi
350 # Ensure validation fails after revoking the key
351 ssh-keygen -y -f $testroot/id_ed25519 >> $testroot/revoked_signers
352 echo "signature: Could not verify signature." > \
353 $testroot/stdout.expected
354 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
355 ret=$?
356 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
357 if [ $ret -eq 0 ]; then
358 diff -u $testroot/stdout.expected $testroot/stdout
359 test_done "$testroot" "1"
360 return 1
361 fi
363 # Later tests expect validation to work
364 echo -n > $testroot/revoked_signers
366 # Ensure that Git recognizes and verifies the tag Got has created
367 (cd $testroot/repo && git checkout -q $tag)
368 ret=$?
369 if [ $ret -ne 0 ]; then
370 echo "git checkout command failed unexpectedly"
371 test_done "$testroot" "$ret"
372 return 1
373 fi
374 (cd $testroot/repo && git config --local gpg.ssh.allowedSignersFile \
375 $testroot/allowed_signers)
376 GIT_STDERR=$(cd $testroot/repo && git tag -v $tag 2>&1 1>/dev/null)
377 if ! echo "$GIT_STDERR" | grep -q "^$GOOD_SIG"; then
378 echo "git tag command failed to validate signature"
379 test_done "$testroot" "1"
380 return 1
381 fi
383 # Ensure Got recognizes the new tag
384 got checkout -c $tag $testroot/repo $testroot/wt >/dev/null
385 ret=$?
386 if [ $ret -ne 0 ]; then
387 echo "got checkout command failed unexpectedly"
388 test_done "$testroot" "$ret"
389 return 1
390 fi
392 # Create another signed tag with a SHA1 commit ID
393 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo \
394 -c $commit_id $tag2 > $testroot/stdout
396 # Create another signed tag with key defined in got.conf(5)
397 echo "signer_id \"$testroot/id_ed25519\"" >> \
398 $testroot/repo/.git/got.conf
399 got tag -m 'test' -r $testroot/repo -c HEAD $tag3 > $testroot/stdout
400 ret=$?
401 if [ $ret -ne 0 ]; then
402 echo "got tag command failed unexpectedly"
403 test_done "$testroot" "$ret"
404 return 1
405 fi
407 # got tag -V behaves like got tag -l, but with verification enabled.
408 got tag -l -r $testroot/repo > $testroot/stdout.list
409 got tag -V -r $testroot/repo > $testroot/stdout.verify
410 diff -U0 $testroot/stdout.list $testroot/stdout.verify |
411 sed -e '/^--- /d' -e '/^+++ /d' > $testroot/stdout
412 echo "@@ -5,0 +6 @@" > $testroot/stdout.expected
413 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
414 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
415 >> $testroot/stdout.expected
416 echo "@@ -19,0 +21 @@" >> $testroot/stdout.expected
417 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
418 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
419 >> $testroot/stdout.expected
420 echo "@@ -33,0 +36 @@" >> $testroot/stdout.expected
421 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
422 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
423 >> $testroot/stdout.expected
424 cmp -s $testroot/stdout $testroot/stdout.expected
425 ret=$?
426 if [ $ret -ne 0 ]; then
427 diff -u $testroot/stdout.expected $testroot/stdout
428 fi
429 test_done "$testroot" "$ret"
432 test_tag_create_ssh_signed_missing_key() {
433 local testroot=`test_init tag_create`
434 local commit_id=`git_show_head $testroot/repo`
435 local tag=1.0.0
437 # Fail to create a signed tag due to a missing SSH key
438 got tag -s $testroot/bogus -m 'test' -r $testroot/repo \
439 -c HEAD $tag > $testroot/stdout 2> $testroot/stderr
440 ret=$?
441 if [ $ret -eq 0 ]; then
442 echo "got tag command succeeded unexpectedly"
443 test_done "$testroot" 1
444 return 1
445 fi
447 got ref -r $testroot/repo -l > $testroot/stdout
448 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
449 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
450 cmp -s $testroot/stdout $testroot/stdout.expected
451 ret=$?
452 if [ $ret -ne 0 ]; then
453 diff -u $testroot/stdout.expected $testroot/stdout
454 test_done "$testroot" "$ret"
455 return 1
456 fi
457 printf "Couldn't load public key $testroot/bogus: " \
458 >> $testroot/stderr.expected
459 printf "No such file or directory\r\n" >> $testroot/stderr.expected
460 echo "got: unable to sign tag" >> $testroot/stderr.expected
461 cmp -s $testroot/stderr $testroot/stderr.expected
462 ret=$?
463 if [ $ret -ne 0 ]; then
464 diff -u $testroot/stderr.expected $testroot/stderr
465 fi
466 test_done "$testroot" "$ret"
469 test_parseargs "$@"
470 run_test test_tag_create
471 run_test test_tag_list
472 run_test test_tag_list_lightweight
473 run_test test_tag_create_ssh_signed
474 run_test test_tag_create_ssh_signed_missing_key