...

Source file src/github.com/sigstore/timestamp-authority/pkg/tests/build_test_data.go

Documentation: github.com/sigstore/timestamp-authority/pkg/tests

     1  // Copyright 2022 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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), &timestamp.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