Skip to content

Commit

Permalink
fix(sdk): reset reader after checking if IsNanoTDF (#1718)
Browse files Browse the repository at this point in the history
Closes #1717
  • Loading branch information
jrschumacher authored Nov 5, 2024
1 parent 39a898d commit f9d6f26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ func (t TdfType) String() string {
return string(t)
}

// String method to make the custom type printable
// GetTdfType returns the type of TDF based on the reader.
// Reader is reset after the check.
func GetTdfType(reader io.ReadSeeker) TdfType {
isValidNanoTdf, _ := IsValidNanoTdf(reader)

Expand Down Expand Up @@ -348,13 +349,12 @@ func IsValidTdf(reader io.ReadSeeker) (bool, error) {
return true, nil
}

// IsValidNanoTdf detects whether, or not the reader is a valid Nano TDF.
// Reader is reset after the check.
func IsValidNanoTdf(reader io.ReadSeeker) (bool, error) {
_, _, err := NewNanoTDFHeaderFromReader(reader)
if err != nil {
return false, err
}

return true, nil
_, _ = reader.Seek(0, io.SeekStart) // Ignore the error as we're just checking if it's a valid nano TDF
return err == nil, err
}

func fetchPlatformConfiguration(platformEndpoint string, dialOptions []grpc.DialOption) (PlatformConfiguration, error) {
Expand Down
12 changes: 12 additions & 0 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ func TestNew_ShouldValidateGoodNanoTdf(t *testing.T) {
require.NoError(t, err)

assert.True(t, isValid)

// Try again to see if the reader has been reset
isValid, err = sdk.IsValidNanoTdf(in)
require.NoError(t, err)

assert.True(t, isValid)
}

func TestNew_ShouldNotValidateBadNanoTdf(t *testing.T) {
Expand All @@ -169,6 +175,12 @@ func TestNew_ShouldValidateStandardTdf(t *testing.T) {
require.NoError(t, err)

assert.True(t, isValid)

// Try again to see if the reader has been reset
isValid, err = sdk.IsValidTdf(in)
require.NoError(t, err)

assert.True(t, isValid)
}

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

0 comments on commit f9d6f26

Please sign in to comment.