Skip to content

Commit

Permalink
Included Test for Search Function
Browse files Browse the repository at this point in the history
- Included test for search function
- Cleaned up test for LoadEnvelope

Signed-off-by: neilnaveen <[email protected]>
  • Loading branch information
neilnaveen authored and jkjell committed Nov 16, 2023
1 parent cae8c4a commit cd48450
Showing 1 changed file with 170 additions and 13 deletions.
183 changes: 170 additions & 13 deletions source/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package source

import (
"context"
"encoding/json"
"fmt"
"reflect"
"testing"

Expand All @@ -25,10 +27,13 @@ import (
)

func TestLoadEnvelope(t *testing.T) {
// Marshal the attestation.Collection into a JSON byte array
predicate, err := json.Marshal(attestation.Collection{})
if err != nil {
t.Fatalf("failed to marshal predicate, err = %v", err)
}

// Define the test cases
tests := []struct {
name string
reference string
Expand Down Expand Up @@ -90,12 +95,10 @@ func TestLoadEnvelope(t *testing.T) {
},
}

// Run the test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

// Marshal the intoto.Statement into a JSON byte array
var err error

statementBytes, _ := json.Marshal(tt.intotoStatment)

// Create a new dsse.Envelope with the marshalled intoto.Statement as the payload
Expand All @@ -109,7 +112,7 @@ func TestLoadEnvelope(t *testing.T) {
if tt.wantRefrenceExistErr {
collEnv, err := envelopeToCollectionEnvelope(tt.reference, envelope)
if err != nil {
t.Errorf("Invalid intotoStatment, err = %v", err)
t.Fatalf("Invalid intotoStatment, err = %v", err)
}
// since this envelope is not in the MemorySource, we can add the collection envelope into the map
memorySource.envelopesByReference[tt.reference] = collEnv
Expand All @@ -120,8 +123,7 @@ func TestLoadEnvelope(t *testing.T) {
if err != nil {
// if we did not want the error
if !tt.wantLoadEnvelopeErr {
t.Errorf("LoadEnvelope() error = %v, wantErr %v", err, tt.wantLoadEnvelopeErr)
return
t.Fatalf("LoadEnvelope() error = %v, wantErr %v", err, tt.wantLoadEnvelopeErr)
}
return

Expand All @@ -136,25 +138,180 @@ func TestLoadEnvelope(t *testing.T) {
Reference: tt.reference,
}
if !reflect.DeepEqual(memorySource.envelopesByReference[tt.reference], expectedCollectionEnvelope) != tt.wantMemorySourceErr {
t.Errorf("Mismatch or non-existence of collection envelope for reference in envelopesByReference map.")
return
t.Fatalf("Mismatch or non-existence of collection envelope for reference in envelopesByReference map.")
}
// Verify if the subjects and attestations are present in the loaded envelope
for _, sub := range tt.intotoStatment.Subject {
for _, digest := range sub.Digest {
if _, ok := memorySource.subjectDigestsByReference[tt.reference][digest]; !ok != tt.wantMemorySourceErr {
t.Errorf("memorySource does not contain passed in digest = %v", digest)
return
t.Fatalf("memorySource does not contain passed in digest = %v", digest)
}
}
}
for _, att := range tt.attCol.Attestations {
if _, ok := memorySource.attestationsByReference[tt.reference][att.Attestation.Type()]; !ok != tt.wantMemorySourceErr {
t.Errorf("memorySource does not contain passed in attestation = %v", att.Attestation.Name())
return
t.Fatalf("memorySource does not contain passed in attestation = %v", att.Attestation.Name())
}
}
})
}
}

func TestSearch(t *testing.T) {
// Marshal the attestation.Collection into a JSON byte array
validPredicate, err := json.Marshal(attestation.Collection{Name: "t"})
if err != nil {
t.Fatalf("failed to marshal predicate, err = %v", err)
}

// Define the arguments for the test cases
type args struct {
ctx context.Context
collectionName string
subDigest []string
attestations []string
}
// Define the test cases
tests := []struct {
name string
statements []intoto.Statement
searchQuery args
wantRefrences map[string]struct{}
wantErr bool
}{
{
name: "all match given query",
statements: []intoto.Statement{
{
Type: "1",
Subject: []intoto.Subject{{Name: "example1", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2", "c": "exampledigest3"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "2",
Subject: []intoto.Subject{{Name: "example2", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "3",
Subject: []intoto.Subject{{Name: "example3", Digest: map[string]string{"a": "exampledigest"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
},
searchQuery: args{
collectionName: "t",
subDigest: []string{"exampledigest", "notincluded"},
attestations: []string{},
},
wantRefrences: map[string]struct{}{"ref0": {}, "ref1": {}, "ref2": {}},
wantErr: false,
},
{
name: "some match",
statements: []intoto.Statement{
{
Type: "1",
Subject: []intoto.Subject{{Name: "example1", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2", "c": "exampledigest3"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "2",
Subject: []intoto.Subject{{Name: "example2", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "3",
Subject: []intoto.Subject{{Name: "example3", Digest: map[string]string{"a": "exampledigest"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "4",
Subject: []intoto.Subject{{Name: "example1", Digest: map[string]string{"a": "not included"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
},
searchQuery: args{
collectionName: "t",
subDigest: []string{"exampledigest"},
attestations: []string{},
},
wantRefrences: map[string]struct{}{"ref0": {}, "ref1": {}, "ref2": {}},
wantErr: false,
},
{
name: "no matches",
statements: []intoto.Statement{
{
Type: "1",
Subject: []intoto.Subject{{Name: "example1", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2", "c": "exampledigest3"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "2",
Subject: []intoto.Subject{{Name: "example2", Digest: map[string]string{"a": "exampledigest", "b": "exampledigest2"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
{
Type: "3",
Subject: []intoto.Subject{{Name: "example3", Digest: map[string]string{"a": "exampledigest"}}},
PredicateType: "https://slsa.dev/provenance/v0.2",
Predicate: json.RawMessage(validPredicate),
},
},
searchQuery: args{
collectionName: "t",
subDigest: []string{},
attestations: []string{},
},
wantRefrences: map[string]struct{}{},
wantErr: false,
},
}

// Run the test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Initialize a new MemorySource
s := NewMemorySource()
expectedResult := []CollectionEnvelope{}
for i := range tt.statements {
// Marshal the intoto.Statement into a JSON byte array
payload, _ := json.Marshal(tt.statements[i])
// Create a new dsse.Envelope with the marshalled intoto.Statement as the payload
dsseEnv := dsse.Envelope{
Payload: payload,
PayloadType: "application/vnd.in-toto+json",
}
// Load the dsse.Envelope into the MemorySource
err := s.LoadEnvelope("ref"+fmt.Sprint(i), dsseEnv)
if err != nil {
t.Fatalf("invalid intoto statment, err = %v", err)
}

if _, ok := tt.wantRefrences["ref"+fmt.Sprint(i)]; ok {
collEnv, _ := envelopeToCollectionEnvelope("ref"+fmt.Sprint(i), dsseEnv)
expectedResult = append(expectedResult, collEnv)
}
}

// Run the search query on the MemorySource
got, err := s.Search(tt.searchQuery.ctx, tt.searchQuery.collectionName, tt.searchQuery.subDigest, tt.searchQuery.attestations)
if (err != nil) != tt.wantErr {
t.Fatalf("MemorySource.Search() error = %v, wantErr %v", err, tt.wantErr)
}
// Check if the search results match the expected results
if !reflect.DeepEqual(got, expectedResult) {
t.Fatalf("MemorySource.Search() = %v, want %v", got, expectedResult)
}
})
}
}
}

0 comments on commit cd48450

Please sign in to comment.