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 tag -m 'test' $tag2 > $testroot/stdout)
65 ret=$?
66 if [ $ret -ne 0 ]; then
67 test_done "$testroot" "$ret"
68 return 1
69 fi
71 tag_id2=`got ref -r $testroot/repo -l \
72 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
73 echo "Created tag $tag_id2" > $testroot/stdout.expected
74 cmp -s $testroot/stdout $testroot/stdout.expected
75 ret=$?
76 if [ $ret -ne 0 ]; then
77 diff -u $testroot/stdout.expected $testroot/stdout
78 test_done "$testroot" "$ret"
79 return 1
80 fi
82 (cd $testroot/repo && git checkout -q $tag2)
83 ret=$?
84 if [ $ret -ne 0 ]; then
85 echo "git checkout command failed unexpectedly"
86 test_done "$testroot" "$ret"
87 return 1
88 fi
90 # Attempt to create a tag pointing at a non-commit
91 local tree_id=`git_show_tree $testroot/repo`
92 (cd $testroot/wt && got tag -m 'test' -c $tree_id foobar \
93 2> $testroot/stderr)
94 ret=$?
95 if [ $ret -eq 0 ]; then
96 echo "git tag command succeeded unexpectedly"
97 test_done "$testroot" "1"
98 return 1
99 fi
101 echo "got: commit $tree_id: object not found" \
102 > $testroot/stderr.expected
103 cmp -s $testroot/stderr $testroot/stderr.expected
104 ret=$?
105 if [ $ret -ne 0 ]; then
106 diff -u $testroot/stderr.expected $testroot/stderr
107 test_done "$testroot" "$ret"
108 return 1
109 fi
111 got ref -r $testroot/repo -l > $testroot/stdout
112 echo "HEAD: $commit_id" > $testroot/stdout.expected
113 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
114 cat $testroot/wt/.got/uuid | tr -d '\n' >> $testroot/stdout.expected
115 echo ": $commit_id" >> $testroot/stdout.expected
116 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
117 echo "refs/tags/$tag: $tag_id" >> $testroot/stdout.expected
118 echo "refs/tags/$tag2: $tag_id2" >> $testroot/stdout.expected
119 cmp -s $testroot/stdout $testroot/stdout.expected
120 ret=$?
121 if [ $ret -ne 0 ]; then
122 diff -u $testroot/stdout.expected $testroot/stdout
123 fi
124 test_done "$testroot" "$ret"
127 test_tag_list() {
128 local testroot=`test_init tag_list`
129 local commit_id=`git_show_head $testroot/repo`
130 local tag=1.0.0
131 local tag2=2.0.0
133 # create tag with Git
134 (cd $testroot/repo && git tag -a -m 'test' $tag)
135 # create tag with Got
136 (cd $testroot/repo && got tag -m 'test' $tag2 > /dev/null)
138 tag_id=`got ref -r $testroot/repo -l \
139 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
140 local tagger_time=`git_show_tagger_time $testroot/repo $tag`
141 d1=`date -u -r $tagger_time +"%a %b %e %X %Y UTC"`
142 tag_id2=`got ref -r $testroot/repo -l \
143 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
144 local tagger_time2=`git_show_tagger_time $testroot/repo $tag2`
145 d2=`date -u -r $tagger_time2 +"%a %b %e %X %Y UTC"`
147 got tag -r $testroot/repo -l > $testroot/stdout
149 echo "-----------------------------------------------" \
150 > $testroot/stdout.expected
151 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
152 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
153 echo "date: $d2" >> $testroot/stdout.expected
154 echo "object: commit $commit_id" >> $testroot/stdout.expected
155 echo " " >> $testroot/stdout.expected
156 echo " test" >> $testroot/stdout.expected
157 echo " " >> $testroot/stdout.expected
158 echo "-----------------------------------------------" \
159 >> $testroot/stdout.expected
160 echo "tag $tag $tag_id" >> $testroot/stdout.expected
161 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
162 echo "date: $d1" >> $testroot/stdout.expected
163 echo "object: commit $commit_id" >> $testroot/stdout.expected
164 echo " " >> $testroot/stdout.expected
165 echo " test" >> $testroot/stdout.expected
166 echo " " >> $testroot/stdout.expected
167 cmp -s $testroot/stdout $testroot/stdout.expected
168 ret=$?
169 if [ $ret -ne 0 ]; then
170 diff -u $testroot/stdout.expected $testroot/stdout
171 test_done "$testroot" "$ret"
172 return 1
173 fi
175 got tag -r $testroot/repo -l $tag > $testroot/stdout
177 echo "-----------------------------------------------" \
178 > $testroot/stdout.expected
179 echo "tag $tag $tag_id" >> $testroot/stdout.expected
180 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
181 echo "date: $d1" >> $testroot/stdout.expected
182 echo "object: commit $commit_id" >> $testroot/stdout.expected
183 echo " " >> $testroot/stdout.expected
184 echo " test" >> $testroot/stdout.expected
185 echo " " >> $testroot/stdout.expected
186 cmp -s $testroot/stdout $testroot/stdout.expected
187 ret=$?
188 if [ $ret -ne 0 ]; then
189 diff -u $testroot/stdout.expected $testroot/stdout
190 test_done "$testroot" "$ret"
191 return 1
192 fi
194 got tag -r $testroot/repo -l $tag2 > $testroot/stdout
196 echo "-----------------------------------------------" \
197 > $testroot/stdout.expected
198 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
199 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
200 echo "date: $d2" >> $testroot/stdout.expected
201 echo "object: commit $commit_id" >> $testroot/stdout.expected
202 echo " " >> $testroot/stdout.expected
203 echo " test" >> $testroot/stdout.expected
204 echo " " >> $testroot/stdout.expected
205 cmp -s $testroot/stdout $testroot/stdout.expected
206 ret=$?
207 if [ $ret -ne 0 ]; then
208 diff -u $testroot/stdout.expected $testroot/stdout
209 fi
210 test_done "$testroot" "$ret"
213 test_tag_list_lightweight() {
214 local testroot=`test_init tag_list_lightweight`
215 local commit_id=`git_show_head $testroot/repo`
216 local tag=1.0.0
217 local tag2=2.0.0
219 # create "lightweight" tag with Git
220 (cd $testroot/repo && git tag $tag)
221 (cd $testroot/repo && git tag $tag2)
223 tag_id=`got ref -r $testroot/repo -l \
224 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
225 local tagger_time=`git_show_author_time $testroot/repo $tag`
226 d1=`date -u -r $tagger_time +"%a %b %e %X %Y UTC"`
227 tag_id2=`got ref -r $testroot/repo -l \
228 | grep "^refs/tags/$tag2" | tr -d ' ' | cut -d: -f2`
229 local tagger_time2=`git_show_author_time $testroot/repo $tag2`
230 d2=`date -u -r $tagger_time2 +"%a %b %e %X %Y UTC"`
232 got tag -r $testroot/repo -l > $testroot/stdout
234 echo "-----------------------------------------------" \
235 > $testroot/stdout.expected
236 echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
237 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
238 echo "date: $d2" >> $testroot/stdout.expected
239 echo "object: commit $commit_id" >> $testroot/stdout.expected
240 echo " " >> $testroot/stdout.expected
241 echo " adding the test tree" >> $testroot/stdout.expected
242 echo " " >> $testroot/stdout.expected
243 echo "-----------------------------------------------" \
244 >> $testroot/stdout.expected
245 echo "tag $tag $tag_id" >> $testroot/stdout.expected
246 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
247 echo "date: $d1" >> $testroot/stdout.expected
248 echo "object: commit $commit_id" >> $testroot/stdout.expected
249 echo " " >> $testroot/stdout.expected
250 echo " adding the test tree" >> $testroot/stdout.expected
251 echo " " >> $testroot/stdout.expected
252 cmp -s $testroot/stdout $testroot/stdout.expected
253 ret=$?
254 if [ $ret -ne 0 ]; then
255 diff -u $testroot/stdout.expected $testroot/stdout
256 fi
257 test_done "$testroot" "$ret"
260 test_tag_create_ssh_signed() {
261 local testroot=`test_init tag_create`
262 local commit_id=`git_show_head $testroot/repo`
263 local tag=1.0.0
264 local tag2=2.0.0
265 local tag3=3.0.0
267 ssh-keygen -q -N '' -t ed25519 -f $testroot/id_ed25519
268 ret=$?
269 if [ $ret -ne 0 ]; then
270 echo "ssh-keygen failed unexpectedly"
271 test_done "$testroot" "$ret"
272 return 1
273 fi
274 touch $testroot/allowed_signers
275 touch $testroot/revoked_signers
276 echo "allowed_signers \"$testroot/allowed_signers\"" >> \
277 $testroot/repo/.git/got.conf
278 echo "revoked_signers \"$testroot/revoked_signers\"" >> \
279 $testroot/repo/.git/got.conf
281 # Create a signed tag based on repository's HEAD reference
282 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo -c HEAD \
283 $tag > $testroot/stdout
284 ret=$?
285 if [ $ret -ne 0 ]; then
286 echo "got tag command failed unexpectedly"
287 test_done "$testroot" "$ret"
288 return 1
289 fi
291 tag_id=`got ref -r $testroot/repo -l \
292 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
293 echo "Created tag $tag_id" > $testroot/stdout.expected
294 cmp -s $testroot/stdout $testroot/stdout.expected
295 ret=$?
296 if [ $ret -ne 0 ]; then
297 diff -u $testroot/stdout.expected $testroot/stdout
298 test_done "$testroot" "$ret"
299 return 1
300 fi
302 # Ensure validation fails when the key is not allowed
303 echo "signature: Could not verify signature." > \
304 $testroot/stdout.expected
305 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
306 ret=$?
307 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
308 if [ $ret -eq 0 ]; then
309 diff -u $testroot/stdout.expected $testroot/stdout
310 test_done "$testroot" "1"
311 return 1
312 fi
314 GOOD_SIG='Good "git" signature for flan_hacker@openbsd.org with ED25519 key '
316 # Validate the signature with the key allowed
317 echo -n 'flan_hacker@openbsd.org ' > $testroot/allowed_signers
318 cat $testroot/id_ed25519.pub >> $testroot/allowed_signers
319 GOT_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
320 ret=$?
321 if [ $ret -ne 0 ]; then
322 echo "got tag command failed unexpectedly"
323 diff -u $testroot/stdout.expected $testroot/stdout
324 test_done "$testroot" "$ret"
325 return 1
326 fi
328 if ! echo "$GOT_STDOUT" | grep -q "^signature: $GOOD_SIG"; then
329 echo "got tag command failed to validate signature"
330 test_done "$testroot" "1"
331 return 1
332 fi
334 # Ensure validation fails after revoking the key
335 ssh-keygen -y -f $testroot/id_ed25519 >> $testroot/revoked_signers
336 echo "signature: Could not verify signature." > \
337 $testroot/stdout.expected
338 VERIFY_STDOUT=$(got tag -r $testroot/repo -V $tag 2> $testroot/stderr)
339 ret=$?
340 echo "$VERIFY_STDOUT" | grep '^signature: ' > $testroot/stdout
341 if [ $ret -eq 0 ]; then
342 diff -u $testroot/stdout.expected $testroot/stdout
343 test_done "$testroot" "1"
344 return 1
345 fi
347 # Later tests expect validation to work
348 echo -n > $testroot/revoked_signers
350 # Ensure that Git recognizes and verifies the tag Got has created
351 (cd $testroot/repo && git checkout -q $tag)
352 ret=$?
353 if [ $ret -ne 0 ]; then
354 echo "git checkout command failed unexpectedly"
355 test_done "$testroot" "$ret"
356 return 1
357 fi
358 (cd $testroot/repo && git config --local gpg.ssh.allowedSignersFile \
359 $testroot/allowed_signers)
360 GIT_STDERR=$(cd $testroot/repo && git tag -v $tag 2>&1 1>/dev/null)
361 if ! echo "$GIT_STDERR" | grep -q "^$GOOD_SIG"; then
362 echo "git tag command failed to validate signature"
363 test_done "$testroot" "1"
364 return 1
365 fi
367 # Ensure Got recognizes the new tag
368 got checkout -c $tag $testroot/repo $testroot/wt >/dev/null
369 ret=$?
370 if [ $ret -ne 0 ]; then
371 echo "got checkout command failed unexpectedly"
372 test_done "$testroot" "$ret"
373 return 1
374 fi
376 # Create another signed tag with a SHA1 commit ID
377 got tag -s $testroot/id_ed25519 -m 'test' -r $testroot/repo \
378 -c $commit_id $tag2 > $testroot/stdout
380 # Create another signed tag with key defined in got.conf(5)
381 echo "signer_id \"$testroot/id_ed25519\"" >> \
382 $testroot/repo/.git/got.conf
383 got tag -m 'test' -r $testroot/repo -c HEAD $tag3 > $testroot/stdout
384 ret=$?
385 if [ $ret -ne 0 ]; then
386 echo "got tag command failed unexpectedly"
387 test_done "$testroot" "$ret"
388 return 1
389 fi
391 # got tag -V behaves like got tag -l, but with verification enabled.
392 got tag -l -r $testroot/repo > $testroot/stdout.list
393 got tag -V -r $testroot/repo > $testroot/stdout.verify
394 diff -U0 $testroot/stdout.list $testroot/stdout.verify |
395 sed -e '/^--- /d' -e '/^+++ /d' > $testroot/stdout
396 echo "@@ -5,0 +6 @@" > $testroot/stdout.expected
397 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
398 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
399 >> $testroot/stdout.expected
400 echo "@@ -19,0 +21 @@" >> $testroot/stdout.expected
401 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
402 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
403 >> $testroot/stdout.expected
404 echo "@@ -33,0 +36 @@" >> $testroot/stdout.expected
405 echo -n "+signature: $GOOD_SIG" >> $testroot/stdout.expected
406 ssh-keygen -l -f $testroot/id_ed25519.pub | cut -d' ' -f 2 \
407 >> $testroot/stdout.expected
408 cmp -s $testroot/stdout $testroot/stdout.expected
409 ret=$?
410 if [ $ret -ne 0 ]; then
411 diff -u $testroot/stdout.expected $testroot/stdout
412 fi
413 test_done "$testroot" "$ret"
416 test_tag_create_ssh_signed_missing_key() {
417 local testroot=`test_init tag_create`
418 local commit_id=`git_show_head $testroot/repo`
419 local tag=1.0.0
421 # Fail to create a signed tag due to a missing SSH key
422 got tag -s $testroot/bogus -m 'test' -r $testroot/repo \
423 -c HEAD $tag > $testroot/stdout 2> $testroot/stderr
424 ret=$?
425 if [ $ret -eq 0 ]; then
426 echo "got tag command succeeded unexpectedly"
427 test_done "$testroot" 1
428 return 1
429 fi
431 got ref -r $testroot/repo -l > $testroot/stdout
432 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
433 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
434 cmp -s $testroot/stdout $testroot/stdout.expected
435 ret=$?
436 if [ $ret -ne 0 ]; then
437 diff -u $testroot/stdout.expected $testroot/stdout
438 test_done "$testroot" "$ret"
439 return 1
440 fi
441 printf "Couldn't load public key $testroot/bogus: " \
442 >> $testroot/stderr.expected
443 printf "No such file or directory\r\n" >> $testroot/stderr.expected
444 echo "got: unable to sign tag" >> $testroot/stderr.expected
445 cmp -s $testroot/stderr $testroot/stderr.expected
446 ret=$?
447 if [ $ret -ne 0 ]; then
448 diff -u $testroot/stderr.expected $testroot/stderr
449 fi
450 test_done "$testroot" "$ret"
453 test_parseargs "$@"
454 run_test test_tag_create
455 run_test test_tag_list
456 run_test test_tag_list_lightweight
457 run_test test_tag_create_ssh_signed
458 run_test test_tag_create_ssh_signed_missing_key