...

Source file src/github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1/provenance_test.go

Documentation: github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/v1

     1  package v1
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/in-toto/in-toto-golang/in_toto/slsa_provenance/common"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDecodeProvenancePredicate(t *testing.T) {
    14  	var data = `
    15  {
    16  	"buildDefinition": {
    17  		"buildType": "https://github.com/Attestations/GitHubActionsWorkflow@v1",
    18  		"resolvedDependencies": [
    19  			{
    20  				"uri": "git+https://github.com/curl/curl-docker@master",
    21  				"digest": { "sha1": "d6525c840a62b398424a78d792f457477135d0cf" }
    22  			}, 
    23  			{
    24  				"uri": "github_hosted_vm:ubuntu-18.04:20210123.1"
    25  			}
    26  		]
    27  	},
    28  	"runDetails": {
    29  		"builder": { 
    30  			"id": "https://github.com/Attestations/GitHubHostedActions@v1" 
    31  		},
    32  		"metadata": {
    33  			"startedOn": "2020-08-19T08:38:00Z"
    34  		}
    35  	}
    36  }
    37  `
    38  	var testTime = time.Unix(1597826280, 0)
    39  	var want = ProvenancePredicate{
    40  		BuildDefinition: ProvenanceBuildDefinition{
    41  			BuildType: "https://github.com/Attestations/GitHubActionsWorkflow@v1",
    42  			ResolvedDependencies: []ResourceDescriptor{
    43  				{
    44  					URI: "git+https://github.com/curl/curl-docker@master",
    45  					Digest: common.DigestSet{
    46  						"sha1": "d6525c840a62b398424a78d792f457477135d0cf",
    47  					},
    48  				},
    49  				{
    50  					URI: "github_hosted_vm:ubuntu-18.04:20210123.1",
    51  				},
    52  			},
    53  		},
    54  		RunDetails: ProvenanceRunDetails{
    55  			Builder: Builder{
    56  				ID: "https://github.com/Attestations/GitHubHostedActions@v1",
    57  			},
    58  			BuildMetadata: BuildMetadata{
    59  				StartedOn: &testTime,
    60  			},
    61  		},
    62  	}
    63  	var got ProvenancePredicate
    64  
    65  	if err := json.Unmarshal([]byte(data), &got); err != nil {
    66  		t.Fatalf("failed to unmarshal json: %s\n", err)
    67  	}
    68  
    69  	// Make sure parsed time have same location set, location is only used
    70  	// for display purposes.
    71  	loc := want.RunDetails.BuildMetadata.StartedOn.Location()
    72  	tmp := got.RunDetails.BuildMetadata.StartedOn.In(loc)
    73  	got.RunDetails.BuildMetadata.StartedOn = &tmp
    74  
    75  	assert.Equal(t, want, got, "Unexpected object after decoding")
    76  }
    77  
    78  func TestEncodeProvenancePredicate(t *testing.T) {
    79  	var testTime = time.Unix(1597826280, 0).In(time.UTC)
    80  	var p = ProvenancePredicate{
    81  		BuildDefinition: ProvenanceBuildDefinition{
    82  			BuildType: "https://github.com/Attestations/GitHubActionsWorkflow@v1",
    83  			ExternalParameters: map[string]string{
    84  				"entryPoint": "build.yaml:maketgz",
    85  				"source":     "git+https://github.com/curl/curl-docker@master",
    86  			},
    87  			InternalParameters: map[string]string{
    88  				"GITHUB_RUNNER": "github_hosted_vm:ubuntu-18.04:20210123.1",
    89  			},
    90  			ResolvedDependencies: []ResourceDescriptor{
    91  				{
    92  					URI: "git+https://github.com/curl/curl-docker@master",
    93  					Digest: common.DigestSet{
    94  						"sha1": "d6525c840a62b398424a78d792f457477135d0cf",
    95  					},
    96  				},
    97  				{
    98  					URI: "github_hosted_vm:ubuntu-18.04:20210123.1",
    99  				},
   100  				{
   101  					URI: "git+https://github.com/curl/",
   102  				},
   103  			},
   104  		},
   105  		RunDetails: ProvenanceRunDetails{
   106  			Builder: Builder{
   107  				ID: "https://github.com/Attestations/GitHubHostedActions@v1",
   108  			},
   109  			BuildMetadata: BuildMetadata{
   110  				StartedOn:  &testTime,
   111  				FinishedOn: &testTime,
   112  			},
   113  		},
   114  	}
   115  	var want = `{"buildDefinition":{"buildType":"https://github.com/Attestations/GitHubActionsWorkflow@v1","externalParameters":{"entryPoint":"build.yaml:maketgz","source":"git+https://github.com/curl/curl-docker@master"},"internalParameters":{"GITHUB_RUNNER":"github_hosted_vm:ubuntu-18.04:20210123.1"},"resolvedDependencies":[{"uri":"git+https://github.com/curl/curl-docker@master","digest":{"sha1":"d6525c840a62b398424a78d792f457477135d0cf"}},{"uri":"github_hosted_vm:ubuntu-18.04:20210123.1"},{"uri":"git+https://github.com/curl/"}]},"runDetails":{"builder":{"id":"https://github.com/Attestations/GitHubHostedActions@v1"},"metadata":{"startedOn":"2020-08-19T08:38:00Z","finishedOn":"2020-08-19T08:38:00Z"}}}`
   116  	b, err := json.Marshal(&p)
   117  	assert.Nil(t, err, "Error during JSON marshal")
   118  	if d := cmp.Diff(want, string(b)); d != "" {
   119  		t.Fatal(d)
   120  	}
   121  	assert.Equal(t, want, string(b), "Wrong JSON produced")
   122  }
   123  
   124  // Test that the default date (January 1, year 1, 00:00:00 UTC) is
   125  // not marshalled
   126  func TestMetadataNoTime(t *testing.T) {
   127  	var md = BuildMetadata{
   128  		InvocationID: "123456-12-1",
   129  	}
   130  	var want = `{"invocationID":"123456-12-1"}`
   131  	var got BuildMetadata
   132  	b, err := json.Marshal(&md)
   133  
   134  	t.Run("Marshal", func(t *testing.T) {
   135  		assert.Nil(t, err, "Error during JSON marshal")
   136  		assert.Equal(t, want, string(b), "Wrong JSON produced")
   137  	})
   138  
   139  	t.Run("Unmashal", func(t *testing.T) {
   140  		err := json.Unmarshal(b, &got)
   141  		assert.Nil(t, err, "Error during JSON unmarshal")
   142  		assert.Equal(t, md, got, "Wrong struct after JSON unmarshal")
   143  	})
   144  }
   145  

View as plain text