aboutsummaryrefslogtreecommitdiff
path: root/object/signed/commit/unit_test.go
blob: 6bbd6cd0b164043266b77ed7557eb45bafdb6e6d (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package signedcommit_test

import (
	"slices"
	"testing"

	objectid "codeberg.org/lindenii/furgit/object/id"
	signedcommit "codeberg.org/lindenii/furgit/object/signed/commit"
)

func TestParseUpstreamMultiplySignedCommit(t *testing.T) {
	t.Parallel()

	// t/t7510-signed-commit.sh
	body := []byte("" +
		"tree 0cfbf08886fca9a91cb753ec8734c84fcbe52c9f\n" +
		"parent 9da738312d24ef0a29be2c8c2b6fc5cf7085a293\n" +
		"author A U Thor <author@example.com> 1112912653 -0700\n" +
		"committer C O Mitter <committer@example.com> 1112912653 -0700\n" +
		"gpgsig -----BEGIN PGP SIGNATURE-----\n" +
		" \n" +
		" iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy\n" +
		" QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC\n" +
		" AKCTqBh+VabJceXcGIZuF0Ry+udbBQ==\n" +
		" =tQ0N\n" +
		" -----END PGP SIGNATURE-----\n" +
		"gpgsig-sha256 -----BEGIN PGP SIGNATURE-----\n" +
		" \n" +
		" iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy\n" +
		" QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO\n" +
		" AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw==\n" +
		" =pIwP\n" +
		" -----END PGP SIGNATURE-----\n" +
		"\n" +
		"second\n")

	commit, err := signedcommit.Parse(body)
	if err != nil {
		t.Fatalf("Parse: %v", err)
	}

	gotPayload := string(commit.AppendPayload(nil))
	wantPayload := "" +
		"tree 0cfbf08886fca9a91cb753ec8734c84fcbe52c9f\n" +
		"parent 9da738312d24ef0a29be2c8c2b6fc5cf7085a293\n" +
		"author A U Thor <author@example.com> 1112912653 -0700\n" +
		"committer C O Mitter <committer@example.com> 1112912653 -0700\n" +
		"\n" +
		"second\n"
	if gotPayload != wantPayload {
		t.Fatalf("payload mismatch:\n got: %q\nwant: %q", gotPayload, wantPayload)
	}

	gotSHA1, ok := commit.AppendSignature(nil, objectid.AlgorithmSHA1)
	if !ok {
		t.Fatal("missing sha1 signature")
	}

	wantSHA1 := "" +
		"-----BEGIN PGP SIGNATURE-----\n" +
		"\n" +
		"iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy\n" +
		"QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC\n" +
		"AKCTqBh+VabJceXcGIZuF0Ry+udbBQ==\n" +
		"=tQ0N\n" +
		"-----END PGP SIGNATURE-----\n"
	if string(gotSHA1) != wantSHA1 {
		t.Fatalf("sha1 signature mismatch:\n got: %q\nwant: %q", string(gotSHA1), wantSHA1)
	}

	gotSHA256, ok := commit.AppendSignature(nil, objectid.AlgorithmSHA256)
	if !ok {
		t.Fatal("missing sha256 signature")
	}

	wantSHA256 := "" +
		"-----BEGIN PGP SIGNATURE-----\n" +
		"\n" +
		"iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy\n" +
		"QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO\n" +
		"AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw==\n" +
		"=pIwP\n" +
		"-----END PGP SIGNATURE-----\n"
	if string(gotSHA256) != wantSHA256 {
		t.Fatalf("sha256 signature mismatch:\n got: %q\nwant: %q", string(gotSHA256), wantSHA256)
	}

	gotAlgorithms := commit.Algorithms()
	wantAlgorithms := []objectid.Algorithm{
		objectid.AlgorithmSHA1,
		objectid.AlgorithmSHA256,
	}
	if !slices.Equal(gotAlgorithms, wantAlgorithms) {
		t.Fatalf("Algorithms() = %v, want %v", gotAlgorithms, wantAlgorithms)
	}
}

func TestParseStripsUnknownGpgsigHeadersFromPayload(t *testing.T) {
	t.Parallel()

	body := []byte("" +
		"tree deadbeef\n" +
		"gpgsig-future header\n" +
		" continued\n" +
		"\n" +
		"message\n")

	commit, err := signedcommit.Parse(body)
	if err != nil {
		t.Fatalf("Parse: %v", err)
	}

	gotPayload := string(commit.AppendPayload(nil))
	wantPayload := "" +
		"tree deadbeef\n" +
		"\n" +
		"message\n"
	if gotPayload != wantPayload {
		t.Fatalf("payload mismatch:\n got: %q\nwant: %q", gotPayload, wantPayload)
	}

	if gotAlgorithms := commit.Algorithms(); len(gotAlgorithms) != 0 {
		t.Fatalf("Algorithms() = %v, want none", gotAlgorithms)
	}
}

func TestParseAllowsDuplicateSignatureHeaders(t *testing.T) {
	t.Parallel()

	body := []byte("" +
		"tree deadbeef\n" +
		"gpgsig one\n" +
		" two\n" +
		"gpgsig three\n" +
		" four\n" +
		"\n" +
		"message\n")

	commit, err := signedcommit.Parse(body)
	if err != nil {
		t.Fatalf("Parse: %v", err)
	}

	gotPayload := string(commit.AppendPayload(nil))
	wantPayload := "" +
		"tree deadbeef\n" +
		"\n" +
		"message\n"
	if gotPayload != wantPayload {
		t.Fatalf("payload mismatch:\n got: %q\nwant: %q", gotPayload, wantPayload)
	}

	gotSignature, ok := commit.AppendSignature(nil, objectid.AlgorithmSHA1)
	if !ok {
		t.Fatal("missing sha1 signature")
	}

	wantSignature := "" +
		"one\n" +
		"two\n" +
		"three\n" +
		"four\n"
	if string(gotSignature) != wantSignature {
		t.Fatalf("signature mismatch:\n got: %q\nwant: %q", string(gotSignature), wantSignature)
	}
}