Skip to content

Commit e470477

Browse files
committed
verify: Add some error cases to TestVerifyInclusionProof
Also refactor a bit, to avoid unnecessary error handling Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
1 parent 9e4e04a commit e470477

File tree

1 file changed

+72
-24
lines changed

1 file changed

+72
-24
lines changed

pkg/verify/verify_test.go

+72-24
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,95 @@ import (
44
"context"
55
"crypto/sha256"
66
"encoding/base64"
7-
"encoding/hex"
87
"testing"
98

109
pbs "github.com/sigstore/protobuf-specs/gen/pb-go/rekor/v1"
1110
rekornote "github.com/sigstore/rekor-tiles/pkg/note"
1211
"github.com/sigstore/sigstore/pkg/signature"
13-
"github.com/stretchr/testify/assert"
1412
f_log "github.com/transparency-dev/formats/log"
1513
note "golang.org/x/mod/sumdb/note"
1614
)
1715

1816
func TestVerifyInclusionProof(t *testing.T) {
19-
hash, err := hex.DecodeString("59a575f157274702c38de3ab1e1784226f391fb79500ebf9f02b4439fb77574c")
20-
if err != nil {
21-
t.Fatal(err)
22-
}
23-
rootHash, err := hex.DecodeString("5be1758dd2228acfaf2546b4b6ce8aa40c82a3748f3dcb550e0d67ba34f02a45")
24-
if err != nil {
25-
t.Fatal(err)
26-
}
17+
hash := []byte{89, 165, 117, 241, 87, 39, 71, 2, 195, 141, 227, 171, 30, 23, 132, 34, 111, 57, 31, 183, 149, 0, 235, 249, 240, 43, 68, 57, 251, 119, 87, 76}
18+
rootHash := []byte{91, 225, 117, 141, 210, 34, 138, 207, 175, 37, 70, 180, 182, 206, 138, 164, 12, 130, 163, 116, 143, 61, 203, 85, 14, 13, 103, 186, 52, 240, 42, 69}
2719
body, err := base64.StdEncoding.DecodeString("eyJhcGlWZXJzaW9uIjoiMC4wLjEiLCJraW5kIjoicmVrb3JkIiwic3BlYyI6eyJkYXRhIjp7Imhhc2giOnsiYWxnb3JpdGhtIjoic2hhMjU2IiwidmFsdWUiOiJlY2RjNTUzNmY3M2JkYWU4ODE2ZjBlYTQwNzI2ZWY1ZTliODEwZDkxNDQ5MzA3NTkwM2JiOTA2MjNkOTdiMWQ4In19LCJzaWduYXR1cmUiOnsiY29udGVudCI6Ik1FWUNJUUQvUGRQUW1LV0MxKzBCTkVkNWdLdlFHcjF4eGwzaWVVZmZ2M2prMXp6Skt3SWhBTEJqM3hmQXlXeGx6NGpwb0lFSVYxVWZLOXZua1VVT1NvZVp4QlpQSEtQQyIsImZvcm1hdCI6Ing1MDkiLCJwdWJsaWNLZXkiOnsiY29udGVudCI6IkxTMHRMUzFDUlVkSlRpQlFWVUpNU1VNZ1MwVlpMUzB0TFMwS1RVWnJkMFYzV1VoTGIxcEplbW93UTBGUldVbExiMXBKZW1vd1JFRlJZMFJSWjBGRlRVOWpWR1pTUWxNNWFtbFlUVGd4UmxvNFoyMHZNU3R2YldWTmR3cHRiaTh6TkRjdk5UVTJaeTlzY21sVE56SjFUV2haT1V4alZDczFWVW8yWmtkQ1oyeHlOVm80VERCS1RsTjFZWE41WldRNVQzUmhVblozUFQwS0xTMHRMUzFGVGtRZ1VGVkNURWxESUV0RldTMHRMUzB0Q2c9PSJ9fX19")
2820
if err != nil {
2921
t.Fatal(err)
3022
}
31-
entry := &pbs.TransparencyLogEntry{
32-
LogIndex: 1,
33-
InclusionProof: &pbs.InclusionProof{
34-
LogIndex: 1,
35-
TreeSize: 2,
36-
Hashes: [][]byte{
37-
[]byte(hash),
23+
24+
for _, test := range []struct {
25+
name string
26+
proof *pbs.InclusionProof
27+
logSize uint64
28+
wantErr bool
29+
}{
30+
{
31+
name: "valid inclusionproof",
32+
proof: &pbs.InclusionProof{
33+
LogIndex: 1,
34+
TreeSize: 2,
35+
Hashes: [][]byte{
36+
[]byte(hash),
37+
},
3838
},
39+
logSize: 2,
40+
wantErr: false,
3941
},
40-
CanonicalizedBody: body,
41-
}
42-
checkpoint := &f_log.Checkpoint{
43-
Size: 2,
44-
Hash: rootHash,
42+
{
43+
name: "invalid hash",
44+
proof: &pbs.InclusionProof{
45+
LogIndex: 1,
46+
TreeSize: 2,
47+
Hashes: [][]byte{
48+
[]byte([]byte{0, 165, 117, 241, 87, 39, 71, 2, 195, 141, 227, 171, 30, 23, 132, 34, 111, 57, 31, 183, 149, 0, 235, 249, 240, 43, 68, 57, 251, 119, 87, 76}),
49+
},
50+
},
51+
logSize: 2,
52+
wantErr: true,
53+
},
54+
{
55+
name: "inclusion index beyond log size",
56+
proof: &pbs.InclusionProof{
57+
LogIndex: 1,
58+
TreeSize: 2,
59+
Hashes: [][]byte{
60+
[]byte(hash),
61+
},
62+
},
63+
logSize: 1,
64+
wantErr: true,
65+
},
66+
{
67+
name: "wrong proof size",
68+
proof: &pbs.InclusionProof{
69+
LogIndex: 1,
70+
TreeSize: 2,
71+
Hashes: [][]byte{
72+
[]byte(hash),
73+
},
74+
},
75+
logSize: 3,
76+
wantErr: true,
77+
},
78+
} {
79+
t.Run(string(test.name), func(t *testing.T) {
80+
checkpoint := &f_log.Checkpoint{
81+
Size: test.logSize,
82+
Hash: rootHash,
83+
}
84+
85+
entry := &pbs.TransparencyLogEntry{
86+
LogIndex: 1,
87+
InclusionProof: test.proof,
88+
CanonicalizedBody: body,
89+
}
90+
gotErr := VerifyInclusionProof(entry, checkpoint)
91+
if (gotErr != nil) != test.wantErr {
92+
t.Fatalf("VerifyCheckpoint = %t, wantErr %t", gotErr, test.wantErr)
93+
}
94+
})
4595
}
46-
gotErr := VerifyInclusionProof(entry, checkpoint)
47-
assert.NoError(t, gotErr)
4896
}
4997

5098
func getTestEntry(t *testing.T, signer signature.Signer, hostname string) *pbs.TransparencyLogEntry {

0 commit comments

Comments
 (0)