Merge pull request #401 from vcaputo/resolved_key_always_exists

cas: make ResolveKey() only return keys which exist
This commit is contained in:
Vito Caputo
2015-01-21 12:28:18 -08:00
2 changed files with 22 additions and 14 deletions
+8 -8
View File
@@ -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]
}
+14 -6
View File
@@ -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)