[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 <hukeping@huawei.com>
This commit is contained in:
HuKeping 2016-03-11 10:17:56 +08:00
parent 30c9cfc113
commit 9501cddc1d
3 changed files with 22 additions and 31 deletions

View File

@ -1,6 +1,7 @@
package client package client
import ( import (
"bytes"
"crypto/sha256" "crypto/sha256"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -252,7 +253,8 @@ func TestChecksumMismatch(t *testing.T) {
orig, err := json.Marshal(sampleTargets) orig, err := json.Marshal(sampleTargets)
assert.NoError(t, err) assert.NoError(t, err)
origHashes := data.GetSupportedHashes(orig) origHashes, err := GetSupportedHashes(orig)
assert.NoError(t, err)
remoteStorage.SetMeta("targets", orig) remoteStorage.SetMeta("targets", orig)
@ -270,7 +272,8 @@ func TestChecksumMatch(t *testing.T) {
orig, err := json.Marshal(sampleTargets) orig, err := json.Marshal(sampleTargets)
assert.NoError(t, err) assert.NoError(t, err)
origHashes := data.GetSupportedHashes(orig) origHashes, err := GetSupportedHashes(orig)
assert.NoError(t, err)
remoteStorage.SetMeta("targets", orig) remoteStorage.SetMeta("targets", orig)
@ -289,7 +292,8 @@ func TestSizeMismatchLong(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
l := int64(len(orig)) l := int64(len(orig))
origHashes := data.GetSupportedHashes(orig) origHashes, err := GetSupportedHashes(orig)
assert.NoError(t, err)
remoteStorage.SetMeta("targets", orig) remoteStorage.SetMeta("targets", orig)
@ -310,7 +314,9 @@ func TestSizeMismatchShort(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
l := int64(len(orig)) l := int64(len(orig))
origHashes := data.GetSupportedHashes(orig) origHashes, err := GetSupportedHashes(orig)
assert.NoError(t, err)
remoteStorage.SetMeta("targets", orig) remoteStorage.SetMeta("targets", orig)
_, _, err = client.downloadSigned("targets", l, origHashes) _, _, err = client.downloadSigned("targets", l, origHashes)
@ -887,3 +893,15 @@ func TestDownloadTimestampLocalTimestampInvalidRemoteTimestamp(t *testing.T) {
err = client.downloadTimestamp() err = client.downloadTimestamp()
assert.NoError(t, err) 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
}

View File

@ -160,25 +160,6 @@ func CheckHashes(payload []byte, hashes Hashes) error {
return nil 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 // CheckValidHashStructures returns an error, or nil, depending on whether
// the content of the hashes is valid or not. // the content of the hashes is valid or not.
func CheckValidHashStructures(hashes Hashes) error { func CheckValidHashStructures(hashes Hashes) error {

View File

@ -124,14 +124,6 @@ func TestCheckHashes(t *testing.T) {
assert.Contains(t, err.Error(), "checksum mismatched") 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) { func TestCheckValidHashStructures(t *testing.T) {
var err error var err error
hashes := make(Hashes) hashes := make(Hashes)