...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package tests
16
17 import (
18 "bytes"
19 "crypto"
20 "encoding/base64"
21 "encoding/json"
22 "math/big"
23 "testing"
24
25 "github.com/digitorus/timestamp"
26 "github.com/sigstore/timestamp-authority/pkg/api"
27 )
28
29 func createBase64EncodedArtifactHash(artifact []byte, hash crypto.Hash) (string, error) {
30 h := hash.New()
31 h.Write(artifact)
32 artifactHash := h.Sum(nil)
33
34 return base64.StdEncoding.EncodeToString(artifactHash), nil
35 }
36
37 func buildJSONReq(t *testing.T, artifact []byte, digestHash crypto.Hash, hashName string, includeCerts bool, nonce *big.Int, oidStr string) []byte {
38 encodedHash, err := createBase64EncodedArtifactHash(artifact, digestHash)
39 if err != nil {
40 t.Fatalf("failed to marshal request")
41 }
42
43 jsonReq := api.JSONRequest{
44 Certificates: includeCerts,
45 HashAlgorithm: hashName,
46 ArtifactHash: encodedHash,
47 Nonce: nonce,
48 TSAPolicyOID: oidStr,
49 }
50
51 marshalled, err := json.Marshal(jsonReq)
52 if err != nil {
53 t.Fatalf("failed to marshal request")
54 }
55 return marshalled
56 }
57
58 func buildTimestampQueryReq(t *testing.T, artifact []byte, opts timestamp.RequestOptions) []byte {
59 tsq, err := timestamp.CreateRequest(bytes.NewReader(artifact), ×tamp.RequestOptions{
60 Hash: opts.Hash,
61 Certificates: opts.Certificates,
62 Nonce: opts.Nonce,
63 TSAPolicyOID: opts.TSAPolicyOID,
64 })
65 if err != nil {
66 t.Fatalf("unexpected error creating request: %v", err)
67 }
68 return tsq
69 }
70
View as plain text