Skip to content

Commit

Permalink
Test newly exported util functions
Browse files Browse the repository at this point in the history
Add tests for newly exported functions in the util package:
* VersionEqual() - simple test of version number comparison
* BytesMatchLenAndHashes() - check function returns appropriate errors
  when incorrect length, hashes, or both are passed

Signed-off-by: Joshua Lock <jlock@vmware.com>
  • Loading branch information
joshuagl committed Jun 23, 2022
1 parent 39b9b0f commit 06b905d
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions util/util_test.go
Expand Up @@ -2,7 +2,10 @@ package util

import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
"testing"

"github.com/theupdateframework/go-tuf/data"
Expand Down Expand Up @@ -195,3 +198,78 @@ func (UtilSuite) TestHashedPaths(c *C) {
delete(expected, path)
}
}

func (UtilSuite) TestVersionEqual(c *C) {
c.Assert(VersionEqual(1, 1), IsNil)
c.Assert(VersionEqual(1, 3), Equals, ErrWrongVersion{3, 1})
}

func makeHash(b []byte, alg string) []byte {
var h hash.Hash

switch alg {
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
}
h.Write(b)
return h.Sum(nil)
}

func (UtilSuite) TestBytesMatchLenAndHashes(c *C) {
type test struct {
name string
bytes []byte
length int64
hashes data.Hashes
err func(test) error
}

b := []byte{82, 253, 252, 7, 33, 130, 101, 79, 22, 63, 95, 15, 154, 98, 29, 114}
bhashes := data.Hashes{
"sha512": makeHash(b, "sha512"),
"sha256": makeHash(b, "sha256"),
}

tests := []test{
{
name: "correct len and hashes",
bytes: b,
length: 16,
hashes: bhashes,
err: func(test) error { return nil },
},
{
name: "incorrect len",
bytes: b,
length: 32,
hashes: bhashes,
err: func(test) error { return ErrWrongLength{32, 16} },
},
{
name: "incorrect hashes",
bytes: b,
length: 16,
hashes: data.Hashes{
"sha512": makeHash(b, "sha256"),
"sha256": makeHash(b, "sha512"),
},
err: func(test) error { return ErrWrongHash{"sha512", bhashes["sha256"], bhashes["sha512"]} },
},
{
name: "incorrect len and hashes",
bytes: b,
length: 32,
hashes: data.Hashes{
"sha512": makeHash(b, "sha256"),
"sha256": makeHash(b, "sha512"),
},
err: func(test) error { return ErrWrongLength{32, 16} },
},
}

for _, t := range tests {
c.Assert(BytesMatchLenAndHashes(t.bytes, t.length, t.hashes), DeepEquals, t.err(t), Commentf("name = %s", t.name))
}
}

0 comments on commit 06b905d

Please sign in to comment.