github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/attestation/attestation_test.go (about)

     1  package attestation_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/in-toto/in-toto-golang/in_toto"
     9  	slsa "github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/devseccon/trivy/pkg/attestation"
    13  )
    14  
    15  func TestStatement_UnmarshalJSON(t *testing.T) {
    16  	tests := []struct {
    17  		name      string
    18  		inputFile string
    19  		want      attestation.Statement
    20  	}{
    21  		{
    22  			name:      "happy path",
    23  			inputFile: "testdata/attestation.json",
    24  			want: attestation.Statement{
    25  				StatementHeader: in_toto.StatementHeader{
    26  					Type:          "https://in-toto.io/Statement/v0.1",
    27  					PredicateType: "cosign.sigstore.dev/attestation/v1",
    28  					Subject: []in_toto.Subject{
    29  						{
    30  							Name: "ghcr.io/devseccon/trivy-test-images",
    31  							Digest: slsa.DigestSet{
    32  								"sha256": "72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb",
    33  							},
    34  						},
    35  					},
    36  				},
    37  				Predicate: &attestation.CosignPredicate{
    38  					Data: "foo\n",
    39  				},
    40  			},
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			f, err := os.Open(tt.inputFile)
    46  			require.NoError(t, err)
    47  			defer f.Close()
    48  
    49  			got := attestation.Statement{Predicate: &attestation.CosignPredicate{}}
    50  			err = json.NewDecoder(f).Decode(&got)
    51  			require.NoError(t, err)
    52  			require.Equal(t, tt.want, got)
    53  		})
    54  	}
    55  }