Extend ErrWrongLength with actual lengths
This extends util.ErrWrongLength to include the expected and actual
lengths. This was done to enable tests to verify the right sizes were
being returned.
Change-Id: I896239c2b90fe2772d60406db6f848ee7bbda9df
diff --git a/client/client.go b/client/client.go
index dd6e08d..c0d91ca 100644
--- a/client/client.go
+++ b/client/client.go
@@ -576,8 +576,8 @@
// check the data has the correct length and hashes
if err := util.TargetFileMetaEqual(actual, localMeta); err != nil {
- if err == util.ErrWrongLength {
- return ErrWrongSize{name, actual.Length, localMeta.Length}
+ if e, ok := err.(util.ErrWrongLength); ok {
+ return ErrWrongSize{name, e.Actual, e.Expected}
}
return ErrDownloadFailed{name, err}
}
diff --git a/repo_test.go b/repo_test.go
index 753fd17..1ffe680 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -376,12 +376,12 @@
// commit with an invalid root hash in snapshot.json due to new key creation
genKey(c, r, "targets")
c.Assert(r.Sign("targets.json"), IsNil)
- c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid root.json in snapshot.json: wrong length"))
+ c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid root.json in snapshot.json: wrong length, expected 1486 got 1729"))
// commit with an invalid targets hash in snapshot.json
c.Assert(r.Snapshot(CompressionTypeNone), IsNil)
c.Assert(r.AddTarget("bar.txt", nil), IsNil)
- c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length"))
+ c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 743 got 918"))
// commit with an invalid timestamp
c.Assert(r.Snapshot(CompressionTypeNone), IsNil)
diff --git a/util/util.go b/util/util.go
index fa196d5..363c284 100644
--- a/util/util.go
+++ b/util/util.go
@@ -7,7 +7,6 @@
"crypto/sha512"
"encoding/hex"
"encoding/json"
- "errors"
"fmt"
"hash"
"io"
@@ -17,7 +16,14 @@
"github.com/flynn/go-tuf/data"
)
-var ErrWrongLength = errors.New("wrong length")
+type ErrWrongLength struct {
+ Expected int64
+ Actual int64
+}
+
+func (e ErrWrongLength) Error() string {
+ return fmt.Sprintf("wrong length, expected %d got %d", e.Expected, e.Actual)
+}
type ErrWrongVersion struct {
Expected int
@@ -76,7 +82,7 @@
func TargetFileMetaEqual(actual data.TargetFileMeta, expected data.TargetFileMeta) error {
if actual.Length != expected.Length {
- return ErrWrongLength
+ return ErrWrongLength{expected.Length, actual.Length}
}
hashChecked := false
for typ, hash := range expected.Hashes {
diff --git a/util/util_test.go b/util/util_test.go
index 0fba9b8..223f543 100644
--- a/util/util_test.go
+++ b/util/util_test.go
@@ -71,7 +71,7 @@
name: "wrong length",
a: data.TargetFileMeta{Length: 1},
b: data.TargetFileMeta{Length: 2},
- err: func(test) error { return ErrWrongLength },
+ err: func(test) error { return ErrWrongLength{Actual: 1, Expected: 2} },
},
{
name: "wrong sha512 hash",