Debian Codegraph @

tests/objectverify/commit_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/assert"
11 "github.com/stretchr/testify/require"
12
13 "github.com/go-git/go-git/v5"
14 )
15
16 func TestCommitVerifyAlignment(t *testing.T) {
17 t.Parallel()
18
19 if testing.Short() {
20 t.Skip("skipping commit verify")
21 }
22
23 if gpg == nil {
24 t.Error("gpg not available")
25 t.FailNow()
26 }
27
28 cases := []struct {
29 name string
30 // build returns the unsigned commit bytes that will be signed
31 // by gpg. The signature is injected as a single canonical
32 // "gpgsig" header in the standard location and the result is
33 // what both verifiers see, unless postSign rewrites it.
34 build func() []byte
35 // postSign optionally rewrites the signed object bytes (e.g.
36 // to inject a duplicated signature header). nil means use the
37 // standard injection path.
38 postSign func(signed []byte, sig string) []byte
39 }{
40 {
41 name: "valid",
42 build: func() []byte {
43 h, m := canonicalCommit()
44 return assembleCommit(h, m)
45 },
46 },
47 {
48 name: "duplicate-tree",
49 build: func() []byte {
50 h, m := canonicalCommit()
51 dup := append([]string{
52 h[0],
53 "tree 5555555555555555555555555555555555555555",
54 }, h[1:]...)
55 return assembleCommit(dup, m)
56 },
57 },
58 {
59 name: "duplicate-author",
60 build: func() []byte {
61 h, m := canonicalCommit()
62 dup := []string{
63 h[0],
64 h[1],
65 h[2],
66 "author Override Author <[email protected]> 1700000001 +0000",
67 h[3],
68 }
69 return assembleCommit(dup, m)
70 },
71 },
72 {
73 name: "duplicate-committer",
74 build: func() []byte {
75 h, m := canonicalCommit()
76 dup := []string{
77 h[0], h[1], h[2], h[3],
78 "committer Override Committer <[email protected]> 1700000001 +0000",
79 }
80 return assembleCommit(dup, m)
81 },
82 },
83 {
84 name: "misplaced-parent-after-committer",
85 build: func() []byte {
86 h, m := canonicalCommit()
87 dup := []string{
88 h[0], h[1], h[2], h[3],
89 "parent 2222222222222222222222222222222222222222",
90 }
91 return assembleCommit(dup, m)
92 },
93 },
94 {
95 // Inject the same signature again as a second gpgsig
96 // header. Both occurrences sit before the blank line; both
97 // signers strip them when computing the payload.
98 name: "double-signature",
99 build: func() []byte {
100 h, m := canonicalCommit()
101 return assembleCommit(h, m)
102 },
103 postSign: injectGpgSig,
104 },
105 }
106
107 for _, tc := range cases {
108 t.Run(tc.name, func(t *testing.T) {
109 t.Parallel()
110
111 if tc.name == "double-signature" && os.Getenv("GIT_VERSION") == "v2.11.0" {
112 t.Skip("multi-signature rejection not yet established in Git 2.11.0")
113 }
114
115 repo := initRepo(t)
116 unsigned := tc.build()
117 sig := gpgSign(t, unsigned)
118 signed := injectGpgSig(unsigned, sig)
119 if tc.postSign != nil {
120 signed = tc.postSign(signed, sig)
121 }
122
123 hash := writeLooseObject(t, repo, "commit", signed)
124 gitErr := gitVerifyCommit(t, repo, hash)
125
126 r, err := git.PlainOpen(repo)
127 require.NoError(t, err)
128 commit, err := r.CommitObject(hash)
129 ggDecodeErr := err
130 var ggVerifyErr error
131 if ggDecodeErr == nil {
132 _, ggVerifyErr = commit.Verify(gpg.pubKey)
133 }
134
135 assertSameVerdict(t, "git verify-commit", gitErr, "go-git Verify",
136 combineErr(ggDecodeErr, ggVerifyErr))
137 })
138 }
139 }
140
141 // canonicalCommit returns the byte-exact unsigned commit body produced by
142 // upstream `git commit-tree` for a single-parent commit. Used as the
143 // baseline; individual scenarios mutate the header lines around it.
144 func canonicalCommit() (headers []string, message string) {
145 return []string{
146 "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904",
147 "parent 1111111111111111111111111111111111111111",
148 "author Test Author <[email protected]> 1700000000 +0000",
149 "committer Test Committer <[email protected]> 1700000000 +0000",
150 },
151 "signed commit message\n"
152 }
153
154 func assembleCommit(headers []string, message string) []byte {
155 var buf bytes.Buffer
156 for _, h := range headers {
157 buf.WriteString(h)
158 buf.WriteByte('\n')
159 }
160 buf.WriteByte('\n')
161 buf.WriteString(message)
162 return buf.Bytes()
163 }
164
165 // combineErr returns the first non-nil of decode, verify.
166 // Decode failure is treated the same way as a verify failure.
167 func combineErr(decode, verify error) error {
168 if decode != nil {
169 return decode
170 }
171 return verify
172 }
173
174 // assertSameVerdict fails the test unless both verifiers agree on
175 // success-or-failure. It does not compare error messages, only verdicts.
176 func assertSameVerdict(t *testing.T, leftLabel string, leftErr error, rightLabel string, rightErr error) {
177 t.Helper()
178 leftOK := leftErr == nil
179 rightOK := rightErr == nil
180 if leftOK == rightOK {
181 if !leftOK {
182 t.Logf("both verifiers rejected (expected for malformed input)\n %s: %v\n %s: %v",
183 leftLabel, leftErr, rightLabel, rightErr)
184 }
185 return
186 }
187 assert.Failf(t, "verifier verdicts diverge",
188 "%s succeeded=%v err=%v\n%s succeeded=%v err=%v",
189 leftLabel, leftOK, leftErr, rightLabel, rightOK, rightErr)
190 }