restic/hashing_test.go

48 lines
1 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-08-01 18:07:38 +00:00
"io/ioutil"
"testing"
2014-04-06 10:22:58 +00:00
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-06 10:22:58 +00:00
)
2014-08-01 18:07:38 +00:00
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"},
}
func TestReader(t *testing.T) {
for _, test := range static_tests {
r := khepri.NewHashingReader(bytes.NewBuffer([]byte(test.text)), test.hash)
buf, err := ioutil.ReadAll(r)
ok(t, err)
equals(t, test.text, string(buf))
equals(t, hex.EncodeToString(r.Hash()), test.digest)
}
}
2014-04-06 10:22:58 +00:00
2014-08-01 18:07:38 +00:00
func TestWriter(t *testing.T) {
for _, test := range static_tests {
var buf bytes.Buffer
w := khepri.NewHashingWriter(&buf, test.hash)
2014-04-06 10:22:58 +00:00
2014-08-01 18:07:38 +00:00
_, err := w.Write([]byte(test.text))
ok(t, err)
2014-04-06 10:22:58 +00:00
2014-08-01 18:07:38 +00:00
equals(t, hex.EncodeToString(w.Hash()), test.digest)
}
}