From dde9531b4aa34541277a9838103eed6fd356cb96 Mon Sep 17 00:00:00 2001 From: Ying Li Date: Wed, 20 Jan 2016 14:40:01 -0800 Subject: [PATCH] Fix an error where we get a JSON syntax error on server 404 or 50X. We were testing to see if the cached metadata was nil, but we actually set it to an empty data.Signed object, but didn't always set it to nil if we failed to get local metadata. Signed-off-by: Ying Li --- client/client_update_test.go | 2 -- tuf/client/client.go | 20 +++++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/client_update_test.go b/client/client_update_test.go index 4cd028fb94..275cf42ce7 100644 --- a/client/client_update_test.go +++ b/client/client_update_test.go @@ -461,7 +461,6 @@ func TestUpdateRemoteRoot50XCannotUseLocalCache(t *testing.T) { // but is missing other data, then we propagate the ErrMetaNotFound. Skipping // force check, because that only matters for root. func TestUpdateNonRootRemoteMissingMetadataNoLocalCache(t *testing.T) { - // TODO: fix json syntax error for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") { if role == data.CanonicalRootRole { continue @@ -531,7 +530,6 @@ func TestUpdateNonRootRemoteMissingMetadataCannotUseLocalCache(t *testing.T) { // If there is no local cache, we just update. If the server 50X's when getting // metadata, we propagate ErrServerUnavailable. func TestUpdateNonRootRemote50XNoLocalCache(t *testing.T) { - // TODO: fix json syntax error for _, role := range append(data.BaseRoles, "targets/a", "targets/a/b") { if role == data.CanonicalRootRole { continue diff --git a/tuf/client/client.go b/tuf/client/client.go index b3ad1ae5d0..33ffa20936 100644 --- a/tuf/client/client.go +++ b/tuf/client/client.go @@ -247,19 +247,21 @@ func (c *Client) downloadTimestamp() error { // We may not have a cached timestamp if this is the first time // we're interacting with the repo. This will result in the // version being 0 - var download bool - old := &data.Signed{} - version := 0 + var ( + saveToCache bool + old *data.Signed + version = 0 + ) cachedTS, err := c.cache.GetMeta(role, maxSize) if err == nil { - err := json.Unmarshal(cachedTS, old) + cached := &data.Signed{} + err := json.Unmarshal(cachedTS, cached) if err == nil { - ts, err := data.TimestampFromSigned(old) + ts, err := data.TimestampFromSigned(cached) if err == nil { version = ts.Signed.Version } - } else { - old = nil + old = cached } } // unlike root, targets and snapshot, always try and download timestamps @@ -278,14 +280,14 @@ func (c *Client) downloadTimestamp() error { logrus.Warn("Error while downloading remote metadata, using cached timestamp - this might not be the latest version available remotely") s = old } else { - download = true + saveToCache = true } err = signed.Verify(s, role, version, c.keysDB) if err != nil { return err } logrus.Debug("successfully verified timestamp") - if download { + if saveToCache { c.cache.SetMeta(role, raw) } ts, err := data.TimestampFromSigned(s)