From cabe4121bb78dd368389c5aff9a589140af37bcf Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 20 Jan 2015 16:18:47 -0800 Subject: [PATCH] cas: make ResolveKey() only return keys which exist ResolveKey would return full-length keys without actually looking them up in the store. --- cas/cas.go | 16 ++++++++-------- cas/cas_test.go | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/cas/cas.go b/cas/cas.go index d9703e3..4b12123 100644 --- a/cas/cas.go +++ b/cas/cas.go @@ -24,7 +24,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "github.com/appc/spec/aci" @@ -84,15 +83,11 @@ func (ds Store) tmpFile() (*os.File, error) { // ResolveKey resolves a partial key (of format `sha512-0c45e8c0ab2`) to a full // key by considering the key a prefix and using the store for resolution. -// If the key is already of the full key length, it returns the key unaltered. // If the key is longer than the full key length, it is first truncated. func (ds Store) ResolveKey(key string) (string, error) { if len(key) > lenKey { key = key[:lenKey] } - if strings.HasPrefix(key, hashPrefix) && len(key) == lenKey { - return key, nil - } cancel := make(chan struct{}) var k string @@ -216,8 +211,13 @@ func (ds Store) Dump(hex bool) { // store the data matching the hash. func HashToKey(h hash.Hash) string { s := h.Sum(nil) - if len(s) != lenHash { - panic(fmt.Sprintf("bad hash passed to hashToKey: %s", s)) + return keyToString(s) +} + +// keyToString takes a key and returns a shortened and prefixed hexadecimal string version +func keyToString(k []byte) string { + if len(k) != lenHash { + panic(fmt.Sprintf("bad hash passed to hashToKey: %x", k)) } - return fmt.Sprintf("%s%x", hashPrefix, s)[0:lenKey] + return fmt.Sprintf("%s%x", hashPrefix, k)[0:lenKey] } diff --git a/cas/cas_test.go b/cas/cas_test.go index bf21505..fca89ce 100644 --- a/cas/cas_test.go +++ b/cas/cas_test.go @@ -17,6 +17,7 @@ package cas import ( "archive/tar" "bytes" + "encoding/hex" "io/ioutil" "net/http" "net/http/httptest" @@ -131,12 +132,19 @@ func TestResolveKey(t *testing.T) { defer os.RemoveAll(dir) ds := NewStore(dir) + // Return a hash key buffer from a hex string + str2key := func(s string) *bytes.Buffer { + k, _ := hex.DecodeString(s) + return bytes.NewBufferString(keyToString(k)) + } + // Set up store (use key == data for simplicity) data := []*bytes.Buffer{ - bytes.NewBufferString("sha512-1234567890"), - bytes.NewBufferString("sha512-abcdefghi"), - bytes.NewBufferString("sha512-abcjklmno"), - bytes.NewBufferString("sha512-abcpqwert"), + str2key("12345678900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + str2key("abcdefabc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + str2key("abcabcabc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + str2key("abc01234500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + str2key("67147019a5b56f5e2ee01e989a8aa4787f56b8445960be2d8678391cf111009bc0780f31001fd181a2b61507547aee4caa44cda4b8bdb238d0e4ba830069ed2c"), } for _, d := range data { if err := ds.WriteStream(d.String(), d); err != nil { @@ -159,8 +167,8 @@ func TestResolveKey(t *testing.T) { // Unambiguous prefix match k, err := ds.ResolveKey("sha512-123") - if k != "sha512-1234567890" { - t.Errorf("expected %q, got %q", "sha512-1234567890", k) + if k != "sha512-1234567890000000000000000000000000000000000000000000000000000000" { + t.Errorf("expected %q, got %q", "sha512-1234567890000000000000000000000000000000000000000000000000000000", k) } if err != nil { t.Errorf("expected err=nil, got %v", err)