From 9501cddc1dab2975cdbeade467926aaee219fc04 Mon Sep 17 00:00:00 2001 From: HuKeping Date: Fri, 11 Mar 2016 10:17:56 +0800 Subject: [PATCH] [MISC 3/4] Refactor: move test helper function to test package The helper function "GetSupportedHashes" is only used in tests, it's better to move it to the relevant test file. Since it's for the test, remove the origin test code for it. And it also a good idea to call "NewfileMeta" instead of implement once again. Signed-off-by: Hu Keping --- tuf/client/client_test.go | 26 ++++++++++++++++++++++---- tuf/data/types.go | 19 ------------------- tuf/data/types_test.go | 8 -------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/tuf/client/client_test.go b/tuf/client/client_test.go index 986a7aabd5..84c9b9f1a2 100644 --- a/tuf/client/client_test.go +++ b/tuf/client/client_test.go @@ -1,6 +1,7 @@ package client import ( + "bytes" "crypto/sha256" "encoding/json" "fmt" @@ -252,7 +253,8 @@ func TestChecksumMismatch(t *testing.T) { orig, err := json.Marshal(sampleTargets) assert.NoError(t, err) - origHashes := data.GetSupportedHashes(orig) + origHashes, err := GetSupportedHashes(orig) + assert.NoError(t, err) remoteStorage.SetMeta("targets", orig) @@ -270,7 +272,8 @@ func TestChecksumMatch(t *testing.T) { orig, err := json.Marshal(sampleTargets) assert.NoError(t, err) - origHashes := data.GetSupportedHashes(orig) + origHashes, err := GetSupportedHashes(orig) + assert.NoError(t, err) remoteStorage.SetMeta("targets", orig) @@ -289,7 +292,8 @@ func TestSizeMismatchLong(t *testing.T) { assert.NoError(t, err) l := int64(len(orig)) - origHashes := data.GetSupportedHashes(orig) + origHashes, err := GetSupportedHashes(orig) + assert.NoError(t, err) remoteStorage.SetMeta("targets", orig) @@ -310,7 +314,9 @@ func TestSizeMismatchShort(t *testing.T) { assert.NoError(t, err) l := int64(len(orig)) - origHashes := data.GetSupportedHashes(orig) + origHashes, err := GetSupportedHashes(orig) + assert.NoError(t, err) + remoteStorage.SetMeta("targets", orig) _, _, err = client.downloadSigned("targets", l, origHashes) @@ -887,3 +893,15 @@ func TestDownloadTimestampLocalTimestampInvalidRemoteTimestamp(t *testing.T) { err = client.downloadTimestamp() assert.NoError(t, err) } + +// GetSupportedHashes is a helper function that returns +// the checksums of all the supported hash algorithms +// of the given payload. +func GetSupportedHashes(payload []byte) (data.Hashes, error) { + meta, err := data.NewFileMeta(bytes.NewReader(payload), data.NotaryDefaultHashes...) + if err != nil { + return nil, err + } + + return meta.Hashes, nil +} diff --git a/tuf/data/types.go b/tuf/data/types.go index 4f72a06894..b1c706ef5b 100644 --- a/tuf/data/types.go +++ b/tuf/data/types.go @@ -160,25 +160,6 @@ func CheckHashes(payload []byte, hashes Hashes) error { return nil } -// GetSupportedHashes returns the checksums of all the supported hash algorithms -// of the given payload -func GetSupportedHashes(payload []byte) Hashes { - hashes := make(Hashes) - - for _, v := range NotaryDefaultHashes { - switch v { - case notary.SHA256: - checksum := sha256.Sum256(payload) - hashes[v] = checksum[:] - case notary.SHA512: - checksum := sha512.Sum512(payload) - hashes[v] = checksum[:] - } - } - - return hashes -} - // CheckValidHashStructures returns an error, or nil, depending on whether // the content of the hashes is valid or not. func CheckValidHashStructures(hashes Hashes) error { diff --git a/tuf/data/types_test.go b/tuf/data/types_test.go index 1d69d14e6a..696c163a89 100644 --- a/tuf/data/types_test.go +++ b/tuf/data/types_test.go @@ -124,14 +124,6 @@ func TestCheckHashes(t *testing.T) { assert.Contains(t, err.Error(), "checksum mismatched") } -func TestGetSupportedHashes(t *testing.T) { - raw := []byte("Illidan") - - hashes := GetSupportedHashes(raw) - err := CheckHashes(raw, hashes) - assert.NoError(t, err) -} - func TestCheckValidHashStructures(t *testing.T) { var err error hashes := make(Hashes)