Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Oct 25, 2024
1 parent f8bbfc3 commit 14d077f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"github.com/notaryproject/tspclient-go/internal/oid"
)

// nonceGenerator generates nonce. Used for unit test
var nonceGenerator func() (*big.Int, error) = generateNonce

// MessageImprint contains the hash of the datum to be time-stamped.
//
// MessageImprint ::= SEQUENCE {
Expand Down Expand Up @@ -121,7 +124,7 @@ func NewRequest(opts RequestOptions) (*Request, error) {
if tspclientasn1.EqualRawValue(hashAlgParameter, asn1.RawValue{}) || tspclientasn1.EqualRawValue(hashAlgParameter, asn1.NullRawValue) {
hashAlgParameter = asn1NullRawValue
}
nonce, err := generateNonce()
nonce, err := nonceGenerator()
if err != nil {
return nil, &MalformedRequestError{Msg: err.Error()}
}
Expand Down
16 changes: 16 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/asn1"
"errors"
"fmt"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -71,6 +72,21 @@ func TestNewRequest(t *testing.T) {
if !reflect.DeepEqual(req.MessageImprint.HashAlgorithm.Parameters, asn1NullRawValue) {
t.Fatalf("expected %v, but got %v", asn1NullRawValue, req.MessageImprint.HashAlgorithm.Parameters)
}

nonceGenerator = func() (*big.Int, error) {
return nil, errors.New("failed to generate nonce")
}
opts = RequestOptions{
Content: message,
HashAlgorithm: crypto.SHA256,
}
expectedErrMsg = "malformed timestamping request: failed to generate nonce"
_, err = NewRequest(opts)
if err == nil || !errors.As(err, &malformedRequest) || err.Error() != expectedErrMsg {
t.Fatalf("expected error %s, but got %v", expectedErrMsg, err)
}
// clean up
nonceGenerator = generateNonce
}

func TestRequestMarshalBinary(t *testing.T) {
Expand Down

0 comments on commit 14d077f

Please sign in to comment.