Addressing comments from review

Signed-off-by: David Lawrence <david.lawrence@docker.com> (github: endophage)
This commit is contained in:
David Lawrence 2016-02-01 13:03:55 -08:00
parent dec9a5a95c
commit 1bf3dd08db
5 changed files with 61 additions and 23 deletions

View File

@ -177,7 +177,7 @@ func (c *Client) downloadRoot() error {
var raw []byte
if download {
// use consistent download if we have the checksum.
raw, s, err = c.downloadSigned(role, size, expectedSha256, len(expectedSha256) > 0)
raw, s, err = c.downloadSigned(role, size, expectedSha256)
if err != nil {
return err
}
@ -265,7 +265,7 @@ func (c *Client) downloadTimestamp() error {
}
// unlike root, targets and snapshot, always try and download timestamps
// from remote, only using the cache one if we couldn't reach remote.
raw, s, err := c.downloadSigned(role, notary.MaxTimestampSize, nil, false)
raw, s, err := c.downloadSigned(role, notary.MaxTimestampSize, nil)
if err == nil {
ts, err = c.verifyTimestamp(s, version, c.keysDB)
if err == nil {
@ -342,7 +342,7 @@ func (c *Client) downloadSnapshot() error {
}
var s *data.Signed
if download {
raw, s, err = c.downloadSigned(role, size, expectedSha256, true)
raw, s, err = c.downloadSigned(role, size, expectedSha256)
if err != nil {
return err
}
@ -419,8 +419,8 @@ func (c *Client) downloadTargets(role string) error {
return nil
}
func (c *Client) downloadSigned(role string, size int64, expectedSha256 []byte, consistent bool) ([]byte, *data.Signed, error) {
rolePath := utils.URLFilePath(role, expectedSha256, consistent)
func (c *Client) downloadSigned(role string, size int64, expectedSha256 []byte) ([]byte, *data.Signed, error) {
rolePath := utils.ConsistentName(role, expectedSha256)
raw, err := c.remote.GetMeta(rolePath, size)
if err != nil {
return nil, nil, err
@ -480,7 +480,7 @@ func (c Client) getTargetsFile(role string, keyIDs []string, snapshotMeta data.F
size := snapshotMeta[role].Length
var s *data.Signed
if download {
raw, s, err = c.downloadSigned(role, size, expectedSha256, true)
raw, s, err = c.downloadSigned(role, size, expectedSha256)
if err != nil {
return nil, err
}

View File

@ -245,7 +245,7 @@ func TestChecksumMismatch(t *testing.T) {
remoteStorage.SetMeta("targets", orig)
_, _, err = client.downloadSigned("targets", int64(len(orig)), origSha256[:], false)
_, _, err = client.downloadSigned("targets", int64(len(orig)), origSha256[:])
assert.IsType(t, ErrChecksumMismatch{}, err)
}
@ -262,14 +262,14 @@ func TestChecksumMatch(t *testing.T) {
remoteStorage.SetMeta("targets", orig)
_, _, err = client.downloadSigned("targets", int64(len(orig)), origSha256[:], false)
_, _, err = client.downloadSigned("targets", int64(len(orig)), origSha256[:])
assert.NoError(t, err)
}
func TestSizeMismatchLong(t *testing.T) {
repo := tuf.NewRepo(nil, nil)
localStorage := store.NewMemoryStore(nil, nil)
remoteStorage := store.NewMemoryStore(nil, nil)
remoteStorage := testutils.NewLongMemoryStore(nil, nil)
client := NewClient(repo, remoteStorage, nil, localStorage)
sampleTargets := data.NewTargets()
@ -278,12 +278,9 @@ func TestSizeMismatchLong(t *testing.T) {
assert.NoError(t, err)
l := int64(len(orig))
orig = append([]byte(" "), orig...)
assert.Equal(t, l+1, int64(len(orig)))
remoteStorage.SetMeta("targets", orig)
_, _, err = client.downloadSigned("targets", l, origSha256[:], false)
_, _, err = client.downloadSigned("targets", l, origSha256[:])
// size just limits the data received, the error is caught
// either during checksum verification or during json deserialization
assert.IsType(t, ErrChecksumMismatch{}, err)
@ -292,7 +289,7 @@ func TestSizeMismatchLong(t *testing.T) {
func TestSizeMismatchShort(t *testing.T) {
repo := tuf.NewRepo(nil, nil)
localStorage := store.NewMemoryStore(nil, nil)
remoteStorage := store.NewMemoryStore(nil, nil)
remoteStorage := testutils.NewShortMemoryStore(nil, nil)
client := NewClient(repo, remoteStorage, nil, localStorage)
sampleTargets := data.NewTargets()
@ -301,11 +298,9 @@ func TestSizeMismatchShort(t *testing.T) {
assert.NoError(t, err)
l := int64(len(orig))
orig = orig[1:]
remoteStorage.SetMeta("targets", orig)
_, _, err = client.downloadSigned("targets", l, origSha256[:], false)
_, _, err = client.downloadSigned("targets", l, origSha256[:])
// size just limits the data received, the error is caught
// either during checksum verification or during json deserialization
assert.IsType(t, ErrChecksumMismatch{}, err)

View File

@ -19,7 +19,7 @@ func NewMemoryStore(meta map[string][]byte, files map[string][]byte) *MemoryStor
// add all seed meta to consistent
for name, data := range meta {
checksum := sha256.Sum256(data)
path := utils.URLFilePath(name, checksum[:], true)
path := utils.ConsistentName(name, checksum[:])
consistent[path] = data
}
}
@ -73,7 +73,7 @@ func (m *MemoryStore) SetMeta(name string, meta []byte) error {
m.meta[name] = meta
checksum := sha256.Sum256(meta)
path := utils.URLFilePath(name, checksum[:], true)
path := utils.ConsistentName(name, checksum[:])
m.consistent[path] = meta
return nil
}
@ -92,7 +92,7 @@ func (m *MemoryStore) SetMultiMeta(metas map[string][]byte) error {
func (m *MemoryStore) RemoveMeta(name string) error {
if meta, ok := m.meta[name]; ok {
checksum := sha256.Sum256(meta)
path := utils.URLFilePath(name, checksum[:], true)
path := utils.ConsistentName(name, checksum[:])
delete(m.meta, name)
delete(m.consistent, path)
}

View File

@ -26,3 +26,46 @@ func (cm CorruptingMemoryStore) GetMeta(name string, size int64) ([]byte, error)
d[0] = '}' // all our content is JSON so must start with {
return d, err
}
// LongMemoryStore corrupts all data returned by GetMeta
type LongMemoryStore struct {
store.MemoryStore
}
// NewLongMemoryStore returns a new instance of memory store that
// returns one byte too much data on any request to GetMeta
func NewLongMemoryStore(meta map[string][]byte, files map[string][]byte) *LongMemoryStore {
s := store.NewMemoryStore(meta, files)
return &LongMemoryStore{MemoryStore: *s}
}
// GetMeta returns one byte too much
func (lm LongMemoryStore) GetMeta(name string, size int64) ([]byte, error) {
d, err := lm.MemoryStore.GetMeta(name, size)
if err != nil {
return nil, err
}
d = append(d, ' ')
return d, err
}
// ShortMemoryStore corrupts all data returned by GetMeta
type ShortMemoryStore struct {
store.MemoryStore
}
// NewShortMemoryStore returns a new instance of memory store that
// returns one byte too little data on any request to GetMeta
func NewShortMemoryStore(meta map[string][]byte, files map[string][]byte) *ShortMemoryStore {
s := store.NewMemoryStore(meta, files)
return &ShortMemoryStore{MemoryStore: *s}
}
// GetMeta returns one byte too few
func (sm ShortMemoryStore) GetMeta(name string, size int64) ([]byte, error) {
d, err := sm.MemoryStore.GetMeta(name, size)
if err != nil {
return nil, err
}
return d[1:], err
}

View File

@ -148,11 +148,11 @@ func FindRoleIndex(rs []*data.Role, name string) int {
return -1
}
// URLFilePath generates the appropriate HTTP URL path for the role,
// ConsistentName generates the appropriate HTTP URL path for the role,
// based on whether the repo is marked as consistent. The RemoteStore
// is responsible for adding file extensions.
func URLFilePath(role string, hashSha256 []byte, consistent bool) string {
if consistent && len(hashSha256) > 0 {
func ConsistentName(role string, hashSha256 []byte) string {
if len(hashSha256) > 0 {
hash := hex.EncodeToString(hashSha256)
return fmt.Sprintf("%s.%s", role, hash)
}