Skip to content

Commit

Permalink
Add trusted metadata set tests (#56)
Browse files Browse the repository at this point in the history
* Fix relative paths for testutils setup

This change allows using the testutils setup across all packages,
while previously it was using a correct path only for metadata

Signed-off-by: Ivana Atanasova <[email protected]>

* Update snapshot meta files in test repo

This change adds delegated roles to snapshot meta for testing
trusted metadata and updates existing tests with the changes

Signed-off-by: Ivana Atanasova <[email protected]>

* Add trusted metadata set tests

This change adds test coverage of 92% for trusted metadata set
corresponding to the python-tuf trusted metadata tests

Signed-off-by: Ivana Atanasova <[email protected]>

---------

Signed-off-by: Ivana Atanasova <[email protected]>
  • Loading branch information
ivanayov authored and rdimitrov committed Jan 29, 2024
1 parent 5f65b8f commit c1c0f21
Show file tree
Hide file tree
Showing 5 changed files with 706 additions and 18 deletions.
27 changes: 20 additions & 7 deletions metadata/metadata_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ import (

func TestMain(m *testing.M) {

err := testutils.SetupTestDirs()
repoPath := "../testutils/repository_data/repository/metadata"
targetsPath := "../testutils/repository_data/repository/targets"
keystorePath := "../testutils/repository_data/keystore"
err := testutils.SetupTestDirs(repoPath, targetsPath, keystorePath)
defer testutils.Cleanup()

if err != nil {
Expand Down Expand Up @@ -283,14 +286,14 @@ func TestToFromBytes(t *testing.T) {
// Case 1: test noncompact by overriding the default serializer.
snapshotBytesWant, err := snapshot.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(data), string(snapshotBytesWant))
assert.Equal(t, data, snapshotBytesWant)

// Case 2: test compact by using the default serializer.
snapshot2, err := Snapshot().FromBytes(snapshotBytesWant)
assert.NoError(t, err)
snapshotBytesActual, err := snapshot2.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(snapshotBytesWant), string(snapshotBytesActual))
assert.Equal(t, snapshotBytesWant, snapshotBytesActual)

// TARGETS
data, err = os.ReadFile(testutils.RepoDir + "/targets.json")
Expand All @@ -301,14 +304,14 @@ func TestToFromBytes(t *testing.T) {
// Case 1: test noncompact by overriding the default serializer.
targetsBytesWant, err := targets.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(data), string(targetsBytesWant))
assert.Equal(t, data, targetsBytesWant)

// Case 2: test compact by using the default serializer.
targets2, err := Targets().FromBytes(targetsBytesWant)
assert.NoError(t, err)
targetsBytesActual, err := targets2.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(targetsBytesWant), string(targetsBytesActual))
assert.Equal(t, targetsBytesWant, targetsBytesActual)

// TIMESTAMP
data, err = os.ReadFile(testutils.RepoDir + "/timestamp.json")
Expand All @@ -319,14 +322,14 @@ func TestToFromBytes(t *testing.T) {
// Case 1: test noncompact by overriding the default serializer.
timestampBytesWant, err := timestamp.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(data), string(timestampBytesWant))
assert.Equal(t, data, timestampBytesWant)

// Case 2: test compact by using the default serializer.
timestamp2, err := Timestamp().FromBytes(timestampBytesWant)
assert.NoError(t, err)
timestampBytesActual, err := timestamp2.ToBytes(true)
assert.NoError(t, err)
assert.Equal(t, string(timestampBytesWant), string(timestampBytesActual))
assert.Equal(t, timestampBytesWant, timestampBytesActual)

}

Expand Down Expand Up @@ -421,6 +424,7 @@ func TestKeyVerifyFailures(t *testing.T) {
// Load sample metadata (timestamp)
timestamp, err := Timestamp().FromFile(testutils.RepoDir + "/timestamp.json")
assert.NoError(t, err)

timestampSig, _ := getSignatureByKeyID(timestamp.Signatures, timestampKeyID)
data, err := timestamp.Signed.MarshalJSON()
assert.NoError(t, err)
Expand Down Expand Up @@ -781,6 +785,15 @@ func TestLengthAndHashValidation(t *testing.T) {
snapshotMetafile := timestamp.Signed.Meta["snapshot.json"]
assert.NotNil(t, snapshotMetafile)

snapshotData, err := os.ReadFile(testutils.RepoDir + "/snapshot.json")
assert.NoError(t, err)
h32 := sha256.Sum256(snapshotData)
h := h32[:]
snapshotMetafile.Hashes = map[string]HexBytes{
"sha256": h,
}
snapshotMetafile.Length = 652

data, err := os.ReadFile(testutils.RepoDir + "/snapshot.json")
assert.NoError(t, err)
err = snapshotMetafile.VerifyLengthHashes(data)
Expand Down
Loading

0 comments on commit c1c0f21

Please sign in to comment.