mirror of https://github.com/docker/docs.git
[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:
parent
30c9cfc113
commit
9501cddc1d
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue