restic/hashing_test.go

82 lines
1.6 KiB
Go
Raw Normal View History

2014-07-28 18:20:32 +00:00
package khepri_test
2014-04-06 10:22:58 +00:00
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"hash"
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-06 10:22:58 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Hashing", func() {
var static_tests = []struct {
hash func() hash.Hash
text string
digest string
}{
{md5.New, "foobar\n", "14758f1afd44c09b7992073ccf00b43d"},
// test data from http://www.nsrl.nist.gov/testdata/
{sha1.New, "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"},
{sha1.New, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"},
}
2014-04-19 17:46:20 +00:00
Describe("Reader", func() {
2014-04-06 10:22:58 +00:00
Context("Static Strings", func() {
It("Should compute digest", func() {
for _, t := range static_tests {
2014-07-28 18:20:32 +00:00
r := khepri.NewHashingReader(bytes.NewBuffer([]byte(t.text)), t.hash)
2014-04-06 10:22:58 +00:00
n, err := r.Read(make([]byte, len(t.text)+1))
if n != len(t.text) {
Fail("not enough bytes read")
}
if err != nil {
panic(err)
}
digest := r.Hash()
h := hex.EncodeToString(digest)
Expect(h).Should(Equal(t.digest))
}
})
})
2014-04-19 17:46:20 +00:00
Context("Random Strings", func() {
})
2014-04-06 10:22:58 +00:00
})
2014-04-19 17:46:20 +00:00
Describe("Writer", func() {
2014-04-06 10:22:58 +00:00
Context("Static Strings", func() {
It("Should compute digest", func() {
for _, t := range static_tests {
var buf bytes.Buffer
2014-07-28 18:20:32 +00:00
w := khepri.NewHashingWriter(&buf, t.hash)
2014-04-06 10:22:58 +00:00
n, err := w.Write([]byte(t.text))
if n != len(t.text) {
2014-04-19 17:46:20 +00:00
Fail("not enough bytes written")
2014-04-06 10:22:58 +00:00
}
if err != nil {
panic(err)
}
digest := w.Hash()
h := hex.EncodeToString(digest)
Expect(h).Should(Equal(t.digest))
}
})
})
})
})