-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsign_test.go
108 lines (96 loc) · 2.32 KB
/
sign_test.go
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
package signedattestation
import (
"context"
"crypto"
"encoding/json"
"fmt"
"testing"
"github.com/in-toto/in-toto-golang/in_toto"
"github.com/openpubkey/openpubkey/client/providers"
"github.com/openpubkey/openpubkey/pktoken"
"github.com/sigstore/rekor/pkg/generated/models"
)
const (
USE_MOCK_TL = true
)
func GetMockTL() TL {
return &MockTL{
UploadLogEntryFunc: func(ctx context.Context, pkToken *pktoken.PKToken, payload []byte, signature []byte, signer crypto.Signer) ([]byte, error) {
return []byte(TestEntry), nil
},
VerifyLogEntryFunc: func(ctx context.Context, entryBytes []byte) error {
return nil
},
VerifyEntryPayloadFunc: func(entryBytes, payload, pkToken []byte) error {
return nil
},
UnmarshalEntryFunc: func(entry []byte) (any, error) {
le := new(models.LogEntryAnon)
err := le.UnmarshalBinary(entry)
if err != nil {
return nil, fmt.Errorf("error failed to unmarshal TL entry: %w", err)
}
return le, nil
},
}
}
func TestSignAndVerify(t *testing.T) {
stmt := in_toto.Statement{
StatementHeader: in_toto.StatementHeader{
Type: in_toto.StatementInTotoV01,
PredicateType: in_toto.PredicateSPDX,
},
Predicate: "test",
}
var err error
provider, err := providers.NewMockOpenIdProvider()
if err != nil {
t.Fatal(err)
}
env, err := SignInTotoStatement(context.Background(), stmt, provider)
if err != nil {
t.Fatal(err)
}
_, err = VerifyInTotoEnvelope(context.Background(), env, provider)
if err != nil {
t.Fatal(err)
}
}
func TestSignAndVerifyExt(t *testing.T) {
stmt := in_toto.Statement{
StatementHeader: in_toto.StatementHeader{
Type: in_toto.StatementInTotoV01,
PredicateType: in_toto.PredicateSPDX,
},
Predicate: "test",
}
var err error
provider, err := providers.NewMockOpenIdProvider()
if err != nil {
t.Fatal(err)
}
var tl TL
if USE_MOCK_TL {
tl = GetMockTL()
} else {
tl = &RekorTL{}
}
ctx := WithTL(context.Background(), tl)
env, err := SignInTotoStatementExt(ctx, stmt, provider)
if err != nil {
t.Fatal(err)
}
serializedEnv, err := json.Marshal(env)
if err != nil {
t.Fatal(err)
}
deserializedEnv := new(Envelope)
err = json.Unmarshal(serializedEnv, deserializedEnv)
if err != nil {
t.Fatal(err)
}
_, err = VerifyInTotoEnvelopeExt(ctx, deserializedEnv, provider)
if err != nil {
t.Fatal(err)
}
}