Debian Codegraph @

tests/objectverify/tag_test.go

raw
1 //go:build linux
2
3 package objectverify_test
4
5 import (
6 "bytes"
7 "os"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11
12 "github.com/go-git/go-git/v5"
13 )
14
15 func TestTagVerifyAlignment(t *testing.T) {
16 t.Parallel()
17
18 if testing.Short() {
19 t.Skip("skipping tag verify")
20 }
21
22 if gpg == nil {
23 t.Error("gpg not available")
24 t.FailNow()
25 }
26
27 cases := []struct {
28 name string
29 // build returns the unsigned tag bytes that will be signed by
30 // gpg. The signature is appended inline after the body, the
31 // canonical layout produced by `git tag -s`.
32 build func() []byte
33 // postSign optionally rewrites the signed object bytes (e.g.
34 // to append a second inline signature block).
35 postSign func(t *testing.T, signed []byte) []byte
36 }{
37 {
38 name: "valid",
39 build: func() []byte {
40 h, m := canonicalTag()
41 return assembleTag(h, m)
42 },
43 },
44 {
45 name: "duplicate-tag",
46 build: func() []byte {
47 h, m := canonicalTag()
48 dup := []string{
49 h[0], h[1], h[2],
50 "tag v1-override",
51 h[3],
52 }
53 return assembleTag(dup, m)
54 },
55 },
56 {
57 name: "duplicate-tagger",
58 build: func() []byte {
59 h, m := canonicalTag()
60 dup := []string{
61 h[0], h[1], h[2], h[3],
62 "tagger Override Tagger <[email protected]> 1700000001 +0000",
63 }
64 return assembleTag(dup, m)
65 },
66 },
67 {
68 name: "double-signature",
69 build: func() []byte {
70 h, m := canonicalTag()
71 return assembleTag(h, m)
72 },
73 postSign: func(t *testing.T, signed []byte) []byte {
74 // Append a second inline signature over the
75 // already-signed bytes. parse_signature in
76 // upstream and parseSignedBytes in go-git both
77 // pick the last PGP block, so the signed
78 // payload is the original tag plus the first
79 // signature.
80 sig2 := gpgSign(t, signed)
81 return append(append([]byte{}, signed...), []byte(sig2)...)
82 },
83 },
84 }
85
86 for _, tc := range cases {
87 t.Run(tc.name, func(t *testing.T) {
88 t.Parallel()
89
90 if tc.name == "double-signature" && os.Getenv("GIT_VERSION") == "v2.11.0" {
91 t.Skip("multi-signature rejection not yet established in Git 2.11.0")
92 }
93
94 repo := initRepo(t)
95 unsigned := tc.build()
96 sig := gpgSign(t, unsigned)
97 signed := append(append([]byte{}, unsigned...), []byte(sig)...)
98 if tc.postSign != nil {
99 signed = tc.postSign(t, signed)
100 }
101
102 hash := writeLooseObject(t, repo, "tag", signed)
103 gitErr := gitVerifyTag(t, repo, hash)
104
105 r, err := git.PlainOpen(repo)
106 require.NoError(t, err)
107 tag, err := r.TagObject(hash)
108 ggDecodeErr := err
109 var ggVerifyErr error
110 if ggDecodeErr == nil {
111 _, ggVerifyErr = tag.Verify(gpg.pubKey)
112 }
113
114 assertSameVerdict(t, "git verify-tag", gitErr, "go-git Verify",
115 combineErr(ggDecodeErr, ggVerifyErr))
116 })
117 }
118 }
119
120 // canonicalTag returns the byte-exact unsigned annotated-tag body
121 // produced by upstream `git tag -a` on a commit. Individual scenarios
122 // mutate the header lines around it.
123 func canonicalTag() (headers []string, message string) {
124 return []string{
125 "object 1eca38290a3131d0c90709496a9b2207a872631e",
126 "type commit",
127 "tag v1",
128 "tagger Test Tagger <[email protected]> 1700000000 +0000",
129 },
130 "signed annotated tag\n"
131 }
132
133 func assembleTag(headers []string, message string) []byte {
134 var buf bytes.Buffer
135 for _, h := range headers {
136 buf.WriteString(h)
137 buf.WriteByte('\n')
138 }
139 buf.WriteByte('\n')
140 buf.WriteString(message)
141 return buf.Bytes()
142 }