-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor materials reading from file to lib
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
- Loading branch information
1 parent
0d8b360
commit 75cb706
Showing
7 changed files
with
110 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package intoto | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// WithMaterials adds additional materials to the predicate | ||
func WithMaterials(materials []Item) StatementOption { | ||
return func(s *Statement) { | ||
s.Predicate.Materials = append(s.Predicate.Materials, materials...) | ||
} | ||
} | ||
|
||
// ReadMaterials reads the material from file | ||
func ReadMaterials(r io.Reader) ([]Item, error) { | ||
var materials []Item | ||
|
||
if err := json.NewDecoder(r).Decode(&materials); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, material := range materials { | ||
if material.URI == "" { | ||
return nil, fmt.Errorf("empty or missing \"uri\" for material") | ||
} | ||
if len(material.Digest) == 0 { | ||
return nil, fmt.Errorf("empty or missing \"digest\" for material") | ||
} | ||
} | ||
|
||
return materials, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package intoto | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMaterials(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
validMaterials := strings.NewReader(`[ | ||
{ | ||
"uri": "pkg:deb/debian/stunnel4@5.50-3?arch=amd64", | ||
"digest": { | ||
"sha256": "e1731ae217fcbc64d4c00d707dcead45c828c5f762bcf8cc56d87de511e096fa" | ||
} | ||
} | ||
]`) | ||
|
||
nonJSON := strings.NewReader(`[ | ||
{ | ||
"uri": "pkg:deb/debian/stunnel4@5.50-3?arch=amd64", | ||
"digest": { | ||
"sha256": "e1731ae217fcbc64d4c00d707dcead45c828c5f762bcf8cc56d87de511e096fa" | ||
} | ||
}`) | ||
|
||
withoutDigest := strings.NewReader(`[ | ||
{ | ||
"uri": "pkg:deb/debian/stunnel4@5.50-3?arch=amd64", | ||
"not-digest": { | ||
"sha256": "e1731ae217fcbc64d4c00d707dcead45c828c5f762bcf8cc56d87de511e096fa" | ||
} | ||
} | ||
]`) | ||
|
||
withoutURI := strings.NewReader(`[ | ||
{ | ||
"digest": { | ||
"sha256": "e1731ae217fcbc64d4c00d707dcead45c828c5f762bcf8cc56d87de511e096fa" | ||
} | ||
} | ||
]`) | ||
|
||
m, err := ReadMaterials(validMaterials) | ||
assert.NoError(err) | ||
assert.Len(m, 1) | ||
|
||
m, err = ReadMaterials(nonJSON) | ||
assert.EqualError(err, "unexpected EOF") | ||
assert.Nil(m) | ||
|
||
m, err = ReadMaterials(withoutDigest) | ||
assert.EqualError(err, "empty or missing \"digest\" for material") | ||
assert.Nil(m) | ||
|
||
m, err = ReadMaterials(withoutURI) | ||
assert.EqualError(err, "empty or missing \"uri\" for material") | ||
assert.Nil(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters